This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:examples:digi:switch [2015/11/05 08:55] – heikopikner | en: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: | + | //Neccesary knowledge: |
+ | [HW] [[en: | ||
+ | [AVR] [[en: | ||
+ | [LIB] [[en: | ||
+ | [PRT] [[en: | ||
===== Theory ===== | ===== Theory ===== | ||
Line 12: | Line 16: | ||
- | ^ Push button switch ^ Toggle switch ^ Rocker | + | ^ Push button switch ^ Toggle switch ^ Rocker switch ^ |
- | |{{: | + | |{{: |
- | | {{: | + | | {{: |
+ | |||
+ | |||
+ | ^ Micro switch ^ DIL switch ^ | ||
+ | |{{: | ||
+ | | {{: | ||
In order to use a switch as a sensor connected to a microcontroller, | In order to use a switch as a sensor connected to a microcontroller, | ||
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; | ||
} | } | ||
Line 112: | Line 121: | ||
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. | 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. | ||
<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 < | #include < | ||
Line 166: | Line 177: | ||
<code c> | <code c> | ||
- | // Button demonstration example of User interface module. | + | // Button demonstration example of User interface module |
#include < | #include < | ||