Test Program for AT241024. Byte R/W, Page R/W

Dependencies:   AT24C1024 mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * AT24C1024 Test
00003  * 
00004  * mbed: Revision: 121
00005  * mbed-rtos: Revision: 117
00006  *
00007  * 2016.11.25 Created
00008  *
00009  */
00010  
00011 #include "mbed.h"
00012 #include "rtos.h"
00013 #include "AT24C1024.h"
00014 
00015 I2C         i2c(D14, D15);      // SDA, SCL
00016 AT24C1024   at24c1024(i2c);     // Atmel 1Mbit EE-PROM
00017 
00018 DigitalOut led1(LED1);
00019 
00020 int main()
00021 {
00022     printf("\r\n*** AT24C1024 Test ***\r\n");
00023     
00024     at24c1024.frequency(100000);
00025 
00026     // Byte Read/Write
00027     // 
00028     uint8_t dt = 0x55;
00029     printf("Byte Write: %02x\r\n", dt);
00030     at24c1024.write(0, dt);     // write addr=0 data=dt
00031     wait_ms(5);
00032     dt = at24c1024.read(0);     // read addr=0
00033     printf("Byte Read: %02x\r\n", dt);
00034 
00035     // Page Read/Write
00036     // 
00037     uint8_t eep_buf[258], r_eep_buf[258];
00038     
00039     // Zero Clear
00040     for (int i = 0; i < 256; i++) {
00041         eep_buf[i] = 0;
00042         r_eep_buf[i] = 0xff;
00043     }
00044     printf("Page Write: Zero Clear\r\n");
00045     AT24C_STATUS status = at24c1024.write_page(0x1ff00, eep_buf, sizeof(eep_buf));
00046     wait_ms(5);
00047     printf("Status: %d\r\n", status);
00048     printf("Page Read:\r\n");
00049     status = at24c1024.read_page(0x1ff00, r_eep_buf, sizeof(r_eep_buf));
00050     printf("Status: %d\r\n", status);
00051     for (int i = 0; i < sizeof(r_eep_buf); i++) {
00052         printf("%d ", r_eep_buf[i]);
00053     }
00054     printf("\r\n");
00055     
00056     // Set 0..255
00057     for (int i = 0; i < 256; i++) {
00058         eep_buf[i] = i;
00059         r_eep_buf[i] = 0x00;
00060     }
00061     printf("Page Write: 0..255\r\n");
00062     status = at24c1024.write_page(0x1ff00, eep_buf, sizeof(eep_buf));
00063     wait_ms(5);
00064     printf("Status: %d\r\n", status);
00065     printf("Page Read:\r\n");
00066     status = at24c1024.read_page(0x1ff00, r_eep_buf, sizeof(r_eep_buf));
00067     printf("Status: %d\r\n", status);
00068     for (int i = 0; i < sizeof(r_eep_buf); i++) {
00069         printf("%d ", r_eep_buf[i]);
00070     }
00071     printf("\r\n");
00072 
00073     while(1) {
00074         led1 = !led1;
00075         wait(0.2);
00076     }
00077 }