Toggle switch (latching switch) state


Closed Thread
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    Did you find this post helpful? Yes | No

    Default Re: Toggle switch (latching switch) state

    Hi,
    If you only want to read it once when it changes you have to know that is has changed and the only way to know that is to read it (or use the Interrupt on change feature) The easiest way is yo keep reading the switches in a loop and compare the current state to the previous state, if they are not the same one or more switches have changed state. Lets say your switches are connected to PortB.0 thru PortB.5, then perhaps something like:
    Code:
    Switches VAR BYTE
    oldState VAR BYTE
    TRISB=%00111111    'PortB.0-5 are inputs.
    oldState = 0
    ReadSwitch:
      Switches = PortB & %00111111  'Get 6 low bits.
      IF Switches <> oldState THEN  ' Has it changed?
        GOSUB ActivateTx            ' Deal with it.
      ENDIF
      oldState = Switches           ' Remeber current state
    Goto ReadSwitch                 ' Do it again.
    
    ActivateTx:
    ' Do what needs be done with the transmitter
    ' and anything else here
    RETURN
    /Henrik.

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    969


    Did you find this post helpful? Yes | No

    Default Re: Toggle switch (latching switch) state

    Henrik has suggested one possibility. Another is to use a XOR function.

    The simple pseudo code for this would be
    Code:
    MyLoop:
            Keys = Read_switch_now
            if (PrevKeys XOR Keys) then
                gosub KeysHaveChanged
            endif
            PrevKeys = Keys
    goto MyLoop
    Cheers

Members who have read this thread : 0

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