diff --git a/Source/FanControl.cpp b/Source/FanControl.cpp index 75160d3..7b76bd4 100644 --- a/Source/FanControl.cpp +++ b/Source/FanControl.cpp @@ -11,6 +11,7 @@ #include "FanControl.hpp" #define TIMER_RELOAD_VALUE 2559UL +#define DUTY_TO_TIM_VALUE(x) TIMER_RELOAD_VALUE * (x) / 100U static std::uint32_t fan_speed; static std::uint32_t duty; @@ -23,10 +24,19 @@ extern "C" void TIM14_IRQHandler(void) } extern "C" void FanControl_SetDuty(uint32_t d) { - duty = TIMER_RELOAD_VALUE * d / 100UL ; + duty = DUTY_TO_TIM_VALUE(d); } namespace ElektronischeLast { + static const std::uint16_t stuetzpunkte [5][2] = + { + { 25U, DUTY_TO_TIM_VALUE(0U) }, + { 40U, DUTY_TO_TIM_VALUE(30U) }, + { 50U, DUTY_TO_TIM_VALUE(50U) }, + { 65U, DUTY_TO_TIM_VALUE(75U) }, + { 80U, DUTY_TO_TIM_VALUE(100U) } + }; + FanControl::FanControl(void) { /* TIM3 PWM-Output @@ -105,7 +115,8 @@ namespace ElektronischeLast LL_TIM_EnableCounter(TIM3); LL_TIM_EnableCounter(TIM14); - this->compare_value = 0UL; + this->last_compare = 0UL; + this->last_temp = 0UL; NVIC_SetPriority(TIM14_IRQn, 0); NVIC_EnableIRQ(TIM14_IRQn); @@ -113,8 +124,38 @@ namespace ElektronischeLast void FanControl::run(std::uint32_t temp) { - this->compare_value = duty; - LL_TIM_OC_SetCompareCH1(TIM3, this->compare_value); + if(duty != 0U) + { + LL_TIM_OC_SetCompareCH1(TIM3, duty); + } + else + { + if(this->last_temp != temp) + { + std::uint16_t cv = this->get_compare_value(temp); + if(this->last_compare != cv) + { + cv = (this->last_compare + cv) / 2U; + LL_TIM_OC_SetCompareCH1(TIM3, cv); + this->last_compare = cv; + } + this->last_temp = temp; + } + } + } + + std::uint16_t FanControl::get_compare_value(std::uint16_t temp) + { + std::uint16_t cv = TIMER_RELOAD_VALUE; + for(std::uint32_t i = 0UL; i < sizeof(stuetzpunkte) / sizeof(stuetzpunkte[0]); i++) + { + if(temp < stuetzpunkte[i][0]) + { + cv = stuetzpunkte[i][1]; + break; + } + } + return cv; } /** diff --git a/Source/FanControl.hpp b/Source/FanControl.hpp index 596d5e3..79ec3a7 100644 --- a/Source/FanControl.hpp +++ b/Source/FanControl.hpp @@ -22,7 +22,9 @@ namespace ElektronischeLast void run(std::uint32_t temp); std::uint32_t get_speed(void); private: - std::uint32_t compare_value; + std::uint32_t last_compare; + std::uint32_t last_temp; + std::uint16_t get_compare_value(std::uint16_t temp); }; }