Add fan control according to temperature
This commit is contained in:
parent
eee348d851
commit
094e61a7c1
@ -12,11 +12,28 @@
|
||||
#define UART0_INT_VECTORS
|
||||
#include "uart/hardware0.hpp"
|
||||
|
||||
#include "pwm.hpp"
|
||||
#include "thermistor.hpp"
|
||||
|
||||
template <typename T>
|
||||
T clamp(double value, T lower, T upper)
|
||||
{
|
||||
if (value < lower)
|
||||
return lower;
|
||||
if (value > upper)
|
||||
return upper;
|
||||
return static_cast<T>(value);
|
||||
}
|
||||
|
||||
uint8_t temperatureCurve(double temperature)
|
||||
{
|
||||
double fanSpeed = (10 * temperature - 200) / 3;
|
||||
return clamp<uint8_t>(fanSpeed, 0, 100);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
uart::Uart0<> serial;
|
||||
uart::Uart0<uart::Config<115200>> serial;
|
||||
serial.init();
|
||||
|
||||
serial << F("Thermistor reader\r\n");
|
||||
@ -28,10 +45,14 @@ int main()
|
||||
|
||||
Thermistor temp;
|
||||
|
||||
pwm::init();
|
||||
pwm::setDuty(100);
|
||||
|
||||
while (true) {
|
||||
const auto adcSample = temp.sampleAdc(adcPin, 1000);
|
||||
const auto thermistorResistance = temp.getResistance(adcSample);
|
||||
const auto temperature = temp.getTemperature(thermistorResistance);
|
||||
const auto fanSpeed = temperatureCurve(temperature);
|
||||
|
||||
char floatBuffer[16];
|
||||
|
||||
@ -41,6 +62,9 @@ int main()
|
||||
serial << F("Resistance: ") << floatBuffer << F("\r\n");
|
||||
sprintf(floatBuffer, "%.2f", temperature);
|
||||
serial << F("Temperature: ") << floatBuffer << F(" C \r\n");
|
||||
serial << F("Fan speed: ") << fanSpeed << F("%\r\n");
|
||||
|
||||
pwm::setDuty(fanSpeed);
|
||||
|
||||
_delay_ms(1000);
|
||||
}
|
||||
|
25
thermistor/pwm.cpp
Normal file
25
thermistor/pwm.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "pwm.hpp"
|
||||
|
||||
#include "io/io.hpp"
|
||||
|
||||
namespace pwm {
|
||||
|
||||
static constexpr uint8_t PWM_TOP = 40;
|
||||
|
||||
void init()
|
||||
{
|
||||
io::Pin<io::P::D5> pwmPin;
|
||||
pwmPin.dir(io::Dir::OUT);
|
||||
pwmPin = false;
|
||||
|
||||
TCCR0A = (1 << COM0B1) | (1 << WGM00);
|
||||
TCCR0B = (1 << WGM02) | (1 << CS01);
|
||||
OCR0A = PWM_TOP;
|
||||
}
|
||||
|
||||
void setDuty(uint8_t percent)
|
||||
{
|
||||
OCR0B = (percent * PWM_TOP) / 100;
|
||||
}
|
||||
|
||||
} // namespace pwm
|
10
thermistor/pwm.hpp
Normal file
10
thermistor/pwm.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace pwm {
|
||||
|
||||
void init();
|
||||
void setDuty(uint8_t percent);
|
||||
|
||||
} // namespace pwm
|
@ -212,6 +212,12 @@
|
||||
<Compile Include="main.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="pwm.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="pwm.hpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="thermistor.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
|
Loading…
Reference in New Issue
Block a user