| ??? 11/03/08 14:49 Read: times |
#159574 - I have added code-tags to Praveen's i2c code Responding to: ???'s previous message |
for other's benefit, I have added the code tags to Praveen's posting. It does appear to be written for a PIC rather than an 8051:
Praveen Kumar said:
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 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 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 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 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 a);
}
IICClockDelay();
SCL = 0;
IICClockDelay();
}
return(b);
}
|



