blob: 72e48751199c95e4369aa2d9d2a3db52e1c30c88 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <avr/io.h>
#include <util/delay.h>
#include "temperature.h"
void temperature_init(void) {
THERMO_DDR |= (1 << THERMO_CS) | (1 << THERMO_SCK);
THERMO_DDR &= ~(1 << THERMO_MISO);
THERMO_PORT |= (1 << THERMO_CS);
}
uint8_t spi_read8(void) {
int i;
uint8_t data = 0;
for (i = 7; i >= 0; i--) {
THERMO_PORT &= ~(1 << THERMO_SCK);
_delay_us(SPI_DELAY_US);
if (PINA & (1 << THERMO_MISO))
data |= (1 << i);
THERMO_PORT |= (1 << THERMO_SCK);
_delay_us(SPI_DELAY_US);
}
return data;
}
uint16_t spi_read16(void) {
uint16_t data;
_delay_us(SPI_DELAY_US);
data = spi_read8();
data <<= 8;
data |= spi_read8();
return data;
}
float temperature_read(void) {
THERMO_PORT &= ~(1 << THERMO_CS);
uint16_t out = spi_read16();
THERMO_PORT |= (1 << THERMO_CS);
// thermocouple open (invalid read) D2 bit
if (out & 0b100)
return 0.0;
out >>= 3; // Skip D0,D1,D2
float temperature = out * THERMO_RESOLUTION;
if (temperature > 100)
temperature *= 1.05;
return temperature;
}
|