Introduction To Interrupts
If you have done any of the programming tutorials on this website or any past programming with the Arduino, you have probably made all kinds of interesting projects. What many people do not know, is that one of the greatest benefits of uCs is their ability to use interrupts. An interrupt is just what it sounds like, a specific event or signal "interrupts" the uC from whatever it is doing. This interrupt forces the uC to execute a set of code to address whatever the interrupt was. This set of codes is known as an Interrupt Service Routine (ISR).
​
You may be wondering what are the benefits of interrupts? Why not just program the controller to check for an event periodically (known as polling)? To better understand the difference between polling and interrupt based code, imagine this scenario. You have just ordered a pizza and are waiting at home for it to arrive. You need to be sure that when the pizza does arrive you are at the door so you can pay the delivery man and get the pizza. Now you have two options to ensure that you are ready when the pizza arrives. First, you can periodically get up and check the door for the pizza maybe every one to two minutes. This is like polling, just like the uC you are checking periodically to see if an event has happened. Your second option would be to have a doorbell at your house. This way you will be alerted once the pizza has arrived, and you can go to the door. This is analogous to interrupts, where an event happens which triggers a response from the uC.
​
Interrupts have several benefits, that are widely used throughout industry. The biggest of these benefits is that you can use interrupts to wake the controller up from a "sleeping" state. What I mean by this is that when using interrupts you can program the controller to turn off its oscillator and central processing unit (CPU) while the uC is waiting for an interrupt. This is analogous to you going to sleep while waiting for your pizza. If you were using a polling-based method (checking the door periodically) you would likely miss the pizza if you were asleep. But if you knew the doorbell would wake you up you could sleep while you wait for the pizza man. This is just what interrupts allow uC to do, and it can save huge amounts of energy allowing your uC to operate for longer on battery power.
​
Think of a garage door opener, this device almost certainly has a uC in it. Think of how many times you use your garage opener in a day, probably 2-6 times. That means that for the vast majority of the day, your uC would just be wasting energy if it were to operate using a polling method because the uC would still be using its oscillator and CPU. In contrast, if we use an interrupt based system we can have the uC go into sleep mode while waiting for the interrupt. Once the interrupt is detected the uC can wake up and handle that interrupt before returning to sleep. The amount of power saved by putting a uC into sleep mode varies by the controller but is substantial.
​
What is even cooler about this system is that many uC operate so quickly that even when events happen that are fast to us, the uC can save power by sleeping between events. For example, some uC used in laptop keyboards can go to sleep in between keystrokes. This saves power allowing the computers to last longer than they normally would.
​
To understand how to implement interrupts using the ST uCs see my tutorial.