负压 STM32 程序1.1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.4 KiB

2 weeks ago
/*
* blowers_pwm_action.c
*
* Created on: Apr 9, 2026
* Author: SHI
*/
#include "blowers_pwm_action.h"
#include "tim.h" // 用于 HAL_TIM_PWM_Start 等
static void Blowers_On_Do(transition_t *p_this);
static void Blowers_Off_Do(transition_t *p_this);
static void setBlowersSpeed(uint8_t percent);
transition_t current_blowers_state;
transition_state_t blowers_on_state = {NULL, Blowers_On_Do, NULL};
transition_state_t blowers_off_state= {NULL, Blowers_Off_Do,NULL};
static void Blowers_On_Do(transition_t *p_this)
{
setBlowersSpeed(GV.speed_blowers_pwm);
}
static void Blowers_Off_Do(transition_t *p_this)
{
setBlowersSpeed(0);
}
// 风机转速百分比控制 (0~100)
// 0%: 停止, 10%: 最低速, 90%: 最高速
static void setBlowersSpeed(uint8_t percent)
{
uint32_t pulse;
if (percent <= 0)
{
pulse = 0; // 停止 (占空比 <10%)
} else if (percent < 10)
{
pulse = 100; // 最低速 (占空比 10%)
} else if (percent > 90)
{
pulse = 900; // 最高速 (占空比 90%)
} else
{
pulse = 100 + (percent - 10) * 10;// 线性映射 10%~90% → 100~900
}
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, pulse);
}
// 风机控制初始化 (在 初始化 中调用)
void blowers_Control_Init(void)
{
// 启动 TIM2 PWM 输出
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
// 初始设置为停止
setBlowersSpeed(0);
}