Question
Question(C++). Please implement the initialize() and checksum() functions in the EEPROM. Part-01 : initialize() initialize() will overwrite memory addresses 0 through 7 with a string
Question(C++). Please implement the initialize() and checksum() functions in the EEPROM.
Part-01: initialize()
initialize() will overwrite memory addresses 0 through 7 with a string of your choice, and recalculate the checksum and store it at address 8. Keep part 2 commented out for now. 1. familiarize yourself with each function and the logical flow in setup 2. Making use of the EEPROM.write() function, write a string into memory addresses 0 through 7 3. Call checksum() and store the value in address 8
Part-02: checksum()
Perform a calculation on addresses 0 through 7 and return the value. To do this, we will make use of bitwise exclusive or (^) and bitwise not (~). 1. Cumulatively xor each address with one another 2. Return the compliment of this value This algorithm is not perfect. The strings hello and ehllo would have the same checksum. If you have time, think about how you could tweak this algorithm so that it is no longer sensitive to element swaps.
Here is the algorithm:
#include
void setup()
{
Serial.begin(9600);
// for test purposes only
// EEPROM.write(0, 'h'); // overwrite something to simulate data corruption
// print initial state of EEPROM
Serial.println("initial check");
printEEPROM();
// if checksum is not OK, reinitialize EEPROM to default values
if((checkSum() & 0xff) != EEPROM.read(0x08)) {
initialize();
Serial.println("EEPROM has been reinitialized");
}
else {
Serial.println("EEPROM checksum is OK");
}
printEEPROM();
}
void loop() {}
void printEEPROM() {
for(int address = 0; address < 0x09; address++) {
// read a byte from the current address of the EEPROM
char value = EEPROM.read(address);
if (address < 0x10)
Serial.print("00");
else if (address < 0x100)
Serial.print("0");
Serial.print(address, HEX);
Serial.print("\t");
Serial.print(value);
Serial.println();
}
Serial.println();
}
void initialize() {
//put some initial values in addresses 0x0 through 0x7
//Example: EEPROM.write(0, 'B'); //store character B at address 0x0
int twobytes = 14200; // For part 2
/* For part 2
EEPROM.write(7, twobytes);
*/
EEPROM.write(8, checkSum()); //recalculate checksum and store it at 0x8
}
char checkSum() {
//implement a checksum algorithm
return '0'; //just a default value so the code compiles
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started