diff --git a/Core/Inc/FanControl.h b/Core/Inc/FanControl.h index 6d0eef0..a9fdfca 100644 --- a/Core/Inc/FanControl.h +++ b/Core/Inc/FanControl.h @@ -5,8 +5,14 @@ #include "stm32f4xx_hal_def.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); +void fan_ctrl_update(); + /** * @brief Set the power of the fan (0 -> off, 100 -> full power). */ diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 88b5bfb..44e1912 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -59,6 +59,10 @@ extern uint8_t slave_id; #define THRESH_OV 55050 /* 4.2V */ #define THRESH_UT 0 /* 0C */ #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 */ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim); diff --git a/Core/Src/FanControl.c b/Core/Src/FanControl.c index 7258526..f7c74be 100644 --- a/Core/Src/FanControl.c +++ b/Core/Src/FanControl.c @@ -1,18 +1,48 @@ #include "FanControl.h" +#include "TMP144.h" +#include "main.h" + #include "stm32f4xx_hal_def.h" #include "stm32f4xx_hal_tim.h" +#include + 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; diff --git a/Core/Src/main.c b/Core/Src/main.c index a572064..5a7aabd 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -188,6 +188,7 @@ int main(void) { afe_measure(); tmp144_read_temps(); check_error_conditions(); + fan_ctrl_update(); delay_period(); } /* USER CODE END 3 */