I was programming for a CFE network monitor to check on the quality of the electricity grid in Mexico
This program and hardware can start up generators and electricity backups in case the grid is out of specifications or specified electric limits. It also will reconnect to the grid under controlled conditions. So this Grid Guard protects your house or business form mall functioning grid conditions.
The “grid guard” has a display showing voltage current, Power, Energy, Cos phi, frequency and network status. More options are possible but that is planned for future releases.
One of the functions I needed was to secure data in case of disconnecting or changing the software in the Grid guard. To do this use the EEProm memory. The EEPROM.h library doesn’t have a general function that can write and update any desired variables to EEPROM. I wrote the two functions for this reason and you can use them without including the EEPROM.h libraries.
After compiling by using the arduino IDE it indicated that the two functions use 444 byte of program memory and 9 byte of RAM.
For the ones interested copy and past the code in your sketch and have fun.
//******************************************************************************************// // This function is independent of data type. It copies all standard types but also string // // and structs. This function reads [n] bytes from RAM pointed by ptr and writes them to // // EEmemory at location EEaddress. This function checks if EEmemory contains the value to be// // written. If it has it skips the programming section and runs for the next byte. // //******************************************************************************************// void MEM2EE (unsigned int EEaddress, byte *ptr, unsigned int n) { n=n&0xFF ; //safety statement can be removed while (n != 0) { while(EECR & (1<<EEPE)); // Wait for completion of previous write EEAR = EEaddress; // Set up address register EECR |= (1<<EERE); // Start eeprom read by writing EERE if (EEDR != *ptr) { // skip write if ptr value is equal to EEmem EEDR = *ptr; // load data register with pointed byte EECR |= (1<<EEMPE); // Write logical one to EEMPE EECR |= (1<<EEPE); // Start eeprom write by setting EEPE } // else Serial.println("EQD"); // test if data ptr and EEmem equal print msg ptr++; EEaddress++; n--; } } //******************************************************************************************// // This function is independent of data type. It copies all standard types but also string // // and structs. This function reads n bytes from EEaddress in EEmemory and writes these to // // pointed location ptr in RAM. // //******************************************************************************************// void EE2MEM (unsigned int EEaddress, byte *ptr, unsigned int n) { while (n != 0) { while(EECR & (1<<EEPE)); // Wait for completion of previous write EEAR = EEaddress; // Set up address register EECR |= (1<<EERE); // Start eeprom read by writing EERE *ptr = EEDR; // Assign EEDR data to memory location ptr++; EEaddress++; n--; } }
Examples of how to use these functions.
Assume you want to write to EEPROM memory addresses 540 and 542
You have the following variables :
float fvar1=3.1416, fvar2;
byte bvar1=66, bvar2;
These are the calls
MEM2EE( 540, (byte*)&bvar1, sizeof(bvar1));
EE2MEM( 540, (byte*)&bvar2, sizeof(bvar2));
Serial.println(bvar2);
MEM2EE( 542, (byte*)&fvar1, sizeof(fvar1));
EE2MEM( 542, (byte*)&fvar2, sizeof(fvar2));
Serial.println(fvar2);
Your can write and read any variable, array or struct in this way to and from EEPROM memory
Write a comment if you like it or want to contribute to this code.