
Originally Posted by
retepsnikrep
Can I use the DT_Analog routine to read the internal 1.024 voltage reference in the 12F1822 chips?
Of course you can.
This is just a natural progression to reduce the component count and increase accuracy.
Well, ... it will reduce the component count. It doesn't use any pins. And it will increase the resolution.
But it won't necessarily increase the "Accuracy".
When they say a "Fixed 1.024V reference" it sounds really good.
Untill you look at the specs. +6/-8%, and -114 PPM/°C
6% of 1.024V = 0.06144V
8% of 1.024V = 0.08192V
So that 1.024V can be any where between 0.94208 and 1.08544V, when it's at 25°C.
Add in some wide temperature fluctuations, and it doesn't look much like a "Reference" anymore.
The 1822 also has an on-chip temperature sensor, so some of those variations can be removed, but the "accuracy" is still off without calibration.
If calibrated, you can get some amazing results using DT_Analog, but every chip must be calibrated individually.
Disregarding the tolerances, here's a routine that will read VDD on a 12F1822 (without DT_Analog).
I'm assuming it's a battery operated device, and only have the FVR turned on while reading it to save current.
Code:
DEFINE ADC_BITS 10
ADvalue VAR WORD
VDD VAR WORD
;----[Get VDD by reading the FVR]-----------------------------------------------
VDD_Res CON 10 ; A/D resolution (bits) used for VDD measurement
FVrefMV CON 1024 ; Fixed Vref Voltage in mV ( must match FVRCON )
Vref_AD5 CON EXT ; Calculate FVref A/D reading when VDD = 5.00V
@Vref_AD5 = (_FVrefMV*100000) / (500000000/(1023 << (_VDD_Res-10)))
GetVDD:
ADCON1 = %10110000 ; A/D Right Justified, FRC clock
FVRCON = %10000001 ; Enable Fixed Voltage Reference (1.024V)
WHILE !FVRCON.6 : WEND ; Wait for fixed ref to be stable
ADCIN 31, ADvalue ; Read the FVR channel
FVRCON = 0 ; Disable Fixed Voltage Reference
VDD = Vref_AD5 ; calculated Vref A/D at 5.00V
VDD = VDD * 500 ; * 500 - two decimal places
VDD = DIV32 ADvalue ; / FVRef reading - converts to VDD volts
RETURN
Just GOSUB GetVDD and the result is returned in the VDD variable with two decimal places (5.00V = 500).
That is actually good enough for most battery powered applications that need to react to the VDD voltage.
It doesn't use the extra current required for a voltage divider, and doesn't use a pin to read it.
A voltage divider would draw current all the time, even when the PIC is asleep. This won't.
Similarly, here's a routine that does use DT_Analog to get 16-bit readings from the reference.
Code:
INCLUDE "DT_Analog.pbp" ; DT's 16-bit Analog Module
DEFINE ADC_BITS 10
VDD VAR WORD
;----[Get VDD by reading the FVR]-----------------------------------------------
VDD_Res CON 16 ; A/D resolution (bits) used for VDD measurement
FVrefMV CON 1024 ; Fixed Vref Voltage in mV ( must match FVRCON )
Vref_AD5 CON EXT ; Calculate FVref A/D reading when VDD = 5.00V
@Vref_AD5 = (_FVrefMV*100000) / (500000000/(1023 << (_VDD_Res-10)))
GetVDD:
ADCON1 = %10110000 ; A/D Right Justified, FRC clock
FVRCON = %10000001 ; Enable Fixed Voltage Reference (1.024V)
WHILE !FVRCON.6 : WEND ; Wait for fixed ref to be stable
ADchan = 31 ; FVR channel
ADbits = VDD_Res ; set the results resolution
GOSUB GetADC ; Get A/D value
FVRCON = 0 ; Disable Fixed Voltage Reference
VDD = Vref_AD5 ; calculated Vref A/D at 5.00V
VDD = VDD * 500 ; * 500 - two decimal places
VDD = DIV32 ADvalue ; / FVRef reading - converts to VDD volts
RETURN
That version gives much more resolution, but only a small amount of accuracy, and only because the steps are smaller.
However, it gives the possibility of VERY accurate readings, if the FVR voltage is calibrated.
If you can determine the actual FVR voltage, the FVrefMV constant can be changed to match, which makes for incredibly accurate readings. (At room temperature).
For now, lets assume you won't need the calibration. Why ... because it's midnight here.
Bookmarks