**Interrupts: A Key Concept in Operating Systems**
You know how we need to get something done right away so it's always ready when we need it, like getting the latest update on our phone. This is called polling, where we constantly check if an event has occurred, and then wait for the next event. However, this approach can be inefficient, especially when dealing with fast-paced events such as sensor inputs or digital data transmission.
**Polling vs Interrupts**
To improve efficiency, operating systems use a technique called interrupts. An interrupt is a special signal that interrupts the current program execution, allowing it to switch to a different routine. This routine, known as the Interrupt Service Routine (ISR), logs the event and then hands back control to the main program. By doing so, the system can respond promptly to events without constantly checking for updates.
In the context of our Arduino code, we've replaced polling with interrupts. Specifically, we've attached an interrupt to the switch pin, which triggers the ISR when the switch is pressed (not released). This approach provides a more reliable and efficient way to detect switch presses, as it doesn't rely on continuous polling. By using interrupts, we can minimize the impact of delays caused by sensor inputs or digital data transmission.
**How Interrupts Work**
So, how do interrupts work exactly? Imagine you're working at your laptop and suddenly there's a knock at the door that interrupts you. You have to remember where you were with your work and go answer the door. After handling the interruption, you return to your task, restoring all the data you had before. This process is similar to how processors handle interrupts.
When an interrupt occurs, the processor saves its current state (data and registers) and jumps to a specific address in memory that contains the ISR code. The ISR executes the necessary actions for the interrupted event, such as logging or processing sensor inputs. Once the ISR has completed its tasks, it restores the original state of the processor, allowing the main program to continue execution.
**Multiple Cores and Complex Programs**
While interrupts are efficient, they're not suitable for all situations. When dealing with multiple cores or complex programs, interrupts can become less practical. For example, in a laptop, there may be multiple processors handling different tasks simultaneously, such as running the operating system, processing data from sensors, and executing user code.
In these cases, more advanced interrupt management techniques are required to ensure efficient communication between processors and the operating system. However, for simpler programs or single-core systems like Arduino, interrupts remain a reliable and efficient way to respond to events without relying on continuous polling.
**Additional Considerations**
It's worth noting that not all events can be handled using interrupts alone. For example, when dealing with digital data transmission, such as USB connections, the microcontroller may need to continuously process incoming data. In these cases, interrupts are used in conjunction with polling or other techniques to ensure efficient processing.
In conclusion, interrupts are a fundamental concept in operating systems, allowing for efficient and reliable response to events without relying on continuous polling. By understanding how interrupts work and when they're suitable for use, developers can optimize their code for better performance and efficiency.
"WEBVTTKind: captionsLanguage: entoday we're going to talk about uh interrupts it's basically a way that your uh programs can respond to uh unexpected events events that might come in at uh any particular time during a program's execution so someone pressing a key on keyboard uh putting a USB stick in a slot um a mouse click code needs to be able to uh detect these events and respond to them and interrupts are a common way to do that we're going to demonstrate it on an Arduino because that's uh uh a bit simpler than than using an actual PC for it so we've got the uino Uno and just a switch and an LED and we want to be able to toggle the LED so it's on it'll go off and if it's off it'll go on the way you might start writing code for that is you've got your main Arduino Loop if the switch is pressed toggle the LED that's fairly simple but if you've got a bunch of other stuff in your program that say here we've just simulated and said that other stuff takes 500 milliseconds to run so you might be writing a load of data to an SD card you might be doing a bunch of complex maths or something that takes a lot of time and that lot of time means that sometimes we'll miss that button press so I press you relatively slowly it toggles but if I press it fast so it's unreliable it's unreliable yeah it's only going to pick it up if it happens to be executing this if switches press line when someone happens to press the switch is this a bit like one of those revolving doors that's always going round and if you need to get out of the building if you'll make it you got to go out got wait to get it get it just right this is called polling you could possibly go out and poll every single thing in the PC saying has this happened has this happened has this happened uh so instead uh we use interrupts and they're basically special signals that interrupt the uh the program that's running and the program execution jumps to a uh a special routine the interrupt service routine ISR and uh that basically logs that that event has happened uh so that then the operating system can pick up uh the event and and handle it appropriately here's the same code so now we've taken the switch polling out of the loop so there's just our other stuff in there and instead in the setup on this line here we've attached an interrupt to the switch pin so that's going to our switch we want to call this function switch pressed ISR and the falling just means we're going to respond to a particular so only when it's pressed not when it's released so can we run that code now and see how much more reliable it is so loading so now when I press the button it's responding every single time I can do it quite fast so is this always the way you do this kind of thing that any event that can happen at an unexpected time anything's happening really fast for for switches in Mar controllers actually normally we we don't do this because user user press switches are comparatively slow and you and uh you can probably get away with with polling it but for um lots of sensors out there they um any Digital Data that's coming in you simply can't um respond to it fast enough just by asking what's what state it's in for example the USB connection here if data is coming in from your PC it's um ultimately coming into the microcontroller bite by bite so you're getting eight bits in and the hardware buffer that that those eight bits are stored in there's probably going to be another eight bits coming down the line behind it so you've got to get in there and store those in your pro those eight bytes in your program before they get overwritten so every time eight bytes comes in there's an interrupt and alduino does it all in the background but essentially your program your sketch execution is halted it jumps to a uh spe the special Handler that takes those eight bits out and stores them in Ram ready so then this the buffer is clear for the next eight bits to come in and in any moderately complex program anything but the simplest program you simply couldn't do that fast enough just by by pulling the pin it's possible but it takes an enormous amount of program time and it's and it's not extremely reliable so this idea of every time something happens that the program gets interrupted is is that how it works things everything has to stop I know these computers very very fast these days but does everything stop just to sort this out or um certainly on on something that's got a um a single call like the arino there's only one thread of execution yes that's exactly what happens you um things stop you go to the interrupt Handler that runs and then your um your main thread of execution resume resumes so on when you've got multiple core processes going on and you have more than one mic controller doing things so in a laptop there will be many many processors so there will be one in the hard drive and uh one on the USB hub and all sorts of uh different places and they will all have probably their own interrupts and things software going on in there um but at the simplest yes an interrupt does exactly how it interrupts the execution so a um a good metaphor might be you're um working at your laptop and um suddenly there's a knock at the door that interrupts you and you have to remember where you where you are with your work you go and answer the door you handle whatever that is and then you come back and start working again and that's exactly what the processor has to do it gets to a certain point it has to remember all the stuff it was doing beforehand so it saves off various bits of data to various registers so it can know where it has to go back to it runs the interrupt service routine jumps back to exactly the same place in execution restores all those registers to how it was before and then gets on with the job so as far as your program is concerned nothing nothing has happened but somewhere something will have changed so a a new bite will be in a buffer or a a flag will be set somewhere that says hey this thing has happened you need to deal with it and interrupts are meant to be fast and simple and get in and get out quickly because they've interrupted the the main uh thread of execution because you're already in the interrupt so you can't interrupt an interruption but no some processes will will allow for something called re-entrant interrupts where you where you can interrupt and interrupt but generally you don't want to go down that road because it leads totoday we're going to talk about uh interrupts it's basically a way that your uh programs can respond to uh unexpected events events that might come in at uh any particular time during a program's execution so someone pressing a key on keyboard uh putting a USB stick in a slot um a mouse click code needs to be able to uh detect these events and respond to them and interrupts are a common way to do that we're going to demonstrate it on an Arduino because that's uh uh a bit simpler than than using an actual PC for it so we've got the uino Uno and just a switch and an LED and we want to be able to toggle the LED so it's on it'll go off and if it's off it'll go on the way you might start writing code for that is you've got your main Arduino Loop if the switch is pressed toggle the LED that's fairly simple but if you've got a bunch of other stuff in your program that say here we've just simulated and said that other stuff takes 500 milliseconds to run so you might be writing a load of data to an SD card you might be doing a bunch of complex maths or something that takes a lot of time and that lot of time means that sometimes we'll miss that button press so I press you relatively slowly it toggles but if I press it fast so it's unreliable it's unreliable yeah it's only going to pick it up if it happens to be executing this if switches press line when someone happens to press the switch is this a bit like one of those revolving doors that's always going round and if you need to get out of the building if you'll make it you got to go out got wait to get it get it just right this is called polling you could possibly go out and poll every single thing in the PC saying has this happened has this happened has this happened uh so instead uh we use interrupts and they're basically special signals that interrupt the uh the program that's running and the program execution jumps to a uh a special routine the interrupt service routine ISR and uh that basically logs that that event has happened uh so that then the operating system can pick up uh the event and and handle it appropriately here's the same code so now we've taken the switch polling out of the loop so there's just our other stuff in there and instead in the setup on this line here we've attached an interrupt to the switch pin so that's going to our switch we want to call this function switch pressed ISR and the falling just means we're going to respond to a particular so only when it's pressed not when it's released so can we run that code now and see how much more reliable it is so loading so now when I press the button it's responding every single time I can do it quite fast so is this always the way you do this kind of thing that any event that can happen at an unexpected time anything's happening really fast for for switches in Mar controllers actually normally we we don't do this because user user press switches are comparatively slow and you and uh you can probably get away with with polling it but for um lots of sensors out there they um any Digital Data that's coming in you simply can't um respond to it fast enough just by asking what's what state it's in for example the USB connection here if data is coming in from your PC it's um ultimately coming into the microcontroller bite by bite so you're getting eight bits in and the hardware buffer that that those eight bits are stored in there's probably going to be another eight bits coming down the line behind it so you've got to get in there and store those in your pro those eight bytes in your program before they get overwritten so every time eight bytes comes in there's an interrupt and alduino does it all in the background but essentially your program your sketch execution is halted it jumps to a uh spe the special Handler that takes those eight bits out and stores them in Ram ready so then this the buffer is clear for the next eight bits to come in and in any moderately complex program anything but the simplest program you simply couldn't do that fast enough just by by pulling the pin it's possible but it takes an enormous amount of program time and it's and it's not extremely reliable so this idea of every time something happens that the program gets interrupted is is that how it works things everything has to stop I know these computers very very fast these days but does everything stop just to sort this out or um certainly on on something that's got a um a single call like the arino there's only one thread of execution yes that's exactly what happens you um things stop you go to the interrupt Handler that runs and then your um your main thread of execution resume resumes so on when you've got multiple core processes going on and you have more than one mic controller doing things so in a laptop there will be many many processors so there will be one in the hard drive and uh one on the USB hub and all sorts of uh different places and they will all have probably their own interrupts and things software going on in there um but at the simplest yes an interrupt does exactly how it interrupts the execution so a um a good metaphor might be you're um working at your laptop and um suddenly there's a knock at the door that interrupts you and you have to remember where you where you are with your work you go and answer the door you handle whatever that is and then you come back and start working again and that's exactly what the processor has to do it gets to a certain point it has to remember all the stuff it was doing beforehand so it saves off various bits of data to various registers so it can know where it has to go back to it runs the interrupt service routine jumps back to exactly the same place in execution restores all those registers to how it was before and then gets on with the job so as far as your program is concerned nothing nothing has happened but somewhere something will have changed so a a new bite will be in a buffer or a a flag will be set somewhere that says hey this thing has happened you need to deal with it and interrupts are meant to be fast and simple and get in and get out quickly because they've interrupted the the main uh thread of execution because you're already in the interrupt so you can't interrupt an interruption but no some processes will will allow for something called re-entrant interrupts where you where you can interrupt and interrupt but generally you don't want to go down that road because it leads to\n"