Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:interrupts [2023/11/21 19:48] – ktokarz | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:interrupts [2023/11/23 10:22] (current) – pczekalski |
---|
====== Interrupts ====== | ====== Interrupts ====== |
| {{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_m.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\ |
//Interrupt// is a signal that stops the normal execution of a program in the processor and starts the function assigned to a specific source. This function is called //Interrupt Service Routine// (//ISR//) or interrupt handler. The ISR can be recognized as a task with higher priority than the main program. Interrupt signals can be generated by the external source, like a change of value on the pin, and by the internal source, like a timer or any other peripheral device. When the interrupt signal is received, the processor stops executing the code and starts the ISR. After completing the interrupt handler, the processor returns to the normal program execution state. | //Interrupt// is a signal that stops the normal execution of a program in the processor and starts the function assigned to a specific source. This function is called //Interrupt Service Routine// (//ISR//) or interrupt handler. The ISR can be recognized as a task with higher priority than the main program. Interrupt signals can be generated by the external source, like a change of value on the pin, and by the internal source, like a timer or any other peripheral device. When the interrupt signal is received, the processor stops executing the code and starts the ISR. After completing the interrupt handler, the processor returns to the normal program execution state. |
| |
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, the availability of specific interrupt sources depends heavily on the microcontroller model. For example, different Arduino models have different external interrupt pin availability. In most Arduino boards, pins numbered 2 and 3 can be used for interrupts; in Arduino Uno, only these two, while in ESP32 and STM32, almost any digital pin is valid. | Because interrupts need support from the hardware layer of the microcontroller, the availability of specific interrupt sources depends heavily on the microcontroller model. For example, different Arduino models have different external interrupt pin availability. In most Arduino boards, pins numbered 2 and 3 can be used for interrupts; in Arduino Uno, only these two, while in ESP32 and STM32, almost any digital pin is valid. |