??? 03/05/06 08:23 Read: times Msg Score: -1 -1 Looks like homework |
#111278 - i2c protocol ,problem writing to eeprom |
hello everybody, iam doing final year project. In that i have to write some data into and read from a EEPROM using I2C protocol. iam using atmel's AT24C04 (4K) EEPROM and AT89s52 controller. I have interfaced the EEPROM using pins P1.0(SCL) and P1.1 (SDA) of thee eeprom.
i have written the code in c language. but when i tested it with pinnacle 52 proffessional development system (evaluation version). iam getting this kind of output on the i2c bus 1 device [START][Addr:50-Write]<ACK>[00]<ACK>[AA]<ACK>[STOP] [START][Addr:50-Write]<ACK>[00]<ACK> [START][Addr:50-Read]<ACK><00>[NAK]<00> but when i check the device in pinnacle there all all zero's in the required memory location. my data that i have written into the required address has not been written and the same data is not being read from it. i have given a device address of 0xA0 but int he above output iam getting 50. i could not understand why it is so. iam attaching mycode here can anyone please help me in this regard. #include<reg52.h> #include <intrins.h> // includes _nop_(); function sbit SCL = P1^0; sbit SDA = P1^1; void i2cstart(void); bit i2cstop(); bit i2cdatawrite(unsigned int ); unsigned int i2cdataread(void); void main() { unsigned int value; SDA = 0; SCL = 0; i2cstart(); value = 0xA0; // device address i2cdatawrite(value); value = 0x00; // data word address i2cdatawrite(value); value = 0xAA; // byte to be written i2cdatawrite(value); i2cstop(); P2 = i2cdataread(); } void i2cstart() { SDA = 1; //data has to be high before the clock //is, this is the definition of a start SCL = 1; _nop_(); SDA= 0; //change the data while CLK is high _nop_(); SCL = 0; } bit i2cstop() { unsigned char retry = 9; SDA = 1; // set it high so we can use it as an input while (retry--) if (!SDA) // while the data line is low { // continue to pulse the clock SCL= 1; _nop_(); SCL = 0; } else { // when the data line actually is high SDA = 0; // we can now execute a valid STOP _nop_(); SCL = 1; _nop_(); SDA = 1; return(1); } SCL = 1; return (0); // stop state has failed. } bit i2cdatawrite(unsigned int byte_to_send) { unsigned char bit_pos; bit ack; SCL = 0; for(bit_pos = 0x80; bit_pos != 0; bit_pos >>= 1) // MSB first { if (byte_to_send & bit_pos) SDA = 1; else SDA = 0; SCL = 1; _nop_(); SCL= 0; } /*--- now get the "ack" bit to make sure everything is good ---*/ SDA = 1; // set for input _nop_(); SCL = 1; // data can be read when the clock is high _nop_(); ack = SDA; // read the ack bit SCL = 0; return(ack == 0);// returning ACK bit, an I2C ACK is a 0, so we'll // change it to positive logic to make it easier } unsigned int i2cdataread(void) { unsigned int x = 0x00,value; unsigned char index; i2cstart(); value = 0xA0; // device address i2cdatawrite(value); value = 0x00; // data word address i2cdatawrite(value); value = 0xA1; // device address with R/W = 1; i2cstart(); i2cdatawrite(value); SDA = 1; // set for a read for(index = 0x80; index != 0; index >>= 1) // MSB first { _nop_(); SCL = 1; if (SDA) x |= index; SCL = 0; } SDA = 1; // ACK (0) or NACK (1) bit _nop_(); SCL= 1; _nop_(); SCL = 0; return(x); } please tell me what the problem is, iam falling short of time. thanks in advance narender |
Topic | Author | Date |
i2c protocol ,problem writing to eeprom | 01/01/70 00:00 | |
Wait for the write to complete | 01/01/70 00:00 | |
but iam getting 00 when reading | 01/01/70 00:00 | |
send micro an e-mail when you address hi | 01/01/70 00:00 | |
Don't trust the simulator! | 01/01/70 00:00 | |
ur read function is not correct | 01/01/70 00:00 | |
snyone know micro's e-mail address![]() | 01/01/70 00:00 |