Converting ADC Value to Voltage Readout On a Picaxe

#1
This code takes advantage of the FVRSETUP and ADCCONFIG commands to provide good accuracy and good resolution when reading a voltage via ADC and displaying the Voltage on a PE Terminal. Of course the output could be sent instead to an LED or OLED display.

The FVRSETUP Command is available on M2 Chips and on the 28X2 and 40X2

In this code the FVRSETUP and ADCCONFIG commands are used to set the internal ADC reference to 2.048V. This gives .002v per step using ADC10. It also means that a change in the Picaxe supply voltage will not affect the ADC value or accuracy. So, for example, in a battery powered system the ADC reading will remain accurate as the battery voltage changes over time. The Picaxe can be operated down to about 2.4v with no effect on the ADC readings.

The only extra hardware required is a two resistor voltage divider to bring the input to the appropriate range. I used a 20K / 10K divider (1%) that gives a range from 0 to 6 volts to allow some head room if the input voltage is a bit higher than 5 volts. There is no odd math needed because the 2.048 reference is precisely 2 times the 10 bit range of 0 - 1023. Change the resistor divider to change the voltage range.

The reading is stable and the accuracy is excellent and is within about 20 mV as read by a TEK THS730A ( DMM)

Below is the code that I used for a 0 - 6 V range. A simple schematic is attached to show the voltage divider.

Code:
=================
#Picaxe 08M2
#No_Data
Pause 100
'=====================

FVRSETUP FVR2048  [COLOR=#008000] 'Set Fixed Voltage Reference = 2.048 Volts [/COLOR]
ADCCONFIG %011    [COLOR=#008000] 'Set ADC to FVR  [/COLOR]

do
      readadc10 C.2, W5 
      w5 = w5  * 6 / 10          [COLOR=#008000]'Scale/MAP [/COLOR]
      bintoascii w5,b0,b0,b1,b2,b3
      sertxd (b0,b1,".",b2,b3," Volts DC",cr,lf)
      pause 2000

loop
Addendum.

To calculate a voltage range.

Use a resistor divider calculator such as PotDesign, to select the resistors so that the voltage is 2.00v at the center tap when max voltage is applied. Then change the code so that:

w5 = w5 * max_volts / 10

For example: The range needs to be from 0 - 24V:

For 2v at the center tap with 24v applied , the voltage divider resistors will be 12K and 1.5K
Then change the mapping code line to w5 = w5 * 24 / 10

Have Fun

Goeytex
 
Top