Differences

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

Link to this comparison view

Next revision
Previous revision
de:examples:digi:switch_debounce [2011/10/11 10:18] – angelegt wittkoepperde: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/Output Moduls zu zeigen. Hierzu wird das folgende Programm genutzt; durch jede Betätigung des Schalters wird die nachfolgende LED aufleuchten. Eine Fehlschaltung lässt die LEDs mehrfach und zufällig aufleuchten. 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> <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 <homelab/pin.h> #include <homelab/pin.h>
  
 // //
-// 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 as output.+ // LED-Pins als Output setzen.
  for (index = 0; index < 3; index++)  for (index = 0; index < 3; index++)
  {  {
Line 44: Line 44:
  }  }
  
- // Setting button'spins as input.+ // 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);
  
- // Controlwether the button is pushed down+ // Kontolleob der Schalter heruntergedrückt wurde
- // that means is the new state and old 0.+ // was bedeutet, dass der neue Status ist, der alte 0.
  if ((new_value) && (!old_value))  if ((new_value) && (!old_value))
  {  {
- // Enlarging the reader and taking module 3+ // Erweiterung des Lesegerätes und Nutzungvon Modul 3
  counter = (counter + 1) % 3;  counter = (counter + 1) % 3;
  
- // Lighting LED witch corresponds to the value of the reader.+ // 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:
 </code> </code>
  
-Several software solutions are used for filteringthis can be performed either easy or complex way both with its advantages and disadvantagesIf the program is set to have only few infrequent pressings of the buttona long pause can be set to follow the pressing, this will rule out reaction to the switching caused by bounceHoweverwhile using this solution must be considered – in case the user holds the button down for a long period of timethe program reacts also on the miss witching caused by the release of the buttonA 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 performedthe better is the result). Below is a function for reading filtered values of a button for Digital i/o module:+Es gibt diverse Softwarelösungen zur Filtrierung. Sie können einfach oder komplex seinwobei jede ihre Vor- und Nachteile hatSieht das Programm nur wenige seltene Betätigungen des Schalters vorkann eine lange Pause als Folge der Betätigung eingefügt werdenAuf diese Weise können durch Kontaktprellung verursachte Wirkungen auf die Schaltung vermieden werden. Jedoch muss bei Anwendung dieser Lösung bedacht werdendasssofern der Nutzer den Schalter für eine längere Zeit gedrückt hält, das Programm ebenfalls auf die Fehlschaltung, welche durch Lösen des Schalters hervorgerufen wird, reagiertEin Programm, welches den Status des Schalters in einer festgeleten Zeitperiode mehrfach überprüft ist zuverlässiger (das Ergebnis ist umso besserje länger die Periode und je größer die Anzahl der Kontrollen ist). Nachfolgend ist eine Funktion abgebildet, um filtrierte Werte eines Schalters für das digitale Input/Output Mpdul auszulesen:
  
 <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 We wait until the status of the button is celar or clearing the state expires
  while (timeout-- > 0)  while (timeout-- > 0)
  {  {
Line 115: Line 115:
 </code> </code>
  
-This function generates a delay using a function which is explained in corresponding exerciseAt 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:+Diese Funktion generiert eine Verzögerung durch Nutzung einer Funktion, die in der entsprechenden Übung erläutert wurdeZu 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önnenBefindet sich der Schalter 8 Lesezyklen lang in der gleichen Position,  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> <code c>
Line 173: Line 173:
 </code> </code>
  
-If we test this program nowthe LEDs are lighting exactly in this sequence as the user is pressing the switch.+Testen wir das Programm nunleuchten die LEDs in genau der Sequenz auf, in welcher der Nutzer den Schalter betätigt.
de/examples/digi/switch_debounce.1318328309.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