![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
weg22
Joined: 08 Jul 2005 Posts: 91
|
Help reading in an analog signal |
Posted: Wed Nov 08, 2006 3:33 pm |
|
|
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
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 08, 2006 3:59 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
weg22
Joined: 08 Jul 2005 Posts: 91
|
|
Posted: Wed Nov 08, 2006 5:27 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 08, 2006 6:29 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
weg22
Joined: 08 Jul 2005 Posts: 91
|
|
Posted: Thu Nov 09, 2006 8:17 am |
|
|
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
|
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|