Implement temperature calculation

This commit is contained in:
BlackMark 2020-03-29 06:20:21 +02:00
parent 3042194aa1
commit 8cf17dc774
2 changed files with 157 additions and 109 deletions

View File

@ -1,6 +1,8 @@
#include "clock.hpp" #include "clock.hpp"
#include <math.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include "flash/flash.hpp" #include "flash/flash.hpp"
#include "io/io.hpp" #include "io/io.hpp"
@ -12,6 +14,38 @@
#define UART0_INT_VECTORS #define UART0_INT_VECTORS
#include "uart/hardware0.hpp" #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;
}
int main() int main()
{ {
uart::Uart0<> serial; uart::Uart0<> serial;
@ -25,8 +59,18 @@ int main()
adcPin.init(); adcPin.init();
while (true) { while (true) {
auto adcSample = adcPin.read(); const auto adcSample = sampleAdc(adcPin, 1000);
serial << F("Read ADC: ") << adcSample << F("\r\n"); 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); _delay_ms(1000);
} }

View File

@ -61,118 +61,122 @@
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<ToolchainSettings> <ToolchainSettings>
<AvrGccCpp> <AvrGccCpp>
<avrgcc.common.Device>-mmcu=atmega328p</avrgcc.common.Device> <avrgcc.common.Device>-mmcu=atmega328p</avrgcc.common.Device>
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex> <avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss> <avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep> <avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec> <avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures> <avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned> <avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned> <avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcc.compiler.directories.IncludePaths> <avrgcc.compiler.symbols.DefSymbols>
<ListValues> <ListValues>
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value> <Value>NDEBUG</Value>
</ListValues> </ListValues>
</avrgcc.compiler.directories.IncludePaths> </avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum> <avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings> <ListValues>
<avrgcc.compiler.warnings.ExtraWarnings>True</avrgcc.compiler.warnings.ExtraWarnings> <Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value>
<avrgcc.compiler.warnings.Pedantic>True</avrgcc.compiler.warnings.Pedantic> </ListValues>
<avrgcc.compiler.miscellaneous.OtherFlags>-fno-threadsafe-statics -std=c11</avrgcc.compiler.miscellaneous.OtherFlags> </avrgcc.compiler.directories.IncludePaths>
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned> <avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level>
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned> <avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcccpp.compiler.directories.IncludePaths> <avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
<ListValues> <avrgcc.compiler.warnings.ExtraWarnings>True</avrgcc.compiler.warnings.ExtraWarnings>
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value> <avrgcc.compiler.warnings.Pedantic>True</avrgcc.compiler.warnings.Pedantic>
</ListValues> <avrgcc.compiler.miscellaneous.OtherFlags>-fno-threadsafe-statics -std=c11</avrgcc.compiler.miscellaneous.OtherFlags>
</avrgcccpp.compiler.directories.IncludePaths> <avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum> <avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings> <avrgcccpp.compiler.symbols.DefSymbols>
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic> <ListValues>
<avrgcccpp.compiler.miscellaneous.OtherFlags>-fno-threadsafe-statics -Wextra -std=c++17</avrgcccpp.compiler.miscellaneous.OtherFlags> <Value>NDEBUG</Value>
<avrgcccpp.linker.libraries.Libraries> </ListValues>
<ListValues> </avrgcccpp.compiler.symbols.DefSymbols>
<Value>libm</Value> <avrgcccpp.compiler.directories.IncludePaths>
</ListValues> <ListValues>
</avrgcccpp.linker.libraries.Libraries> <Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value>
<avrgcccpp.assembler.general.IncludePaths> </ListValues>
<ListValues> </avrgcccpp.compiler.directories.IncludePaths>
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value> <avrgcccpp.compiler.optimization.level>Optimize for size (-Os)</avrgcccpp.compiler.optimization.level>
</ListValues> <avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
</avrgcccpp.assembler.general.IncludePaths> <avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
<avrgcc.compiler.symbols.DefSymbols> <avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
<ListValues> <avrgcccpp.compiler.miscellaneous.OtherFlags>-fno-threadsafe-statics -Wextra -std=c++17</avrgcccpp.compiler.miscellaneous.OtherFlags>
<Value>NDEBUG</Value> <avrgcccpp.linker.general.UseVprintfLibrary>True</avrgcccpp.linker.general.UseVprintfLibrary>
</ListValues> <avrgcccpp.linker.libraries.Libraries>
</avrgcc.compiler.symbols.DefSymbols> <ListValues>
<avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level> <Value>libm</Value>
<avrgcccpp.compiler.symbols.DefSymbols> <Value>libprintf_flt</Value>
<ListValues> </ListValues>
<Value>NDEBUG</Value> </avrgcccpp.linker.libraries.Libraries>
</ListValues> <avrgcccpp.assembler.general.IncludePaths>
</avrgcccpp.compiler.symbols.DefSymbols> <ListValues>
<avrgcccpp.compiler.optimization.level>Optimize for size (-Os)</avrgcccpp.compiler.optimization.level> <Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value>
</AvrGccCpp> </ListValues>
</avrgcccpp.assembler.general.IncludePaths>
</AvrGccCpp>
</ToolchainSettings> </ToolchainSettings>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<ToolchainSettings> <ToolchainSettings>
<AvrGccCpp> <AvrGccCpp>
<avrgcc.common.Device>-mmcu=atmega328p</avrgcc.common.Device> <avrgcc.common.Device>-mmcu=atmega328p</avrgcc.common.Device>
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex> <avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss> <avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep> <avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec> <avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures> <avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned> <avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned> <avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcc.compiler.directories.IncludePaths> <avrgcc.compiler.symbols.DefSymbols>
<ListValues> <ListValues>
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value> <Value>DEBUG</Value>
</ListValues> </ListValues>
</avrgcc.compiler.directories.IncludePaths> </avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum> <avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings> <ListValues>
<avrgcc.compiler.warnings.ExtraWarnings>True</avrgcc.compiler.warnings.ExtraWarnings> <Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value>
<avrgcc.compiler.warnings.Pedantic>True</avrgcc.compiler.warnings.Pedantic> </ListValues>
<avrgcc.compiler.miscellaneous.OtherFlags>-fno-threadsafe-statics -std=c11</avrgcc.compiler.miscellaneous.OtherFlags> </avrgcc.compiler.directories.IncludePaths>
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned> <avrgcc.compiler.optimization.level>Optimize (-O1)</avrgcc.compiler.optimization.level>
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned> <avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcccpp.compiler.directories.IncludePaths> <avrgcc.compiler.optimization.DebugLevel>Maximum (-g3)</avrgcc.compiler.optimization.DebugLevel>
<ListValues> <avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value> <avrgcc.compiler.warnings.ExtraWarnings>True</avrgcc.compiler.warnings.ExtraWarnings>
</ListValues> <avrgcc.compiler.warnings.Pedantic>True</avrgcc.compiler.warnings.Pedantic>
</avrgcccpp.compiler.directories.IncludePaths> <avrgcc.compiler.miscellaneous.OtherFlags>-fno-threadsafe-statics -std=c11</avrgcc.compiler.miscellaneous.OtherFlags>
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum> <avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings> <avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic> <avrgcccpp.compiler.symbols.DefSymbols>
<avrgcccpp.compiler.miscellaneous.OtherFlags>-fno-threadsafe-statics -Wextra -std=c++17</avrgcccpp.compiler.miscellaneous.OtherFlags> <ListValues>
<avrgcccpp.linker.libraries.Libraries> <Value>DEBUG</Value>
<ListValues> </ListValues>
<Value>libm</Value> </avrgcccpp.compiler.symbols.DefSymbols>
</ListValues> <avrgcccpp.compiler.directories.IncludePaths>
</avrgcccpp.linker.libraries.Libraries> <ListValues>
<avrgcccpp.assembler.general.IncludePaths> <Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value>
<ListValues> </ListValues>
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value> </avrgcccpp.compiler.directories.IncludePaths>
</ListValues> <avrgcccpp.compiler.optimization.level>Optimize (-O1)</avrgcccpp.compiler.optimization.level>
</avrgcccpp.assembler.general.IncludePaths> <avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcc.compiler.symbols.DefSymbols> <avrgcccpp.compiler.optimization.DebugLevel>Maximum (-g3)</avrgcccpp.compiler.optimization.DebugLevel>
<ListValues> <avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
<Value>DEBUG</Value> <avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
</ListValues> <avrgcccpp.compiler.miscellaneous.OtherFlags>-fno-threadsafe-statics -Wextra -std=c++17</avrgcccpp.compiler.miscellaneous.OtherFlags>
</avrgcc.compiler.symbols.DefSymbols> <avrgcccpp.linker.general.UseVprintfLibrary>True</avrgcccpp.linker.general.UseVprintfLibrary>
<avrgcc.compiler.optimization.level>Optimize (-O1)</avrgcc.compiler.optimization.level> <avrgcccpp.linker.libraries.Libraries>
<avrgcc.compiler.optimization.DebugLevel>Maximum (-g3)</avrgcc.compiler.optimization.DebugLevel> <ListValues>
<avrgcccpp.compiler.symbols.DefSymbols> <Value>libm</Value>
<ListValues> <Value>libprintf_flt</Value>
<Value>DEBUG</Value> </ListValues>
</ListValues> </avrgcccpp.linker.libraries.Libraries>
</avrgcccpp.compiler.symbols.DefSymbols> <avrgcccpp.assembler.general.IncludePaths>
<avrgcccpp.compiler.optimization.level>Optimize (-O1)</avrgcccpp.compiler.optimization.level> <ListValues>
<avrgcccpp.compiler.optimization.DebugLevel>Maximum (-g3)</avrgcccpp.compiler.optimization.DebugLevel> <Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include</Value>
<avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel> </ListValues>
</AvrGccCpp> </avrgcccpp.assembler.general.IncludePaths>
<avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel>
</AvrGccCpp>
</ToolchainSettings> </ToolchainSettings>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>