Use a switch by change not on/off status.


Closed Thread
Results 1 to 17 of 17

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,159


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Off the top of my head in pseudo-code:

    Code:
    SwitchIn VAR PortB.0
    OldState VAR BIT
    SwitchState VAR BIT
    
    OldState = SwitchIn
    SwitchState = 0
    
    Main:
      IF SwitchIn not = OldState THEN
        SwitchState = SwitchState ^ %1  ' Switch has been toggled
        OldState = SwitchIn
      ENF-IF
    GOTO Main
    Robert

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    I would use interrupt on change. ISR just changes output port status. Set port status in start up code to " your safe off status" set interrupts off until pic is stable then enable interrupts. Whatever status toggle switch is in initally is irrelevant as changing it triggers the interrupt.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,159


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Interrupt on change is more efficient. But this is Koert's first post and he just joined, he hasn't given any indication to his level of PBP expertise..

    Assuming he is a beginner, he's better off trying logic instead of interrupts to get the ball started, then switch to interrupts later as a challenge.

    Koert, search for Darrel Taylor's Instant Interrupts in FAQ section. Unless you only do super simple programming, they help A LOT when using USB, timers, interrupts, and many more features.

    Robert

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    I agree with Robert contact bounce and such can really confuse an interrupt based solution ,unless you have some experience behind you. slow changing inputs can easily be read (and debounced) by using polling style solutions

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    contact bounce can be marginalized with a short pause. IOC would trigger going to either logic state and his output would always start out at what it is initialized to. If he is not too worried about latency then even ON INTERRUPT would work, though I prefer Darrel's asm interrupts.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. Toggle switch (latching switch) state
    By craigwb in forum General
    Replies: 4
    Last Post: - 6th November 2011, 14:18
  2. Switch sensor vs. switch PIC pin
    By fratello in forum Schematics
    Replies: 10
    Last Post: - 4th August 2011, 11:17
  3. Interrupt status?
    By circuitpro in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 28th February 2011, 01:27
  4. Replies: 6
    Last Post: - 9th January 2011, 15:26

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