View previous topic :: View next topic |
Author |
Message |
subi
Joined: 29 Aug 2010 Posts: 24
|
PIC18F25K20 ADC problem |
Posted: Fri Sep 30, 2011 3:12 pm |
|
|
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
|
|
Posted: Fri Sep 30, 2011 4:06 pm |
|
|
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
|
PIC18F25K20 ADC problem |
Posted: Sat Oct 01, 2011 11:59 am |
|
|
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
|
|
Posted: Sat Oct 01, 2011 3:30 pm |
|
|
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
|
|
Posted: Sun Oct 02, 2011 6:13 am |
|
|
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
|
PIC18F25K20 ADC problem |
Posted: Wed Oct 05, 2011 12:43 pm |
|
|
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
|
|
Posted: Wed Oct 05, 2011 2:31 pm |
|
|
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
|
|
Posted: Thu Oct 06, 2011 7:35 am |
|
|
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: 19535
|
|
Posted: Thu Oct 06, 2011 9:22 am |
|
|
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
|
|
Posted: Thu Oct 06, 2011 11:12 am |
|
|
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
|
|
Posted: Fri Oct 07, 2011 4:17 am |
|
|
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
|
PIC18F25K20 ADC problem |
Posted: Sat Oct 08, 2011 11:08 am |
|
|
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 |
|
|
|