• AVR Freaks

Hot!Encoder switch debounce

Page: 12 > Showing page 1 of 2
Author
didierleplae
Senior Member
  • Total Posts : 164
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
2020/03/18 12:52:08 (permalink)
0

Encoder switch debounce

Can anyone recommend a good method for debouncing a pushbutton encoder switch that does not use a delay?
#1

35 Replies Related Threads

    katela
    Super Member
    • Total Posts : 1647
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: Encoder switch debounce 2020/03/18 13:15:01 (permalink)
    +1 (1)
    If you are using a PIC with core independent peripherals, you could debounce it this way: https://microchipdevelope...ce-using-tmr2-with-hlt

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #2
    Jim Nickerson
    User 452
    • Total Posts : 6584
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: Encoder switch debounce 2020/03/18 13:17:44 (permalink)
    +1 (1)
    didierleplae
    Can anyone recommend a good method for debouncing a pushbutton encoder switch that does not use a delay?


    Read it x times till it does not change
    this ends up using a delay as x is relative to your sample loop delays
    edit: I hope this is not homework smile :)
    post edited by Jim Nickerson - 2020/03/18 13:18:49
    #3
    ric
    Super Member
    • Total Posts : 26159
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Encoder switch debounce 2020/03/18 16:10:41 (permalink)
    +2 (2)
    Do it in a timer interrupt service.
    If that is acceptable comes down to what you mean by "not using a delay".
     
    Do you just want to trigger off the FIRST change, and ignore any bounces that occur soon after,
    or wait until the bounces have stopped?
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    didierleplae
    Senior Member
    • Total Posts : 164
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/18 17:40:41 (permalink)
    0
    katela
    If you are using a PIC with core independent peripherals, you could debounce it this way: https://microchipdevelope...ce-using-tmr2-with-hlt

    I followed the instructions in the link you provided except, since I am using it not for LEDs but for navigating through menu option on a LCD screen, I omitted using CLC to create a flip flop and instead enabled the timer interrupt and put a code to alternate a bool variable in the ISR.
    My system clock is also currently running at 32 MHz, so using the LFINTOSC.
    I wound up setting the timer period rather high, around 100ms. I am still finding that the switch double triggers sometimes.
    #5
    didierleplae
    Senior Member
    • Total Posts : 164
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/18 17:44:50 (permalink)
    0
    ric
    Do you just want to trigger off the FIRST change, and ignore any bounces that occur soon after,or wait until the bounces have stopped?  


    Honestly I’m not sure. I’m after whatever would be the most reliable, as far as not getting double triggers sometimes.
    #6
    ric
    Super Member
    • Total Posts : 26159
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Encoder switch debounce 2020/03/18 17:46:46 (permalink)
    +1 (1)
    Do you have a storage scope to examine the signal, and see exactly what bounce you are getting?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    didierleplae
    Senior Member
    • Total Posts : 164
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/18 18:53:07 (permalink)
    0
    I have an oscilloscope
    #8
    davea
    Super Member
    • Total Posts : 212
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/18 19:26:38 (permalink)
    +1 (1)
    bounce usually occurs only on closure 
    you can use timer2 in monostable mode
    set 10 - 20mS
    insure POL is set correctly for pull-up or down switch 
    or use a simple RC
    #9
    didierleplae
    Senior Member
    • Total Posts : 164
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/19 00:27:37 (permalink)
    +1 (1)
    katela
    If you are using a PIC with core independent peripherals, you could debounce it this way: https://microchipdevelope...ce-using-tmr2-with-hlt

    Actually, got this to work! I had the WPU turned on from a previous version. Maybe that was causing a problem, I'm not sure. "Starts on rising/falling edge" is what made it work, although I'm not sure I understand why.
    #10
    didierleplae
    Senior Member
    • Total Posts : 164
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/19 12:08:12 (permalink)
    0
    Now for a follow up question: is it possible to use the same TMR2 for multiple encoders, or for a rotary encoder as well?
    #11
    pcbbc
    Super Member
    • Total Posts : 1649
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/19 13:11:52 (permalink)
    0
    My understanding is the method you are using resets the timer when the switch is pressed? Obviously in that case a single timer can only handle a single switch.
     
    if you change to a different technique, for example sampling all the switches on a regular interrupt derived from a single timer, then yes it is possible.
     
    What exactly are your requirements, other than “not using a delay”. Do you mean not spin locking in your code (which I agree is usually desirable) or do you mean responding to key presses instantaneously (usually I find less of a requirement).
    #12
    didierleplae
    Senior Member
    • Total Posts : 164
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/19 13:33:59 (permalink)
    0
    pcbbc
    My understanding is the method you are using resets the timer when the switch is pressed? Obviously in that case a single timer can only handle a single switch.
     
    if you change to a different technique, for example sampling all the switches on a regular interrupt derived from a single timer, then yes it is possible.
     
    What exactly are your requirements, other than “not using a delay”. Do you mean not spin locking in your code (which I agree is usually desirable) or do you mean responding to key presses instantaneously (usually I find less of a requirement).


    My reason for not wanting the delay is because I'm working on a sort of drum machine, so timing is important. I found that when I had the delay, each time I would press the encoder it would throw off the rhythm a tiny bit. Not so noticeable if it's just once, but if clicking a bunch of times, it became very apparent.
    I have several switches, although only one of them, because of the way I'm using the others, seems to need the debouncing. However I do have a rotary encoder as well, which is working pretty well, although occasionally it will randomly switch back when I have switched it forward for example.
     
    Part of my question I guess was hypothetical, for future reference.
    #13
    acharnley
    Super Member
    • Total Posts : 517
    • Reward points : 0
    • Joined: 2016/05/01 06:51:28
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/20 01:40:35 (permalink)
    0
    I've just done something similar.

    Use a CLC to SR latch/interrupt on the leading edge and a HLT in monostable mode to reset the CLC on the falling edge (after ~10ms).
    #14
    davea
    Super Member
    • Total Posts : 212
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/20 14:48:55 (permalink)
    0
    if your still having problems
    set reset to either edge in HLT in monostable mode
    you only need 10-20 mS to debounce
    enable ISR and check the input state from there
    latency will be whatever the debounce is set for
     
    #15
    didierleplae
    Senior Member
    • Total Posts : 164
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/21 03:27:43 (permalink)
    0
    acharnley
    I've just done something similar.

    Use a CLC to SR latch/interrupt on the leading edge and a HLT in monostable mode to reset the CLC on the falling edge (after ~10ms).


    I was trying to get this to work, but haven't had any luck. I haven't really used the CLC much, so might be setting it up wrong. I'm setting CLC1 to SR latch mode. Setting the top input to CLCIN0, which I've defined as the pin from my encoder switch. Then I've set TMR2 external reset source to CLC1_out. 
    #16
    didierleplae
    Senior Member
    • Total Posts : 164
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/21 03:42:01 (permalink)
    0
    davea
    if your still having problems
    set reset to either edge in HLT in monostable mode
    you only need 10-20 mS to debounce
    enable ISR and check the input state from there
    latency will be whatever the debounce is set for

     
    I did a bunch of experimenting with this. It seems that if I have the HLT set to either "Starts on rising edge" or "Starts on falling edge", my switch seems to trigger about half the time. If I have it set to "Starts on rising/falling edge" it triggers almost every time. I guess it's picking up the rising edge when the falling doesn't work and vise versa. Not ideal, but at least it works ok. 
    Ideally, I'd like to just use falling edge because I have some situations where I want to have a hold the button for a couple of seconds to switch, but not trigger upon release. 
    #17
    davea
    Super Member
    • Total Posts : 212
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/21 08:39:23 (permalink)
    0
    is it switch or quadrature encoder ?
    is it subjected to vibration ?
    how OLD is the encoder/switch ?
    if it is a switch is in made for low current IE: gold contacts
     
    you can set 2 flags in the ISR new switch, now state
    and disable TMR2 ISR when your in possible "hold"
    handle the rest it in code, polling the current port state 
    enable ISR when port state is hi/low for what is needed
     
     
    #18
    didierleplae
    Senior Member
    • Total Posts : 164
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/21 11:05:10 (permalink)
    0
    davea
    is it switch or quadrature encoder ?is it subjected to vibration ?how OLD is the encoder/switch ?if it is a switch is in made for low current IE: gold contacts you can set 2 flags in the ISR new switch, now stateand disable TMR2 ISR when your in possible "hold"handle the rest it in code, polling the current port state enable ISR when port state is hi/low for what is needed  


    It is the switch component of a rotary encoder. It is new and isn’t being subjected to vibration, or than the actual switch action I guess.
    It does not have gold contacts.

    Thanks for the tip for “hold”. I will try that.
    #19
    RISC
    Super Member
    • Total Posts : 5392
    • Reward points : 0
    • Status: offline
    Re: Encoder switch debounce 2020/03/21 16:32:49 (permalink)
    +2 (2)
    Hi,
    An excellent article about debouncing : http://www.ganssle.com/debouncing.htm
    Regards
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5