I will be storing a word (uint16_t) in eeprom. It will be rarely changed and read at the start of running of the program code. Time or space are not limiting factors.
Is the code below correct and effective for this purpose?
(XC8 and MPLABX)
Is the code below correct and effective for this purpose?
(XC8 and MPLABX)
Code:
uint16_t EepromRead16(uint8_t Address)
{
uint8_t LS;
uint8_t MS;
uint8_t Sum;
MS = eeprom_read(Address);
LS = eeprom_read(Address + 1);
Sum = eeprom_read(Address + 2);
if(MS + LS == Sum)
return (uint16_t)(MS << 8) + LS;
else
return 0;
}
void EepromWrite16(uint8_t Address, uint16_t Value)
{
uint8_t LS;
uint8_t MS;
uint8_t Sum;
LS = Value & 0xFF;
MS = Value >> 8;
Sum = LS + MS;
eeprom_write(Address, MS);
eeprom_write(Address + 1, LS);
eeprom_write(Address + 2, Sum);
}