??? 08/10/11 13:13 Read: times |
#183288 - eeprom accessing problem |
Hello friends I am using AT89C51 and AT24C08A for accessing serial EEPROM using I2C protocol.I am able to write and read back the data from EEPROM(AT24C08A)
I want to increment the data and store it in EEPROM whenever an external interrupt(INTO) occurs. I am sending the code for your reference. Kindly help me in this regard. ...insert code here #include <reg52.h> //---------------------------------Defining Macros--------------------------------------------------------------------------- sbit sda = P3^6; //P3^2;//P1^1; sbit sclk = P3^7; //P3^3;//P1^0; sbit stat = P3^2; unsigned char mt; //unsigned char i,j; //--------------------------------------------------------------------------------------------------------------------------- void start_s_eeprom(); void transmit(char); char get_byte_s_eeprom(); void stop_s_eeprom(); void save(char, char); unsigned int get_from_mem(char); void wait(); void acknowledge(); void delay(unsigned int); //-----------------fxn to send data to serial eeprom------------------------------------------------------------------------- // give the adress and the data as the parametrs to the fxn void save(char s_address, char s_data) { start_s_eeprom(); // sending start condition to eeprom transmit(0XA0); // A0 = 10100000 = sending device address word for write acknowledge(); transmit(s_address); // sending data address acknowledge(); transmit(s_data); // sending data acknowledge(); stop_s_eeprom(); // sending stop condition to eeprom acknowledge(); return; } //--------------------------------------------------------------------------------------------------------------------------- //------------------fxn to get the data back frm the serial eeprom----------------------------------------------------------- // just give the adress from where the data is to be retrieved unsigned int get_from_mem(char s_address) { char i = 0; //-------dummy write seq----+ word address------------------------------------ start_s_eeprom(); // sending start condition to eeprom transmit(0XA0); // sending A0 = 10100000 = device address word for write acknowledge(); transmit(s_address); // sending data address acknowledge(); //----------------dummy over---------------------------------------------------- start_s_eeprom(); transmit(0XA1); // sending A1 =10100001 = device adress word for read acknowledge(); i = get_byte_s_eeprom(); // sending data acknowledge(); stop_s_eeprom();// sending stop condition to eeprom acknowledge(); return(i); } //--------------------------------------------------------------------------------------------------------------------------- //----------------------fxn to transmit a byte to the eeprom ---------------------------------------------------------------- //this fxn just send the 8 bits serialy on the SDA line //this fnx does not store the data in eeprom but just transmits it, this fxn is used by the storing fxn //just pass the byte to be transmitted as parameter to this fxn void transmit (char s_byte) { char temp = s_byte; char i ; for(i = 7 ; i >= 0 ; i--) { temp = s_byte; temp = temp >> i; temp = temp & 0X01; if(temp == 0) sda = 0; else sda = 1; sclk = 1; wait(); sclk = 0; } return; } //--------------------------------------------------------------------------------------------------------------------------- //---------------------------fxn to receive 8 bits serialy from sda line----------------------------------------------------- // this is not a fxn to read from eeprom // it just receives 8 bits serialy and retuns the byte received to the calling fxn char get_byte_s_eeprom() { char temp, temp_h, i; temp = 0; temp_h = 1; sda = 1; // making SDA as input pin for microcontroller sclk = 0; for(i = 7; i >=0 ; i--) { sclk = 1; if(sda == 1) { temp = temp | temp_h<<i ; } wait(); sclk = 0; } sclk = 0; return(temp); } //--------------------------------------------------------------------------------------------------------------------------- //--------------------------fxn to send the start condition ----------------------------------------------------------------- void start_s_eeprom() { sda = 1; sclk = 1; wait(); sda = 0; sclk = 0; return; } //--------------------------------------------------------------------------------------------------------------------------- //------------------------------fxn to send stop condition-------------------------------------------------------------------- void stop_s_eeprom() { sda = 0; sclk = 1; wait(); sda = 1; sclk = 0; return; } //--------------------------------------------------------------------------------------------------------------------------- //-------------------------------------fxn for acknowledging the eeprom------------------------------------------------------ // this fxn actualy does not read the acknowledge signal // it just waits for sufficient time and assumes that the eeprom has given tha ack by the time the wait gets over void acknowledge() { sclk = 1; wait(); sclk = 0; return; } //--------------------------------------------------------------------------------------------------------------------------- //--------------------------a small delay fxn to ensure the line settels down after transition------------------------------- void wait() { char i; for(i=0;i<=20;i++) i++; return; } void main(void) { unsigned char j; IE = 0x81; IT0 = 1; delay(100); while(1) { j = get_from_mem(0x00); P0= ~j; } } void external0_ISR(void) interrupt 0 { mt = mt + 1; save(0x00, mt); delay(2); } void delay(unsigned int count) { int i,j; for(i=0;i<count;i++) for(j=0;j<1275;j++); } |
Topic | Author | Date |
eeprom accessing problem | 01/01/70 00:00 | |
first problem | 01/01/70 00:00 | |
Side effects are always possible to make use of | 01/01/70 00:00 | |
Increment and Store | 01/01/70 00:00 |