multiple switches~one ADC


Closed Thread
Results 1 to 15 of 15
  1. #1
    Join Date
    Sep 2006
    Posts
    42

    Default multiple switches~one ADC

    Planning to connect multiple switches into single port.one ADC pin to detect voltage level when each switch for each state is pressed and each switch is associated with a voltage.connecting 8 switches with 1 adc.. Looking for some coding tips.. Thanx.
    Attached Images Attached Images  

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    That should work. After you read the ADC have a gosub with some if/thens or case statements to do ??? at a certain ADC reading. Then go back and read the ADC again.

    You may want the code to loop around reading the ADC and have the value with out a switch being pressed do nothing.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Wink

    Hi, Dave

    This also works very well with POT command ( no ADC required ) ... a well known STAMP Application.

    EPE had also published an original design around a 555 ... key pressing generates a Pulse depending on the key pressed ...
    Interrupt use to detect keystroke ... nice !

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Never cared much for the POT command. Seems not as stable. But that is me.

    I am going to look at your other option. Sounds interesting.
    Thanks!
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Wink

    Quote Originally Posted by mackrackit View Post
    Never cared much for the POT command. Seems not as stable. But that is me.

    !
    Hi, Dave

    I generally do not use POT ... but for 10-12 keys or less result is reliable with a reasonnable range of values for each key.

    soooo, I do it simple ( for once !!! )

    I'll try dig out the EPE Keybd ... it's somewhere in my HDD !!!

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  6. #6
    Join Date
    Sep 2006
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Hi All, tried this code, its working but is there any other reliable way....

    adval var byte ' Create adval to store result

    TRISA = %11111111 ' Set PORTA to all input
    ADCON1 = %00000010 ' Set PORTA analog
    Pause 500 ' Wait .5 second
    loop: ADCIN 0, adval ' Read channel 0 to adval
    Lcdout $fe, 1 ' Clear LCD
    Lcdout "Value: ", DEC adval ' Display the decimal value
    Select case adval
    CASE is <51
    high porte.0
    cASE 102
    high porte.1
    CASE 153
    high portd.2
    CASE 204
    high portd.3
    CASE is >225
    low porte.0 : low porte.1 : low portd.2 : low portd.3
    Pause 150
    END SELECT
    pause 100
    Goto loop
    End
    * this below line is not accepting :ERROR
    case >1 to <53
    'do some thing
    case >53 to < 102
    'do some thing..
    So On.....

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    That way looks good to me.

    This part ...
    Code:
    * this below line is not accepting :ERROR
    case >1 to <53
    'do some thing
    case >53 to < 102
    'do some thing..
    So On.....
    Work something like this into your code, maybe before the CASE part. If the conditions are not met then the code will run to the CASE select part.
    Code:
    IF (ADVAL > 1) AND (ADVAL < 53) THEN ....
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Sep 2006
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Thanks mackrackit, IF (ADVAL > 1) AND (ADVAL < 53) THEN ... this code is working..
    Since we using ADCIN for switches, should be able to TOGGLE the ports.. I have tried with WHILE..WEND also.. no luck.. Could you pls ..Thanks again.

  9. #9
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Can we see your WHILE/WEND code? Or better yet your new and updated code.
    But something like this should work
    Code:
    ADCIN 0, adval
    WHILE (ADVAL > 1) AND (ADVAL < 53)
    do something
    ADCIN 0, adval
    WEND
    SELECT CASE ....
    Dave
    Always wear safety glasses while programming.

  10. #10
    Join Date
    Sep 2006
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Hi, The debounce was the problem..Following both codes are working after adding some delay.. I didnt use CASE.. Pls suggest if it needs to be fine tuned..Thanx..
    if (ADVAL > 200) AND (ADVAL < 256) THEN
    toggle porte.0
    pause 150
    endif
    -----------------------------------------------
    ADCIN 0, adval
    WHILE (ADVAL = 255) 'AND (ADVAL < 256)
    toggle porte.0
    pause 150
    ADCIN 0, adval
    WEND

  11. #11
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    If it is all working then I would say it is good.
    not knowing yhe whole project it is hard to say if it needs fine tuned, but what you have looks like a good solution.
    Dave
    Always wear safety glasses while programming.

  12. #12
    Join Date
    Sep 2006
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    If it is all working then I would say it is good.
    not knowing yhe whole project it is hard to say if it needs fine tuned, but what you have looks like a good solution.
    Thanks..Shall post the complete project shortly..

  13. #13


    Did you find this post helpful? Yes | No

    Default

    Hi Naga. I did something like this before. I edited the code & schematic to show only the relevent info. The only thing different is if you press the bottom 2 switches at the same time, it will do a 5th function.

    ANSEL = 0 'all inputs digital ADC command converts to analog
    CMCON = 7 'comparators off
    DEFINE OSCCAL_1K 1 ' Set OSCCAL for 1K device
    @ device pic12F675, INTRC_OSC_NOCLKOUT, wdt_on, BOD_ON, pwrt_on, mclr_off, protect_on
    BUT VAR BYTE 'MESSAGE BUTTONS VARIABLE
    BUT2 VAR BYTE 'VARIABLE TO COMPARE BUT READINGS TO PREVENT WRONG ADC READINGS

    Pause 500 'SETTLE DOWN

    START:
    NAP 0 'REDUCE STANDBY CURRENT
    ADCIN 3,BUT
    IF BUT < 100 Then TABLE 'BUTTON PUSHED
    GoTo START

    TABLE:
    ADCIN 3,BUT
    Pause 100
    ADCIN 3,BUT2
    IF BUT <> BUT2 Then TABLE 'RE-READ THE ADC

    IF BUT < 91 AND BUT > 71 Then (DO 1ST THING)
    IF BUT < 50 AND BUT > 40 Then (DO 2ND THING)
    IF BUT < 15 AND BUT > 8 Then (DO 3RD THING)
    IF BUT < 8 AND BUT > 4 Then (DO 4TH THING)
    IF BUT < 5 AND BUT > 2 Then (DO 5TH THING)
    GOTO START
    Attached Images Attached Images

  14. #14
    Join Date
    Sep 2006
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    @ Peterdeco1,Thank you very much .. Very nice..Working perfectly.. Thanks to all of you..
    Other problem is..
    When using MeLabs default (PM) Assembler compiles OK:
    @ device pic12F675, INTRC_OSC_NOCLKOUT, wdt_on, BOD_ON, pwrt_on, mclr_off, protect_on

    When using Microchip's (MPASM) Assembler:

    @ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    3 errors,image attached..
    Attached Images Attached Images  
    Last edited by naga; - 18th August 2009 at 08:45.

  15. #15
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    1 error and two warnings.
    Not sure about the warnings but they are just warnings.
    The error looks like the Inc file has not been commented out.
    http://www.picbasic.co.uk/forum/showthread.php?t=543
    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. Stable Adc Reading Routine
    By gebillpap in forum General
    Replies: 27
    Last Post: - 13th May 2015, 03:18
  2. multiple ADC using DSpic 30f3010
    By angeline in forum mel PIC BASIC
    Replies: 6
    Last Post: - 22nd February 2008, 11:03
  3. 18F1320 ADC multiple channel select
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th November 2005, 22:40
  4. Problem reading multiple ADC ports
    By jswayze in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 4th November 2004, 17:46
  5. Reading multiple ADC channels FAST
    By lwindridge in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st April 2004, 23:37

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