This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:interrupts [2023/11/13 16:55] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:interrupts [2023/11/23 10:22] (current) – pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ==== Interrupts ==== | + | ====== Interrupts |
| + | {{: | ||
| // | // | ||
| Line 7: | Line 7: | ||
| Interrupts are used to detect critical real-time events which occur during normal code execution of the code. ISR is executed only when there is a need to do it. | Interrupts are used to detect critical real-time events which occur during normal code execution of the code. ISR is executed only when there is a need to do it. | ||
| - | === Polling vs. interrupts === | + | ==== Polling vs. interrupts |
| Interrupts can help in efficient data transmission. Using interrupts and checking if some situation occurred periodically is unnecessary. Such continuous checking is named polling. For example, a serial port interrupt is executed only when new data comes without polling the incoming buffer in a loop. This approach saves the processor time and, in many situations, creates code that is more energy efficient. | Interrupts can help in efficient data transmission. Using interrupts and checking if some situation occurred periodically is unnecessary. Such continuous checking is named polling. For example, a serial port interrupt is executed only when new data comes without polling the incoming buffer in a loop. This approach saves the processor time and, in many situations, creates code that is more energy efficient. | ||
| - | === Interrupt handling example === | + | ==== Interrupt handling example |
| Because interrupts need support from the hardware layer of the microcontroller, | Because interrupts need support from the hardware layer of the microcontroller, | ||
| Line 30: | Line 30: | ||
| <code c> | <code c> | ||
| - | volatile bool button_toggle = 0; //A variable to pass the information from ISR to the main program | + | volatile bool button_toggle = 0; //A variable to pass the information |
| + | //from ISR to the main program | ||
| - | void setup() { | + | void setup() { |
| - | //Define LED pin | + | pinMode(13, |
| - | pinMode(13, | + | pinMode(2, |
| - | | + | |
| - | pinMode(2, | + | |
| - | | + | |
| attachInterrupt(digitalPinToInterrupt(2), | attachInterrupt(digitalPinToInterrupt(2), | ||
| + | // | ||
| } | } | ||
| - | void ButtonIRS() { //IRS function | + | void ButtonIRS() { |
| button_toggle =!button_toggle; | button_toggle =!button_toggle; | ||
| } | } | ||