How does the processor know to get the required information from the external system?
An interrupt is an external or internal event that interrupts the
microcontroller to inform it that a device needs its service.
Interrupts vs. Polling
• A single microcontroller can serve several devices.
That are two ways to do that: interrupts or polling.
• The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler
A) The first method is the simple one - Polling:
In the Polling method, the microcontroller must "access by himself" the device and “ask” for the information it needs for processing. In fact we see that in the Polling method the external devices are not independent systems; they depend on the microcontroller, and only the micro is entitled to obtain access to the information it needs.
The main drawback of this method when writing program is waste of time of microcontroller, which needs to wait and check whether the new information has arrived.
The microcontroller continuously monitors the status of a given device. When the condition is met, it performs the device. After that, it moves on to monitor the next device until everyone is serviced. The microcontroller checks all devices in a round robin fashion.
B) The second method is - Interrupt:
Interrupt is the signal sent to the micro to mark the event that requires immediate attention. Interrupt is “requesting" the processor to stop to perform the current program and to “make time” to execute a special code. Whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal. Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device. The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.
The “request” for the microcontroller to “free itself” to execute the interrupt could come from several sources:
• External hardware devices. Common example is pressing on the key on the keyboard, which causes to the keyboard to send interrupt to the microcontroller to read the information of the pressed key.
• The processor can send interrupts to itself as a result of executing the program, to report an error in the code. For example, division by 0 will causes an interrupt.
• In the multi-processor system, the processors can send to each other interrupts as a way to communicate.
Here are some examples of interrupt sources on one microcontroller
There are many advantages of using Interrupts. The microcontroller can serve many devices. Each device can get service based on the priority assigned to it. The microcontroller can ignore (mask) a device request. The use of microcontroller is more efficient.
Definition of ‘Interrupt’
Event that disrupts the normal execution of a program and causes the execution of special instructions
When interrupt occurs microcontroller saves whatever it is currently doing and executes corresponding ISR code. When this part of code finishes saved data gets restored and programs continues where it was paused by interrupt as ISR didn’t even happened. This is shown on next picture.
Interrupt Service Routine
For every interrupt, there is a fixed location in memory that holds the address of its ISR. The group of memory locations set aside to hold the addresses of ISRs is called the interrupt vector table. You don’t have to know exact locations of these vectors. Compiler does this for you. For examples of how to write ISR for different interrupt sources check out tutorials sections for examples with full source code and detailed explanation.
So basically when timing is important for microcontroller to react or when it should detect signal from outside world that occurs relatively rear but lasts for very short interval than interrupt is better solution.
Let’s take example if it should detect pulse lasting for 1ms and it appears once in 10s at random timing. If we use polling method we would have to check every 500us for example for this pulse so we don’t miss it. But if we use interrupt detection ISR would trigger itself and execute this only once at the moment when pulse occurs. In this case interrupt method is much more efficient.
Polling is good for operations that are not dependent on exact timings. By using this you can create multitasking on microcontroller.
Read more about this at Time sliced multi-tasking system on microcontroller