I was trying to write some values to 24c02 and read back the same value from that location.But in the serial window I m only getting a black space in the place of data.Can any one help me regarding this problem.The code that i written was pre> #include <reg52.h> #include <string.h>
#define HIGH 0x01 // Value representing ON #define LOW 0x00 // Value representing OFF
sbit SDATA= P1^0; // Define the individual bit; sbit SCLK= P1^1; // Define the individual bit
char readchar_EE(char location); void writechar_EE(char location,char val); void i2c_write (unsigned char output_data); char i2c_read (void); void i2c_start (void); void i2c_stop (void); void i2c_ack (void); void DelayMs(unsigned int number);
char readchar_EE(char location) { unsigned char data_in;
i2c_start(); // Send I2C Start Transfer i2c_write(0xA0); // Send identifier I2C address - Write i2c_write(location); // SEND START LOCATION in RTC (last 2 bits is the channel) i2c_stop(); // Send I2C Stop Transfer
i2c_start(); // Send I2C Start Transfer i2c_write(0xA1); // Send identifier I2C address - Read data_in = i2c_read(); i2c_stop(); return data_in;
}
void writechar_EE(char location,char val) {
i2c_start(); // Send I2C Start Transfer i2c_write(0xA0); // Send identifier I2C address - Write i2c_write(location); // SEND START LOCATION in RTC (last 2 bits is the channel) i2c_write(val); // SEND START LOCATION in RTC (last 2 bits is the channel) i2c_stop();
void i2c_write (unsigned char output_data) { unsigned char index;
for(index = 0; index < 8; index++) // Send 8 bits to the I2C Bus { // Output the data bit to the I2C Bus SDATA = ((output_data & 0x80) ? 1 : 0); output_data <<= 1; // Shift the byte by one bit SCLK = HIGH; // Clock the data into the I2C Bus SCLK = LOW; }
index = SDATA; // Put data pin into read mode SCLK = HIGH; // Clock the ACK from the I2C Bus SCLK = LOW; SDATA=HIGH; }
char i2c_read (void) { char index, input_data;
index = SDATA; // Put data pin into read mode
input_data = 0x00; for(index = 0; index < 8; index++) // Send 8 bits to the I2C Bus { input_data <<= 1; // Shift the byte by one bit SCLK = HIGH; // Clock the data into the I2C Bus input_data |= SDATA; // Input the data from the I2C Bus SCLK = LOW; } return input_data; }
void i2c_start (void) {
SDATA = HIGH; // Set data line high SCLK = HIGH; // Set clock line high SDATA = LOW; // Set data line low (START SIGNAL) SCLK = LOW; // Set clock line low }
void i2c_stop (void) { unsigned char input_var;
SCLK = LOW; // Set clock line low SDATA = LOW; // Set data line low SCLK = HIGH; // Set clock line high SDATA = HIGH; // Set data line high (STOP SIGNAL) input_var = SDATA; // Put port pin into HiZ }
void i2c_ack (void) { char in_var; SDATA=LOW; SCLK = HIGH; // Clock the ACK from the I2C Bus SCLK = LOW; SDATA = HIGH; // Set data line high (STOP SIGNAL) in_var = SDATA; // Put port pin into HiZ }
void DelayMs(unsigned int number) { unsigned char temp; for(;number!=0;number--) { for(temp=112;temp!=0;temp--) ; } } void main() { unsigned char inputvalue,readvalue=0x00; inputvalue='A';
TMOD=0X20; //timer 1 auto reload TH1=0XFD; //9600 baud rate SCON=0X50; TR1=1;
writechar_EE(0x02,inputvalue); DelayMs(10); readvalue=readchar_EE(0x02);
SBUF=readvalue; while(TI==0); TI=0;
SBUF='B'; while(TI==0); TI=0;
while(1); }