introducing readadc command corrupts calibadc10 readings 08M2

afb

Member
In my 08M2 project I'm continually measuring the battery supply voltage and also reading a 5k pot (connected across the supply rails - wiper to C.4) which is part of the main function of the program. I found the measured supply voltage reading varied as I adjusted the pot - the error coming in after the pot got somewhere over half setting. I wrote some simple test programs to explore the phenomenon and they are appended at the end of this post - rem out each section in turn to see the story develop. But first here is a picture of the terminal readout when adjusting the pot running the "case 2" code snippet. It shows the pot setting (0-255) followed by the measured supply voltage, in this case 5v (value 50 returned = 5.0v) As you will see with the pot at full rotation the reported supply voltage has dropped to 3.0v !!

readadc problem.gif



As you will see I found a workaround - the far more extensive project code requires to run at 32Mhz

Code:
'08M2 - measuring supply voltage
'5k pot across supply, wiper to leg 3 (C.4)

#rem
'------------------------------------------------------------------------
'1st case - works ok, returns 50 (5.0v) regardless of pot setting
'------------------------------------------------------------------------
 setfreq m32
 do
   calibadc10 w1
   w1 = 10500/w1
   sertxd(#w1,13,10)
   pause 5000
 loop
#endrem



'------------------------------------------------------------------------ 
'2nd case - introduce readadc and now an error in voltage is returned 
'as the the pot is adjusted (see attached image of terminal readout)
'------------------------------------------------------------------------
 setfreq m32
 do
   readadc C.4,b0
   calibadc10 w1
   w1 = 10500/w1
   sertxd(#w0,"  ",#w1,13,10)
   pause 5000
 loop  

#rem 
 
'------------------------------------------------------------------------ 
'3rd case - reduce clock speed - works correctly
'------------------------------------------------------------------------
 setfreq m4
 do
   readadc C.4,b0
   calibadc10 w1
   w1 = 10500/w1
   sertxd(#b0,"  ",#w1,13,10)
   pause 500
 loop
   
 
 
 
'------------------------------------------------------------------------ 
'4th case - workaround - just the calibadc10 command was sensitive
'------------------------------------------------------------------------  
 do
   setfreq m4
   calibadc10 w1
   setfreq m32
   readadc C.4,b0
   w1 = 10500/w1
   sertxd(#b0,"  ",#w1,13,10)
   pause 5000
 loop
 
#endrem
 

hippy

Technical Support
Staff member
A better test program would show the raw ADC readings as well as the calculated power rail voltages -

Code:
setfreq m32
 do
   readadc C.4, w0
   calibadc10 w1
   w2 = 10500/w1
   sertxd(#w0,"  ",#w1, " ", #w2,13,10)
   pause 5000
 loop
I have not investigated the issue but my guess would be that it is related to the internal sample and hold capacitor of the ADC hardware.

The higher the READADC pot voltage, the longer it takes the capacitor to decay to the CALIBADC reference voltage, and the faster the PICAXE runs the less it will have decayed before the reference voltage is read.

Thus, at high speed, the CALIBADC may read an artificially high value on the capacitor which translates to a lower than actual rail voltage.

I recall similar issues in the past. The solution may be to take multiple READADC/CALIBADC readings so the final reading is when the capacitor has reached the level it should be.
 

afb

Member
Hippy - thanks for a very insightful reply. Certainly several readings until the value stabilises may execute more quickly than temporarily changing the clock rate but may take more code space.
 
Top