Summary of Working with External Interrupts in AVR micro controller
This article introduces microcontroller interrupts, explaining their efficiency over polling and the requirements for execution: enabling interrupt masks, defining subroutines, setting the global enable flag (via `sei()`), and triggering events. It details using AVR Studio's `avr/interrupt.h`, writing ISRs with macros like `ISR(INT0_vect)`, and configuring the MCU Control Register (MCUCR) to define interrupt senses for external pins INT0 and INT1.
Parts used in Interrupt Implementation:
- Digital I/O Ports
- Internal Modules
- External Signal Pins (INT0 and INT1)
- Interrupt Mask/Interrupt Enable Bit
- AVR Status register (SREG)
- sei() Function
- cli() Function
- avr/interrupt.h Header File
- MCU Control Register (MCUCR)
- GICR Register
Introduction to Interrupts
Although micro controllers can accept inputs from Digital I/O Ports,interrupts are preferred for accepting inputs generated by external events. This is mainly because of the less attention required for an interrupt based program. An Interrupt Event directs the flow of program execution to a totally independent piece of code, known as “Interrupt Sub-Routine”. There are many sources of Interrupts that are available for a micro controller. Most of them are generated by the internal modules and are known as internal interrupts. And there are two pins that accept external signals directly and allows external sources to generate interrupts.
Note:- You can read a detailed article about Interrupts in 8051 microcontroller here:-
For an interrupt subroutine to be executed, following requirements are necessary
- The particular interrupt source must be activated by setting the corresponding Interrupt Mask/Interrupt Enable Bit.
- The Interrupt sub routine must exist. If there are no codes to be executed, then an Empty Subroutine must occur at the particular memory space allocated to that Interrupt.
- The Interrupt Enable flag bit in the AVR Status register (SREG) must be set (=1). For this, there is an instruction named ‘sei’ (Set Interrupt Enable). In AVR Studio, there is a function named “sei()”. This function must be called in order to enable the Execution of interrupt sub routine.
- Finally the event must occur, so that the execution of the routine gets triggered.
There is another instruction called “cli” (AVR studio includes a C Function [cli()]) that disables the execution of any subroutine by resetting the Interrupt Enable Flag.
AVR Studio includes “avr/interrupt.h” header file that takes care of the interrupt sub routines and makes it simpler to integrate into the C code. You can Find more about the Header file at its online documentation available at the GNU website.
Writing an Interrupt Sub Routine in AVR Studio
It’s a bit tricky to include an interrupt subroutine into the C code of a micro controller, a bit complex for beginners. So the AVR GCC developers has declared a few symbols to represent interrupts and macros that shortened the code size in many programs. The Symbol lists and macros are listed in the online document. Here we are supposed to write the Interrupt subroutine for External Interrupt 0 and external interrupt 1. The code will look something like this:
// Interrupt sub routine code starts
ISR(INT0_vect)
{
// Code for interrupt 0
}
ISR(INT1_vect)
{
// Code for interrupt 1
}
// Interrupt Subroutine Code Ends
And for the part of the code that would enable interrupts, we would be using “sei()” function.
Registers Associated with External Interrupts
|
MCU Control Register– MCUCR |
||||||||
| Bit |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
| Bit Name | SE | SM2 | SM1 | SM0 | ISC11 | ISC10 | ISC01 | ISC00 |
| Read/Write |
RW |
RW |
RW |
RW |
RW |
RW |
RW |
RW |
| Initial value |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
ISC11, ISC10, ISC01, ISC00: Interrupt Sense Control Bit 1 and Bit 0 for interrupt 1 and interrupt 0
The External Interrupt 1 is activated by the external pin INT1, if the SREG I-bit and the corresponding interrupt mask in the GICR are set. The level and edges on the external INT1 pin that activate the interrupt are defined in table below. The value at the INT1 pin is sampled before detecting edges. If edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. Shorter pulses are not guaranteed to generate an interrupt. If low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt.
Read more: Working with External Interrupts in AVR micro controller
-
Why are interrupts preferred over Digital I/O Ports?
Interrupts are preferred because they require less attention for an interrupt based program compared to polling inputs. -
What instruction enables the execution of an interrupt subroutine?
The sei instruction or sei function must be called to set the Interrupt Enable flag bit in the AVR Status register. -
How do you disable any subroutine execution?
You use the cli instruction or cli function which resets the Interrupt Enable Flag. -
Which header file simplifies integrating interrupt subroutines into C code?
The avr/interrupt.h header file included in AVR Studio takes care of the interrupt sub routines. -
What symbols represent interrupts in AVR GCC for writing subroutines?
Symbols like ISR(INT0_vect) and ISR(INT1_vect) are used to represent and write interrupt subroutines. -
Which register controls the sense bits for external interrupts?
The MCU Control Register (MCUCR) contains the ISC11, ISC10, ISC01, and ISC00 bits that define interrupt senses. -
What condition is required for a low level interrupt to generate an event?
The low level must be held until the completion of the currently executing instruction to generate an interrupt. -
What happens if pulses last longer than one clock period on an edge interrupt?
Pulses that last longer than one clock period will generate an interrupt if edge or toggle interrupt is selected.


