Hi, I'm just starting to use an EEPROM. I have a Mircochip 24AA02 UID (I2C, 2k) it is very similar (perhaps identical) in addressing to the 24LCxxx series.
I'm at the point I am writing and reading bytes to learn the addressing structure and what can go wrong.
This device is:
- 2048 bits 256 total bytes
- 2 blocks @ 128 bytes / block I believe the 1st block is at 0x50 and the 2nd at 0x51. I'm currently only working with block 1
- Page = 8 bytes
Now my issue is:
In a loop, I fill the first block with 0xAD (just a number to use)
then I write 0x00 to address 1 (should be the second location.
When I read the array back it shows all 0's
If i rerun but comment out the write to location 1, the array reports 0xAD for every location.
I'm guessing the whole array is not being reset to 0 and there is some basic addressing faux pas I'm making but I'm at a loss to identify it.
I'm using the example from Nick Gammon, modifying the writes and reads to show me what is going on.
Can anyone point me in the right direction.
One last thing, I'm using an STM32. However I've successfully used this setup to run the BME680 sensor and an OLED display so I'm confident the setup is capable.
My code:
// Original code Written by Nick Gammon - February 2011
/*
Goal of this test: To determine the effect of writing past location 127 in a single block.
Test 1; Fill 0 to 127 with 0xAD
Read 126 to 140 and compare
0xAD = 173 = 0b10101101 chosen because of almost alternate bits.
*/
#include <Wire.h>
const byte rom = 0x50; // Address of 24AA02 eeprom
void setup (void)
{
Serial.begin (9600); // debugging
Wire.begin ();
delay(5000);
Serial.println(" Starting.......");
for (int i=0;i<127;i++)
{
writeEEPROM (rom, i,0xAD);
}
writeEEPROM (rom, 1,0); // <<------------------------------leaving or commenting this line seems to change the whole array
Serial.println(" Completed filling EEPROM addresses 0 - 126....... ");
// read back to confirm
for (int i=0;i<127;i++)
{
int a = readEEPROM (rom, i);
Serial.print (i, DEC); // display to confirm
Serial.print(" ");
Serial.println (a); // display to confirm
}
} // --- end of setup ---------------
void loop() {
} // --- end of main loop --------
//
// ----- EEPROM functions ---------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------
byte writeEEPROM (byte device, unsigned int addr, byte * data, byte len )
{
byte err;
byte counter;
if (len > BUFFER_LENGTH) // 32 (in Wire.h)
return 0xFF; // too long
Wire.beginTransmission(device);
Wire.write ((byte) (addr >> 8)); // high order byte
Wire.write ((byte) (addr & 0xFF)); // low-order byte
Wire.write (data, len);
err = Wire.endTransmission ();
if (err != 0)
return err; // cannot write to device
// wait for write to finish by sending address again
// ... give up after 100 attempts (1/10 of a second)
for (counter = 0; counter < 100; counter++)
{
delayMicroseconds (300); // give it a moment
Wire.beginTransmission (device);
Wire.write ((byte) (addr >> 8)); // high order byte
Wire.write ((byte) (addr & 0xFF)); // low-order byte
err = Wire.endTransmission ();
if (err == 0)
break;
}
return err;
} // end of writeEEPROM
// write one byte to device, returns non-zero on error
byte writeEEPROM (byte device, unsigned int addr, byte data )
{
return writeEEPROM (device, addr, &data, 1);
} // end of writeEEPROM
// read len (max 8) bytes from device, returns non-zero on error
// return code: 0xFF means buffer too long
// 0xFE means device did not return all requested bytes
// other: other error (eg. device not present)
// Note that if reading multiple bytes the address plus
// length must not cross a 8-byte boundary or it will "wrap"
byte readEEPROM (byte device, unsigned int addr, byte * data, byte len )
{
byte err;
byte counter;
if (len > BUFFER_LENGTH) // 32 (in Wire.h)
return 0xFF; // too long
Wire.beginTransmission (device);
Wire.write ((byte) (addr >> 8)); // high order byte
Wire.write ((byte) (addr & 0xFF)); // low-order byte
err = Wire.endTransmission ();
if (err != 0)
return err; // cannot read from device
// initiate blocking read into internal buffer
Wire.requestFrom (device, len);
// pull data out of Wire buffer into our buffer
for (counter = 0; counter < len; counter++)
{
data [counter] = Wire.read ();
}
return 0; // OK
} // end of readEEPROM
// read one byte from device, returns 0xFF on error
byte readEEPROM (byte device, unsigned int addr )
{
byte temp;
if (readEEPROM (device, addr, &temp, 1) == 0)
return temp;
return 0xFF;
} // end of readEEPROM