huyongji1.1-system/App/wendu/wendu.c

59 lines
1.9 KiB
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"wendu.h"
// 添加温度阈值变量(默认值)
float temp_lower_limit = 15.0f; // 默认下限温度15℃
float temp_upper_limit = 20.0f; // 默认上限温度20℃
uint16_t adc_value = 0;
float voltage = 0.0f, temperature = 0.0f;
// 读取和打印温度的函数
#define BALANCE_R 10000.0f // 10K分压电阻
#define NTC_R25 10000.0f // NTC在25℃时的电阻值假设是10K NTC
#define BETA 3950.0f // B值需要根据您的NTC型号修改
#define KELVIN_OFFSET 273.15f
void Print_Temperature(void)
{
const float CALIBRATION_FACTOR = 0.6f;
HAL_ADC_Start(&hadc3);
if(HAL_ADC_PollForConversion(&hadc3, 100) == HAL_OK)
{
// 读取ADC值
adc_value = HAL_ADC_GetValue(&hadc3);
// 转换为电压值,并应用校准系数
voltage = (float)adc_value * (3.3f / 4096.0f) * CALIBRATION_FACTOR;
// 计算NTC电阻值
// 如果NTC接VCC上拉用这个
float ntc_r = BALANCE_R * voltage / (3.3f - voltage);
// 如果NTC接地下拉用这个
// float ntc_r = BALANCE_R * (3.3f - voltage) / voltage;
// 计算温度
temperature = 1.0f / (log(ntc_r/NTC_R25)/BETA + 1.0f/(25.0f + KELVIN_OFFSET)) - KELVIN_OFFSET;
// 控制加热膜逻辑
// 使用可调节的温度阈值进行控制
if (temperature < temp_lower_limit) {
// 温度低于下限,开启加热
Control_Heater(1);
//printf("温度%.2f低于%.2f℃,开启加热\n", temperature, temp_lower_limit);
}
else if (temperature > temp_upper_limit) {
// 温度高于上限,关闭加热
Control_Heater(0);
// printf("温度%.2f高于%.2f℃,关闭加热\n", temperature, temp_upper_limit);
}
}
HAL_ADC_Stop(&hadc3);
}