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

A2D channels

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



Joined: 15 Jan 2010
Posts: 8

View user's profile Send private message

A2D channels
PostPosted: Mon Jul 26, 2010 8:54 am     Reply with quote

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

View user's profile Send private message

A2D channels: a few more details...
PostPosted: Mon Jul 26, 2010 9:00 am     Reply with quote

The hardware I'm attaching are Sharp IRSensors:
http://www.acroname.com/robotics/info/articles/sharp/sharp.html

My compiler version is 4.104
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 26, 2010 6:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 27, 2010 12:18 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 27, 2010 12:33 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 27, 2010 1:05 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 27, 2010 1:07 pm     Reply with quote

That's what I was just looking at. The layout of the User Interface
connector doesn't show any connections to the AN3 pin. Only AN1, AN2.
http://www.ccsinfo.com/pdfs/product_PDFs/8722_board.pdf
reneec



Joined: 15 Jan 2010
Posts: 8

View user's profile Send private message

PostPosted: Tue Jul 27, 2010 1:22 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 27, 2010 1:40 pm     Reply with quote

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.
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