-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEEPROMFunctions.cpp
More file actions
53 lines (44 loc) · 1.94 KB
/
EEPROMFunctions.cpp
File metadata and controls
53 lines (44 loc) · 1.94 KB
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
// Functions to read and write datato the Arduino EEPROM memory
//
//
#include "EEPROMFunctions.h"
//This function will write a 4 byte (32bit) long to the eeprom at
//the specified address to address + 3.
void EEPROMWritelong(int address, uint32_t value)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
// Write the 4 bytes into the eeprom memory. Update function only writes if value was updated (Minimizd EEPROM wear)
EEPROM.update(address, one);
EEPROM.update(address + 1, two);
EEPROM.update(address + 2, three);
EEPROM.update(address + 3, four);
}
//This function will return an unsigned 4 byte (32bit) long from the eeprom
//at the specified address to address + 3.
uint32_t EEPROMReadUnsignedLong(int address)
{
//Read the 4 bytes from the eeprom memory.
uint32_t one = (uint32_t)EEPROM.read(address);
uint32_t two = (uint32_t)EEPROM.read(address + 1);
uint32_t three = (uint32_t)EEPROM.read(address + 2);
uint32_t four = (uint32_t)EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
//This function will return a signed 4 byte (32bit) long from the eeprom
//at the specified address to address + 3.
int32_t EEPROMReadSignedLong(int address)
{
//Read the 4 bytes from the eeprom memory.
int32_t one = (uint32_t)EEPROM.read(address);
int32_t two = (uint32_t)EEPROM.read(address + 1);
int32_t three = (uint32_t)EEPROM.read(address + 2);
int32_t four = (uint32_t)EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}