PDA

View Full Version : ADCIN question



Greg McFadden
- 14th September 2008, 18:46
Pardon the newbie question. I was searching and never came across an answer that I understood to this question. For reference, I am working with a 10F222 chip running at 5V Vdd.

If I read in a voltage using ADCIN from gpio.0, and store that value as a byte variable, I am trying to figure out what that value will be for various voltages at the gpio.0 pin.

Specifically, the voltage will vary. slowly, from 2.5VDC to 0.7VDC before the pic shuts down due to the boost regulator not being able to take in anything lower than 0.7VDC and convert to 5VDC to drive the pic.

This is the chunk of code I am curious about. One of my friends tossed me the front end information to get me started (and so I can hopefully figure it out).

@ device pic10F222, iofscs_8mhz, wdt_on, mclr_off, protect_off
ADCON0 = 0 'better disable the A/D module so PBP can config it right
osccal = %00000000 'turn off clockout!
OPTION_REG = %00000000 'bit5 is critical here for GPIO.2 TOCS = FOSC/4 I don't yet understand this one either....

DEFINE OSC 8 '8MHz
DEFINE ADC_BITS 8 ' set adcin to 8 bit

Vin var BYTE
gpio.0 = 1 'this is the a/d input
ADCIN 0,Vin


and if Vin in real life is 2.5V, 1V, or 0.7V... how do I figure out what value will get placed in the Vin variable?

thanks a lot
Greg

tenaja
- 14th September 2008, 19:24
If you are reading an 8-bit a/d converter, then your input will be from 0 to 255. The scaling depends on your reference. typically, for cheap setups, this is Vin. If your Vin is 3.0 volts, you divide that by 256 to see how many volts each bit is worth. 3/256=.0117. So, if you get a reading of 200, your voltage is just over 2.3. Just plug the math in to convert it in code, or for more compact code, simply use precalculated constants.

Darrel Taylor
- 15th September 2008, 05:55
IOFSCS_8MHZ     Hmmm, dyslexia day at Microchip :)

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2858&stc=1&d=1221454287" /><!-- 2858 -->

Your device line is OK, just pointing out an oddity.
<hr>

> how do I figure out what value will get placed in the Vin variable?

1 decimal place ...
Volts = (Vin+1) */ 50

2 decimals ...
Volts = (Vin+1) */ 500

Darrel Taylor
- 15th September 2008, 06:21
I read that question again, and maybe you want the other way around.

> how do I figure out what value will get placed in the Vin variable?

With 8-bit resolution, the values will range from 0 to 255.

If the input is 2.5 volts, then the value that gets placed in Vin will be ...

255 / (5 / 2.5) = 127

For 0.7v that's ...

255 / (5 / 0.7) = 35

hth,

Greg McFadden
- 16th September 2008, 02:53
thanks! all!. This is all quite new to me. the last thing I programed was some cfd in fortran 95, so the hardware side of things is different.

I think I may end up going to a PIC12F683 just for flexibility, as I can still (barely) fit it on the circuit board I am making.