diff --git a/thermistor/main.cpp b/thermistor/main.cpp index d87c722..d8ccdc6 100644 --- a/thermistor/main.cpp +++ b/thermistor/main.cpp @@ -1,6 +1,8 @@ #include "clock.hpp" +#include #include +#include #include "flash/flash.hpp" #include "io/io.hpp" @@ -12,6 +14,38 @@ #define UART0_INT_VECTORS #include "uart/hardware0.hpp" +static constexpr auto SERIES_RESISTOR = 9951; +static constexpr auto THERMISTOR_NOMINAL = 9270; +static constexpr auto BETA_COEFFICIENT = 3212; +static constexpr auto NOMINAL_TEMPERATURE = 25; + +template +double sampleAdc(Adc &adcPin, uint16_t numSamples = 100) +{ + double samples = 0; + + for (uint16_t i = 0; i < numSamples; ++i) + samples += adcPin.read(); + + return samples / numSamples; +} + +double getResistance(double adcSample) +{ + return SERIES_RESISTOR * adcSample / (1023 - adcSample); +} + +double getTemperature(double resistance) +{ + double steinhart = resistance / THERMISTOR_NOMINAL; + steinhart = log(steinhart); + steinhart /= BETA_COEFFICIENT; + steinhart += 1.0 / (NOMINAL_TEMPERATURE + 273.15); + steinhart = 1.0 / steinhart; + steinhart -= 273.15; + return steinhart; +} + int main() { uart::Uart0<> serial; @@ -25,8 +59,18 @@ int main() adcPin.init(); while (true) { - auto adcSample = adcPin.read(); - serial << F("Read ADC: ") << adcSample << F("\r\n"); + const auto adcSample = sampleAdc(adcPin, 1000); + const auto thermistorResistance = getResistance(adcSample); + const auto temperature = getTemperature(thermistorResistance); + + char floatBuffer[16]; + + sprintf(floatBuffer, "%.2f", adcSample); + serial << F("Read ADC: ") << floatBuffer << F("\r\n"); + sprintf(floatBuffer, "%.2f", thermistorResistance); + serial << F("Resistance: ") << floatBuffer << F("\r\n"); + sprintf(floatBuffer, "%.2f", temperature); + serial << F("Temperature: ") << floatBuffer << F(" C \r\n"); _delay_ms(1000); } diff --git a/thermistor/thermistor.cppproj b/thermistor/thermistor.cppproj index 21b9891..65221a8 100644 --- a/thermistor/thermistor.cppproj +++ b/thermistor/thermistor.cppproj @@ -61,118 +61,122 @@ - -mmcu=atmega328p - True - True - True - True - False - True - True - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - True - True - True - True - -fno-threadsafe-statics -std=c11 - True - True - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - True - True - True - -fno-threadsafe-statics -Wextra -std=c++17 - - - libm - - - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - - - NDEBUG - - - Optimize for size (-Os) - - - NDEBUG - - - Optimize for size (-Os) - + -mmcu=atmega328p + True + True + True + True + False + True + True + + + NDEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize for size (-Os) + True + True + True + True + -fno-threadsafe-statics -std=c11 + True + True + + + NDEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize for size (-Os) + True + True + True + -fno-threadsafe-statics -Wextra -std=c++17 + True + + + libm + libprintf_flt + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + - -mmcu=atmega328p - True - True - True - True - False - True - True - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - True - True - True - True - -fno-threadsafe-statics -std=c11 - True - True - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - True - True - True - -fno-threadsafe-statics -Wextra -std=c++17 - - - libm - - - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - - - DEBUG - - - Optimize (-O1) - Maximum (-g3) - - - DEBUG - - - Optimize (-O1) - Maximum (-g3) - Default (-Wa,-g) - + -mmcu=atmega328p + True + True + True + True + False + True + True + + + DEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize (-O1) + True + Maximum (-g3) + True + True + True + -fno-threadsafe-statics -std=c11 + True + True + + + DEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize (-O1) + True + Maximum (-g3) + True + True + -fno-threadsafe-statics -Wextra -std=c++17 + True + + + libm + libprintf_flt + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Default (-Wa,-g) +