User Manual
Version 1.1
2011-07-15
deRFnode and deRFgateway
dresden elektronik
ingenieurtechnik gmbh
Enno-Heidebroek-Str. 12
12 7 Dr
n
rm  n
Tel.: +49 351 31 85 00
Fax: +49 351 3 18 50 10
wireless@dresden-elektronik.de
www. r
n- l k r nik.
Page 46 of 56
For AVR MCUs, an equivalent code snippet is:
#define VREF 1.6 // reference voltage; either 1.6 or 3.3 [V]
uint16_t adc_val; // ADC measurement value
double v_bat; // real battery voltage
double c ; // conversion factor
c = ((double)(1040/(double)220)) * VREF;
#if (VREF==1.6)
// Select internal 1.6V reference voltage, left AVDREF pin open
ADMUX = (1 << REFS1) | (1 << REFS0);
#else
// External reference voltage on AVDREF pin, selected by default
#endif
// Analog channel (0) and gain selection (none) <-> MUX5:0 = 0b00000
// -> no changes required
// select prescaler for 500 kHz frequency
#if (F_CPU == (16000000UL))
ADCSRA |= (1 << ADPS2) | (1 <
// Prescaler = 32
#elif (F_CPU == (8000000UL))
ADCSRA |= (1 << ADPS2); // Prescaler = 16
#elif (F_CPU == (4000000UL))
ADCSRA |= (1 << ADPS1) | (1 << ADPS0); // Prescaler = 8
#elif (F_CPU == (1000000UL))
ADCSRA |= (1 << ADPS0); // Prescaler = 2
#else
#error "unsupported F_CPU"
#endif
// put into free running mode (ADTS2:0 = 0b000) -> no changes required
// enable ADC
ADCSRA |= (1 << ADEN);
// Start Conversion, Clear ADIF
ADCSRA |= (1 << ADSC);
// wait for completion
while (!(ADCSRA & (1 << ADIF))) ;
// get measurement
adc_val = ADC;
// disable ADC
ADCSRA &= ~(1 << ADEN);
// convert to real battery voltage (mv)
v_bat = c * adc_val;