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

ADC giving weird Value!!

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








ADC giving weird Value!!
PostPosted: Sat Jan 27, 2007 2:45 pm     Reply with quote

I am using an 18 seris PIC device. I am using it for reading ADC values from a sensor.
It has a 10 bit ADC.
So I implemented:
#device adc=10
right at the top of my code.

Since the default is an 8 bit ADC.

I tried reading the value of the sensor into an int16 or long variable.
When I checked the value I got 7788(hex) as my hex value. This is way… more than even the range of 1023 (dec) or 03FF(hex) that should be coming out of the ADC.
Any ideas why I could be having this problem!!

This is what I am doing:
int16 output;

SETUP_ADC_PORTS(AN0_TO_AN4 | VSS_VDD) setup_adc(ADC_CLOCK_DIV_4);

output = Read_ADC();

The value I should be getting instead of 7788(hex) should be 01FF(hex) as the voltmeter shows the output of the sensor as 2.5 V.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jan 27, 2007 3:34 pm     Reply with quote

Notice that the value is close to your desired result if you divide it by 64.
In other words, the 10-bit result is currently left justified in the 16-bit
word and you want it to be right justified. There is a register bit that
controls this in the PIC. See my post in the following thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=29074&highlight=adfmbit

Because you're using an 18F PIC, you will have to change the register
address of the ADFM bit. (The example code is for a 16F series PIC).
Check the data sheet to find the correct register address.
Guest








PostPosted: Sun Jan 28, 2007 2:41 pm     Reply with quote

I am using a PIC18F2520. And I implemented the following code. But it did not make any difference!

#device adc=10
#bit ADFM_BIT = 0xFC0.7
int16 output;

SETUP_ADC_PORTS(AN0_TO_AN4 | VSS_VDD)
setup_adc(ADC_CLOCK_DIV_64);

ADFM_BIT = 1;

output = Read_ADC();

The value I got for output is 7800(hex), where as I should be getting around 01FF(hex) as the voltmeter shows the output of the sensor as 2.5 V.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 28, 2007 3:44 pm     Reply with quote

Compile the code and look at the .LST file. I did that with PCH vs. 3.249
and it's there:
Code:

.................... ADFM_BIT = 1; 
002C:  BSF    FC0.7

0xFC0 is the address of the ADCON2 register and the ADFM bit is
in bit 7. The 18F2520 data sheet says:
Quote:
1 = Right justified


I have a few questions:

1. I never see you set the A/D channel. This is done with:
Code:
    set_adc_channel(0);

What channel are you reading ? What pin on the PIC do you
have your analog signal connected to ?

2. What's your crystal frequency ?

3. What's your compiler version ?

4. Is this project being done on an emulator like Proteus or is it
being done on real hardware ?

5. Can you post a complete program ? You may be putting certain
lines of code in the wrong position in the program.
Here's an example of a test program for the A/D converter:
http://www.ccsinfo.com/forum/viewtopic.php?t=28887&start=7
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