|
|
View previous topic :: View next topic |
Author |
Message |
Jean_Paul
Joined: 23 Jun 2004 Posts: 3
|
About PIC16f877A ADC - bad readings |
Posted: Tue Jul 13, 2004 11:44 am |
|
|
Hi guys
I wrote a code to read using the adc of the pic16f877a three external analog inputs. I connect to AN0, AN1 and AN3 and then send the data to the serial port. when I connect the AN3 input to ground 0v, the AN0 decrease the value to the same value. I dont know what is happening.
My code is:
#include "E:\pictest\ac2877a.h"
#include <stdio.h>
#include <string.h>
void main()
{
int8 value1, value2, value3;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
do {
delay_ms(5);
putc(0xAA);
set_adc_channel(0);
value1 = read_adc();
putc(value1);
set_adc_channel(1);
value2 = read_adc();
putc(value2);
set_adc_channel(3);
value3 = read_adc();
putc(value3);
} while (TRUE);
} |
|
|
Guest Guest
|
ADC readings |
Posted: Tue Jul 13, 2004 12:01 pm |
|
|
J-P
You must wait between switching between the ADC input channel and sampling. Plenty of posts here about the importance of this delay.
HTH,
Bill |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
ADC Delay |
Posted: Tue Jul 13, 2004 12:35 pm |
|
|
Example of the delay needed:
set_adc_channel(1);
delay_us(20);
value2 = read_adc(); |
|
|
Joshua Lai
Joined: 19 Jul 2004 Posts: 42 Location: Malaysia, PJ
|
|
Posted: Mon Jul 19, 2004 8:43 pm |
|
|
How to calculate the time needed for ADC dalay and the input current? |
|
|
valemike Guest
|
|
Posted: Tue Jul 20, 2004 8:38 am |
|
|
In the Microchip databook, there's an exhaustive explanation of the linear approximation method that Microchip uses, along with a bunch of formulas.
However, I don't understand the formulas. So what the earlier post that tells you to wait 20 us might be fine. I usually wait 1 millisecond after changing the adc channel.
-Mike |
|
|
Joshua Lai
Joined: 19 Jul 2004 Posts: 42 Location: Malaysia, PJ
|
|
Posted: Sun Sep 18, 2005 9:19 pm |
|
|
I tried using setup_adc(ADC_CLOCK_INTERNAL), then tried setup_adc(TC_DIV_32);
I know with 20Mhz frequency, Tosc32 is 1.6us, using RC oscillator is 4us.
Why I get random A/D value when using setup_adc(ADC_CLOCK_INTERNAL)?
What actually use internal RC oscillator means? Why need to use SLEEP? |
|
|
Ttelmah Guest
|
|
Posted: Mon Sep 19, 2005 4:19 am |
|
|
If you have access to a Spice modelling package, you can use the figures given for the equivalent internal capacitance, internal resistance in the multiplexer and the feeds, source impedance, and the required accuracy, to give a very accurate estimate of the charging time needed. Generally the results agree quite closely to those from the simple capacitor calculation used in the data sheet. The current flow, varies massively, with the rate of change of the input signal.
The point about 'sleep', is noise. The best way of reading the ADC, is always to stop as many noise sources in the chip as possible. With the internal RC, because the sample timing becomes asynchronous to the main clock, this becomes even more important.
Provided your signal source meets the suggestions on impedance, a delay of between 10, and 20uSec is adequate. this varies with chip versions (which have different capacitances and internal reistances, and impedance recommendations), with the 16F877, needing about 20uSec, using a 10K source impedance, and the 18F452, needing 10uSec, using a 2.5K source impedance.
Best Wishes |
|
|
|
|
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
|