Step-by step introduction to I2C EEPROM

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /***
00002 **** ST M24512 512-Kbit I2C EEPROM
00003 **** E0-E2 pulled low
00004 **** 10k pull up resistors on SDA and SCL lines
00005 ***/
00006 #include "mbed.h"
00007 
00008 #define EEPROM_ADDR   (0b10100000) // EEPROM address
00009 
00010 I2C i2c(I2C_SDA, I2C_SCL);
00011 DigitalIn mybutton(USER_BUTTON); // Activate button
00012 Serial pc(SERIAL_TX, SERIAL_RX); // Initialize UART connection
00013 // Use a terminal program (eg. putty)!
00014 
00015 int main()
00016 {
00017     pc.printf("Read data from EEPROM sequentially\r\n");
00018     char data[1];
00019     char data_write[3]; //Array for address and data
00020     // Type in address
00021     data_write[0] = 0;
00022     data_write[1] = 0;
00023     // Type data
00024     data_write[2] = 1;
00025     uint8_t i;
00026     for( i=0; i < 255; i++ ) {
00027         data_write[1] = i;
00028         data_write[2] = i;
00029         i2c.write(EEPROM_ADDR, data_write, 3);
00030         wait_ms(6); // Wait eeprom internal write cycle (5ms) + 1ms
00031     }
00032     // Reset eeprom pointer address
00033     char eeprom_address[2] = {0, 0};
00034     i2c.write(EEPROM_ADDR, eeprom_address, 2, true);
00035     i2c.read(EEPROM_ADDR, data, 1, false);
00036     pc.printf("Written data: %d\n\r",data[0]);
00037 
00038     while (1) {
00039         if (mybutton == 0) { // Button is pressed
00040             i2c.read(EEPROM_ADDR, data, 1);
00041             pc.printf("%d\n\r",data[0]);
00042             wait_ms(500);
00043         }
00044     }
00045 
00046 }