Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:examples:digi:switch [2015/11/05 09:07] – heikopikner | en:examples:digi:switch [2020/07/20 09:00] (current) – external edit 127.0.0.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 ===== |
| |
| |
^ 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. |
| |
<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) |
{ | { |
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); |
} | } |
| |
// 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) |
{ | { |
} | } |
| |
// 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 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> |
| |
| |
<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> |
| |