??? 09/20/08 08:38 Read: times |
#158429 - problem in FLOAT values in AT24C512 EEPROM |
Hello !
I am facing a problem in writing and reading floating values in EEPROM. I am using P89V51RD2 microcontroller and AT24C512 EEPROM ... Previously also I have queried the same question but at that time i got to change my UC to PIC. The solutions you all have provided is working fine with PIC UC with internal EEPROM. But now I have to interface the EEPROM with 8051 and the same problem is still persisting. Below I have given the code I am using. As per the datasheet I have writen this code. For single BYTE read and write the code is working fine, but not for the FLOAT values. Please give me the solution ... regards Arvind // MAIN FUNCTION FILE ... void main() { BYTE _buff_[16], dat = 0x00; float fl_RETVAL; delay(1000); P0 = 0x00; P1 = 0x00; LCD_INIT(); delay(1000); MAIN_WRITE_FLOAT(0x02, 0x02, 90.87); delay(1000); fl_RETVAL = MAIN_READ_FLOAT(0x02, 0x02); LCD_ADD(FIRST_LINE, 1); sprintf(_buff_, "float = %.2f", fl_RETVAL); send_string(_buff_); while(1) ; } // ============================================================= // EEPROM FILE ... #include<header_file.h> bit FLOAT_VALUES = LOW; int final = 0; void MAIN_WRITE_FLOAT(BYTE FIRST_ADD, BYTE SECOND_ADD, float fl_BUFF) { BYTE a, _buff_[16], *ptr = &fl_BUFF; final = sizeof(fl_BUFF); START(); WRITE_EEPROM(WRITE_ADD); WRITE_EEPROM(FIRST_ADD); WRITE_EEPROM(SECOND_ADD); for(a = 0; a < 4; a++) WRITE_EEPROM(*(ptr++)); STOP(); } float MAIN_READ_FLOAT(BYTE FIRST_ADD, BYTE SECOND_ADD) { BYTE *ptr, a; float fl_VAL; ptr = &fl_VAL; START(); WRITE_EEPROM(WRITE_ADD); WRITE_EEPROM(FIRST_ADD); WRITE_EEPROM(SECOND_ADD); START(); WRITE_EEPROM(READ_ADD); for(a = 0; a < 4; a++) { *(ptr++) = READ_EEPROM(); if(a != 3) { SDA = LOW; I2C_CLOCK_CYCLE(); } } STOP(); return(fl_VAL); } void START() { SCL = HIGH; delay(5); SDA = HIGH; delay(5); SDA = LOW; delay(5); SCL = LOW; delay(5); } void STOP() { SCL = HIGH; delay(5); SDA = LOW; delay(5); SDA = HIGH; delay(5); SCL = LOW; delay(5); } // SINGLE BYTE WRITE FUNCTION void MAIN_WRITE(BYTE FIRST_ADD, BYTE SECOND_ADD, BYTE b_DATA) { START(); WRITE_EEPROM(WRITE_ADD); WRITE_EEPROM(FIRST_ADD); WRITE_EEPROM(SECOND_ADD); WRITE_EEPROM(b_DATA); STOP(); } // SINGLE BYTE READ FUNCTION BYTE MAIN_READ(BYTE FIRST_ADD, BYTE SECOND_ADD) { BYTE value = 0x00; START(); WRITE_EEPROM(WRITE_ADD); WRITE_EEPROM(FIRST_ADD); WRITE_EEPROM(SECOND_ADD); START(); WRITE_EEPROM(READ_ADD); value = READ_EEPROM(); STOP(); return(value); } void I2C_CLOCK_CYCLE() { SCL = HIGH; delay(5); SCL = LOW; delay(5); } void ACKNOWLEDGE() { BYTE _buff[16]; SDA = HIGH; while(SDA) ; I2C_CLOCK_CYCLE(); SDA = LOW; } void WRITE_EEPROM(BYTE value) { BYTE count = 8, compare = 0x80; while(count--){ SDA = ((value & compare) ? 1 : 0); I2C_CLOCK_CYCLE(); compare >>= 1; } ACKNOWLEDGE(); } BYTE READ_EEPROM() { BYTE count = 8, value = 0x00; SDA = HIGH; while(count--){ value |= ((SDA) ? 1 : 0); I2C_CLOCK_CYCLE(); if(count) value <<= 1; } return(value); } |
Topic | Author | Date |
problem in FLOAT values in AT24C512 EEPROM | 01/01/70 00:00 | |
What problem? | 01/01/70 00:00 | |
reply | 01/01/70 00:00 | |
So how do you know it's the EEPROM? | 01/01/70 00:00 | |
serial EEPROM | 01/01/70 00:00 | |
read CODE ... | 01/01/70 00:00 | |
why do you think it IS the code? | 01/01/70 00:00 | |
What are the possibilities? | 01/01/70 00:00 | |
Other possibilities | 01/01/70 00:00 | |
Reads clock 9 bits too | 01/01/70 00:00 | |
Also seeming to be missing... | 01/01/70 00:00 | |
On Sequential reads, the eeprom | 01/01/70 00:00 | |
Acknowledge bit | 01/01/70 00:00 | |
You are right Henry. It should be 'bit'. | 01/01/70 00:00 | |
code is given ... plz read ... | 01/01/70 00:00 | |
But, Arvind, you DON'T send the ACK after reading! | 01/01/70 00:00 | |
What code? What tests? | 01/01/70 00:00 | |
try to read your name | 01/01/70 00:00 | |
commented code ... | 01/01/70 00:00 | |
Where is your 9th clock in Read... | 01/01/70 00:00 | |
Dan Henry has already found the mistake!![]() | 01/01/70 00:00 |