Implement simple fan control

This commit is contained in:
jazzpi
2022-06-24 04:38:23 +02:00
parent 405b255936
commit 2c4a428534
4 changed files with 41 additions and 0 deletions

View File

@ -1,18 +1,48 @@
#include "FanControl.h"
#include "TMP144.h"
#include "main.h"
#include "stm32f4xx_hal_def.h"
#include "stm32f4xx_hal_tim.h"
#include <stdint.h>
TIM_HandleTypeDef* timer;
uint32_t channel;
FanState fan_state;
HAL_StatusTypeDef fan_ctrl_init(TIM_HandleTypeDef* handle, uint32_t channel_) {
timer = handle;
channel = channel_;
fan_state = FANS_OFF;
return HAL_TIM_PWM_Start(timer, channel);
}
void fan_ctrl_update() {
if (fan_state == FANS_OFF && max_temperature > THRESH_FANS_ON_HALF) {
fan_state = FANS_ON;
} else if (fan_state == FANS_ON && max_temperature < THRESH_FANS_OFF) {
fan_state = FANS_OFF;
}
uint32_t power;
if (fan_state == FANS_OFF) {
power = 0;
} else {
if (max_temperature < THRESH_FANS_ON_HALF) {
power = 50;
} else if (max_temperature > THRESH_FANS_ON_FULL) {
power = 100;
} else {
power = (100 * (max_temperature - THRESH_FANS_ON_HALF)) /
(THRESH_FANS_ON_FULL - THRESH_FANS_ON_HALF);
}
}
fan_ctrl_set_power(power);
}
void fan_ctrl_set_power(uint32_t percent) {
if (percent > 100) {
percent = 100;

View File

@ -188,6 +188,7 @@ int main(void) {
afe_measure();
tmp144_read_temps();
check_error_conditions();
fan_ctrl_update();
delay_period();
}
/* USER CODE END 3 */