- Name:
- calibadc10
- Syntax:
-
CALIBADC10 wordvariable
Wordvariable - receives the adc reading.
- Description:
-
Calibrate the microcontrollers internal ADC by measuring a fixed internal fixed voltage reference.
0.6V 20M, 28X1, 40X1 1.2V 28X2-3V, 28X2-3V 1.024V All other parts that support this command Note that this command is not available on 28X2-5V/40X2-5V.
The reference voltage used by the PICAXE microcontrollers ADC reading (readadc/ readadc10) commands is the supply voltage. In the case of a battery powered system, this supply voltage can change over time (as the battery runs down), resulting in a varying ADC reading for the same voltage input.
The calibadc/calibadc10 commands can help overcome this issue by providing the ADC reading of a nominal internal reference. Therefore by periodically using the calibadc command you can mathematically calibrate/compensate the readadc command for changes in supply voltage.
calibadc can be considered as 'carry out a readadc on a fixed reference'.
Note that the voltage specified is a nominal voltage only and will vary with each part. Microchip datasheet AN1072 provides further details on how to software calibrate and use this advanced feature.
At clock speeds >= 32MHz it is necessary to use two consecutive 'calibadc' commands together to ensure the Vref has time to settle.
Theory of operation
Calibadc10 will give a result (Nref) which will depend on the reference voltage (Vref) and the PICAXE power supply voltage (Vpsu) as follows -
Nref = Vref * 1023 / Vpsu
This can be rearranged to determine the power supply voltage (Vpsu) from the calibadc10 result (Nref) -
Vpsu = Vref * 1023 / Nref
If the Vref were 1.024 volts then this will be -
Vpsu = 1.024 * 1023 / Nref
Vpsu = 1047.552 / Nref
Rounded to the nearest whole number -
Vpsu = 1048 / NrefThis will give the voltage in 1V units.
To obtain a voltage in millivolts (mV) -
Vpsu = 1.024 * 1023 * 1000
Vpsu = 1047552 / Nref
Which, accounting for PICAXE numerical constraints -Vpsu = 52378 / Nref * 20
- Applies To:
- All (except 08, 08M, 14M, 18, 18A, 18M, 18X, 28, 28A, 28X, 40X)
- See Also:
- Related Create:
Using 'calibadc10'
This program reads the 10-bit 'calibadc10' value into the word variable 'w1' and then reports what that reading is.
- Code Example:
-
main: calibadc10 w1 ; read the adc reading debug ; display current value pause 500 ; wait a while goto main ; loop back to start
Copy Code Submit an Example
Determine the PICAXE supply voltage
This code demonstrates how to determine the PICAXE supply voltage. For example it may be that a vital peripheral chip must run at >=3.15 volts or else it will be unpredictable in function.
- Code Example:
-
Symbol Nref = w2 ; Word variable for the calibadc10 reading Symbol Vpsu = w3 ; Word variable for the supply voltage Main: calibadc10 Nref ; Take the reference reading Vpsu = 52378 / Nref * 2 ; Work out supply voltage if Vpsu > 315 then ; Is greater than 3.15 volts ; Battery is above 3.15 ; Yes ; volts so it is OK. gosub SafeToWork else ; No ; Shut down this project ; safely now, because the ; battery is going flat! gosub Shutdown end if goto Main ; Continue ; Notes ; ; Calibadc10 will give a result (Nref) which will depend on the reference ; voltage (Vref) and the PICAXE power supply voltage (Vpsu) as follows - ; ; Nref = Vref * 1023 / Vpsu ; ; This can be rearranged to determine the power supply voltage (Vpsu) from ; the calibadc10 result (Nref) - ; ; Vpsu = Vref * 1023 / Nref ; ; This will give Vpsu in units of volts (1000mV). To give Vpsu in units ; of 10mV we can multiply by 100 - ; ; Vpsu = Vref * 1023 * 100 / Nref ; ; For a Vref of 1.024 volts this will be - ; ; Vpsu = 1.024 * 1023 * 100 / Nref ; ; Vpsu = 104755.2 / Nref ; ; Unfortunately 104755.2 is larger than the maximum value which can be ; held within a 16-bit word value (65535), however we can divide ; 104755.2 by 2 and later multiply the result by 2 and still have the ; same answer albeit with a little less accuracy - ; ; Vpsu = 104755.2 / Nref ; ; Vpsu = ( (104755.2/2) / Nref ) * 2 ; ; Vpsu = ( 52377.6 / Nref ) * 2 ; ; Rounding to the nearest whole number as the PICAXE does not do decimals ; and taking into account the left to right nature of PICAXE calculations ; gives - ; ; Vpsu = 52378 / Nref * 2
Copy Code Submit an Example
Battery voltage percentage indicator
This will determine the percentage of a battery voltage available when the PICAXE is powered by that battery. 'Vpsu100' should be set to the battery mV which indicates 100%. 'Vpsu0' should be set to the battery mV which indicates 0%.
- Code Example:
-
#picaxe 20X2 #terminal 9600 symbol Vpsu100 = 5000 ; 5000mV = 100% symbol Vpsu0 = 4000 ; 4000mV = 0% symbol Nref100 = 1047552 / Vpsu100 ; 1024 * 1023 / Vpsu100 symbol Nref0 = 1047552 / Vpsu0 ; 1024 * 1023 / Vpsu0 symbol NrefDif = Nref0 - Nref100 symbol Nref = w0 ; b1:b0 symbol Vpsu = w1 ; b3:b2 symbol percent = b4 do calibadc10 Nref percent = Nref min Nref100 - Nref100 * 100 / NrefDif max 100 percent = 100 - percent Vpsu = 52378 / Nref * 2 sertxd( #Vpsu, "0mV", tab, #percent, "%", cr, lf ) pause 1000 loop
Copy Code Submit an Example
Submit Your Own Code!
You must be logged in to submit code examples. Login now.