Files
linear_trans_temp/Main.c
T
2026-10-22 10:08:59 +08:00

34 lines
959 B
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "sys.h"
/*
* 湿度量程参数
* 湿度原始值 VW[WORD(100)] 为 16 位无符号整数,量程 4000 ~ 20000,
* 对应湿度 0.0% ~ 100.0% RH(工业 4-20mA 惯例)。
*/
#define HUM_RAW_MIN 4000 /* 量程下限原始值,对应 0% RH */
#define HUM_RAW_MAX 20000 /* 量程上限原始值,对应 100% RH */
void init(void)
{
}
void loop(void)
{
/*
* 1. 读取湿度原始值(U16),并转换为浮点参与运算;
* 2. 线性映射:raw 4000~20000 -> 0.0~100.0 %RH;
* 3. 限幅到 [0.0, 100.0],防止越界;
* 4. 结果以 32 位浮点(F32)写入地址 WORD(200)
* (一个浮点占用 2 个字,即 WORD(200) 和 WORD(201))。
*/
float raw = (float)VW[WORD(100)];
float hum = (raw - HUM_RAW_MIN) * 100.0f / (float)(HUM_RAW_MAX - HUM_RAW_MIN);
if (hum < 0.0f)
hum = 0.0f;
if (hum > 100.0f)
hum = 100.0f;
VR[WORD(200)] = hum;
}