Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:examples:digi:switch_debounce [2012/06/29 11:56] raivo.sellen:examples:digi:switch_debounce [2015/03/05 10:26] (current) – removed raivo.sell
Line 1: Line 1:
-====== Filtration of switch bounce  ====== 
  
-//Vajalikud teadmised: [HW] [[en:hardware:homelab:digi]], [AVR] [[en:avr:io]], [LIB] [[en:software:homelab:library:pin]],  [LIB] [[en:software:homelab:library:delay]], [PRT] [[en:examples:digi:switch]]// 
- 
-===== Theory ===== 
- 
-[{{  :examples:digi:switch:switch_bounce.png?200|Bouncing of contacts of a switch}}] 
- 
-As mentioned in the introductory chapter of switches, there exists an issue of bouncing or flickering when dealing with mechanical switches. Problem arises since contacts are made of metal which has some elasticity, at the moment of connecting or disconnecting the contacts bounce and this results in number of false switching’s. The number and duration of the switching's depends on the switch, but usually fits between just few milliseconds. In case a switch is used to start an electrical device it is not considered as a big issue but if the switch is used for controlling a device, multiple switching’s may turn out to be harmful for the device. 
- 
- 
-[{{  :examples:digi:switch:switch_input_rc_filter.png?200|RC-filter of a switch}}] 
- 
-Main method used for avoiding flickering caused by the bouncing of contacts is filtering. Filtering can be done electrically or by software. To filter electrically the switch must be connected through a low-pass filter – for example a RC filter – witch smoothed changes of voltage and therefore the pin of microcontroller does not have transient values. RC filter is shown on the drawing. Filtering by software is done by assessing the value of the pin where the switch is connected; if the value remains the same at number of times in pre set time limit, it is considered to have a steady position and hence has no flickering problem. However, with each type of filtering a delay factor in defining the status must be taken under consideration.  
- 
-  
-===== Practice ===== 
- 
-Electrical filtering is not used on HomeLab switches, since it would not allow practicing the elimination of miss switching’s with software. The exercise is in two parts. The goal of the first part is to demonstrate the bouncing of Digital i/o module switches. The following program is used for this; each pressing on the button will light the next LED in line. Wrongly pressed button will causes LEDs to light several times and it appears as the LEDs light randomly.   
-<code c> 
-// 
-// The Program for demonstrating switch bounce on Digital i/o module. 
-// 
-#include <homelab/pin.h> 
- 
-// 
-// Determining pins of LEDs and buttons. 
-// 
-pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; 
-pin button  = PIN(C, 0); 
- 
-// 
-// Main program 
-// 
-int main(void) 
-{ 
- unsigned char new_value, old_value = 0; 
- unsigned char index, counter = 0; 
- 
- // Setting LED pins as output. 
- for (index = 0; index < 3; index++) 
- { 
- pin_setup_output(leds[index]);  
- } 
- 
- // Setting button'spins as input. 
- pin_setup_input(button); 
- 
- // Endless loop 
- while (true) 
- { 
- // Reading the state of the button. 
- new_value = pin_get_value(button); 
- 
- // Control, wether the button is pushed down, 
- // that means is the new state 1 and old 0. 
- if ((new_value) && (!old_value)) 
- { 
- // Enlarging the reader and taking module 3 
- counter = (counter + 1) % 3; 
- 
- // Lighting LED witch corresponds to the value of the reader 
- for (index = 0; index < 3; index++) 
- { 
- pin_set_to(leds[index], index != counter); 
- } 
- } 
- 
- // Remember the old state. 
- old_value = new_value; 
- } 
-} 
-</code> 
- 
-Several software solutions are used for filtering, this can be performed either easy or complex way both with its advantages and disadvantages. If the program is set to have only few infrequent pressings of the button, a long pause can be set to follow the pressing, this will rule out reaction to the switching caused by bounce. However, while using this solution must be considered – in case the user holds the button down for a long period of time, the program reacts also on the miss witching caused by the release of the button. A program which controls the state of the switch several times in fixed period of time is more dependable (the longer is the time and the higher is the number of controls performed, the better is the result). Below is a function for reading filtered values of a button for Digital i/o module: 
- 
-<code c> 
-// 
-// Function for reading filtered values of a IO extension module. 
-// 
-unsigned char pin_get_debounced_value(pin button) 
-{ 
- unsigned char buffer = 0xAA;  
- unsigned char timeout = 100; 
- 
- // We wait until the status of the button is celar  
- // or clearing the state expires 
- while (timeout-- > 0) 
- { 
- // Having 8 place (bit) bufffer of state. 
- // All previous states (bits) are shifted to left 
- // and a new state(bit) is added to the right. 
- buffer <<= 1; 
- buffer |= (pin_get_value(button) ? 0x01 : 0x00); 
-  
- // If all 8 bits are high, then the button is pressed down 
- if (buffer == 0xFF) 
- { 
- return 1; 
- } 
- 
- // If all 8 bits are low, then the button is definitely up. 
- if (buffer == 0x00) 
- { 
- return 0; 
- } 
- 
- // 1 ms break. 
- // This function can be found from the library of the HomeLab. 
- sw_delay_ms(1); 
- } 
- 
- // If can't examine the state, then assume that button was not pressed. 
- return 0; 
-} 
-</code> 
- 
-This function generates a delay using a function which is explained in corresponding exercise. At this moment all we need to know about the delay function is that it generates a 1 ms delay at the end of each cycle for reading the state of the button. If the button is in the same position during 8 readings, it returns to the counted position. In case the button is unstable the entire procedure may take up to 100 ms. This function is included in the library of pins, hence there is no need to add it to the program for passing the example. In order to try this first part of the exercise, it has to be altered a little - include library of generating the delay in the program and at the point where the value of the button was read, directly apply the function with filter. The result is as follows: 
- 
-<code c> 
-// 
-// The program for filtering the debounce of buttons of Digital i/o module. 
-// 
-#include <homelab/delay.h> 
-#include <homelab/pin.h> 
- 
-// 
-// Determining pins of LEDs and buttons. 
-// 
-pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; 
-pin button  = PIN(C, 0); 
- 
-// 
-// Main program 
-// 
-int main(void) 
-{ 
- unsigned char new_value, old_value = 0; 
- unsigned char index, counter = 0; 
- 
- // Setting the pins of LEDs as outputs. 
- for (index = 0; index < 3; index++) 
- { 
- pin_setup_output(leds[index]);  
- } 
- 
- // Setting the pins of button as input. 
- pin_setup_input(button); 
- 
- // Endless loop. 
- while (true) 
- { 
- // Reading the state of the button. 
- new_value = pin_get_debounced_value(button);  
- 
- // Control whether the button was pressed down, that means,  
- // is the new state 1 and the old state 0. 
- if ((!new_value) && (old_value)) 
- { 
- // Widening the counter and taking module number 3. 
- counter = (counter + 1) % 3; 
- 
- // Lighting corresponds to the value of the counter. 
- for (index = 0; index < 3; index++) 
- { 
- pin_set_to(leds[index], index != counter); 
- } 
- } 
- 
- // Remember the old state. 
- old_value = new_value; 
- } 
-} 
-</code> 
- 
-If we test this program now, the LEDs are lighting exactly in this sequence as the user is pressing the switch. 
en/examples/digi/switch_debounce.1340970980.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0