diff options
| author | Akshay Nair <phenax5@gmail.com> | 2026-04-12 18:51:11 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2026-04-12 19:44:28 +0530 |
| commit | 7a973773bfb948ebc5a3d8bbbea7035896795e65 (patch) | |
| tree | 139c9ae2cf79a6d3c03605d45aa686a405079b46 /src/temperature.c | |
| parent | 9ed512b6863311ff53f07b76f6b679fa1ec6a9c9 (diff) | |
| download | temperature-sensor-attiny84a-7a973773bfb948ebc5a3d8bbbea7035896795e65.tar.gz temperature-sensor-attiny84a-7a973773bfb948ebc5a3d8bbbea7035896795e65.zip | |
Add error correction for temp + cleanup
Diffstat (limited to 'src/temperature.c')
| -rw-r--r-- | src/temperature.c | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/src/temperature.c b/src/temperature.c index b804fc9..72e4875 100644 --- a/src/temperature.c +++ b/src/temperature.c @@ -37,13 +37,17 @@ uint16_t spi_read16(void) { float temperature_read(void) { THERMO_PORT &= ~(1 << THERMO_CS); - uint16_t v = spi_read16(); + uint16_t out = spi_read16(); THERMO_PORT |= (1 << THERMO_CS); - // invalid temp - if (v & 0x4) - return 999.9; + // thermocouple open (invalid read) D2 bit + if (out & 0b100) + return 0.0; - v >>= 3; - return v * 0.25; + out >>= 3; // Skip D0,D1,D2 + + float temperature = out * THERMO_RESOLUTION; + if (temperature > 100) + temperature *= 1.05; + return temperature; } |
