View previous topic :: View next topic |
Author |
Message |
weg22
Joined: 08 Jul 2005 Posts: 91
|
How to read in analog voltages on AN0 & AN1?? |
Posted: Thu Jan 04, 2007 2:01 pm |
|
|
Hi all,
I have 2 IR sensors which output an analog signal corresponding to distance. I'm feeding the output from sensor #1 into AN0 and the output from sensor #2 to AN1. I'm trying to read each value, do ADC, and print it to hyperterminal. When running the code below, I get similar (erroneous) values for "value1" and "value2" despite holding them at different distances from obstacles.
When just using 1 sensor and hooking up to AN0, both "value1" and "value2" again have similar distance readings, but are accurate. Not sure why AN0 and AN1 seem to be linked together? Maybe it's something wrong in my code?
Code: |
#include <16F877A.h>
#device adc=10
#include <stdlib.h>
#include <math.h>
#fuses HS,NOWDT,NOPROTECT,PUT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=PC)
#define LED PIN_C0
main()
{
long value1=0, value2=0;
output_high(LED);
// setup to do A/D conversion
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
while(1)
{
// read in value on AN0
set_adc_channel(0);
value1 = read_adc();
delay_ms(10);
// read in value on AN1
set_adc_channel(1);
value2 = read_adc();
delay_ms(10);
fprintf(PC, "%ld, ", value1);
fprintf(PC, "%ld\r\n", value2);
}
} // end of main
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
jfk1965
Joined: 21 Oct 2003 Posts: 58
|
|
Posted: Fri Jan 05, 2007 5:46 am |
|
|
It might be me PCM but the code seems to show that there is a 10ms delay between changing channels!
Can you verify that the voltage on the A/D input is actually changing by using a scope?
What version compiler are you using? On the newer ones your setup ports info is not correct ,it's been changed, check the 16F877A.h file to get the correct one for your compiler.
JFK |
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Fri Jan 05, 2007 9:06 am |
|
|
The delay needs to be between changing channels and reading the adc.
i.e.
set_adc_channel(0);
delay_us(20);
value = read_adc(); _________________ David |
|
|
weg22
Joined: 08 Jul 2005 Posts: 91
|
|
Posted: Fri Jan 05, 2007 11:05 am |
|
|
Yeah, that worked - thanks PCM! |
|
|
|