Reading the fixed voltage reference

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
bradsprojects
Posts: 28
Joined: Thu Nov 29, 2012 12:29 am
Location: Australia

Reading the fixed voltage reference

Post by bradsprojects » Mon Oct 30, 2017 9:38 pm

Hi everyone,

I have been trying to read the fixed voltage reference value on a PIC18F46K22 microcontroller. I have set the positive reference to Vdd and the negative reference to Vss.

The FVR channel is 11111 on the 18f46k22 however it would seem that I can't simply plug this number into the ADC.Read function. I therefore looked into making my own code however this doesn't seem to work since the values I read actually decrease with an increasing Vdd voltage.

Here is the function which I came up with:

Code: Select all

function GetFVR() as word
    ADCON0 = %01111100                              ' select the fixed voltage reference (which is channel b11111 in bits 2 - 6)
    ADCON0.bits(0) = 1                              ' enable adc
    ADCON0.bits(1) = 1                              ' start the ADC conversion sequence
    While adcon0.bits(1) = 1                        ' stay in this loop until conversion is complete
    Wend
    ADCON0.bits(0) = 0                              ' disable adc
    result = adresh << 8 or adresl                  ' store result
End Sub
My setup contains this code:

Code: Select all

ADCON1 = %00000000 ' sets the positive and negative references to VDD and VSS        
ADCON2 = %10100001 ' data is right justified, 8 TAD, fosc / 8
Are there any glaring issues that anyone might be able to point out to me? Any help would be much appreciated.

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Reading the fixed voltage reference

Post by Jerry Messina » Tue Oct 31, 2017 9:50 am

doesn't seem to work since the values I read actually decrease with an increasing Vdd voltage
I use a different sequence (I leave the ADC on and have an acquision delay after changing the channel), but what you're seeing sounds correct.

Think of it this way:
If FVR = 2V and VDD=2V then the adc should read 2/2 = 100%
If FVR = 2V and VDD=3V then the adc should read 2/3 = 66%
If FVR = 2V and VDD=4V then the adc should read 2/4 = 50%

So, as VDD increases the FVR reading will decrease. As long as your results make sense and are stable then you probably have the setup ok.

bradsprojects
Posts: 28
Joined: Thu Nov 29, 2012 12:29 am
Location: Australia

Re: Reading the fixed voltage reference

Post by bradsprojects » Tue Oct 31, 2017 11:25 am

Thanks for the detailed reply Jerry.

Now that you mention it, the datasheet did mention having a specific delay when reading FVR - i'll have to double check the specifics.
That makes sense about the inverse relationship - i'll run through some experiments to see the relationship.

Thank you again Jerry - much appreciated!

Post Reply