CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Voltage Information with FVR in PIC18F46K40

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sema_tufan



Joined: 03 Mar 2020
Posts: 14
Location: turkey

View user's profile Send private message

Voltage Information with FVR in PIC18F46K40
PostPosted: Tue Dec 22, 2020 5:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 22, 2020 5:53 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 22, 2020 6:04 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 22, 2020 6:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 22, 2020 1:04 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 22, 2020 1:21 pm     Reply with quote

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;
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group