7 years, 1 month ago.

The integer value above 250 is giving different values in 24LC256 EEPROM?

the input value is the same as output value but as we increase the value to 300 the output value is different. it comes in 2 digit units. I can't figure out what. Please tell me what i am doing wrong.

eeprom 24lc256

#include "mbed.h"
#include "SerialLCD.h"


SerialLCD lcd(p13,NC);

int Q=300;

I2C i2c(p28, p27);  // sda, scl
//DigitalOut wp(p29); // write protect
 
int main()
{
    //wp = 0; // disable write protect
    lcd.clear();
      lcd.setPosition(0, 2);
    lcd.printf("Writing bytes 0-16\n");
 wait(0.3);
    char data[10];
    for(int i=0; i<16; i++) {
        data[0] = 0;     // MSB address
        data[1] = i;     // LSB address
        data[2] = i*3; // data
        if(i2c.write(0xA0, data, 3)) {
            error("Write failed\n");
        }
        while(i2c.write(0xA0, NULL, 0)); // wait to complete
    }
 wait(0.3);
    data[0] = 0;       // MSB address
    data[1] = 255;     // LSB address
    data[2] = Q;     // data
    if(i2c.write(0xA0, data, 3)) {
        error("Write failed\n");
    }
    while(i2c.write(0xA0, NULL, 0)); // wait to complete
 lcd.clear();
      lcd.setPosition(0, 2);
    lcd.printf("Setting read pointer to 0\n");
     wait(0.3);
 
    data[0] = 0;                   // MSB address
    data[1] = 0;                   // LSB address
    if(i2c.write(0xA0, data, 2)) { // send address, but no data
        error("Write failed\n");
    }
 
    lcd.printf("Reading back data bytes 0-16\n");
     wait(0.3);
     
    char response[1];
    for(int i=0; i<256; i++) {
        if(i2c.read(0xA0, response, 1)) {
            error("Read failed\n");
        }
        
        lcd.clear();
        lcd.setPosition(0, 0);
        lcd.printf("add %03d = %d", i, response[0]);
    }
 
}
 

1 Answer

7 years, 1 month ago.

Data is a char array, a char is 8 bits. So you are trying to set an 8 bit value to 300, 300 required 9 bits to represent in binary. The most significant bit will be dropped and you'll get a value of 4.

If you need to write values over 255 but under 65535 (the max for 16 bits) then you need to use two bytes and set them to the high and low 8 bits of the 16 bit value. If you need more than that then you will need more than two bytes.

Accepted Answer

Thank you. so what should I change in the code. Also I would like to read more about this. Please guide me a good reading source/

posted by jayendra mishra 20 Mar 2017

#include "mbed.h"
#include "SerialLCD.h"


SerialLCD lcd(p13,NC);

//int Q=257;

I2C i2c(p28, p27);  // sda, scl
//DigitalOut wp(p29); // write protect
int Q=257;


 
     // Point at low byte of integer
    
    
int main()
{
    //wp = 0; // disable write protect
    lcd.clear();
      lcd.setPosition(0, 2);
    lcd.printf("Writing bytes 0-16\n");
   wait(0.3);
 
    char data[4];
    for(int i=0; i<8; i++) {
          data[0] = 0;     // MSB address
        data[1] = i;     // LSB address
        data[2] = i * 4; // data
        if(i2c.write(0xA0, data, 4)) {
            error("Write failed\n");
        }
        while(i2c.write(0xA0, NULL, 0)); // wait to complete
    }
  data[0] = 0;       // MSB address
    data[1] = 255;     // LSB address
    data[2] = Q;     // data
    data[3] = Q >> 8;
    if(i2c.write(0xA0, data, 4)) {
        error("Write failed\n");
    }
 
    while(i2c.write(0xA0, NULL, 0)); // wait to complete
 lcd.clear();
      lcd.setPosition(0, 2);
    lcd.printf("Setting read pointer to 0\n");
    wait(0.3);
    data[0] = 0;                   // MSB address
    data[1] = 0;                   // LSB address
    
    if(i2c.write(0xA0, data, 2)) { // send address, but no data
        error("Write failed\n");
    }
 
    lcd.printf("Reading back data bytes 0-16\n");
    
    char response[1];
    for(int i=0; i<256; i++) {
        if(i2c.read(0xA0, response, 1)) {
            error("Read failed\n");
        }
        lcd.clear();
      lcd.setPosition(0, 2);
       lcd. printf("addr %03d = %d", i, response[0]);
       
    }
    
 
}
 

I tried this code but still not working

posted by jayendra mishra 21 Mar 2017