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 support@ccsinfo.com

How to read in analog voltages on AN0 & AN1??

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



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

How to read in analog voltages on AN0 & AN1??
PostPosted: Thu Jan 04, 2007 2:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jan 04, 2007 2:13 pm     Reply with quote

You need a delay after you switch the ADC channel.
This is in the data sheet. See the sample code at this link:
http://www.ccsinfo.com/forum/viewtopic.php?t=29074&start=1
jfk1965



Joined: 21 Oct 2003
Posts: 58

View user's profile Send private message

PostPosted: Fri Jan 05, 2007 5:46 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 05, 2007 9:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 05, 2007 11:05 am     Reply with quote

Yeah, that worked - thanks PCM!
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