我需要编写一个函数,该函数使用查找表来获取温度传感器模拟输入的ADC值,并通过"插值" - 线性近似来找出给定ADC值的温度.我已经创建了一个函数并为它编写了一些测试用例,我想知道是否有一些你可以建议改进代码的东西,因为这应该是嵌入式uC,可能是stm32.
我发布我的代码并附加我的C文件,它将编译并运行.
如果您有任何改进意见/建议,请与我们联系.
我还想知道一些关于从uint32_t到我正在做的浮动的转换,如果它是有效的代码方式.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define TEMP_ADC_TABLE_SIZE 15
typedef struct
{
int8_t temp;
uint16_t ADC;
}Temp_ADC_t;
const Temp_ADC_t temp_ADC[TEMP_ADC_TABLE_SIZE] =
{
{-40,880}, {-30,750},
{-20,680}, {-10,595},
{0,500}, {10,450},
{20,410}, {30,396},
{40,390}, {50,386},
{60,375}, {70,360},
{80,340}, {90,325},
{100,310}
};
// This function finds the indices between which the input reading lies.
// It uses an algorithm that doesn't need to loop through all the values in the
// table but instead it keeps …Run Code Online (Sandbox Code Playgroud) c ×1