PDA

View Full Version : really simple adcin question



kitcat
- 17th April 2006, 14:06
here is some simple code reading a pot with adcin and it works fine:


vin var byte
compvalue var byte

main:

adcin porta.0, vin

if compvalue <> vin then serout porta.5,6,[#vin,10,13]

compvalue= vin

goto main



However the pot is a little jittery and I want to add a routine to allow for a slight variation in the input.


This is where I have had real problems

I thought this would work:

if compvalue > (vin + 1) then serout porta.5,6,[#vin,10,13]

But I get no result. There must be something really dumb I am missing!

mister_e
- 17th April 2006, 15:04
that's interesting, how about if you just change to


if compvalue > (vin + 1) then
serout porta.5,6,[#vin,10,13]
endif


Or having anoher Var like



TestVar=vin+1
if compvalue > TestVar then
serout porta.5,6,[#vin,10,13]
endif

kitcat
- 18th April 2006, 13:35
Hmmn. Neither of these seem to work as I would expect.
I find it works better if I put a "pause 5" at the end. Not quite sure if it is down to a speed issue?

mister_e
- 19th April 2006, 14:02
How about if you just monitor you reading on a LCD or Serial PC communication? Is there variation as it's suppose to have, or the results are just messy?

How about your ADC config?

How about the impedance of your POT? Must meet the maximum state in the datasheet...

kitcat
- 19th April 2006, 15:18
Hmmn. I wonder what happened to my previous reply

adcin porta.0, vin
if compvalue > (vin + 1) or compvalue < (vin - 1) then serout porta.5,6,[#vin]
pause 10
compvalue = vin

Ok. I am using a pickit with the on board test pot.
with no if statement I get a stream of numbers in hyperterminal that range from 0 to 255 when I turn the pot so it seems like my adcin is working as I would expect.

Without a pause the if statement is always false. With a pause of 10 the if statement becomes true when I turn the pot fast but not when I turn it slowly. The longer the pause the more slowly I can turn the pot and get a result in Hyper terminal.

Is it possible that the whole code executes while the adcin is being done in the background? it seems that compvalue is always = to vin. I am confused!

mischl
- 19th April 2006, 20:58
i would go a step back first :

which pic did you use? how is your setup of ANSEL, CMCON? OSC?

best is you post the whole code

kitcat
- 20th April 2006, 10:32
pic16f688

OSCCON = %01110000
DEFINE OSC 8
trisa = %101111
vin var byte
compvalue var byte
main:
adcin porta.0, vin
if compvalue > (vin + 1) or compvalue < (vin - 1) then serout porta.5,6,[#vin]
compvalue = vin
goto main

CMCON0 = 7 gives me a syntax error for some reason.

kitcat
- 20th April 2006, 10:39
CMCON0 = 7 gives me a syntax error for some reason.......
no it doesn't. I got the patch. This setting has not made a difference though.

peterdeco1
- 20th April 2006, 11:28
Try this. Change your serout to HIGH to light an LED as an indicator. Then sample compvalue only once.

start:
low portb.0
adcin porta.0, compvalue

main:
adcin porta.0, vin
if compvalue > (vin + 1) or compvalue < (vin - 1) then high portb.0 : pause 1000 : goto start
goto main

kitcat
- 20th April 2006, 12:19
Hey. That works. THis is my code:

start:
low porta.5
adcin porta.0, compvalue

main:
adcin porta.0, vin
if compvalue > (vin + 1) or compvalue < (vin - 1) then serout porta.5,6,[#vin] : goto start
goto main


can you explain why this works and the other way doesn't?

Melanie
- 20th April 2006, 12:50
Without going into your code in any detail, your original code (below) has a flaw in it's thinking...

if compvalue > (vin + 1) or compvalue < (vin - 1) then serout porta.5,6,[#vin]
compvalue = vin

If vin increases by ONE, no change happens, but you replace compvalue with the content of vin.

Next time around, if vin changes by ONE, you do the same.

So vin can slowly change by ONE, you don't get a SEROUT but compvalue starts drifting.

What your code should have done is this...

if compvalue > (vin + 1) or compvalue < (vin - 1) then
serout porta.5,6,[#vin]
compvalue = vin
endif

this way, compvalue is updated ONLY if you've done a SEROUT, no SEROUT so no update of compvalue.

kitcat
- 20th April 2006, 13:02
Oh yes. Thankyou. It all makes sense now!

peterdeco1
- 21st April 2006, 09:06
After reading a different post concerning the ADCIN command, I overlooked an obvious mistake. The command should be ADCIN 0,VIN not ADCIN PORTA.0,VIN. Always address the channel name not the port pin number.