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!!
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
Posted: Sat Jan 27, 2007 3:34 pm
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
Posted: Sun Jan 28, 2007 2:41 pm
I am using a PIC18F2520. And I implemented the following code. But it did not make any difference!
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
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