2020-03-29 01:46:11 +01:00
|
|
|
#include "clock.hpp"
|
|
|
|
|
2020-03-29 06:20:21 +02:00
|
|
|
#include <math.h>
|
2020-03-29 01:46:11 +01:00
|
|
|
#include <stdint.h>
|
2020-03-29 06:20:21 +02:00
|
|
|
#include <stdio.h>
|
2020-03-29 01:46:11 +01:00
|
|
|
|
|
|
|
#include "flash/flash.hpp"
|
2020-03-29 04:18:26 +02:00
|
|
|
#include "io/io.hpp"
|
2020-03-29 01:46:11 +01:00
|
|
|
#include "uart/uart.hpp"
|
|
|
|
|
2020-03-29 04:18:26 +02:00
|
|
|
#define ADC_INT_VECTOR
|
|
|
|
#include "adc/adc.hpp"
|
|
|
|
|
2020-03-29 01:46:11 +01:00
|
|
|
#define UART0_INT_VECTORS
|
|
|
|
#include "uart/hardware0.hpp"
|
|
|
|
|
2020-03-29 06:20:21 +02:00
|
|
|
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 <typename Adc>
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-29 01:29:59 +01:00
|
|
|
int main()
|
|
|
|
{
|
2020-03-29 01:46:11 +01:00
|
|
|
uart::Uart0<> serial;
|
|
|
|
serial.init();
|
|
|
|
|
|
|
|
serial << F("Thermistor reader\r\n");
|
|
|
|
|
2020-03-29 04:18:26 +02:00
|
|
|
using adc_conf = adc::Config<adc::SingleMode>;
|
|
|
|
|
|
|
|
adc::Adc<adc_conf, io::P, io::P::C0> adcPin;
|
|
|
|
adcPin.init();
|
|
|
|
|
|
|
|
while (true) {
|
2020-03-29 06:20:21 +02:00
|
|
|
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");
|
2020-03-29 04:18:26 +02:00
|
|
|
|
|
|
|
_delay_ms(1000);
|
|
|
|
}
|
|
|
|
|
2020-03-29 01:46:11 +01:00
|
|
|
serial.flushTx();
|
|
|
|
|
2020-03-29 01:29:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|