From 19b2a3f088c90967babb3b03e21a36a10005278d Mon Sep 17 00:00:00 2001 From: Carsten Keller Date: Sun, 9 Jun 2024 17:58:12 +0200 Subject: [PATCH] =?UTF-8?q?L=C3=BCftersteuerung=20ist=20abh=C3=A4ngig=20vo?= =?UTF-8?q?n=20der=20Temperatur=20und=20muss=20nicht=20mehr=20manuell=20ge?= =?UTF-8?q?setzt=20werden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/FanControl.cpp | 49 +++++++++++++++++++++++++++++++++++++++---- Source/FanControl.hpp | 4 +++- 2 files changed, 48 insertions(+), 5 deletions(-) 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); }; }