Working with External Interrupts in AVR micro controller

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.

Working with External Interrupts in AVR micro controller

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

Working with External Interrupts in AVR micro controller schematics

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


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top