??? 11/03/08 14:22 Read: times |
#159569 - i2c code Responding to: ???'s previous message |
hi..i have written a universal code for any eeprom with any microcontroller just change its write ID and READ ID..
if you are using 24c1024 than take upr_adr 0,1but if use lower than 24c1024 take upr_adr as zero. ex- if you are using 24c16 than always take upr_adr as 0, WRITE_C--0xA0 READ_C--0xA1 connect A0,A1,A2 pin of eeprom to ground here is code--- /*I2C ROUTINE.upr_addr is for AT24C1024.Its value shud be 1 for AT24C1024 else it shud be 0.*/ #define WRITE_C 0xA0 //Command for write #define READ_C 0xA1 //Command for read #define SDA PORTC.F4 #define SCL PORTC.F3 #define SDA_Direction TRISC.F4 unsigned int WriteByteEEPROM(unsigned char upr_addr,unsigned int Address,unsigned char eep_data); unsigned int ReadByteEEPROM(unsigned char upr_addr,unsigned int Address); void PollAck(unsigned char IDD); void SendOneByteIIC(unsigned char d); void StartBit(void); void StopBit(void); char SlaveAck(void); void SlaveNoAck(void); void IICClockDelay(void); void Set_SDA_As_Output(void); void Set_SDA_As_Input(void); unsigned char ReadOneByteIIC(void); void SendAck(void); unsigned int WriteByteEEPROM(unsigned char upr_addr,unsigned int Address,unsigned char eep_data) { unsigned char ID; ID = WRITE_C | (upr_addr<<1);// ((Address & 0x0300) >> 7) | 0x0e; StartBit(); //Give start bit SendOneByteIIC(ID); //Send the write command SlaveAck(); //Get the acknowledge from slave SendOneByteIIC((Address>>8) & 0x00ff); //Send the address msb SlaveAck(); //Get the acknowledge from slave SendOneByteIIC(Address & 0x00ff); //Send the address lsb SlaveAck(); //Get the acknowledge from slave SendOneByteIIC(eep_data); //Send the Data SlaveAck(); //Get the acknowledge from slave StopBit(); //Give stop bit PollAck(ID); return Address; } unsigned int ReadByteEEPROM(unsigned char upr_addr,unsigned int Address) { unsigned char ID; unsigned char eep_dataR; ID = WRITE_C | (upr_addr<<1); // ((Address & 0x0300) >> 7) | 0x0e; //Write Address StartBit(); //Give start bit SendOneByteIIC(ID); //Send the write command SlaveAck(); //Get the acknowledge from slave SendOneByteIIC((Address>>8) & 0x00ff); //Send the address msb SlaveAck(); //Get the acknowledge from slave SendOneByteIIC(Address & 0x00ff); //Send the address lsb SlaveAck(); //Get the acknowledge from slave //Read a byte ID = READ_C | (upr_addr<<1); //((Address & 0x0300) >> 7) | 0x0e; StartBit(); //Give start bit SendOneByteIIC(ID); //Send the read command SlaveAck(); //Get the acknowledge from slave eep_dataR=ReadOneByteIIC(); //Read the byte SlaveNoAck(); StopBit(); //Give stop bit return eep_dataR; } void PollAck(unsigned char IDD) { char a; for(a=0;a<0x40;a++) { StartBit(); //Give start bit SendOneByteIIC(IDD); //Send the read Acknowledgement command if(SlaveAck()==0) //Get the acknowledge from slave { //acknowledge received break; } } } void SendOneByteIIC(unsigned char d) { char a; for(a=0;a<8;a++) { SCL = 0; IICClockDelay(); if(d & (0x80 >> a)) { Set_SDA_As_Input(); SDA = 1; } else { SDA = 0; Set_SDA_As_Output(); } IICClockDelay(); SCL = 1; IICClockDelay(); } } void StartBit(void) { SCL = 1; IICClockDelay(); Set_SDA_As_Input(); SDA = 1; IICClockDelay(); SDA = 0; Set_SDA_As_Output(); IICClockDelay(); SCL = 0; IICClockDelay(); } void StopBit(void) { Set_SDA_As_Output(); SDA = 0; IICClockDelay(); SCL = 1; IICClockDelay(); Set_SDA_As_Input(); SDA = 1; IICClockDelay(); SCL = 0; IICClockDelay(); } char SlaveAck(void) { char a; SCL = 0; IICClockDelay(); Set_SDA_As_Input(); SDA = 1; IICClockDelay(); SCL = 1; IICClockDelay(); a = SDA; IICClockDelay(); SCL = 0; IICClockDelay(); return(a); } void SlaveNoAck(void) { Set_SDA_As_Input(); SDA = 1; IICClockDelay(); SCL = 1; IICClockDelay(); SCL = 0; IICClockDelay(); } void SendAck(void) { Set_SDA_As_Output(); SDA = 0; IICClockDelay(); SCL = 1; IICClockDelay(); SCL = 0; IICClockDelay(); } void IICClockDelay(void) { char aa; for(aa=0;aa<20;aa++); } void Set_SDA_As_Output(void) { SDA_Direction = 0; //Set as output } void Set_SDA_As_Input(void) { SDA_Direction = 1; //Set as input } unsigned char ReadOneByteIIC() { unsigned char a; unsigned char b; b=0x00; Set_SDA_As_Input(); for(a=0;a<8;a++) { SCL = 1; IICClockDelay(); if(SDA) { b |= (0x80 >> a); } IICClockDelay(); SCL = 0; IICClockDelay(); } return(b); } |