Hello I have two simple function to write value to EEPROM, but this don't work correctly. What am i doing wrong something with the conversion?
HAL_StatusTypeDef writeEEPROMByte(uint32_t address, uint8_t data) {
HAL_StatusTypeDef status;
address = address + EEPROM_BASE_ADDRESS;
HAL_FLASHEx_DATAEEPROM_Unlock(); //Unprotect the EEPROM to allow writing
status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, address, data);
HAL_FLASHEx_DATAEEPROM_Lock(); // Reprotect the EEPROM
return status;
}
uint8_t readEEPROMByte(uint32_t address) {
uint8_t data = 0;
address = address + EEPROM_BASE_ADDRESS;
data = *(__IO uint32_t*)address;
return data;
}
void saveToEEPROM (uint32_t address, float data)
{
uint8_t *array;
array = (uint8_t*)(&data);
for(uint32_t i=0;i<4;i++) //float to array of uint8_t
{
writeEEPROMByte(address, array[i]);
}
}
float loadFromEEPROM (uint32_t address)
{
float value = 0;
uint8_t data[3];
for(uint32_t i=0;i<4;i++) //float to array of uint8_t
{
data[i] = readEEPROMByte(i+address);
}
value = *(float *)(&data);
return value;
}
Output for float is 64.00 or 65-70 for bigger numbers
Thanks for answers. I edited functions and change to double precision because I using atof(). But still I don't have good readout, data[7-i] = readEEPROMByte(i+address); Give better results e.g. save - read 2 - 64, 3 - 2112, 4 - 4160,
void saveConfigToEEPROM (uint32_t address, double data)
{
uint8_t *array;
array = (uint8_t*)(&data);
for(int i=0;i<8;i++) //float to array of uint8_t
{
writeEEPROMByte(address+i, array[i]);
}
}
double loadConfigFromEEPROM (uint32_t address)
{
double value = 0;
uint8_t data[8];
for(int i=0;i<8;i++) //float to array of uint8_t
{
data[i] = readEEPROMByte(address+i);
}
value = *(double *)(&data);
return value;
}