#include <AT89X52.H> // Header file for IO operations
#include "intrins.h"
/* AT24C64 Memory*/
#define SDA P3_7
#define SCL P3_5
#define WP P3_4
bit ack_bit;
unsigned char output;
void I2CInit(){
SDA = 1;
SCL = 1;
}
void I2CStart(){
SDA = 1;
SCL = 1;
_nop_(); //No operation
_nop_();
_nop_();
SDA = 0;
_nop_(); //No operation
_nop_();
_nop_();
//SCL = 0;
}
void I2CRestart(){
SCL = 0;
SDA = 1;
SCL = 1;
SDA = 0;
}
void I2CStop(){
SDA = 0;
SCL = 1;
_nop_(); //No operation
_nop_();
_nop_();
SDA = 1;
_nop_(); //No operation
_nop_();
_nop_();
//SCL = 0;
}
void I2CAck(){
SDA = 0;
SCL = 1;
_nop_(); //No operation
_nop_();
_nop_();
SCL = 0;
//SDA = 1;
}
void I2CNak(){
SCL = 0;
SDA = 1;
SCL = 1;
SCL = 0;
SDA = 0;
}
unsigned char I2CSend1(unsigned char Data)
{
unsigned char i;
for(i=0;i<8;i++){
SCL = 0;
if((Data&0x80)==0)
SDA = 0;
else
SDA = 1;
SCL = 1;
Data<<=1;
}
SCL = 0;
ack_bit = SDA;
SCL = 1;
SCL = 0;
//SDA = 1;
return !ack_bit;
}
void I2CSend(unsigned char value) //send byte serially
{
unsigned int i;
unsigned char send;
send=value;
for(i=0;i<8;i++)
{
SDA=send/128; //extracting MSB
send=send<<1; //shiftng left
SCL=1;
_nop_();
_nop_();
SCL=0;
}
ack_bit=SDA; //reading acknowledge
SDA=0;
}
unsigned char I2CRead1() //reading from EEPROM serially
{
unsigned int i,reead;
SDA=1;
reead=0;
for(i=0;i<8;i++)
{
reead=reead<<1;
SCL=1;
_nop_();
_nop_();
if(SDA==1)
reead++;
SCL=0;
}
SDA=0;
return reead; //Returns 8 bit data here
}
unsigned char I2CRead(){
unsigned char i, Data=0;
for(i=0;i<8;i++){
SCL = 0;
SCL = 1;
if(SDA)
Data |=1;
if(i<7)
Data<<=1;
}
SCL = 0;
SDA = 1;
return Data;
}
void Save() //save in EEPROM
{
I2CStart();
I2CSend(0xA0); //device address
I2CAck();
I2CSend(0x11); //word address
I2CAck();
I2CSend("1"); //send data
I2CAck();
I2CStop();
if(ack_bit==0)
{
//
}
else
//
I2CAck();
}
void Read()
{
I2CStart();
I2CSend(0xA0);
I2CAck();
I2CSend(0x11);
I2CAck();
I2CStart();
I2CSend(0xA1); //device address
I2CAck();
output=I2CRead(); //read the value
I2CAck();
I2CStop();
I2CAck();
}
int main(void)
{
Save(); //first save the values in EEPROM
Read(); //Then read the values from EEPROM
}