Simon Ford / Mbed 2 deprecated Test24LC256

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Test24LC256 hello world example
00002 // Copyright (c) 2009, sford
00003 // Released under the MIT License: http://mbed.org/license/mit
00004 //
00005 // Does some basic stuff with an 24LC256 to show it is working
00006 
00007 #include "mbed.h"
00008 
00009 I2C i2c(p28, p27);  // sda, scl
00010 DigitalOut wp(p29); // write protect
00011 
00012 int main() {
00013 
00014     wp = 0; // disable write protect
00015     
00016     printf("Writing bytes 0-16\n");
00017 
00018     char data[3];
00019     for(int i=0; i<16; i++) {
00020         data[0] = 0;     // MSB address
00021         data[1] = i;     // LSB address
00022         data[2] = i * 3; // data
00023         if(i2c.write(0xA0, data, 3)) {
00024             error("Write failed\n");
00025         }
00026         while(i2c.write(0xA0, NULL, 0)); // wait to complete
00027     }
00028 
00029     printf("Setting read pointer to 0\n");
00030 
00031     data[0] = 0;  // MSB address
00032     data[1] = 0;  // LSB address
00033     if(i2c.write(0xA0, data, 2)) { // send address, but no data
00034         error("Write failed\n");
00035     }
00036         
00037     printf("Reading back data bytes 0-16\n");
00038     
00039     char response[1];
00040     for(int i=0; i<16; i++) {
00041         if(i2c.read(0xA0, response, 1)) {
00042             error("Read failed\n");
00043         }
00044         printf("address %03d = 0x%02X\n", i, response[0]); 
00045     }
00046 
00047 }