Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:interrupts [2023/11/17 14:35] pczekalskien:iot-open:introductiontoembeddedprogramming2:cppfundamentals:interrupts [2023/11/23 10:22] (current) pczekalski
Line 1: Line 1:
 ====== 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.
  
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, 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.
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,OUTPUT);            //Define LED pin 
-  pinMode(13,OUTPUT);        +  pinMode(2,INPUT_PULLUP);       //Define button pin
-  //Define button pin +
-  pinMode(2,INPUT_PULLUP);   +
-  //Attach interrupt to button pin+
   attachInterrupt(digitalPinToInterrupt(2),ButtonIRS,FALLING);    attachInterrupt(digitalPinToInterrupt(2),ButtonIRS,FALLING); 
 +                                 //Attach interrupt to button pin
 } }
  
-void ButtonIRS() {  //IRS function+void ButtonIRS() {               //IRS function
   button_toggle =!button_toggle;   button_toggle =!button_toggle;
 } }
en/iot-open/introductiontoembeddedprogramming2/cppfundamentals/interrupts.1700231743.txt.gz · Last modified: 2023/11/17 14:35 by pczekalski
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0