Measuring Your Own Supply Voltage 2

This appendix to the original article Measuring Your Own Supply Voltage shows an alternative way of measuring the supply voltage on an AVR DA-Series microcontroller, using the 'backwards' technique used on older ATtiny and ATmega chips.

Measuring VDD on an AVR128DA28 - alternative method

The 'backwards' technique is to make VDD the voltage reference for the ADC, and then measure a fixed voltage reference of 1.024V from the VREF peripheral, via the Analog Comparator's DACREF voltage divider.

First set up the ADC as follows:

• Set the ADC's voltage reference to VDD.
• Set the Analog Comparator's shared voltage reference to 1.024V.
• Set Analog Comparator 0's DACREF value to 255 (not strictly necessary as it's the default).

Here's the code to implement this:

```void ADCSetup () {
VREF.ACREF = VREF_REFSEL_1V024_gc;
AC0.DACREF = 255;                                    // Maximum DACREF0 voltage
}```

Here's the routine to measure the supply voltage:

```void MeasureVoltage () {
Buffer[0] = voltage/10; Buffer[1]= voltage%10;
}```

DACREF0 is 255/256 times the Analog Comparator reference, which we've set to 1.024V. So suppose we have a 12-bit reading of R. Then:

R4096
× VDD = 1.024 ×
255256

which gives:

VDD = 1.024 ×
255256
×
4096R
=
4177.9R
volts

To get VDD in tenths of a volt we therefore need to divide 41779 by the ADC reading.

The minimum voltage we can measure this way is 4177.9/4095 or 1.02V, which is below the minimum supply voltage of 1.8V.