You are on page 1of 7

Lab 07- Introduction and implementation

plementation of interrupts

Lab 07
Configuring and executing interrupts on
n ATMEGA16
microcontroller

Prepared by
Abdul Samad

Department of Electrical Engineering CIIT, Islamabad

Objectives:
Introduction to interrupt architecture of ATMEGA16 microcontroller.
Implementation of interrupts on microcontroller.

Tools:
AVR Studio, WinAVR and Proteus ISIS. .

COMSATS Institute of Information Technology, Islamabad

Lab 07- Introduction and implementation of interrupts

1- Introduction to Interrupts
Interrupts are a mechanism of a microcontroller which enables it to
respond to some events at the moment when they occur, regardless
of what microcontroller is doing at the time.
Interrupts VS Polling method
A single microcontroller can serve several devices. There are two
ways to do that; interrupt or polling. In the interrupt method,
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. In polling, the microcontroller continuously monitors the
status of a given device; when the condition is met, it performs the
service. After that it moves on to monitor the status of next device
until everyone is serviced.
Advantages of Interrupt method:
Priority can be assigned.
Controller does not waste time checking if a device needs
service or not.
Steps involved executing an interrupt:
Upon activation of an interrupt the microcontroller goes through the
following steps:
1. It finishes the instruction that it is executing and saves the
address of next instruction on the stack.
2. The program branches to the interrupt vector table at the entry
of generated interrupt source. The code starting here is called
interrupt handler.
COMSATS Institute of Information Technology, Islamabad

Lab 07- Introduction and implementation of interrupts

3. Executes the corresponding interrupt service subroutine.


4. Upon executing the last instruction in ISR the microcontroller
returns to the place where it was interrupted. First it gets the
program counter address by popping it from the stack. Then it
starts to execute from that address.

Available interrupts in ATMEGA16 micro controller:


There are 21 interrupts in ATmega16 with 18 internal and 3 external
interrupts. The external interrupts are RESET, INT0 (pin16) and INT1
(pin17). All 21 interrupts are listed in table below in descending order
of priority.

COMSATS Institute of Information Technology, Islamabad

Lab 07- Introduction and implementation of interrupts

In this lab we will focus on external interrupts i.e. INT0,INT1


and INT2.
External Interrupts:

The External Interrupts are triggered by the INT0, INT1, and


INT2 pins. Observe that, if enabled, the interrupts will trigger
even if the INT0..2 pins are configured as outputs. This feature
provides a way of generating a software interrupt. The external
interrupts can be triggered by a falling or rising edge or a low
level (INT2 is only an edge triggered interrupt).
COMSATS Institute of Information Technology, Islamabad

Lab 07- Introduction and implementation of interrupts

The following registers are involved in configuring the external


interrupts:

In addition to above registers you will need to set GIE(Global


Interrupt Enable) bit. GIE is bit7 of status register(SREG).
For interrupt handling we need to include following header file
into our project:
#include <avr/interrupt.h>

COMSATS Institute of Information Technology, Islamabad

Lab 07- Introduction and implementation of interrupts

The following format is used to declare interrupt service


routine:
ISR(ISR_Vect)
{
//Interrupt handling code goes here..
}
ISR_Vect takes different values depending on the type of
interrupt that needs to be processed.
For example:
For external interrupt 0: ISR_Vect = INT0_vect.
For external interrupt 1: ISR_Vect = INT1_vect.
For external interrupt 2: ISR_Vect = INT2_vect.
For analog to digital converter:ISR_Vect=ADC_Vect.

In lab task:
Interface a push button to external interrupt 0 and an LED to PORTD pin
1. Configure the external interrupt zero for rising edge. When ever there
is an interrupt the LED should blink.

COMSATS Institute of Information Technology, Islamabad

Lab 07- Introduction and implementation of interrupts

Post lab task:


Implement keypad scanning using external interrupts. Microcontroller
should not continuously scan the key pad. The key pad should be
scanned only if an interrupt is generated on key press. The pressed digit
should be displayed on 7-segment.

COMSATS Institute of Information Technology, Islamabad

You might also like