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 CCS Technical Support

Help reading in an analog signal

 
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

Help reading in an analog signal
PostPosted: Wed Nov 08, 2006 3:33 pm     Reply with quote

Hi all,

I'm looking to read in an analog voltage from an infrared sensor:
http://www.acroname.com/robotics/parts/R144-GP2Y0A02YK.html

and display the digital value. I plan to hardwire the sensor output to PIN_F0 of my pic 18F8722. I've never done A/D conversion with a PIC before, so I was wondering if my code below looks okay? Also, I'm not sure how the #device adc=8 factors in??

The sensor is due in on Friday, so it's not like I can just hook it up and see if it works...trying to get the bugs ironed out beforehand :-)

Code:

#include <18F8722.h>
#device adc=8

#include <stdlib.h>
#include <math.h>

#fuses HS,NOWDT,NOPROTECT,PUT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_A4, rcv=PIN_A5, stream=PC)

#define LED PIN_A0


main()
{
    int value=0;

   
   while(1)
   {
      // setup to do A/D conversion on AN5
        setup_adc_ports(AN0_TO_AN5);
        setup_adc(ADC_CLOCK_DIV_8);
        set_adc_channel(5);
       
        delay_us(20);
        value = read_adc();
        setup_adc(ADC_OFF);

        fprintf(PC, "IR Value: %u\r\n", value);
   }

} // end of main
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 08, 2006 3:59 pm     Reply with quote

Look at the items shown in bold below:
Quote:
#use rs232(baud=9600, xmit=PIN_A4, rcv=PIN_A5, stream=PC)

#define LED PIN_A0


// setup to do A/D conversion on AN5
setup_adc_ports(AN0_TO_AN5);
setup_adc(ADC_CLOCK_DIV_8);

You're setting up AN0-AN5 as analog pins.

Here is the correspondence between the PIC pins and Analog pins,
from the 18F8722 data sheet:
Code:

RA0   AN0
RA1   AN1
RA2   AN2
RA3   AN3
RA5   AN4
RF0   AN5

But you can't specify pin RA5 as the UART rcv pin, and also configure it
as an analog pin. The same thing with the LED pin. It must be a digital
pin. You can't configure it as analog, which you are doing with the
setup_adc_ports(AN0_TO_AN5) statement.
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Wed Nov 08, 2006 5:27 pm     Reply with quote

Yeah, I kinda figured that. The problem is I am using a PCB that's been made up a while back. It uses PIN_A0 as the LED pin so I can't change that. And all the other analog pins are used up except for RF0, RF2, and RF4. So I need to use one of those 3 pins as the analog but based on the 18F8722.h file, there's no way to specify this without including AN0-AN4. I guess I have to hard code this myself, but I'm not sure how?

Besides that small mistake, does everything else in the code look correct?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 08, 2006 6:29 pm     Reply with quote

The 18F8722 doesn't permit the enabling/disabling of individual analog
pins. It has to be done in pre-defined groups. You probably have
to cut and jumper the board. I can't really help.
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Thu Nov 09, 2006 8:17 am     Reply with quote

Oh, I thought you could come up with a workaround to that...but I guess not. So if I do decide to cut the LED from A0 and hard wire it to G0, will the code below work?

Also, what if I wanted to read an analog signal on pins A0 and A1...what else would I have to change in the code below besides the setup_adc_ports statement?

Thanks in advance,
-weg

Code:

#include <18F8722.h>
#device adc=8

#include <stdlib.h>
#include <math.h>

#fuses HS,NOWDT,NOPROTECT,PUT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_A4, rcv=PIN_A5, stream=PC)

#define LED PIN_G0


main()
{
    int value=0;

   
   while(1)
   {
      // setup to do A/D conversion on AN5
        setup_adc_ports(AN0);
        setup_adc(ADC_CLOCK_DIV_8);
        set_adc_channel(0);
       
        delay_us(20);
        value = read_adc();
        setup_adc(ADC_OFF);

        fprintf(PC, "IR Value: %u\r\n", value);
   }

} // end of main
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