hi i have a problem i communicating with 24c256 from 89c51rd2. i have used the same i2c code to communicate with the rtc and it works fine. please help me to solve this problem .....my code is as follows
#include "lcd.h" #include <REG51.H> #include<stdio.h> #include<stdlib.h> #include<intrins.h>
sbit sda=P2^1; sbit scl=P2^0;
#define not_ack 0 #define ack 1 void delay_50msec();
void i2cstart(); void i2cstop(); bit i2cclock(); bit i2cwrite(unsigned char ); unsigned char I2CRead(bit ); bit writei2cport(unsigned char , unsigned char ); unsigned char readi2cport(unsigned char ); void send(); void main(void) { char byte2='9'; bit status=1;
init_lcd(); cmd_write(0x80); delay_50msec(); data_write(byte2); delay_50msec();
i2cstart(); status=1; while(status=i2cwrite(0xa6)); status=1; while(status=i2cwrite(0x00)); status=1; while(status=i2cwrite(0x00)); status=1; while(status=i2cwrite('6')); i2cstop();
i2cstart(); status=1; while(status=i2cwrite(0xa6)); status=1; while(status=i2cwrite(0x00)); status=1; while(status=i2cwrite(0x00)); //i2cstop(); i2cstart(); status=1; while(status=i2cwrite(0xa7)); status=1; byte2=I2CRead(0); i2cstop(); delay_50msec();
data_write(byte2); delay_50msec(); data_write('4'); delay_50msec(); //send(); while(1);
}
void i2cstart(void) { sda=scl=1; _nop_(); _nop_(); sda=0; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); scl=0; }
void i2cstop(void) { sda=scl=1; _nop_(); _nop_(); sda=1; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); scl=0; }
bit i2cclock(void) { bit level; scl=1; _nop_(); _nop_(); while(!scl); _nop_(); _nop_(); _nop_(); level=sda; _nop_(); _nop_(); scl=0; return(level); }
bit i2cwrite(unsigned char byte) { unsigned char mask=0x80; while(mask) { if(byte & mask) sda=1; else sda=0; i2cclock(); mask>>=1; } sda=1; return(i2cclock()); }
unsigned char I2CRead(bit a) { unsigned char mask=0x80; unsigned char byte=0; while(mask) { if(i2cclock()) byte|=mask; mask>>=1; } if(a) { sda=0; i2cclock(); sda=1; } else { sda=1; i2cclock(); } return (byte); }
bit writei2cport(unsigned char add, unsigned char databyte) { bit status; status=1; i2cstart(); if(!i2cwrite(add)) { if(!i2cwrite(databyte)) status=0; } i2cstop(); if(!status) delay_50msec(); return(status); }
unsigned char readi2cport(unsigned char add) { unsigned char khit; bit status=1; i2cstart(); if(!i2cwrite(add|0x1)) { status=0; khit=I2CRead(not_ack); } i2cstop(); return(khit); }
void delay_50msec() {
TMOD=0x01;
TL0=0xb0;
TH0=0x3c; TR0=1; while(!TF0); TF0=0;