Improved Debounce Routines Print
Written by Vladimir Mitrović   

Bascom’s Debounce statement solves this problem, by checking the switch’s state over again a bit later, when a level change is detected at an input pin. For example, if Debounce is configured to recognise a falling edge at an input pin (a logical level change from High to Low), it will delay the program execution for 25 ms and then check the input pin again to see if it is still at a logic “0” or not. If it is, an associated subroutine will be executed.

If not, the level change will be considered as just a “bounce”, and will be ignored. This reduces the probability of false readings significantly. Also, the Debounce statement reacts to any change of input state only once, and does not block program execution while waiting for the desired state change to happen.

From my early days with Bascom, I realised the value of the Debounce statement and have even succeeded in expanding its capabilities. Let’s see how!

Debouncing using external interrupts

The test circuit shown in Figure 1 is designed for testing the behaviour of the Debounce statement. An example of a suitable test program follows:

‘Program *** Debounce1 ***
Dim Up_dn_counter As Byte
Sw1_pin Alias Pind.2
Config Sw1_pin = Input
Sw2_pin Alias Pind.3
Config Sw2_pin = Input
Up_dn_counter = 100
Do
  
Debounce Sw1_pin , 0 , Sw1_sub , Sub
  
Debounce Sw2_pin , 0 , Sw2_sub , Sub
  
Home L
  
Lcd “Counter = “ ; Up_dn_counter ; “ “        ‘do whatever
Loop
Sw1_sub:
   If
Up_dn_counter > 0 Then
     
Decr Up_dn_counter
  
End If
Return
Sw2_sub:
   If
Up_dn_counter < 255 Then
     
Incr Up_dn_counter
  
End If
Return

It is assumed that an LCD display is configured to match the wiring shown in Fig 1, but those statements are not shown, for simplicity. Pressing push-buttons SW1 or SW2 briefly will decrement or increment the value of the Up_down_counter variable, which will be immediately displayed on the LCD. Normally, each press on a push-button will be counted only once - erroneous readings are extremely rare. The Debounce statement works very well!

However, there is one detail that must be emphasized. To work properly, the Debounce statement must be located inside an endless loop, which is executed frequently. The time between consecutive loop iterations should be as short as possible. For example, if we want to reliably detect a push-button activation that lasts only 150 ms, the time between two loop iterations should be less than 125 ms (the remaining 25 ms are taken up by the execution of the Debounce statement itself). In other words, the loop containing the Debounce statement should execute at least 8 times per second. We can illustrate this problem if we insert a Wait 2 statement at the end of the loop structure, in the previous example. Most short switch activations are now missed, and sometimes it’s necessary to press the button for a pretty long time before the counter changes its value.

Improved Debounce Routines

2012_AVR_UK_27

Continue reading

Shop area

Last Updated on Wednesday, 01 August 2012 14:33