Mudanças entre as edições de "ESTE: GPIO and External Interrupts - part 2"
(Criou página com 'This experiment is part of this project. Here we are going to build a experiment with an led and a ...') |
|||
(13 revisões intermediárias pelo mesmo usuário não estão sendo mostradas) | |||
Linha 1: | Linha 1: | ||
This experiment is part of [[Embedded_Systems_Experiments_for_the_Telecommunication_Engineering_Course|this project]]. | This experiment is part of [[Embedded_Systems_Experiments_for_the_Telecommunication_Engineering_Course|this project]]. | ||
− | Here we are going to build | + | Here we are going to build another experiment with an led and a push button, using the GPIO to consolidate concepts of interrupts. The exercise here proposed is to turn on a led always when a push button is pressed and off when the button is no longer pressed. To complete this experiment it is recommended to see at first the [http://wiki.sj.ifsc.edu.br/index.php/ESTE:_General_Purpose_Input_and_Output_%28GPIO%29 GPIO] and [http://wiki.sj.ifsc.edu.br/index.php/ESTE:_GPIO_and_External_Interrupts_-_part_1 External Interrupts - part 1] scripts. |
− | The exercise here proposed is to turn on and off | ||
− | |||
==Pseudo code== | ==Pseudo code== | ||
Linha 10: | Linha 8: | ||
<syntaxhighlight lang=c> | <syntaxhighlight lang=c> | ||
+ | |||
+ | interrupt_source = 0; //you can select two interrupt sources: 0 (pin 2) and 1 (pin 3) | ||
+ | led_state = LOW; //state = 0, led is off; state = 1, led is on | ||
+ | debounce_limit = 200; //limit time in ms between interrupts to debounce the electric signal | ||
+ | sensitivity = HIGH; //select signal event that generates interrupt (SIGNAL_HIGH, SIGNAL_LOW, SIGNAL_CHANGE, RISING_EDGE, FALLING_EDGE) | ||
+ | button_state = LOW; //to check exactly if it is happenig a RISING_EDGE or a FALLING_EDGE | ||
+ | |||
int main(void) { | int main(void) { | ||
− | + | while(1) { | |
− | + | led_value(led_state); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | return 0; | + | return 0; |
} | } | ||
− | void | + | |
− | if (debounce()) | + | void interrupt_handler(){ |
− | // | + | if (debounce()){ |
+ | if (button_state == HIGH) | ||
+ | led_state = HIGH; //if the button is pressed, the led is on | ||
+ | else | ||
+ | led_state = LOW; //if the button isn’t pressed anymore, the led is off | ||
+ | } | ||
} | } | ||
+ | |||
int debounce(){ | int debounce(){ | ||
− | //debounce is the function | + | //debounce is the function responsible for stabilizing the interruption requests |
− | + | ||
− | + | if( (now - time_last_accepted_interrupt) > debounce_limit) { | |
− | + | time_last_accepted_interrup = now; | |
− | + | return 1; | |
+ | } | ||
else | else | ||
− | + | return 0; | |
− | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | This pseudo-code always send a led_state to the led so that it atualizes his brightness acording to the states on and off. When the microcontroller detects a special “moment”, it will call the function specified to a certain type_interrupt (each type of interruption can call only one function or perform one determinated action). To truly perform the led_change() it is necessary to check if the debounce() was done correctly, because the debounce will treat all the calling signals to the interruption - commonly more then thousands calls for each second in a push button - so that they stay regular. After the treatment of the interrupt signals, the | + | This pseudo-code always send a led_state to the led so that it atualizes his brightness acording to the states on and off. When the microcontroller detects a special “moment”, it will call the function specified to a certain type_interrupt (each type of interruption can call only one function or perform one determinated action). To truly perform the led_change() it is necessary to check if the debounce() was done correctly, because the debounce will treat all the calling signals to the interruption - commonly more then thousands calls for each second in a push button - so that they stay regular. After the treatment of the interrupt signals, it will be checked if the button is still pressed or not. If yes we turn the led on, if not the led will be turned off. |
==Schematic== | ==Schematic== | ||
− | [[Arquivo: | + | [[Arquivo:Led_push_button_interrupts_Esquemático_new.png|450px|thumb|center|Schematic of the circuit]] |
==Part List== | ==Part List== | ||
*1 LED | *1 LED | ||
*1 push button | *1 push button | ||
− | * | + | *1 10k ohm resistor to the button |
+ | *1 220 ohm resistor to the led | ||
*1 Protoboard | *1 Protoboard | ||
*5 copper wires (tinned) or jumpers | *5 copper wires (tinned) or jumpers | ||
Linha 55: | Linha 58: | ||
==Assembly== | ==Assembly== | ||
− | [[Arquivo: | + | [[Arquivo:Led_push_button_interrupts_bb_new.png|450px|thumb|center|Assembly of the circuit]] |
==Solutions== | ==Solutions== | ||
− | + | *[https://drive.google.com/open?id=0B_chZ-d1CkpCTU9iOWg2ajViZkU C - AVR] | |
− | *[https://drive.google.com/open?id=0B_chZ- | + | *[https://drive.google.com/open?id=0B_chZ-d1CkpCQW5PblZwNHNtbFk Arduino UNO] |
− | *[https://drive.google.com/open?id=0B_chZ-d1CkpCQW5PblZwNHNtbFk Arduino UNO | ||
==Tools== | ==Tools== | ||
*AVR GCC and tools | *AVR GCC and tools | ||
*Arduino IDE | *Arduino IDE | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Edição atual tal como às 12h17min de 14 de outubro de 2015
This experiment is part of this project.
Here we are going to build another experiment with an led and a push button, using the GPIO to consolidate concepts of interrupts. The exercise here proposed is to turn on a led always when a push button is pressed and off when the button is no longer pressed. To complete this experiment it is recommended to see at first the GPIO and External Interrupts - part 1 scripts.
Pseudo code
Let's begin with a solution. The pseudo code (actual coding is up to you) is below:
interrupt_source = 0; //you can select two interrupt sources: 0 (pin 2) and 1 (pin 3)
led_state = LOW; //state = 0, led is off; state = 1, led is on
debounce_limit = 200; //limit time in ms between interrupts to debounce the electric signal
sensitivity = HIGH; //select signal event that generates interrupt (SIGNAL_HIGH, SIGNAL_LOW, SIGNAL_CHANGE, RISING_EDGE, FALLING_EDGE)
button_state = LOW; //to check exactly if it is happenig a RISING_EDGE or a FALLING_EDGE
int main(void) {
while(1) {
led_value(led_state);
}
return 0;
}
void interrupt_handler(){
if (debounce()){
if (button_state == HIGH)
led_state = HIGH; //if the button is pressed, the led is on
else
led_state = LOW; //if the button isn’t pressed anymore, the led is off
}
}
int debounce(){
//debounce is the function responsible for stabilizing the interruption requests
if( (now - time_last_accepted_interrupt) > debounce_limit) {
time_last_accepted_interrup = now;
return 1;
}
else
return 0;
}
This pseudo-code always send a led_state to the led so that it atualizes his brightness acording to the states on and off. When the microcontroller detects a special “moment”, it will call the function specified to a certain type_interrupt (each type of interruption can call only one function or perform one determinated action). To truly perform the led_change() it is necessary to check if the debounce() was done correctly, because the debounce will treat all the calling signals to the interruption - commonly more then thousands calls for each second in a push button - so that they stay regular. After the treatment of the interrupt signals, it will be checked if the button is still pressed or not. If yes we turn the led on, if not the led will be turned off.
Schematic
Part List
- 1 LED
- 1 push button
- 1 10k ohm resistor to the button
- 1 220 ohm resistor to the led
- 1 Protoboard
- 5 copper wires (tinned) or jumpers
Assembly
Solutions
Tools
- AVR GCC and tools
- Arduino IDE