Dependencies:   mbed

main.cpp

Committer:
simon
Date:
2009-09-08
Revision:
0:ebb4c3fcf4a9

File content as of revision 0:ebb4c3fcf4a9:

// Test24LC256 hello world example
// Copyright (c) 2009, sford
// Released under the MIT License: http://mbed.org/license/mit
//
// Does some basic stuff with an 24LC256 to show it is working

#include "mbed.h"

I2C i2c(p28, p27);  // sda, scl
DigitalOut wp(p29); // write protect

int main() {

    wp = 0; // disable write protect
    
    printf("Writing bytes 0-16\n");

    char data[3];
    for(int i=0; i<16; i++) {
        data[0] = 0;     // MSB address
        data[1] = i;     // LSB address
        data[2] = i * 3; // data
        if(i2c.write(0xA0, data, 3)) {
            error("Write failed\n");
        }
        while(i2c.write(0xA0, NULL, 0)); // wait to complete
    }

    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
        error("Write failed\n");
    }
        
    printf("Reading back data bytes 0-16\n");
    
    char response[1];
    for(int i=0; i<16; i++) {
        if(i2c.read(0xA0, response, 1)) {
            error("Read failed\n");
        }
        printf("address %03d = 0x%02X\n", i, response[0]); 
    }

}