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

@ -5,8 +5,14 @@
#include "stm32f4xx_hal_def.h" #include "stm32f4xx_hal_def.h"
#include "stm32f4xx_hal_tim.h" #include "stm32f4xx_hal_tim.h"
typedef enum { FANS_OFF, FANS_ON } FanState;
extern FanState fan_state;
HAL_StatusTypeDef fan_ctrl_init(TIM_HandleTypeDef* handle, uint32_t channel); HAL_StatusTypeDef fan_ctrl_init(TIM_HandleTypeDef* handle, uint32_t channel);
void fan_ctrl_update();
/** /**
* @brief Set the power of the fan (0 -> off, 100 -> full power). * @brief Set the power of the fan (0 -> off, 100 -> full power).
*/ */

View File

@ -59,6 +59,10 @@ extern uint8_t slave_id;
#define THRESH_OV 55050 /* 4.2V */ #define THRESH_OV 55050 /* 4.2V */
#define THRESH_UT 0 /* 0C */ #define THRESH_UT 0 /* 0C */
#define THRESH_OT 880 /* 55C */ #define THRESH_OT 880 /* 55C */
#define THRESH_FANS_ON_HALF 640 /* 40C */
#define THRESH_FANS_ON_FULL 800 /* 50C */
#define THRESH_FANS_OFF 560 /* 35C */
/* USER CODE END EM */ /* USER CODE END EM */
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim); void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim);

View File

@ -1,18 +1,48 @@
#include "FanControl.h" #include "FanControl.h"
#include "TMP144.h"
#include "main.h"
#include "stm32f4xx_hal_def.h" #include "stm32f4xx_hal_def.h"
#include "stm32f4xx_hal_tim.h" #include "stm32f4xx_hal_tim.h"
#include <stdint.h>
TIM_HandleTypeDef* timer; TIM_HandleTypeDef* timer;
uint32_t channel; uint32_t channel;
FanState fan_state;
HAL_StatusTypeDef fan_ctrl_init(TIM_HandleTypeDef* handle, uint32_t channel_) { HAL_StatusTypeDef fan_ctrl_init(TIM_HandleTypeDef* handle, uint32_t channel_) {
timer = handle; timer = handle;
channel = channel_; channel = channel_;
fan_state = FANS_OFF;
return HAL_TIM_PWM_Start(timer, channel); 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) { void fan_ctrl_set_power(uint32_t percent) {
if (percent > 100) { if (percent > 100) {
percent = 100; percent = 100;

View File

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