|
Sir, I need a code to access at24c02 connected with (8051).Please send me the code. I also generate the code but failed. I am sending that also. If you can correct it or send me the code. I will be thankful to you.
/* Program to write and then read (and dispay on lcd) data from at24c02 connected with at89s52*/ /////////////////////////////header files////////////////////////////// #include<reg51.h> #include<absacc.h>
////////////////////////////read/write/////////////////////////// #ifndef READ #define READ 1 #endif #ifndef WRITE #define WRITE 0 #endif
//////////////////////////// sda and scl ports///////////////////////// sbit SDA=P2^1; sbit SCL=P2^0;
////////////////////////////lcd ports///////////////////////////////// sbit rs=P1^2; sbit rw=P1^3; sbit en=P1^4;
/////////////////////////// lcd fon prototypes/////////////////////// void startlcd(); void delay(unsigned char value); void lcdWrite(unsigned char value,bit x);
//////////////////////////// sda scl elated funtion prototypes/////// void I2CBitDly(void); void I2CSCLHigh(void); void I2CSendAddr(unsigned char addr, unsigned char rd); void I2CSendByte(unsigned char bt); unsigned char I2CGetByte(unsigned char lastone); void I2CSendStart(void); void I2CSendStop(void);
///////////////////////////////main body//////////////////////////// void main (void){ unsigned char chr=0x61, chr1 ;
/// Set up the serial ports/// PCON =0x00; PCON |=0x80; SCON = 0x50; // SCON: mode 1, 8-bit UART, enable rcvr TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload TH1=0xFE; //TL0 = (unsigned int)(256 - ( (XtalFreq / BaudRate) / 192)); TR0=1; TR1 = 1; // TR1: timer 1 run TI = 1; // TI: set TI to send first char of UART
///send data/// I2CSendAddr(10100110,WRITE); I2CSendByte(2); ///address I2CSendByte(chr); I2CSendStop(); I2CBitDly();
///reading/// I2CSendAddr(10100110,WRITE); I2CSendByte(2); ///address I2CSendAddr(10100110,READ); chr1 = I2CGetByte(1); I2CSendStop();
///dispay on lcd/// startlcd(); lcdWrite(chr1,1); }
void I2CBitDly(void){ // wait approximately 4.7uS // tune to xtal. This works at 11.0592MHz unsigned int time_end = 10; unsigned int index; for (index = 0; index < time_end; index++); return; }
void I2CSCLHigh(void){ // Set SCL high, and wait for it to go high register int err; SCL = 1; while (! SCL) { err++; if (!err) { return; } } }
void I2CSendAddr(unsigned char addr, unsigned char rd) { I2CSendStart(); I2CSendByte(addr+rd); // send address byte }
void I2CSendByte(unsigned char bt){ register unsigned char i; for (i=0; i<8; i++) { if (bt & 0x80) SDA = 1; // Send each bit, MSB first changed 0x80 to 0x01 else SDA = 0; I2CSCLHigh(); I2CBitDly(); SCL = 0; I2CBitDly(); bt = bt << 1; } SDA = 1; // Check for ACK I2CBitDly(); I2CSCLHigh(); I2CBitDly(); if (SDA) SCL = 0; I2CBitDly(); SDA = 1; // end transmission SCL = 1; }
unsigned char I2CGetByte(unsigned char lastone){ // last one == 1 for last byte; 0 for any other byte register unsigned char i, res; res = 0; for (i=0;i<8;i++) // Each bit at a time, MSB first { I2CSCLHigh(); I2CBitDly(); res *= 2; if (SDA) res++; SCL = 0; I2CBitDly(); } SDA = lastone; // Send ACK according to 'lastone' I2CSCLHigh(); I2CBitDly(); SCL = 0; SDA = 1; // end transmission SCL=1; I2CBitDly(); return(res); }
void I2CSendStart(void) { SCL = 1; I2CBitDly(); SDA = 0; I2CBitDly(); SCL = 0; I2CBitDly(); }
void I2CSendStop(void) { SDA = 0; I2CBitDly(); SCL = 1; I2CBitDly(); SDA = 1; I2CBitDly(); }
void delay(unsigned char value){ unsigned int x,y; for(x=0;x<1275;x++) for(y=0;y<value;y++); }
void lcdWrite(unsigned char value,bit x){ delay(4); P0=value; rs=x; rw=0; en=0; delay(1); en=1; return; }
void startlcd(){ lcdWrite(0x38,0); lcdWrite(0x0E,0); lcdWrite(0x01,0); }
|