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 [2015/11/05 08:35] heikopikneren:examples:digi:switch [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Switch ====== ====== Switch ======
  
-//Neccesary knowledge: [HW] [[en:hardware:homelab:digi]], [AVR] [[en:avr:registers]], [AVR] [[en:avr:io]], [LIB] [[en:software:homelab:library:pin]], [PRT] [[en:examples:digi:led]]//+//Neccesary knowledge:  
 +[HW] [[en:hardware:homelab:digi]],  
 +[AVR] [[en:avr:io]],\\ 
 +[LIB] [[en:software:homelab:library:pin]],  
 +[PRT] [[en:examples:digi:led]] //
  
 ===== Theory ===== ===== Theory =====
Line 12: Line 16:
  
  
-^ Push button switch ^ Toggle switch ^ Rocker switch ^ Micro switch ^ DIL switch ^ +^ Push button switch ^ Toggle switch ^ Rocker switch ^ 
-|{{:examples:digi:switch:switch_pushbutton.jpg?100|}} | {{:examples:digi:switch:switch_tumbler.jpg?100|}} | {{:examples:digi:switch:switch_rocker.jpg?100|}} |  {{:examples:digi:switch:switch_micro.jpg?100|}} | {{:examples:digi:switch:switch_dil.jpg?100|}} +|{{:examples:digi:switch:switch_pushbutton.jpg?100|}} | {{:examples:digi:switch:switch_tumbler.jpg?100|}} | {{:examples:digi:switch:switch_rocker.jpg?100|}} | 
-| {{:examples:digi:switch:switch_sch_dpst.png?100|}} | {{:examples:digi:switch:switch_sch_spdt.png?100|}} | {{:examples:digi:switch:switch_sch_spdt.png?100|}} | {{:examples:digi:switch:switch_sch_spdt.png?100|}} | {{:examples:digi:switch:switch_sch_2_spst.png?100|}} |+{{:examples:digi:switch:switch_sch_dpst.png?100|}} | {{:examples:digi:switch:switch_sch_spdt.png?100|}} | {{:examples:digi:switch:switch_sch_spdt.png?100|}} 
 + 
 + 
 +^ Micro switch ^ DIL switch ^ 
 +|{{:examples:digi:switch:switch_micro.jpg?100|}} | {{:examples:digi:switch:switch_dil.jpg?100|}} 
 +| {{:examples:digi:switch:switch_sch_spdt.png?100|}} | {{:examples:digi:switch:switch_sch_2_spst.png?100|}} |
  
 In order to use a switch as a sensor connected to a microcontroller, one contact of the switch is connected to the microcontrollers pin and is set as input in the program. If contact is connected to the ground or input potential, the bus’s bit rate of microcontroller’s pin is changed. It would sound logical to use toggle switch, which allows connecting one contact with desired contact (in this case ground or input). For our purpose it is not as simple, since on the moment of shifting of the connections the contacts are not connected. The moment itself is very short (milliseconds), but during this moment microcontrollers input pin is connected to nowhere and therefore has indefinite value. Due to electromagnetic interference (which exists everywhere) input pin that is connected to nowhere can have a value of 0 or 1 at random moments in time. In order to use a switch as a sensor connected to a microcontroller, one contact of the switch is connected to the microcontrollers pin and is set as input in the program. If contact is connected to the ground or input potential, the bus’s bit rate of microcontroller’s pin is changed. It would sound logical to use toggle switch, which allows connecting one contact with desired contact (in this case ground or input). For our purpose it is not as simple, since on the moment of shifting of the connections the contacts are not connected. The moment itself is very short (milliseconds), but during this moment microcontrollers input pin is connected to nowhere and therefore has indefinite value. Due to electromagnetic interference (which exists everywhere) input pin that is connected to nowhere can have a value of 0 or 1 at random moments in time.
Line 74: Line 83:
  
 <code c> <code c>
-// Function for reading filtered values of a IO extension module.+// Function for reading filtered values of a IO extension module
 unsigned char button_read(pin button) unsigned char button_read(pin button)
 { {
Line 84: Line 93:
  while (timeout-- > 0)  while (timeout-- > 0)
  {  {
- // Having 8 place (bit) bufffer of state.+ // Having 8 place (bit) bufffer of state
  // All previous states (bits) are shifted to left  // All previous states (bits) are shifted to left
- // and a new state(bit) is added to the right.+ // and a new state(bit) is added to the right
  buffer <<= 1;  buffer <<= 1;
  buffer |= (pin_get_value(button) ? 0x01 : 0x00);  buffer |= (pin_get_value(button) ? 0x01 : 0x00);
Line 96: Line 105:
  }  }
  
- // If all 8 bits are low, then the button is definitely up.+ // If all 8 bits are low, then the button is definitely up
  if (buffer == 0x00)  if (buffer == 0x00)
  {  {
Line 102: Line 111:
  }  }
  
- // 1 ms break. + // 1 ms break 
- // This function can be found from the library of the HomeLab.+ // This function can be found from the library of the HomeLab
  _delay_ms(1);  _delay_ms(1);
  }  }
- + // If can't examine the state, then assume that button was not pressed
- // If can't examine the state, then assume that button was not pressed.+
  return 0;  return 0;
 } }
 </code> </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.
  
- +The following example illustrates the use of buttons and the elimination of multiple counting using an empty cycle. The button is expected to release during an empty cycle.
-Sample code for using buttons is based on the HomeLab pins library, which was introduced in the example of LED. +
- +
-   +
- +
- +
-===== Filtration of switch bounce ===== +
- +
-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. +
- +
- +
-  +
-===== 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 User interface 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> +
-// +
-// Homelab User interface board switch debounce test program +
-// +
-#include <homelab/pin.h> +
- +
-// +
-// Main program +
-// +
-int main(void) +
-+
- int counter = 0; +
-  +
- // Set LED pins as output and switch pin as input +
- pin_setup_output(led_red); +
- pin_setup_output(led_yellow); +
- pin_setup_output(led_green); +
-  +
- pin_setup_input(S1); +
- +
- // Endless loop +
- while(1) +
-+
- // Check if switch S1 is pressed +
- if(pin_get_value(S1) == 0) +
-+
- // Light the corresponding LED +
- if(counter == 0) led_on(led_green); +
- else led_off(led_green); +
- if(counter == 1) led_on(led_yellow); +
- else led_off(led_yellow); +
- if(counter == 2) led_on(led_red); +
- else led_off(led_red); +
-  +
- // Add counter and take a module +
- counter = (counter + 1) % 3; +
-  +
- // Wait until switch is unpressed +
- while(pin_get_value(S1) == 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 buttonIf 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>
-// +// The program for filtering the debounce of buttons of User interface module
-// The program for filtering the debounce of buttons of User interface module+
-//+
 #include <homelab/pin.h> #include <homelab/pin.h>
  
-// 
 // Main program // Main program
-// 
 int main(void) int main(void)
 { {
Line 215: Line 158:
   
  // Wait until switch is unpressed  // Wait until switch is unpressed
- while(pin_get_value(S1) == 0);+ while(button_read(S1) != 0);
  }  }
  }  }
Line 234: Line 177:
  
 <code c> <code c>
-// Button demonstration example of User interface module.+// Button demonstration example of User interface module
 #include <homelab/pin.h> #include <homelab/pin.h>
  
en/examples/digi/switch.1446712529.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