Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 0:14e4f511a1bd
- Child:
- 1:8ded5dafc62c
diff -r 000000000000 -r 14e4f511a1bd main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sat Jun 20 06:13:44 2020 +0000
@@ -0,0 +1,66 @@
+#include "mbed.h"
+
+I2C i2c(p9, p10);
+Serial pc(USBTX, USBRX);
+
+#define ADDR 0xA0
+
+int pointerAdddress = 0;
+char s[10];
+
+
+// this function has 63 bytes write limit
+void writeEEPROM(char address, unsigned int eeaddress, char *data, int size)
+{
+ char i2cBuffer[size + 2];
+ i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB
+ i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF00); // LSB
+
+ for (int i = 0; i < size; i++) {
+ i2cBuffer[i + 2] = data[i];
+ }
+
+ int result = i2c.write(ADDR, i2cBuffer, size + 2);
+ wait_ms(6);
+}
+
+// this function has no read limit
+void readEEPROM(char address, unsigned int eeaddress, char *data, int size)
+{
+ char i2cBuffer[2];
+ i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB
+ i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF00); // LSB
+
+ // Reset eeprom pointer address
+ int result = i2c.write(ADDR, i2cBuffer, 2);
+ wait_ms(6);
+
+ // Read eeprom
+ i2c.read(ADDR, data, size);
+ wait_ms(6);
+}
+
+int main()
+{
+ int i, length;
+ char data_read[1];
+ readEEPROM(ADDR, pointerAdddress, data_read, 1);
+ pc.printf("Previous data stored: %s\n", data_read);
+
+ while(1)
+ {
+ i = 1
+ s[0] = i;
+
+ // write tha data
+ writeEEPROM(ADDR, pointerAdddress, s, 1);
+ pc.printf("Data written: %s\n", s);
+
+ // read the data
+ readEEPROM(ADDR, pointerAdddress, data_read, 1);
+ pc.printf("Data read: %s\n", data_read);
+
+ wait(1);
+ }
+
+}
\ No newline at end of file