View previous topic :: View next topic |
Author |
Message |
picprogrammer12345
Joined: 27 Jun 2013 Posts: 22
|
PIC16F720 FVR not working with ADC |
Posted: Fri Aug 09, 2013 1:51 pm |
|
|
Hello all,
I am using PIC16F720 for a project and I am not being able to use the Fixed voltage reference (FVR) or the PIC chip for reading the adc channels.
Below is the code:
Code: |
#include <16F720.h> //The chip we are using
#use delay(clock=8000000) //set clock speed to 8Mhz
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#FUSES INTRC_IO,PLLEN,NOWDT,NOPUT,MCLR,PROTECT,NODEBUG
#byte FVRCON = 0x9D
void main(void)
{
disable_interrupts(GLOBAL);
FVRCON = 0xC2;
set_tris_a(0xFF);
set_tris_b(0xFF);
set_tris_c(0xFF);
setup_adc_ports(sAN5,VSS_FVR);
setup_adc( ADC_CLOCK_INTERNAL);
while(true)
{
set_adc_channel(5);
read_adc();
printf("%u ", read_adc());
delay_ms(500);
}
} |
According to my understanding and reading multiple examples online, my code should enable the fixed voltage reference using the FVRCON register.
I have set appropriate bits high and set the reference at 2.048V.
Then I have set the adc channel 5 (AN5 or Pin C1) as analog and the voltage range from VSS to FVR.
I am feeding 1 volt into the adc channel 5.
Based on my understanding the ADC value reading should not change as long as the input is constant at 1V and the main PIC chip VDD is between 2.5 to 5 volts.
But, the ADC value keeps changing when I increase and decrease the main VDD to PIC. It seems like it is still referencing VDD as its higher voltage range and thus the 1 volt input adc value changes with the VDD change.
Am I missing something else on the code.
I have worked on it all day and cant seem to get it working.
Any help will be highly appreciated.
The compiler version I have is PCWH 4.129
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Fri Aug 09, 2013 2:17 pm |
|
|
Start by using setup_vref, instead of writing to fvrcon.
setup_vref(VREF_2v048);
Your setting is trying to write a '1' to a read only bit. Probably not the problem, but shouldn't be done....
Then sequence to read the ADC, is always
1) Select channel.
2) wait Tacq
3) read channel.
You are not pausing at any point between selecting the channel and reading the adc....
The include file talks for a long time about the optional second argument, but on most compilers, it works better to OR the values together.
So:
setup_adc_ports(sAN5|VSS_FVR);
Worth trying this instead.
Best Wishes |
|
|
picprogrammer12345
Joined: 27 Jun 2013 Posts: 22
|
|
Posted: Fri Aug 09, 2013 2:22 pm |
|
|
Tried it with
setup_vref(VREF_2v048);
and also tried it with both
setup_vref(VREF_2v048);
FVRCOn = 0xC2;
Also gave delay of 500ms after setting the channel.
No change. ADC value still changing when VDD is changed between 2.3 and 5 volts |
|
|
picprogrammer12345
Joined: 27 Jun 2013 Posts: 22
|
|
Posted: Fri Aug 09, 2013 2:23 pm |
|
|
FYI also tried it with
sAN5|VSS_FVR |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Fri Aug 09, 2013 2:34 pm |
|
|
Study bit 7 of FVRCON. This is a read only bit.
I'd suspect your compiler has problems with this chip it was fairly new when this was supplied.
Best Wishes |
|
|
Grkan13
Joined: 06 Apr 2011 Posts: 14
|
|
Posted: Tue Sep 24, 2013 1:14 pm |
|
|
Hello,
I have the same problem with 16f721. Did you able to find a solution for that?
Thanks.
edit: compiler version 5.012 |
|
|
picprogrammer12345
Joined: 27 Jun 2013 Posts: 22
|
|
Posted: Tue Sep 24, 2013 1:22 pm |
|
|
Yes,
I contacted ccs and they said that the function VSS_FVR is been placed by mistake in the .h file. PIC16F720 is incapable of setting fixed Vdd as FVR for the ADC pins.
It cannot be done on this chip. |
|
|
Grkan13
Joined: 06 Apr 2011 Posts: 14
|
|
Posted: Wed Sep 25, 2013 8:55 am |
|
|
Thank you picprogrammer12345 |
|
|
|