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

Problem with A/D - code included - please help

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



Joined: 06 Jan 2007
Posts: 12

View user's profile Send private message

Problem with A/D - code included - please help
PostPosted: Tue Jan 23, 2007 8:49 am     Reply with quote

Hi All,

I have some problem with some A/D code I have written. I have 1.2ish Volts applied to AN1/RA1 on my PIC18F2550. I did not use AN0/RA0 ( is this a problem )

Now all I get is various random numbers from the ADC and sometimes I just get 0 and sometimes 1023 and also other numbers. To measure the A/D I just hook the voltage to the pin? Its the temperature pin coming out of a memsic 2125. When I measure the output from the pin it is showing 1.22

Below is the code - hopefully its something simple :(

#include <18F2550.h>
#device adc=10
//configure a 20MHz crystal to operate at 48MHz
#fuses HSPLL,NOWDT,NOPROTECT,LVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#include <usb_cdc.h>

#rom int 0xf00000={1,2,3,4}

void main() {
long temp;
usb_init();
while(!usb_cdc_connected()) {}
setup_adc_ports( AN0_TO_AN1 | VSS_VDD );
setup_adc(ADC_CLOCK_DIV_8 );
set_adc_channel( 1 );

do {
temp = read_adc();
printf(usb_cdc_putc, "ADC 1 : %ld \n\r", temp);
delay_ms(5000);
} while (TRUE);
}

Please help Smile

Peter
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 12:41 pm     Reply with quote

Quote:

#use delay(clock=48000000)

void main() {
long temp;
usb_init();
while(!usb_cdc_connected()) {}
setup_adc_ports( AN0_TO_AN1 | VSS_VDD );
setup_adc(ADC_CLOCK_DIV_8 );
set_adc_channel( 1 );


Download the latest data sheet for the 18F2550:
http://ww1.microchip.com/downloads/en/DeviceDoc/39632c.pdf

Look at this table in the data sheet:
Quote:
TABLE 21-1: TAD vs. DEVICE OPERATING FREQUENCIES
It's on page 265 (page 267 in the Acrobat reader).

Look at this column in the table:
Quote:
Maximum Device Frequency
18Fxxxx

Locate the frequency that you're using (48 MHz). Now look over to
the left side of the table. What divisor value is recommended ?
Compare that to the one that you have in your code above.
PeterDove



Joined: 06 Jan 2007
Posts: 12

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 4:12 pm     Reply with quote

PCM programmer wrote:
Quote:

#use delay(clock=48000000)

void main() {
long temp;
usb_init();
while(!usb_cdc_connected()) {}
setup_adc_ports( AN0_TO_AN1 | VSS_VDD );
setup_adc(ADC_CLOCK_DIV_8 );
set_adc_channel( 1 );


Download the latest data sheet for the 18F2550:
http://ww1.microchip.com/downloads/en/DeviceDoc/39632c.pdf

Look at this table in the data sheet:
Quote:
TABLE 21-1: TAD vs. DEVICE OPERATING FREQUENCIES
It's on page 265 (page 267 in the Acrobat reader).

Look at this column in the table:
Quote:
Maximum Device Frequency
18Fxxxx

Locate the frequency that you're using (48 MHz). Now look over to
the left side of the table. What divisor value is recommended ?
Compare that to the one that you have in your code above.


Changed it to

setup_adc(ADC_CLOCK_DIV_64 );

Though the output is still random? I assume when I set up them as analogue that thye are set up as inputs?

Peter
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 4:21 pm     Reply with quote

Post your compiler version. It's a number such as 3.191, 3.249, 4.020,
etc. You can find it at the top of the .LST file, which is in your project
directory. Don't post any numbers that come after the version number.
PeterDove



Joined: 06 Jan 2007
Posts: 12

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 5:22 pm     Reply with quote

PCM programmer wrote:
Post your compiler version. It's a number such as 3.191, 3.249, 4.020,
etc. You can find it at the top of the .LST file, which is in your project
directory. Don't post any numbers that come after the version number.


Its 4.020

Just in case it helps, I am taking the out from a parrelex/memsic 2125, its the temperature out and applying it straight to RA1. Also the memsic has its own power supply/circiut so the wire just runs from that circuit up to the RA1 pin.

Peter
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 5:32 pm     Reply with quote

Do you have a ground connection between the Gnd pin on the Memec
and the Vss pin (i.e., ground) on the PIC ?
PeterDove



Joined: 06 Jan 2007
Posts: 12

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 5:38 pm     Reply with quote

PCM programmer wrote:
Do you have a ground connection between the Gnd pin on the Memec
and the Vss pin (i.e., ground) on the PIC ?


No, do I need this, do I need to put a line between the ground of the PIC and the ground of the memsic. Both are separately powered circuits. Sounds like I am about to learn something Smile I really appreciate you taking the time

Peter
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 5:47 pm     Reply with quote

Yes, you need a common ground connection between the two devices.
PeterDove



Joined: 06 Jan 2007
Posts: 12

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 5:51 pm     Reply with quote

PCM programmer wrote:
Yes, you need a common ground connection between the two devices.


Thanks a lot for your help - I was just hoping I could mash the circuits together but looks like I will need to build a whole new circuit. Shouldnt take me long though.

Thanks

Peter
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