将浮点数写入 EEPROM 将浮点数转换为 uint8_t [英] write float to EEPROM convert float to uint8_t

查看:89
本文介绍了将浮点数写入 EEPROM 将浮点数转换为 uint8_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有两个简单的函数可以将值写入 EEPROM,但这不能正常工作.我在转换时做错了什么?

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;
}

浮点数的输出为 64.00 或 65-70 以获取更大的数字

Output for float is 64.00 or 65-70 for bigger numbers

感谢您的回答.我编辑了函数并更改为双精度,因为我使用了 atof().但我仍然没有很好的读数,数据[7-i] = readEEPROMByte(i+address);给出更好的结果,例如保存 - 读取2 - 64,3 - 2112,4 - 4160,

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;
}

推荐答案

在函数 saveToEEPROM 的循环中,您重复覆盖相同的地址.

In the loop in function saveToEEPROM you overwrite the same address repeatedly.

在 loadFromEEPROM 的循环中,您从 4 个不同的地址读取 4 个字节,但尝试将它们保存在 3 个字节长的缓冲区中.

In the loop in loadFromEEPROM you read 4 bytes from 4 different addresses but try to save them in a buffer 3 bytes long.

此外,在 readEEPROMByte 中,您将地址转换为 uint32_t 指针,读取 uint32_t,然后将其作为 uint8_t 返回.这可能是未对齐的读取,这在这个平台上可能没问题(您没有准确指定您正在使用的部分)但为了安全起见并使代码更易于阅读,我只会读取一个字节(强制转换为 uint8_t 指针).

Also, in readEEPROMByte you cast the address to uint32_t pointer, read a uint32_t and then return it as a uint8_t. This could be a misaligned read, which might be fine on this platform (you don't specify exactly which part you are using) but to be on the safe side and to make the code easier to read I would just do a byte read (cast to uint8_t pointer).

类似地将字节数组转换为 loadFromEEPROM 中的浮点指针不是一个好习惯,但同样可能仍然没问题.

Similarly casting the array of bytes to float pointer in loadFromEEPROM is not good practice, but again might still be alright.

这篇关于将浮点数写入 EEPROM 将浮点数转换为 uint8_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆