View previous topic :: View next topic |
Author |
Message |
reneec
Joined: 15 Jan 2010 Posts: 8
|
A2D channels |
Posted: Mon Jul 26, 2010 8:54 am |
|
|
Hopefully this is pretty straight forward... I have a PIC18F8722 CCS development board, and I'm using the PCW compiler on XP. I'd like to read analog input from two different sensors. My approach is pretty straight forward - set the sensors up on channels 2 and 3. My problem is that only channel 2 appears to be working. My code is as follows:
Code: |
#include <18f8722.h>
#device ICD=TRUE ADC=10
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#use rs232 (DEBUGGER)
void main () {
unsigned long int voltage;
float distance;
float m = .000399;
float b = .000757;
float k = 1.5;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
while(TRUE) {
set_adc_channel( 2 );
delay_ms(1);
voltage = read_adc();
distance = ( (1/m) / (voltage + (b/m) ) ) - k;
delay_ms(1000);
printf("%f\n", distance);
}
}
|
So the odd thing is that this code works just fine when I try channel 2, but when I switch to channel 3 (or 4 or 5...) I just get bogus values. I'm plugging my sensor into the user interface block as follows (p=power, g=ground, S=sensor, x=blank):
channel 2 (works):
p S x x ...etc... g
p x x x ...etc... g
channel 3(doesn't work):
p x x x ...etc... g
p x S x ...etc... g
I've tried switching around my sensor to other locations (in case what I thought to be channel 3 was wrong), and it still doesn't work.
Any suggestions?
Thanks,
R |
|
|
reneec
Joined: 15 Jan 2010 Posts: 8
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 26, 2010 6:09 pm |
|
|
1. What's the exact part number of your Sharp sensor ? About half-way
down the page (on the link that you gave) it has links to 7 different
sensors. Some are analog and some are digital. Which one are you using ?
2. You posted a program works. Post one that fails.
3. Look at the schematic for the 8722 board. Are there any other
signals already connected to the pins for A/D channels 3, 4 and 5 ?
Such as LEDs, etc.
4. This clock setting is incorrect for a 20 MHz oscillator:
Code: |
setup_adc(ADC_CLOCK_INTERNAL);
|
According to the 18F8722 data sheet, you should use this setting:
Code: |
setup_adc(ADC_CLOCK_DIV_16);
|
|
|
|
reneec
Joined: 15 Jan 2010 Posts: 8
|
|
Posted: Tue Jul 27, 2010 12:18 pm |
|
|
Thanks for your response.
1. Here is the exact part: http://www.acroname.com/robotics/parts/R48-IR12.html Yes, it is the analog sensor that I'm using.
2. Here is the "failed" program. I have revised the clock setting.
Code: | #include <18f8722.h>
#device ICD=TRUE ADC=10
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#use rs232 (DEBUGGER)
void main () {
unsigned long int voltage;
float distance;
float m = .000399;
float b = .000757;
float k = 1.5;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
while(TRUE) {
set_adc_channel( 2 );
delay_ms(1);
voltage = read_adc();
distance = ( (1/m) / (voltage + (b/m) ) ) - k;
delay_ms(250);
printf("%f\n", distance);
}
}
|
3. No, there aren't any other parts connected to the other channels I was using. I was using pins A2 (channel 2 - works) and I've tried C5, C4, C3, etc (corresponding to channels 3, 4, 5). |
|
|
reneec
Joined: 15 Jan 2010 Posts: 8
|
|
Posted: Tue Jul 27, 2010 12:33 pm |
|
|
Sorry, I posted the working program by accident. Here is the fail one:
Code: | #include <18f8722.h>
#device ICD=TRUE ADC=10
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#use rs232 (DEBUGGER)
void main () {
unsigned long int voltage;
float distance;
float m = .000399;
float b = .000757;
float k = 1.5;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
while(TRUE) {
set_adc_channel( 3 );
delay_ms(1);
voltage = read_adc();
distance = ( (1/m) / (voltage + (b/m) ) ) - k;
delay_ms(250);
printf("%f\n", distance);
}
} |
|
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Jul 27, 2010 1:05 pm |
|
|
reneec wrote: | I was using pins A2 (channel 2 - works) and I've tried C5, C4, C3, etc (corresponding to channels 3, 4, 5). |
Maybe that is why it doesn't work. According to the 8722 datasheet AN3 is RA3, AN4 is RA5, and AN5 is RF0.
Where did you get C5, C4 and C3 ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
reneec
Joined: 15 Jan 2010 Posts: 8
|
|
Posted: Tue Jul 27, 2010 1:22 pm |
|
|
Thanks again for taking the time to look at this. I just found my answer in the device file:
#define ALL_ANALOG 0x00 // A0 A1 A2 A3 A5 F0 F1 F2 F3 F4 F5 F6 H4 H5 H6 H7
Looks like channel 3 corresponds to pin A3, which isn't on the UTB. I'll try using the F0, F1 pins for my channels. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Jul 27, 2010 1:40 pm |
|
|
Or you can just look at page 3 of the PIC 18F8722 datasheet to see where AN0, AN1, AN2 etc. are. You can't just randomly plug things into different ports and assume it would work. |
|
|
|