Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years, 8 months ago.
I2C & EEPROM
Can anyone tell me how use the I2C API with an EEPROM. I'm using the 24AA64F, and I can't figure it out.
I think I understand how the 24AA64F expects to be addressed, but I'm having a hard time understanding the API.
The hello world sketch is for something completely unrelated, and seems to have an unused variable
const int addr7bit = 0x48; // 7 bit I2C address
I don't know if this is an error, or just an indicator that I'm completely missing something.
This is code I wrote months ago, which I thought worked. It does something, but I don't think it's communicated with the eeprom at all.
#include "mbed.h" I2C i2c(I2C_SDA, I2C_SCL ); int readVariable; int address = 0xa1; char array[1] ; char arrayRead[1]; int main() { while (1) { array[1] = 0x07; // just a random number i2c.start(); i2c.write(address,array,1); //write to address i2c.read(address,arrayRead,1); //read from address i2c.stop(); wait(.01); readVariable = arrayRead[0]; printf("variable = %i\r\n", readVariable); wait(1); } }
2 Answers
5 years, 8 months ago.
You could have a look here:
https://os.mbed.com/users/bborredon/code/eeprom/file/925096a4c7f0/eeprom.h/
I use this for 24Cxxx series, it may work the same for yours with modifications to the addressing.
I believe the 24Axxx eeproms are compatible, but can operate at a lower voltage. I tried a couple libraries, but don't think I tried this one.
One I did try https://os.mbed.com/users/rhourahane/code/I2CEeprom/ I was getting compile errors just from initializing the class
I2CEeprom (PinName sda, PinName scl, int address, size_t pageSize, size_t chipSize, int busSpeed=400000)
I set the values appropriately as far as i could tell, but the compiler was telling me it wanted closing parentheses after the SDA pin name.
I'll take a look at eeprom.h. thanks.
posted by 09 Mar 20195 years, 8 months ago.
Hi, I see two problems. The i2c and array are used incorrectly.
The start and stop are already included in these methods. And you can printf results
I2c
// i2c.start(); result_write = i2c.write(address,array,1); //write to address printf("write = %d\n", result_write); result_read = i2c.read(address,arrayRead,1); //read from address printf("read= %d\n", result_read); // i2c.stop();
Please see documentation https://os.mbed.com/docs/mbed-os/v5.11/apis/i2c.html#a37956c174106ecc64cae8f87dcf9fc7a
Array
char array[1] ; // an array with one char array[1] = 0x07; //it start form zero so position 1 is out of range and your i2c.write always write empty position 0 array[0] = 0x07; //correct
Please see documentation http://www.cplusplus.com/doc/tutorial/arrays/