
459
42073A-MCU Wireless-02/13
ATmega2564/1284/644RFR2
ldi
run_cmd, (1<<ADEN)+(1<<ADSC)+(4<<ADPS0)
run_conversion:
sts
ADCSRA, run_cmd
wait_adsc:
lds
r17, ADCSRA
sbrc r17, ADSC
; flag cleared at conversion complete
rjmp wait_adsc
lds
r18, ADCL
; measured temperature in ADCL and ADCH
lds
r19, ADCH
…
The above Assembly code example enables the temperature measurement step by
step. Waiting for AVDDOK and REFOK is optional. The conversion will not start before
the two bits are set. The wait time can be extended if necessary or choose a longer
start-up time. An 8 MHz CPU clock is assumed.
C Code Example
(1)
uint16_t adc_meastemp (void)
{
ADCSRC = 10<<ADSUT0;
// set start-up time
ADCSRB = 1<<MUX5;
// set MUX5 first
ADMUX
= (3<<REFS0) + (9<<MUX0);
// store new ADMUX, 1.6V AREF
// switch ADC on, set prescaler, start conversion
ADCSRA = (1<<ADEN) + (1<<ADSC) + (4<<ADPS0);
do
{} while( (ADCSRA & (1<<ADSC))); // wait for conversion end
ADCSRA
= 0;
// disable the ADC
return (ADC);
}
Notes:
The C Code Example fully relies on the integrated start-up mechanism of the A/D
converter. The accuracy can be increased by averaging and/or oversampling. In
addition a dummy conversion can be inserted before the first temperature measurement
is assessed.
The A/D conversion result ADCTEMP will always be a positive number. The ideal result
can be calculated when using the internal 1.6V reference voltage according to the
following equation:
C
ADC
TEMP
°
+
=
/
885
.
0
4
.
241
θ
Similar the Celsius-temperature θ can be extracted from the A/D conversion result with
this formula:
8
.
272
13
.
1
/
=
°
TEMP
ADC
C
θ
Note that the above equations are only valid in the allowed operating temperature
range. The translation of the A/D measurement result to a Celsius-temperature value
can be easily achieved with a look-up table in software. The temperature sensor is