This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:programming_fundamentals_rtu:interrupts_and_sub-programs [2018/02/02 14:20] – Agrisnik | en:iot-open:programming_fundamentals_rtu:interrupts_and_sub-programs [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 73: | Line 73: | ||
===== Interrupts ===== | ===== Interrupts ===== | ||
- | // | + | // |
- | IRS should be as short as possible and the return type of it is void. Some of normal Arduino functions | + | IRS should be as short as possible and the return type of it is void. Some of normal Arduino functions |
Interrupts are used to detect important real time events, that occur during the normal code execution of the code, without continuously checking them, like pushing a button. | Interrupts are used to detect important real time events, that occur during the normal code execution of the code, without continuously checking them, like pushing a button. | ||
- | Different Arduino types has different external interrupt pin availability. In most Arduino boards pins 2 and 3 are usable | + | Different Arduino types has different external interrupt pin availability. In most Arduino boards pins number |
+ | |||
+ | To attach interrupt, the function // | ||
+ | - //pin// – the number of a pin number where the interrupt signal generating device will be attached, | ||
+ | - //ISR// – the name of a function of interrupt service routine, | ||
+ | - //mode// - defines when interrupt signal is triggered. There are four basic //mode// values: | ||
+ | * LOW - interrupt is triggered when the pin value is LOW, | ||
+ | * HIGH - interrupt is triggered when the pin value is HIGH, | ||
+ | * CHANGE - interrupt is triggered when the pin value is changed, | ||
+ | * RISING - interrupt is triggered when the pin value is changed from LOW to HIGH. | ||
+ | |||
+ | The example program that uses interrupt: | ||
<code c> | <code c> | ||
+ | volatile bool button =0; //a variable to save button state | ||
+ | |||
void setup() { | void setup() { | ||
pinMode(13, | pinMode(13, | ||
Line 87: | Line 100: | ||
attachInterrupt(digitalPinToInterrupt(2), | attachInterrupt(digitalPinToInterrupt(2), | ||
} | } | ||
- | |||
- | volatile bool button =0; //variable to save button state | ||
void ButtonIRS() { // IRS function | void ButtonIRS() { // IRS function | ||
Line 98: | Line 109: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | **Check yourself** | ||
+ | |||
+ | 1. What are the built-in functions used for? | ||
+ | * To reduce the size of the program | ||
+ | * To delete unnecessary functions | ||
+ | * To simplify the source file | ||
+ | * To increase the speed of the program | ||
+ | |||
+ | 2. Which of the following statements are true? | ||
+ | * built-in functions must return a value. | ||
+ | * built-in functions can not return values. | ||
+ | * The compiler can ignore the declaration of the built-in function. | ||
+ | * built-in functions can not contain more than 10 lines of code. | ||
+ | |||
+ | 3. Is it possible to guarantee that the declared built-in function is really built-in? | ||
+ | * guarantee is not possible, in each individual case it is different | ||
+ | |||
+ | * can be confidently ensured that the function you have declared as built-in is really built-in | ||
+ | |