View previous topic :: View next topic |
Author |
Message |
majidramzani
Joined: 09 Nov 2018 Posts: 1
|
use pic10f222 internal absolute voltage reference |
Posted: Fri Nov 09, 2018 10:33 am |
|
|
Hi everybody,
I want to use pic10f222 ADC and i have no problem in it:
Code: |
#include <10F222.h>
#list
#device PIC10F222
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCPU //Master Clear Pull-up disabled
#FUSES NOMCLR
#FUSES IOSC4 //INTOSC speed 4 MHz
#device adc=8
#use delay(internal=4000000)
//------------define relay and stair pins---------
#define set_options(value){#ASM \
MOVLW value \
OPTION \
#ENDASM}
setup_adc(true);
set_tris_b (0b111); //set as output
set_options(0xDF); // Enable pin B2 for normal i/o
set_adc_channel(0);
adc1=read_adc();
|
But, when when VDD changes the ADC shows different values (and that is also OK because the reference voltage is VDD).
I looked in PIC10F222 data sheet and found "absolute voltage reference" that equals:
result = 0.6 * 256 / VDD
I thought i can get VDD from the equation above and then get the precise adc value as follows:
Code: |
adc_set_channel(2);
adc1=read_adc();
vdd=0.6 * 256 / adc1 ;
adc_set_channel(0);
adc=read_adc();
//get the precise adc in proportion of VDD
....
|
But the adc1 (absolute voltage reference is not going to change with different VDD from 4.9 to 5.2),
How can i fix this problem?
Any help would be appreciated. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Fri Nov 09, 2018 11:34 am |
|
|
First thing, you need to always have a Tacq pause between selecting a channel and reading it. About 7uSec. Otherwise the value will not be correct.
Also, Vdd needs to be declared as 'float' or it won't work (since the value needs to be in fractions of a volt).
Honestly 'better' to do this in integer in a chip this small, so (for instance) declare Vdd as int16, and use.
Vdd=15360/adc1;
This will perform an int16 division (much smaller than the float division), and give a result in tens of mV. So 200 for a 2v supply. |
|
|
|