Calculate temperature from thermistor resistance

This commit is contained in:
2024-09-15 20:09:57 +02:00
parent ccbd964111
commit 1cc319cd38
3 changed files with 38 additions and 13 deletions

View File

@@ -2,7 +2,9 @@
#include "bus/i2c.h"
#include <avr/io.h>
#include <math.h>
// I2C status flags
#define TW_START 0x08 // Start condition transmitted
#define TW_REP_START 0x10 // Repeated start condition transmitted
#define TW_MT_SLA_ACK 0x18 // SLA+W transmitted, ACK received
@@ -12,6 +14,11 @@
#define TW_MR_SLA_NACK 0x48 // SLA+R transmitted, NACK received
#define TW_MR_DATA_ACK 0x50 // Data received, ACK returned
// NTC conversion constants
#define NTC01 1.009249522e-3
#define NTC02 2.378405444e-4
#define NTC03 2.019202697e-7
// TODO: Error handling and recovery besides watchdog timer.
// TODO: Add more documentation from the atmel data sheet.
// TODO: Implement TWI_vect ISR? This may not actually be
@@ -528,7 +535,25 @@ word I2C_ADS1115_Read(int channel)
// Single-shot conversion, active low, continous mode
config = os | rate | gain | mux;
I2C_ADS1115_WriteRegister(0x01, config);
return I2C_ADS1115_ReadRegister(0x00);
}
float I2C_ADS1115_ReadThermistor(int channel)
{
word raw;
float r1, t;
float logr2;
// TODO: Improve readability
// https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
raw = I2C_ADS1115_Read(channel);
r1 = (float) raw / 264.8f * 0.05f;
logr2 = log(10000.0f / (5.0f / r1 - 1.0f));
t = 1.0f / (NTC01 + NTC02 * logr2 + NTC03 * pow(logr2, 3));
return t - 273.15f; // Convert fahrenheit to celsius
}

View File

@@ -28,6 +28,7 @@ int I2C_AHT20_Init(void);
int I2C_AHT20_Read(float *temp, float *rhum);
// ADS1115 analog to digital converter
word I2C_ADS1115_Read(int channel);
word I2C_ADS1115_Read(int channel);
float I2C_ADS1115_ReadThermistor(int channel);
#endif // MAD_CORE_BUS_I2C_H