-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEEprom.cpp
86 lines (74 loc) · 2.62 KB
/
EEprom.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "EEprom.h"
#include "Wire.h"
EEprom::EEprom(int addr)
// initialization function to start the class and set the device address of the eeprom
// addr = the I2C device address of the eeprom
{
_Addr = addr;
}
bool EEprom::write(int address, byte data)
// function to write a byte to the eeprom
// address = is the address to write to in the EEprom
// data = is the data byte to write to the eeprom
{
byte bytewritten;
Wire.beginTransmission(_Addr); // starts communication of the I2C to the I2c device address
Wire.write((uint16_t)(address >> 8)); // writes the first byte of the pointer address to the device
Wire.write((uint16_t)(address & 0xFF)); // writes the second byte of the pointer address to the device
Wire.write(data); // writes the byte data to the EEPROM at the address specified above
Wire.endTransmission(); // stops the write communication on the I2C
delay(10);
if ((serialDebug & 8) == 8) // if serialDebug is on for eeprom read/writes print to the serial console
{
Serial.print("Write ");
Serial.print(address);
Serial.print(",");
Serial.println(data);
}
bytewritten = read(address); // read the value that was written to the eeprom back out
if (bytewritten == data){ return true; } // compare the value written to the value that was written to the eeprom and return the bool
else false;
}
byte EEprom::read(int address)
// function to read out a byte from the eeprom
// address = the address to read from in the eeprom
{
byte result; // returned value
Wire.beginTransmission(_Addr); // starts communication of the I2C to the I2c device address
Wire.write((uint16_t)(address >> 8)); // writes the first byte of the pointer address to the device
Wire.write((uint16_t)(address & 0xFF)); // writes the second byte of the pointer address to the device
Wire.endTransmission(); // stops the write communication on the I2C
Wire.requestFrom(_Addr, 1); // gets 1 byte of data from the device
result = Wire.read(); // sets the value read to data
if ((serialDebug & 8) == 8)
{
Serial.print("Read ");
Serial.print(address);
Serial.print(",");
Serial.println(result);
}
return result; // returns data to the previous call
}
void EEprom::eraseAll()
// function to erase all of the eeprom
{
Serial.println("Erasing EE2");
uint16_t address = 0;
byte data = 0;
while (address < 256)
{
write(address, 0);
address++;
}
}
void EEprom::readAll()
// function to read out all of the eeprom in one sweep. this will not return anything.
{
Serial.println("Reading EE2");
uint16_t address = 0;
while (address < 256)
{
read(address);
address++;
}
}