This shows you the differences between two versions of the page.
| en:iot-open.eu:wp5:programming:interrupts [2023/06/25 17:51] – created ktokarz | en:iot-open.eu:wp5:programming:interrupts [2023/06/25 18:21] (current) – removed ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ==== Interrupts ==== | ||
| - | |||
| - | // | ||
| - | |||
| - | ISR should be as short as possible, good practice is to avoid delays and long code sequences in it. If there is a need to trigger execution of a long part of the code with an incoming interrupt signal the good practice is to define the synchronization variable, modify this variable in the ISR with a single instruction, | ||
| - | |||
| - | Interrupts are used to detect important real-time events, which occur during the normal code execution of the code. ISR is executed only when there is a need to do it. | ||
| - | |||
| - | === Polling vs. interrupts === | ||
| - | |||
| - | Interrupts can help in efficient data transmission. Using interrupt there is no need to periodically check if some situation occurred. Such continuous checking is named polling. For example, a serial port interrupt is executed only when new data came, without polling the incoming buffer in a loop. This approach saves the processor time and in many situations preserves energy. | ||
| - | |||
| - | === Interrupt handling example === | ||
| - | |||
| - | Because interrupts need support from the hardware layer of the microcontroller, | ||
| - | |||
| - | Very often interrupts are used together with hardware timers to generate stable frequency signals. It ensures accurate timing independent of the main loop content and delays. Because internal peripherals are very different for different microcontrollers in this chapter the example for external interrupt is shown. | ||
| - | |||
| - | To attach an interrupt to the handler the function // | ||
| - | - //pin// – the pin number where the interrupt signal-generating device will be attached. | ||
| - | - //ISR// – the name of an ISR function. | ||
| - | - //mode// – defines when an interrupt signal is triggered. There are five basic //mode// values: | ||
| - | * //LOW// – interrupt is triggered when the pin value is //LOW//, | ||
| - | * //HIGH// – interrupt is triggered when the pin value is // | ||
| - | * //RISING// – interrupt is triggered when the pin value is changed from //LOW// to //HIGH//, | ||
| - | * //FALLING// – interrupt is triggered when the pin value is changed from //HIGH// to //LOW//, | ||
| - | * //CHANGE// – interrupt is triggered when the pin value is changed in any direction. | ||
| - | |||
| - | The example program that uses external interrupt: | ||
| - | |||
| - | <code c> | ||
| - | volatile bool button_toggle = 0; //A variable to pass the information from ISR to the main program | ||
| - | |||
| - | void setup() { | ||
| - | //Define LED pin | ||
| - | pinMode(13, | ||
| - | //Define button pin | ||
| - | pinMode(2, | ||
| - | //Attach interrupt to button pin | ||
| - | attachInterrupt(digitalPinToInterrupt(2), | ||
| - | } | ||
| - | |||
| - | void ButtonIRS() { //IRS function | ||
| - | button_toggle =!button_toggle; | ||
| - | } | ||
| - | |||
| - | void loop() { | ||
| - | digitalWrite (13, | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | In this example, the code needed to handle the interrupt signal is just one instruction but it shows how to use the synchronization variable to pass information from ISR to the main program keeping the ISR very short. | ||
| - | |||
| - | |||
| - | |||
| - | |||