-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermal.c
53 lines (40 loc) · 813 Bytes
/
thermal.c
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 "thermal.h"
#include "avr_mcu.h"
static void thermal_init_hw();
void thermal_init()
{
thermal_init_hw();
}
short thermal_read_temperature()
{
#ifdef THERMAL_SUPPORT
ADCSRA |= (1 << ADSC);
while ((ADCSRA & (1 << ADSC)) != 0) { }
short adcr;
short t;
adcr = ADCL;
adcr |= (ADCH << 8);
// An approximate conversion to degrees C.
adcr -= 324;
t = adcr;
t *= 55;
t -= (adcr * 10);
t /= 55;
return t;
#else
return THERMAL_TEMP_NONE;
#endif
}
static void thermal_init_hw()
{
#ifdef THERMAL_SUPPORT
#if (defined AVRSYSH_MCU_328P)
PRR &= ~(1 << PRADC);
ADMUX = (1 << REFS1) | (1 << REFS0) | (1 << MUX3);
ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
#else
#error "MCU type not defined or not supported!"
#endif
#endif // THERMAL_SUPPORT
}