Shop OBEX P1 Docs P2 Docs Learn Events
BS2 and a rotary dip switch. — Parallax Forums

BS2 and a rotary dip switch.

MikeSabMikeSab Posts: 7
edited 2006-04-23 16:59 in BASIC Stamp
Hey folks, I basically want to integrate a rotary dip switch into my project, but how would I be able to detect the different states of the switch through the BS2? Basically I have a rotary dip with 1-10 positions and I would like each to represent the amount of minutes for a count down timer. Thanks.

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-04-01 19:37
    Mike -

    Which DIP switch are you using? The answer lies in how it is supposed to be wired up, and if it produces some kind of specialized output (BCD, HEX, gray code, etc).

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • SSteveSSteve Posts: 808
    edited 2006-04-01 19:54
    The StampWorks manual has demo code for this in experiments #14 (connecting the pins directly to BS2 I/O pins) and #24 (connecting through a 74HC165 shift register).

    Once you read the pin state, you just need to interpret the value in code (like Bruce said).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • MikeSabMikeSab Posts: 7
    edited 2006-04-23 15:32
    Sorry for the looong delay in responding. I have this rotary dip. How would I go about connecting this to my BS2 and detecting the 10 different states of the switch. It's BCD code type if that means anything to you.
    Thanks!
  • SSteveSSteve Posts: 808
    edited 2006-04-23 16:04
    I'm still new to the hardware side, but I'm guessing you can wire this up either active high or active low. I'll let someone else give you pointers on that end because I don't want to suggest something that will fry your stamp.

    The programming part is easy, though. With the "1" terminal connected to P0, the "2" terminal connected to P1, the "4" terminal connected to P2, and the "8" terminal connected to P3, the value of the switch is INA (or ~INA if it's active low).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-04-23 16:44
    Mike -

    I don't know where the link went to, that was in your message above, but if you go there, and download the data sheet, that will show you almost everything you need to know. Personally, I'm not real keen on how they structured the truth tables, so I have re-structured them here for you, in a manner that is hopefully more understandable. My tables and theirs SHOULD be identical, except for the actual format. If not, I may have a typo·in mine:

    /code

    Connection Pin··· Pos'n
    · on Switch······ Number
    · 8· 4· 2· 1··········· N
    ·
    ········· ---
    ··1··0· 0 ·1· ······· · 9
    ··1··0· 0· 0 ········ · 8
    ··0··1··1··1········· · 7
    ··0· 1· 1· 0········· · 6
    · 0· 1· 0· 1········· · 5
    · 0··1· 0· 0········· · 4
    · 0· 0· 1· 1········· ··3
    · 0· 0· 1· 0·········· ·2
    · 0· 0· 0· 1········· · 1

    0 = inactive, low or zero
    1 = active, high or one

    code/

    BCD = binary coded decimal number representation
    HEX = number representation using base 16 numbers (0=>F)

    When using single digit BCD codes (as you are above) they are the same as the HEX representations of those same BCD numbers. Note the descending progression of bits above. As they are·read from left to right, the columns actually represent·the values of ·23 22 21 20 or 8, 4, 2, 1 as they are so labelled. Note also that the position number is nothing more than the SUM (left to right) of the ACTIVE columns! So, said differently, the qualitative VALUE is actually the POSITION number.

    PBASIC permits us to read four, eight or all pin ports at once, if we choose to do so, rather than reading only one pin port at a time. The only caveats are: the pin ports must be in succession, and they must start on certain boundaries. Here are those permissible multiple pin ports and their respective names:

    /code

    · Pin Port Sets Permissible

    | 0-3· | 4-7 |·8-11· |·12-15 |<= Pin port numbers
    | INA |·INB·|· INC· |· IND·· |····· Four input ports at a time
    |·· ···INL···· |······ ·INH······· |····· Eight·input ports at a time
    |··· ····· ··· INS·· ··············· |·····All 16·input ports at once

    So (say) our switch pins (8-4-2-1) are hooked up to pin ports 0-3 respectively, we can read them all at once by saying:

    Dip_Switch VAR NIB

    Dip_Switch = INA· 'Grab all four pin ports (0-3) at once

    Now we can use a computed GOTO to perform the necessary actions based on the reported position (based on its value):

    ON Dip_Switch GOTO POSN1, POSN2, POSN3, ... POSN9

    POSN1:

    · 'Do this
    GOTO DIP_Exit

    POSN2:

    · 'Do that
    GOTO Dip_Exit

    ...

    POSN9:

    · 'Do something else

    Dip_Exit:
    code/

    That should get you well on your way.

    Regards,

    Bruce Bates



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • MikeSabMikeSab Posts: 7
    edited 2006-04-23 16:59
    Thank you so much Steve and Bruce!!!!!
Sign In or Register to comment.