One of the most fundamental and useful principles of modern embedded processors are interrupts. An interrupt is a way for an external (or, sometimes, internal) event to pause the current processor's activity, so that it can complete a brief task before resuming execution where it left off.
Let's say we are at home, writing an excellent tutorial on how a principle of modern embedded processors works. We are very interested in this topic, so we are devoting all our concentration to our keyboard. However, half way though, the phone rings. Despite not being by the phone waiting for a call, we are able to stop what we are doing, take the call, and go back where we left off once we have hung up.
Consider you’re at home watching an excellent movie(HD print). You are very much interested in watching the end of the movie and suddenly the doorbell rings. Now you have to pause your movie, get up from your chair, open the door and then resume the movie form where you left it.
This is how a microprocessor’s interrupt works. We can tell the processor to look for some specific specific external event (like data reception complete or pin changing its state like going from high to low) to become true. While processor checks for these events we can parallely do some other job. When these events occur, we stop the current task, handle the interrupt, and resume back where we left off. This gives us a great deal of flexibility. Rather that constantly checking the FLAG bit via code we can trust interrupts to do the job of checking.
What we are doing is called asynchronous processing - that is, we are processing the interrupt events outside the regular "execution thread" of the main program. The point to be noted here is that we aren’t doing parallel programing (- running two or more codes simultaneously) but we are just pausing the current program and resuming to it after handing the interrupt.
We can link a specific interrupt source to a specific handler routine, called an Interrupt Service Routine, or ISR for short.
There are two main sources of interrupts:
In 8-bit AVRs the software interrupts are not available, which are basically used for generating user defined Exceptions and handling them as and when they occur.
Each microcontroller has a set of interrupt sources available.
Some of the interrupts available in ATmega16 are as follows:
The method of handling interrupts differs in different languages.
For an Interrupt to fire ISR three things must be true
When all three conditions are met, the AVR will fire our ISR each time the interrupt event occurs.
The C code for writing the ISR is as follows
pseudo C code:
// ISR code to execute here
If you simply add an ISR to your existing program, you will find that it appears to do nothing when you try to fire it. This is because while we have defined an ISR, we haven't enabled the interrupt source.
Firstly, we need to set the I bit in the SREG register. This is the Global Interrupt Enable bit, without which the AVR will simply ignore any and all interrupts.
In C, we can use predefined library functions. In the case of AVR-GCC we just use the sei() and cli() macro equivalents defined in <avr/interrupt.h>:
pseudo C code:
sei(); // Enable Global Interrupts
cli(); // Disable Global Interrupts
Now, we need to enable a specific interrupt source, to satisfy the second condition for firing an ISR. The way to do this varies greatly between interrupt sources, but always involves setting a specific flag in one of the peripheral registers. Let's set an interrupt on the USART Receive Complete (USART RX) interrupt. According to the datasheet, we want to set the RXCIE bit in the UCSRB register:
pseudo C code:
UCSRB |= (1 << RXCIE);
After sei(); and setting RXCIE the ISR will code will run when the data receive is complete.
There are a few things to keep in mind when using interrupts in your program:
#include <avr/interrupt.h>int MyValue;
SetupInterrupts(); // Function for initializationwhile (MyValue == 0); // Wait for interrupt
Now we have learnt what are interrupts in general. In the next tutorial we will see how to implement specific interrupts like ADC Conversion Complete Interrupt, External Interrupts, Timer Interrupts etc.