View previous topic :: View next topic |
Author |
Message |
sema_tufan
Joined: 03 Mar 2020 Posts: 14 Location: turkey
|
Voltage Information with FVR in PIC18F46K40 |
Posted: Tue Dec 22, 2020 5:34 am |
|
|
How can I learn the voltage information from the instant battery with the FVR using the PIC18F46K40 processor?
I've done it before in PIC18f25k20. I pasted the codes I used into my PIC18f46k40 processor project. 0 read.
Code: | void batteryStatus()
{
FVREN=TRUE; //enable the FVR
setup_adc(ADC_CLOCK_DIV_16|VSS_VDD);
set_adc_channel(15); //select FVR as input channel
delay_us(30); //needs 15uSec minimum when reading FVR
value=read_adc(ADC_START_AND_READ);
supply=(1.2/value)*1024;
setup_adc_ports(NO_ANALOGS); //
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9250 Location: Greensville,Ontario
|
|
Posted: Tue Dec 22, 2020 5:53 am |
|
|
Quick comment..
I don't use that PIC, never used FVR either but....
this...
Quote: | setup_adc_ports(NO_ANALOGS); // |
just looks wrong !
After the function executes, you won't have ANY ADC ???
Would have to read the ADC chapter to see if FVR can be read with 'no_analogs' selected... |
|
|
sema_tufan
Joined: 03 Mar 2020 Posts: 14 Location: turkey
|
|
Posted: Tue Dec 22, 2020 6:04 am |
|
|
setup_adc_ports (NO_ANALOGS); I copied this line extra. I did not add such a line of code in the project, but I always get 0 data.
temtronic wrote: | Quick comment..
I don't use that PIC, never used FVR either but....
this...
Quote: |
setup_adc_ports(NO_ANALOGS); // |
just looks wrong !
After the function executes, you won't have ANY ADC ???
Would have to read the ADC chapter to see if FVR can be read with 'no_analogs' selected... |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9250 Location: Greensville,Ontario
|
|
Posted: Tue Dec 22, 2020 6:17 am |
|
|
Is the variable 'supply' a floating point ?
One debug procedure is to breakdown the line
Code: | supply=(1.2/value)*1024; |
into smaller parts and display the results.
1) display value // the actual ADC reading
2) 1.2/value // don't know where 1.2 comes from...
3) supply // which you say is zero
Be sure to format the display properly based on the variable types !
If it still doesn't work, code and post a small program that shows the problem, something we can 'cut/paste' compile/test.
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 22, 2020 1:04 pm |
|
|
CCS has a defined constant for the FVR and it's not 15.
Look in 18F46K40.h, in the SET_ADC_CHANNEL section. It says:
Quote: | // Constants used in SET_ADC_CHANNEL() are:
// either use the channel number or one of the following
#define AVSS_CHANNEL 0x3C
#define TEMPERATURE_INDICATOR 0x3D
#define DAC_CHANNEL 0x3E
#define FVR_CHANNEL 0x3F |
This also matches the 18F46K40 data sheet's ADC channel table.
The proper way to code it is not to use magic numbers.
Instead, do it like this:
Code: | set_adc_channel(FVR_CHANNEL); |
|
|
|
sema_tufan
Joined: 03 Mar 2020 Posts: 14 Location: turkey
|
|
Posted: Tue Dec 22, 2020 1:21 pm |
|
|
Thank you for your answers. I solved the problem with this code on PIC18f46k40 processor.
Code: |
FVREN=TRUE; //enable the FVR
setup_adc(ADC_CLOCK_DIV_16|VSS_VDD);
setup_vref(VREF_ON|VREF_ADC_2v048); // Setup FVR
set_adc_channel(FVR_CHANNEL); //select FVR as input channel
delay_us(30); //needs 15uSec minimum when reading FVR
resultResolution=read_adc(ADC_START_AND_READ);
batteryVolt=(2.048/resultResolution)*1024;
|
|
|
|
|