Debouncing rotary encoder for MCU

Discussion in 'The Projects Forum' started by guitarguy12387, Apr 19, 2010.

  1. guitarguy12387

    Thread Starter Active Member

    Apr 10, 2008
    359
    12
    Hey!

    So i'm using a rotary encoder to control some relays via MCU and i'm searching for the best debouncer for the thing! I seem to have found a decent solution using some caps in combination with a software debouncer. Any of you guys have success with any specific implementations?

    Thanks!
     
  2. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Do the debouncing in software.
     
  3. guitarguy12387

    Thread Starter Active Member

    Apr 10, 2008
    359
    12
    Yeah thats what i have been doing mostly... just trying to optimize the delay time because encoders can be turned pretty quickly... so its a bit tough to balance between 'real turns' and bounces. Any suggestions on delay values?
     
  4. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Pushbuttons are usually 30ms, I would try 5ms to start.
     
  5. guitarguy12387

    Thread Starter Active Member

    Apr 10, 2008
    359
    12
    Cool! Thanks for the tips!
     
  6. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    With a quadrature decoder there should be no need to debounce it.

    If you properly decode all states the count will jitter with the bounce but still settle to the correct value.

    The simplest way to do a full decode is, at each pass through the reading loop, to save the two inputs from the previous pass (eg. at bits 2 & 3 of a memory location) and store the new inputs at bits 0 & 1.

    You can then mask that to $0F and use a 16 value lookup table to get the change value ( 0, +1, -1) that matches the states.
     
  7. guitarguy12387

    Thread Starter Active Member

    Apr 10, 2008
    359
    12
    Okay i sort of follow you. I don't understand how it solves the bouncing problem though. If you mask the old value with the current value every clock cycle, it seems like the input values would just follow the bounce. Does that make sense what i'm saying?

    Also, i have no idea what ya need lookup table for...?

    Thanks for the help and patience :)
     
  8. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    I'll call the encoder data store 'quad'.

    Each loop:
    shift quad left two bits.
    Load the two encoder inputs B & A to bits 1 & 0 of quad.
    AND quad with 0x0F
    (So old bits plus new bits are in the low four bits; 16 possible values).

    Concept:
    The quadrature count sequence is this:
    S: 0 1 2 3 0 1 2 3...
    A: 0 1 1 0 0 1 1 0
    B: 0 0 1 1 0 0 1 1

    By looking at both the present values and last values, you can see where in the sequence it is and which way it's counting, eg.

    quad 1011
    bits= BABA
    That's old state 3, new state 2; counting down.

    quad 0111
    bits= BABA
    That's old state 1, new state 2; counting up.

    Back to the program:
    Do a lookup table based on the value of quad:

    0: 0000 = 00 ; No change
    1: 0001 = 01 ; A 0>1, count up
    2: 0010 = FF ; B 0>1, count down
    3: 0011 = 00 ; Both changed, invalid
    4: 0100 = FF ; A 1>0, Down
    5: 0101 = 00 ; no change
    6: 0110 = 00 ; Invalid
    7: 0111 = 01 ;
    8: 1000 = 01
    9: 1001 = 00
    A: 1010 = 00
    B: 1011 = FF
    C: 1100 = 00
    D: 1101 = FF
    E: 1110 = 01
    F: 1111 = 00

    Table is entry number hex or binary, then = output value.

    Add the output to your main counter store. 01 increments it, FF decrements it, 00 no change.
     
  9. navaidstech

    New Member

    Nov 12, 2010
    1
    0
    I understand this is an old post but I've been searching for ways to debounce rotary encoders and I came upon this posting. There was another one very similar to this but for some reason both don't seem to work.

    Let's assume a mechanical switch that, unfortunately, has bounce... the waveform would look like the following two waveforms with bounce spikes preceding main pulses on both channels. Let's assume initial counter setting of 5. Since I can't draw waveforms here, I'll just post the bits and the single 1's denote a bounce spike in each stream:

    Equivalent bits are:

    A 00100111111100000
    B 00000010011111100


    So the sequence is as follows

    0000: No change, counter at 5
    0001: Count up to 6
    0100: Count down to 5
    0000: No change, counter at 5
    0001: Count up to 6
    0111: Count up to 7
    1101: Count down to 6
    0101: No change, counter at 6
    0111: Count up to 7
    1111: No change, counter at 7
    1111: No change, counter at 7
    1110: Count up to 8
    1010: No change, counter at 8
    1010: No change, counter at 7
    1000: Count up to 8
    0000: No change, counter at 8

    So it would seem that with a single click of the rotary switch, I'm moving my count up 3 times! My question at this point would be: what am I doing wrong, or is this scheme not suitable for switch debouncing?

    thanks
     
    Last edited: Apr 7, 2012
Loading...