really simple adcin question


Closed Thread
Results 1 to 13 of 13
  1. #1
    kitcat's Avatar
    kitcat Guest

    Default really simple adcin question

    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!

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    that's interesting, how about if you just change to
    Code:
    if compvalue > (vin + 1) then 
        serout porta.5,6,[#vin,10,13]
        endif
    Or having anoher Var like

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

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    kitcat's Avatar
    kitcat Guest


    Did you find this post helpful? Yes | No

    Default

    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?

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    kitcat's Avatar
    kitcat Guest


    Did you find this post helpful? Yes | No

    Default

    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!

  6. #6
    Join Date
    Jan 2005
    Posts
    72


    Did you find this post helpful? Yes | No

    Default

    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
    i know it's only microcontrolling, but i like it!

  7. #7
    kitcat's Avatar
    kitcat Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  8. #8
    kitcat's Avatar
    kitcat Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  9. #9


    Did you find this post helpful? Yes | No

    Default

    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

  10. #10
    kitcat's Avatar
    kitcat Guest


    Did you find this post helpful? Yes | No

    Default

    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?

  11. #11
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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.

  12. #12
    kitcat's Avatar
    kitcat Guest


    Did you find this post helpful? Yes | No

    Default

    Oh yes. Thankyou. It all makes sense now!

  13. #13


    Did you find this post helpful? Yes | No

    Default

    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.

Similar Threads

  1. ADCIN question
    By Greg McFadden in forum General
    Replies: 4
    Last Post: - 16th September 2008, 02:53
  2. Really simple question for you experts :)
    By lew247 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 4th June 2008, 01:43
  3. Simple question regarding ports
    By rngd in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd February 2008, 16:37
  4. SIMPLE question
    By ngeronikolos in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 5th February 2008, 18:27
  5. really simple, dumb question
    By picster in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 3rd March 2007, 22:02

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts