View previous topic :: View next topic |
Author |
Message |
b101101011011011
Joined: 03 Sep 2007 Posts: 7
|
Reading 3 simultaneous entries AD |
Posted: Fri Nov 09, 2007 5:41 am |
|
|
Hello!
I am having trouble reading three simultaneous entries AD in my PIC16F877A.
The entries come into conflict and the values divide among themselves.
Please, somebody can help-me?
My Source code:
Code: |
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel(0);
value_1 = read_adc();
setup_adc( ADC_OFF );
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel(1);
value_2 = read_adc();
setup_adc( ADC_OFF );
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel(2);
value_3 = read_adc();
setup_adc( ADC_OFF );
|
|
|
|
Foppie
Joined: 16 Sep 2005 Posts: 138 Location: The Netherlands
|
|
Posted: Fri Nov 09, 2007 6:21 am |
|
|
I think you should add a delay after the set_adc_channel() statement.
A delay_us(25) should be enough.
Hope this helps |
|
|
b101101011011011
Joined: 03 Sep 2007 Posts: 7
|
|
Posted: Fri Nov 09, 2007 7:20 am |
|
|
Thanks very much!
I put delay_ms(25), and worked!
Code: |
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
while (true) {
set_adc_channel(0);
delay_ms(25);
valor0 = read_adc();
val320 = valor0 * 4 + ((int32)valor0 * 113)/128;
p0 = ((int32)valor0 * 100) / 1024;
set_adc_channel(1);
delay_ms(25);
valor1 = read_adc();
val321 = valor1 * 4 + ((int32)valor1 * 113)/128;
p1 = ((int32)valor1 * 100) / 1024;
set_adc_channel(2);
delay_ms(25);
valor2 = read_adc();
val322 = valor2 * 4 + ((int32)valor2 * 113)/128;
p2 = ((int32)valor2 * 100) / 1024;
|
|
|
|
Foppie
Joined: 16 Sep 2005 Posts: 138 Location: The Netherlands
|
|
Posted: Fri Nov 09, 2007 7:50 am |
|
|
great.
But delay_ms(25) is a long time. as I said, delay_us(25) works perfectly for me so maybe you could try that to speed up your program ;)
Greetings |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 09, 2007 8:57 am |
|
|
Yes.
This is a 'read the data sheet' question/answer...
The ADC internally, is 'seen' as a small capacitor, with a series resistor feeding it, (from the resistance of the gates comrising the multiplexer etc..). The capacitor takes time to charge. On the 16F877A, the capacitor is 120pF, with the internal resistances being typically about 6KR for 5v operation. The pin also draws some leakage current, which is why they also specify that the source impedance feeding it, should not be above 10KR. With a 10K source impedance (the worst case allowed), the time taken for the capacitor to charge to within 0.5 bit of the 'real' voltage, over the worst value change (from a signal at the low end of the Vref, to a signal at the high end, or vice versa), is 19.72uSec. Working slightly over this (20 to 25uSec), is reasonable, going much longer, just wastes time...
It is documented in the data sheets. A lot of newer chips have smaller capacitances, and allow shorter times to be used.
Best Wishes |
|
|
|