PDA

View Full Version : AD Conversion 8bit 10bit



Eric123
- 28th May 2007, 06:29
I am using a pic16f870 with picbasic to read an analog input from a temp sensor. The 870 has a 10 bit AD converter but I am only geting 8 bits. How can I change my code to get the full 10 bits. Here is my code

Poke $1f, $45
pause 1
peek $1e, w3

when I put a pot on the analog pin and vary the voltage from 0 to 5 volts I get a number from 0 to 255 so I know I am only geting 8 bit instead of 10 bit like I want.

Thanks

skimask
- 28th May 2007, 07:22
Poke $1f, $45
pause 1
peek $1e, w3

when I put a pot on the analog pin and vary the voltage from 0 to 5 volts I get a number from 0 to 255 so I know I am only geting 8 bit instead of 10 bit like I want.

Thanks

That's because you've got your A/D converter set up for Left-Justified operation (see the PIC16F870 datasheet, section 11.0, Register 11-2, left-justified is the default operation).
You're peeking $1E, which is ADRESH (A/D result High), the upper 8 bits of the A/D result. You have to peek the other, lower, 2 bits out of the upper 2 bits of $9E.

So try this (I assume you're using PicBasic, and not PicBasicPro, and I'm not 100% with the limitations of PicBasic):

Poke $1f, $45
pause 1
peek $1e, w3
peek $9e, w4
w3 = w3 * 256 'put adresh BYTE into upper 8 bits of w3 (that's a word variable right?)
w3 = w3 + w4 'add adresl into adresh word
w3 = w3 / 64 'shift the whole result down by 6 bits for a 0-1023 result

Eric123
- 28th May 2007, 15:44
Thanks that is exactly what I needed.