5.EEPROM (Electrically Erasable Programmable read only memory)
The usage of EEPROM is the same as Arduino UNO, there are two operations: read and write.
Read:
  • I2C address of EEPROM
  • The internal address of EEPROM (the address for storing data)
  • Read data
Write:
  • I2C address of EEPROM
  • The internal address of EEPROM (the address for storing data)
  • Write data
In the BiBoard demo, the address of EEPROM on the I2C bus is 0x54, and the capacity is 8192Bytes (64Kbit). We sequentially write a total of 16 values from 0 to 15 in the EEPROM from the first address, and then read them for comparison. Theoretically, the data written in EEPROM and the data read from the corresponding address should be the same.
In the NyBoard factory test, we also use this method, but it is more complicated. We will use a fixed list to fill the EEPROM and read it out for comparison.
1
#include <Wire.h>
2
3
#define EEPROM_ADDRESS 0x54
4
#define EEPROM_CAPACITY 8192 // 64Kbit
5
#define EEPROM_TESTBYTES 16
6
7
// write 1 byte EEPROM by address
8
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
9
{
10
Wire.beginTransmission(deviceaddress);
11
Wire.write((int)(eeaddress >> 8)); // MSB
12
Wire.write((int)(eeaddress & 0xFF)); // LSB
13
Wire.write(data);
14
Wire.endTransmission();
15
16
delay(5);
17
}
18
19
// read 1 byte EEPROM by address
20
byte readEEPROM(int deviceaddress, unsigned int eeaddress )
21
{
22
byte rdata = 0xFF;
23
24
Wire.beginTransmission(deviceaddress);
25
Wire.write((int)(eeaddress >> 8)); // MSB
26
Wire.write((int)(eeaddress & 0xFF)); // LSB
27
Wire.endTransmission();
28
29
Wire.requestFrom(deviceaddress,1);
30
31
if (Wire.available())
32
rdata = Wire.read();
33
return rdata;
34
}
35
36
void testI2CEEPROM(){
37
38
byte tmpData = 0;
39
40
Serial.println("EEPROM Testing...");
41
42
// write EEPROM from 0 to EEPROM_TESTBYTES
43
for(int i = 0; i < EEPROM_TESTBYTES; i++){
44
writeEEPROM(EEPROM_ADDRESS, i, i % 256);
45
delay(1);
46
}
47
48
Serial.println();
49
50
// read from 0 to EEPROM_TESTBYTES
51
for(int i = 0; i < EEPROM_TESTBYTES; i++){
52
tmpData = (int)readEEPROM(EEPROM_ADDRESS, i);
53
Serial.print(tmpData);
54
Serial.print("\t");
55
}
56
}
57
58
59
void setup(){
60
61
Serial.begin(115200);
62
Wire.begin();
63
64
testI2CEEPROM();
65
}
66
67
void loop(){
68
69
}
Copied!
Note: the EEPROM operations, especially write operations, are generally not put into the loop() loop. Although the EEPROM is resistant to erasing (100,000 times), if a certain block is frequently written in the loop, It will cause the EEPROM to malfunction.
Last modified 6mo ago
Export as PDF
Copy link