This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| fr:examples:digi:switch_debounce [2010/02/23 19:07] – créée sdeniaud | fr:examples:digi:switch_debounce [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Élimination du rebond d'un interrupteur | ||
| + | //Necessary knowledge: [HW] [[en: | ||
| + | // | ||
| + | |||
| + | ===== Théorie ===== | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | Comme il a déjà été mentionné dans l' | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | La principale méthode utilisée pour éviter le clignotement est de filtrer le rebond causé par les contacteurs. Le filtrage peut être réalisé électriquement ou par un programme. Pour filtrer électriquement, | ||
| + | |||
| + | ===== Pratique ===== | ||
| + | |||
| + | Le filtrage électrique ne sera pas utilisé dans les interrupteurs du //Home Lab// car il ne permettrait pas d' | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // The Program for demonstrating switch bounce on digital input-output module of home lab. | ||
| + | // | ||
| + | #include < | ||
| + | |||
| + | // | ||
| + | // Determining pins of LED-s and buttons. | ||
| + | // | ||
| + | pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | ||
| + | pin button | ||
| + | |||
| + | // | ||
| + | // 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' | ||
| + | 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], | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Remember the old state. | ||
| + | old_value = new_value; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Plusieurs solutions logiciels sont utiliser pour filtrer, tout cela peut être réalisé à la fois simplement ou de manière compliquée avec ses avantages et inconvénients. Si le programme est configuré pour n' | ||
| + | |||
| + | <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 definitely 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 home lab. | ||
| + | sw_delay_ms(1); | ||
| + | } | ||
| + | |||
| + | // If we can not examine the state, then we assume that the button was not pressed. | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Cette fonction génère un délai en utilisant une fonction qui est expliquée dans l' | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // The program for filtering the debounch of buttons of digital input-output | ||
| + | // module of the home lab. | ||
| + | // | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // | ||
| + | // Determining pins of LED-s and buttons. | ||
| + | // | ||
| + | pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | ||
| + | pin button | ||
| + | |||
| + | // | ||
| + | // Main program | ||
| + | // | ||
| + | int main(void) | ||
| + | { | ||
| + | unsigned char new_value, old_value = 0; | ||
| + | unsigned char index, counter = 0; | ||
| + | |||
| + | // Setting the pins of LED-s 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 the LED witch corresponds to the value of the counter. | ||
| + | for (index = 0; index < 3; index++) | ||
| + | { | ||
| + | pin_set_to(leds[index], | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Remember the old state. | ||
| + | old_value = new_value; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | En testant ce nouveau programme, les LEDs s' | ||