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 | |
| parent | 9ed512b6863311ff53f07b76f6b679fa1ec6a9c9 (diff) | |
| download | temperature-sensor-attiny84a-7a973773bfb948ebc5a3d8bbbea7035896795e65.tar.gz temperature-sensor-attiny84a-7a973773bfb948ebc5a3d8bbbea7035896795e65.zip | |
Add error correction for temp + cleanup
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | LICENSE | 2 | ||||
| -rw-r--r-- | src/temperature.c | 16 | ||||
| -rw-r--r-- | src/temperature.h | 2 |
4 files changed, 13 insertions, 10 deletions
@@ -2,6 +2,3 @@ .ccls a.out out/ -*.zip -fp-info-cache -*.lck @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 Akshay Nair +Copyright (c) 2026 Akshay Nair Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal 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; } diff --git a/src/temperature.h b/src/temperature.h index 31fa92a..c15fe22 100644 --- a/src/temperature.h +++ b/src/temperature.h @@ -4,6 +4,8 @@ #define THERMO_PORT PORTA #define THERMO_DDR DDRA +#define THERMO_RESOLUTION 0.25 + #define SPI_DELAY_US 10 void temperature_init(void); |
