-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatmega.h
50 lines (35 loc) · 1.13 KB
/
atmega.h
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
#ifndef mpelib_atmega_h
#define mpelib_atmega_h
// Internal temperature for Atmel mega/tiny
// Calibration: http://www.atmel.com/images/doc8108.pdf
#ifndef TEMP_OFFSET
#define TEMP_OFFSET 0
#endif
#ifndef TEMP_K
//#define TEMP_K 1.0
#define TEMP_K 10
#endif
static double internalTemp(int offset, float k)
{
unsigned int wADC;
double t;
// The internal temperature has to be used
// with the internal reference of 1.1V.
// Channel 8 can not be selected with
// the analogRead function yet.
// Set the internal reference and mux.
ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
ADCSRA |= _BV(ADEN); // enable the ADC
delay(10); // wait for voltages to become stable.
ADCSRA |= _BV(ADSC); // Start the ADC
// Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));
// Reading register "ADCW" takes care of how to read ADCL and ADCH.
wADC = ADCW;
// One or two point calibration should be used to correct the output, see AVR122.
// With my current setup and atmega328P chips, offsets of -50 or -60 are usual.
t = ( wADC - 273 + offset ) * k;
// The returned temperature is in degrees Celcius.
return (t);
}
#endif