Refactor thermistor code into static class
This commit is contained in:
parent
2575863048
commit
eee348d851
@ -1,7 +1,5 @@
|
||||
#include "clock.hpp"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "flash/flash.hpp"
|
||||
@ -14,37 +12,7 @@
|
||||
#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 <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;
|
||||
}
|
||||
#include "thermistor.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
@ -58,10 +26,12 @@ int main()
|
||||
adc::Adc<adc_conf, io::P, io::P::C0> adcPin;
|
||||
adcPin.init();
|
||||
|
||||
Thermistor temp;
|
||||
|
||||
while (true) {
|
||||
const auto adcSample = sampleAdc(adcPin, 1000);
|
||||
const auto thermistorResistance = getResistance(adcSample);
|
||||
const auto temperature = getTemperature(thermistorResistance);
|
||||
const auto adcSample = temp.sampleAdc(adcPin, 1000);
|
||||
const auto thermistorResistance = temp.getResistance(adcSample);
|
||||
const auto temperature = temp.getTemperature(thermistorResistance);
|
||||
|
||||
char floatBuffer[16];
|
||||
|
||||
|
19
thermistor/thermistor.cpp
Normal file
19
thermistor/thermistor.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "thermistor.hpp"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
double Thermistor::getResistance(double adcSample)
|
||||
{
|
||||
return SERIES_RESISTOR * adcSample / (1023 - adcSample);
|
||||
}
|
||||
|
||||
double Thermistor::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;
|
||||
}
|
@ -212,6 +212,12 @@
|
||||
<Compile Include="main.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="thermistor.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="thermistor.hpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="uart\config.hpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
|
27
thermistor/thermistor.hpp
Normal file
27
thermistor/thermistor.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
class Thermistor {
|
||||
public:
|
||||
template <typename Adc>
|
||||
static 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;
|
||||
}
|
||||
|
||||
static double getResistance(double adcSample);
|
||||
|
||||
static double getTemperature(double resistance);
|
||||
|
||||
private:
|
||||
static constexpr auto SERIES_RESISTOR = 9951;
|
||||
static constexpr auto THERMISTOR_NOMINAL = 9270;
|
||||
static constexpr auto BETA_COEFFICIENT = 3212;
|
||||
static constexpr auto NOMINAL_TEMPERATURE = 25;
|
||||
};
|
Loading…
Reference in New Issue
Block a user