View previous topic :: View next topic |
Author |
Message |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
PIC16F1709 and ADC VREF |
Posted: Sun Dec 04, 2016 2:39 am |
|
|
Hi All,
Can someone throw a quick eye over this and have a look to try to see where I am going wrong?
CCS = 5.025
Code: |
#include <16f1709.h>
#fuses INTRC_IO, PLLEN, NOFCMEN, NOIESO, NOWDT, NODEBUG, NOMCLR, PUT
#use delay(clock=32M)
int16 test;
void main()
{
setup_vref(VREF_ON | VREF_ADC_4v096);
setup_adc(ADC_CLOCK_DIV_32);
setup_adc_ports(sAN3 | VSS_VREF);
set_adc_channel(3);
delay_ms(100);
while(1)
{
test = read_adc();
delay_us(10);
}
}
|
If I comment out the setup_vref line out and change the setup_adc to be VSS_VDD, all works fine.
With the Vref, however, all I ever get in "test" is almost full range reading (about 65470).
Can anyone see what I am doing wrong?
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 04, 2016 2:52 am |
|
|
AN3 is on pin RA4 in the 16F1709. First question is, are you using pin RA4
for the input voltage ? What is the voltage on pin RA4 ? |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Sun Dec 04, 2016 3:05 am |
|
|
Hi PCM,
Yes, learned my lesson well last time I mixed up the port and channel names
I set the input voltage at about 2 volts for testing.
If I don't use the vref the reading is ok, so I must be using the right pin on the hardware. It has something to do with how I am setting up the vref but I have never used a internal one before.
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Sun Dec 04, 2016 4:11 am |
|
|
Very simple little fault. What is 'Vref'?. It is not the internal fixed voltage reference. It is the Vref pin.....
To connect to the internal 'fixed voltage reference' (FVR), you need:
setup_adc_ports(sAN3 | VSS_FVR);
If you look at the diagram of the ADC in the data sheet, you will see that they show the three sources for the ADC +ve reference, as Vdd, Vref/AN1, or FVR.
As one other comment, 65472 is the maximum reading you can get from the ADC.
Remember it is a 10bit ADC. 0 to 1023. It is running in 'left shifted' mode by default, so will read in steps of 64. 0, 64.... 65408, 65472.
Normally much easier to specify #device ADC=10 so it reads as 0 to 1023.
Because the Vref pin in unconnected, the internal diodes in the ADC multiplexer, will be pulling it up to just below the voltage on your input, and you will get a maximum reading all the time.... |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Sun Dec 04, 2016 5:04 pm |
|
|
Thanks Ttelmah,
That thought also went thought my head, but the device file only has two options for setup_adc_ports;
Code: |
// Optional Second argument:
#define VSS_VDD 0x00000000 //| Range 0-Vdd
#define VSS_VREF 0x02000000 //| Range 0-VrefH
|
My logic thinking says if I enable the VREF I would like to use it. So I assumed
Quote: |
setup_vref(VREF_ON | VREF_ADC_4v096);
|
set ADPREF=10.
Checking the .lst file I find
Code: | .................... setup_adc_ports(sAN3 | VSS_VREF);
0093: BCF 1E.0
0094: BSF 1E.1
|
ADCON1 is at 1E on this chip, 2 low bits are ADPREF. Looking at the image on page 218 in the datasheet this should connect the vref to AN1 making it look as if I was using a external vref.
Is my thinking correct, or is there something I am missing?
Noted on the #device line, I will indeed be taking a 10 bit reading once I have this bit worked out. Just wanted to keep testing nice and clean.
Thanks |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Sun Dec 04, 2016 5:13 pm |
|
|
After typing all that I think I know what is going on. Funny how talking about a problem makes you work it out..
AN1 is also the ISCP CLK pin. I have my pickit connected for debugging.
I think this is working OK, but the pickit is making AN1 high.
I will check this tonight, but I think this will be it.
Now I need another way to debug :(
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Mon Dec 05, 2016 1:39 am |
|
|
The third option to select the FVR, was obviously 'post' your compiler.
It'd probably work if you just add it. The value is:
#define VSS_FVR 0x3000000 //| Range 0-FVR
Since the value is just or'ed into the register, it will probably work. |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Mon Dec 05, 2016 3:20 am |
|
|
Well my idea did not work, but yours did
I now see what is going on. I was reading the datasheet the wrong way around..
For some resign I can't fathom I thought the box labelled ADC was the voltage reference source...
This is the second stupid question I have posted here in a row
Thanks again. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Mon Dec 05, 2016 4:02 am |
|
|
It's a 'nomenclature' problem.
The chips for years had Vref inputs. Then they added an internal Vref. Now problem is what can they call it?. They call it a Vref in the section about it, but then when using it for the ADC, call it the 'fixed voltage reference' (FVR). Now it sort of makes sense, to give it a different name, but would have made more sense I think if they had just called it the Internal voltage reference. Then the ADC settings could have had IVref as an option.
In this case the problem was made more confusing by CCS having 'left it out' on the older compilers....
There is really no such thing as a 'stupid question', provided you actually follow the answers. The ones that annoy are where the posters don't actually try anything for themselves, or give us good data. In your case, you 'came back' and said 'the device file only has two options for setup_adc_ports'. A quick look at an older compiler showed that CCS had failed to include it on these. Nice and easy to solve, but 'not obvious' unless you have been playing with CCS for years (too long actually...).
Other good thing is you probably now understand these options better than most..... |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Mon Dec 05, 2016 4:17 am |
|
|
Indeed, my understanding has gone up.
As soon as I looked at the fact that the register should be set '11' I knew what was going on. Sometimes I just need a little push in the right direction.
Might be time for a compile upgrade too.
Thanks again. |
|
|
|