This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| de:examples:digi:switch_debounce [2011/10/11 10:18] – angelegt wittkoepper | de:examples:digi:switch_debounce [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 15: | Line 15: | ||
| - | ===== Practice | + | ===== Praktisches Beispiel |
| - | 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. | + | Elektrische Filtrierung wird für die HomeLab Schalter nicht verwendet, da hier die Beseitigung von Fehlschaltungen mit Hilfe von Software geübt werden soll. Die Übung besteht aus zwei Teilen. Das Ziel des ersten Teils ist, das Prellen der Schalter des digitalen Input/ |
| <code c> | <code c> | ||
| // | // | ||
| - | // The Program for demonstrating switch bounce on Digital i/o module. | + | // Das Programm um Kontaktprellung am digitalen Input/Output Modul zu demonstrieren. |
| // | // | ||
| #include < | #include < | ||
| // | // | ||
| - | // Determining pins of LEDs and buttons. | + | // Festlegung der LED-Pins und Schalter. |
| // | // | ||
| pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | ||
| Line 31: | Line 31: | ||
| // | // | ||
| - | // Main program | + | // Hauptprogramm |
| // | // | ||
| int main(void) | int main(void) | ||
| Line 38: | Line 38: | ||
| unsigned char index, counter = 0; | unsigned char index, counter = 0; | ||
| - | // Setting | + | // LED-Pins als Output setzen. |
| for (index = 0; index < 3; index++) | for (index = 0; index < 3; index++) | ||
| { | { | ||
| Line 44: | Line 44: | ||
| } | } | ||
| - | // Setting button' | + | // Schalter-Pins als Input setzen. |
| pin_setup_input(button); | pin_setup_input(button); | ||
| - | // Endless loop | + | // Endlosschleife |
| while (true) | while (true) | ||
| { | { | ||
| - | // Reading the state of the button. | + | // Status des Schalters auslesen. |
| new_value = pin_get_value(button); | new_value = pin_get_value(button); | ||
| - | // Control, wether the button is pushed down, | + | // Kontolle, ob der Schalter heruntergedrückt wurde, |
| - | // that means is the new state 1 and old 0. | + | // was bedeutet, dass der neue Status |
| if ((new_value) && (!old_value)) | if ((new_value) && (!old_value)) | ||
| { | { | ||
| - | // Enlarging the reader and taking module | + | // Erweiterung des Lesegerätes und Nutzungvon Modul 3 |
| counter = (counter + 1) % 3; | counter = (counter + 1) % 3; | ||
| - | // Lighting | + | // Aufleuchten der LED, welche mit dem Wert des Lesegerätes übereinstimmt. |
| for (index = 0; index < 3; index++) | for (index = 0; index < 3; index++) | ||
| { | { | ||
| Line 67: | Line 67: | ||
| } | } | ||
| - | // Remember the old state. | + | // Berücksichtigung des alten Status. |
| old_value = new_value; | old_value = new_value; | ||
| } | } | ||
| Line 73: | Line 73: | ||
| </ | </ | ||
| - | 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 | + | Es gibt diverse Softwarelösungen zur Filtrierung. Sie können einfach oder komplex sein, wobei jede ihre Vor- und Nachteile hat. Sieht das Programm nur wenige seltene Betätigungen des Schalters vor, kann eine lange Pause als Folge der Betätigung eingefügt werden. Auf diese Weise können durch Kontaktprellung verursachte Wirkungen auf die Schaltung vermieden werden. Jedoch muss bei Anwendung dieser Lösung bedacht werden, dass, sofern der Nutzer den Schalter für eine längere Zeit gedrückt hält, das Programm ebenfalls auf die Fehlschaltung, |
| <code c> | <code c> | ||
| // | // | ||
| - | // Function for reading filtered values of a IO extension module. | + | // Funktion zum Auslesen filtrierter Werte aus dem I/O Erweiterungsmodul. |
| // | // | ||
| unsigned char pin_get_debounced_value(pin button) | unsigned char pin_get_debounced_value(pin button) | ||
| Line 84: | Line 84: | ||
| unsigned char timeout = 100; | unsigned char timeout = 100; | ||
| - | // We wait until the status of the button is celar or clearing the state expires | + | // Abwarten, bis der Status des Schalters |
| while (timeout-- > 0) | while (timeout-- > 0) | ||
| { | { | ||
| Line 115: | Line 115: | ||
| </ | </ | ||
| - | This function generates a delay using a function which is explained | + | Diese Funktion generiert eine Verzögerung durch Nutzung einer Funktion, die in der entsprechenden Übung erläutert wurde. Zu dieser Zeit müssen wir nur wissen, dass die Verzögerungsfunktion eine Verzögerung von 1 ms am Ende jedes Zyklus generiert, um den Status des Schalters lesen zu können. Befindet sich der Schalter 8 Lesezyklen lang in der gleichen Position, |
| <code c> | <code c> | ||
| Line 173: | Line 173: | ||
| </ | </ | ||
| - | If we test this program now, the LEDs are lighting exactly | + | Testen wir das Programm nun, leuchten die LEDs in genau der Sequenz auf, in welcher der Nutzer den Schalter betätigt. |