Created
August 23, 2012 18:24
-
-
Save donghee/3439906 to your computer and use it in GitHub Desktop.
24lc256
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --+-------+--+-- 3.3v | |
// | | | | |
// +--------+ | # # 1k pullups | |
// +-- A0 -| 1 8 |- VDD --+ # # | |
// +-- A1 -| 2 7 |- WP ----- p29 | | | |
// +-- A2 -| 3 6 |- SCL ----------+--|-- p27 | |
// +- GND -| 4 5 |- SDA -------------+-- p28 | |
// | +--------+ | |
// GND | |
#include "mbed.h" | |
I2C i2c(p28, p27); // sda, scl | |
DigitalOut wp(p29); | |
Serial pc(USBTX, USBRX); | |
int main() { | |
pc.printf("Writing byte 0 of 24LC256 with 0x5A\n"); | |
wp = 0; // disable write protect | |
char data[] = {0x00, 0x00, 0x5A}; // address is 0x0000, data is 0x5A | |
if(i2c.write(0xA0, data, 3)) { | |
pc.printf("Write failed\n"); | |
} | |
int poll = 0; | |
while(i2c.write(0xA0, NULL, 0)) { // wait until we get an acknowledge | |
poll++; | |
} | |
pc.printf("Chip acknowledged after %d polls\n", poll); | |
pc.printf("Setting read pointer to 0\n"); | |
data[0] = 0; // MSB address | |
data[1] = 0; // LSB address | |
if(i2c.write(0xA0, data, 2)) { // send address, but no data | |
pc.printf("Write failed\n"); | |
} | |
pc.printf("Reading back data bytes 0-16\n"); | |
char response[1]; | |
for(int i=0; i<16; i++) { | |
if(i2c.read(0xA0, response, 1)) { | |
pc.printf("Read failed\n"); | |
} | |
pc.printf("address %03d = 0x%02X\n", i, response[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment