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

PIC18F25K20 ADC problem

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



Joined: 29 Aug 2010
Posts: 24

View user's profile Send private message

PIC18F25K20 ADC problem
PostPosted: Fri Sep 30, 2011 3:12 pm     Reply with quote

Hi Everybody,

I would like to ask a little help. So I am a beginner and I would like to use the PIC18F25K20 for measure voltage with FVR - 1,2 V reference voltage in it. I can not found a CCS soft ver, how I have to use it. But I have ADC in the other PIC but that code does not work with PIC18F25K20.
Please give me some advices and send a program if it is possible.

Thank you for your help and kindness.

Subi (sorry for my English)
n-squared



Joined: 03 Oct 2006
Posts: 99

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Sep 30, 2011 4:06 pm     Reply with quote

FVR is read on channel 15 of the ADC.
use set_adc_channel(15) to read FVR.
The FVR has to be enabled by setting FVREN bit in CVRCON2.

Code:

#byte CVRCON2 = 0xFB4
#bit FVREN = CVRCON2.7
#bit FVRST = CVRCON2.6

void enable_fvr(void)
  {
  FVREN = 1;
  while (!FVRST); // wait for FVR to stabilize
  }

int8 get_fvr(void)
  {
  set_adc_channel(15);
  delay_us(10);
  return read_adc();
  }

void main(void)
  {
  enable_fvr();
  printf("FVR = %u\r\n", get_fvr());
  }


Hope this helps

BR
n-squared
_________________
Every solution has a problem.
subi



Joined: 29 Aug 2010
Posts: 24

View user's profile Send private message

PIC18F25K20 ADC problem
PostPosted: Sat Oct 01, 2011 11:59 am     Reply with quote

Dear n-squared,

Thank you for your answer. I have tried your code and I have measured 0V. As I wrote I am a beginner so pehaps I have not set well the port.
I set the PORTA:
Code:
 
 setup_adc_ports(sAN0|sAN3|VREF_FVR);
 setup_adc(ADC_CLOCK_DIV_16|ADC_TAD_MUL_8);

Further I don't know whis is the 15 channel.

So please write more about it. If you give complete setup it will be very good.

Thank you for your help.

Subi
n-squared



Joined: 03 Oct 2006
Posts: 99

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Oct 01, 2011 3:30 pm     Reply with quote

Code:


#include <18F25K20.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(int=8000000,RESTART_WDT)



#byte CVRCON2 = 0xFB4
#bit FVREN = CVRCON2.7
#bit FVRST = CVRCON2.6

void enable_fvr(void)
  {
  FVREN = 1;
  while (!FVRST); // wait for FVR to stabilize
  }

int8 get_fvr(void)
  {
  int16 ret;
  set_adc_channel(15);  // channel 15 IS FVR
  delay_us(20);
  ret = read_adc();
  return ret;
  }


void main()
  {
  int16 ret, x;
  setup_adc_ports(sAN0|sAN1|VSS_VDD);
  setup_adc(ADC_CLOCK_DIV_32|ADC_TAD_MUL_16);
  setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
  setup_comparator(NC_NC_NC_NC);

  enable_fvr();
  ret = get_fvr();
  x++;
  x++;
  }


put a breakpoint on first x++; following a call to get_fvr();

BR
n-squared
_________________
Every solution has a problem.
subi



Joined: 29 Aug 2010
Posts: 24

View user's profile Send private message

PostPosted: Sun Oct 02, 2011 6:13 am     Reply with quote

Dear n-squared,

Thank you for your code. My PIC 18F25K20 perhaps is not good, because the measured voltage is zero. I have to order some new pieces. But my PIC18f45K25 is going with your program code. But I have a little mistake. The measured voltage is not stable. I don't understand it.

So I would like to thank you again for your helping.

Subi
subi



Joined: 29 Aug 2010
Posts: 24

View user's profile Send private message

PIC18F25K20 ADC problem
PostPosted: Wed Oct 05, 2011 12:43 pm     Reply with quote

Dear n-squared,

I have used your sent code. The program is running well but I have a little mistake. The FVR voltage is not stable. If I put on the voltage the measured FVR is less. If I put down the voltage the measured FVR is more. Why do I experience it? Do you have any idea?

Thank you for your answer.

Subi
n-squared



Joined: 03 Oct 2006
Posts: 99

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Oct 05, 2011 2:31 pm     Reply with quote

Check your Vdd. If it is not stable, the FVR readings will not be stable ether.
_________________
Every solution has a problem.
subi



Joined: 29 Aug 2010
Posts: 24

View user's profile Send private message

PostPosted: Thu Oct 06, 2011 7:35 am     Reply with quote

Dear n-squared,

Thanks, it is a good advice. So perhaps I understand what is the problem because I would like to measure the battery voltage changing. But the PIC have got the energia form the battery. So if the battery voltage put down the FVR is not stable.
Do you suggest a outer voltage refernce for the PIC to measure the battery voltage?

Subi
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Oct 06, 2011 9:22 am     Reply with quote

The FVR, is relatively stable.
It is your reading of it that isn't, and this can be used to tell you the supply voltage.
Imagine, you have the ADC setup to use Vss-Vdd as it's reference.
You read the voltage on the FVR source, and get a reading of 285. You can then say that the Vdd, is approximately (1.2/284)*1023 = 4.31v. However the accuracy of this is fairly poor. The actual voltage could be anything from just under 4.14v to nearly 4.5v. Even more if you are using the full temperature range of the chip. So if +/-just under 5% is acceptable for your readings, then this allows you to estimate the battery supply voltage. If not, then a higher accuracy source is needed, and the same trick can be used.

Best Wishes
subi



Joined: 29 Aug 2010
Posts: 24

View user's profile Send private message

PostPosted: Thu Oct 06, 2011 11:12 am     Reply with quote

Dera Ttelmah,

Thank you for your exposition about FVR measuring. It is very deep. So I will look over my circuit about my circuit mistake.

Best wishes,

Subi
tinley



Joined: 09 May 2006
Posts: 67

View user's profile Send private message Visit poster's website

PostPosted: Fri Oct 07, 2011 4:17 am     Reply with quote

In the example code posted by n-squared the ADC has been set up outside the recommended oscillator range which will cause the unstable non linear results that you have suggested.

From the datasheet for an 8MHz oscillator use the following:

setup_adc(ADC_CLOCK_DIV_8)

Although the variations in voltage regulators is a factor when using VSS_VDD as reference, it can mostly be calibrated out in code.
subi



Joined: 29 Aug 2010
Posts: 24

View user's profile Send private message

PIC18F25K20 ADC problem
PostPosted: Sat Oct 08, 2011 11:08 am     Reply with quote

Dear tinley,

Thank you for your helping too. I could not use well the FVR to measure the battery when give energy the PIC. But I solved the problem used the PORT AN3 and a diode for reference voltage.
It is the conventional result but it is good.

Subi
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