MICROPROCESSOR AND INTERFACING LAB
Lab Experiment
Title
Implementation of External Interrupt (INT0) on ATmega328P Using AVR
Assembly Language
1. Objective
The objectives of this experiment are:
To understand the concept of interrupts in AVR microcontrollers.
To study the operation of External Interrupt INT0.
To configure interrupt-related registers of the ATmega328P.
To interface a push button with INT0 (PD2).
To control an LED using an Interrupt Service Routine (ISR).
To understand the difference between polling and interrupt-driven
systems.
2. Apparatus Required
ATmega328P Development Board / Arduino Uno
Push Button Switch
LED
220 Ω Resistor
Breadboard
Jumper Wires
AVR Studio / Microchip Studio
Programmer (if required)
3. Introduction
Microcontrollers often interact with external devices such as sensors,
switches, communication modules, and actuators. Monitoring these devices
continuously through polling wastes CPU resources. Interrupts provide an
efficient mechanism whereby the processor responds immediately whenever
an important event occurs.
An interrupt temporarily suspends the normal execution of a program and
transfers control to a special routine known as an Interrupt Service Routine
(ISR). Once the ISR completes its task, the processor resumes execution of
the main program.
The ATmega328P provides two dedicated external interrupt pins:
INT0 (PD2)
INT1 (PD3)
In this experiment, INT0 is used to detect a push-button press and illuminate
an LED.
4. Theory of Interrupts
4.1 What is an Interrupt?
An interrupt is a hardware or software signal that requests immediate
attention from the CPU.
Instead of continuously checking device status, the CPU can execute its
normal program and respond only when an interrupt occurs.
Polling Approach
The processor repeatedly checks the status of the input device.
Advantages:
Easy to implement
Disadvantages:
Wastes processor time
Slow response to events
Interrupt Approach
The processor executes the main program normally and responds only when
an interrupt occurs.
Advantages:
Efficient CPU utilization
Faster response
Suitable for real-time systems
5. Interrupt Execution Sequence
When an interrupt occurs:
1. Current instruction completes.
2. Program Counter (PC) is automatically saved on the stack.
3. Interrupt Flag is recognized.
4. CPU jumps to the Interrupt Vector Address.
5. Interrupt Service Routine (ISR) executes.
6. RETI instruction restores normal program execution.
7. Main program resumes from where it was interrupted.
6. External Interrupts in ATmega328P
Interru
Pin Description
pt
PD External
INT0
2 Interrupt 0
PD External
INT1
3 Interrupt 1
This experiment uses INT0.
7. Interrupt Related Registers
7.1 EICRA (External Interrupt Control Register A)
Purpose:
Defines the triggering condition for external interrupts.
Relevant Bits:
ISC01 and ISC00 control INT0 triggering.
ISC0 ISC0 Trigger
1 0 Condition
0 0 Low Level
ISC0 ISC0 Trigger
1 0 Condition
Any Logical
0 1
Change
1 0 Falling Edge
1 1 Rising Edge
For push-button applications using pull-up resistors, Falling Edge detection is
commonly used.
Example:
; Falling Edge Trigger
ldi r16, 0x02
sts EICRA, r16
This sets ISC01 = 1 and ISC00 = 0.
7.2 EIMSK (External Interrupt Mask Register)
Purpose:
Enables or disables external interrupts.
Relevant Bits:
Bit Function
INT Enable External
0 Interrupt 0
INT Enable External
1 Interrupt 1
Example:
ldi r16, 0x01
out EIMSK, r16
Enables INT0.
7.3 EIFR (External Interrupt Flag Register)
Purpose:
Stores interrupt occurrence flags.
Functio
Bit
n
INTF INT0
0 Flag
INTF INT1
1 Flag
The hardware automatically sets these flags when interrupts occur.
7.4 SREG (Status Register)
Purpose:
Contains processor status bits.
Important Bit:
I = Global Interrupt Enable Bit
Interrupts are enabled using:
SEI
Interrupts are disabled using:
CLI
8. Hardware Circuit
LED Connection
PB0 ---- 220Ω ---- LED ---- GND
Push Button Connection
PD2(INT0) ---- Push Button ---- GND
Internal Pull-Up Enabled in Software
Logic Levels:
Button Released = Logic 1
Button Pressed = Logic 0
Therefore Falling Edge Interrupt Detection is used.
9. Program Flowchart
START
Configure PB0 as Output
Configure PD2 as Input
Enable Internal Pull-Up
Configure INT0 for Falling Edge
Enable INT0
Enable Global Interrupts
Infinite Loop
Button Pressed?
Interrupt Generated
ISR Executes
↓
LED ON
Return to Main Program
10. AVR Assembly Program
.include "[Link]"
.org 0x0000
rjmp MAIN
.org INT0addr
rjmp INT0_ISR
MAIN:
; PB0 Output
ldi r16, 0x01
out DDRB, r16
; LED OFF
ldi r16, 0x00
out PORTB, r16
; PD2 Input
ldi r16, 0x00
out DDRD, r16
; Enable Pull-Up
ldi r16, 0x04
out PORTD, r16
; Falling Edge Trigger
ldi r16, 0x02
sts EICRA, r16
; Enable INT0
ldi r16, 0x01
out EIMSK, r16
; Enable Global Interrupts
sei
LOOP:
rjmp LOOP
INT0_ISR:
; Turn LED ON
ldi r16, 0x01
out PORTB, r16
reti
11. Program Explanation
Step 1:
Configure PB0 as Output.
Step 2:
Turn LED OFF initially.
Step 3:
Configure PD2 as Input.
Step 4:
Enable Internal Pull-Up Resistor.
Step 5:
Configure Falling Edge Detection through EICRA.
Step 6:
Enable INT0 through EIMSK.
Step 7:
Enable Global Interrupts using SEI.
Step 8:
Execute Infinite Loop.
Step 9:
When the button is pressed, INT0 generates an interrupt.
Step 10:
ISR executes and turns ON the LED.
Step 11:
RETI returns execution to the main program.
Implementation of External Interrupt INT0 on ATmega328P Using
AVR (C Language) for LED Control
CODE:
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(INT0_vect)
// External LED on PB0 (D8)
PORTB |= (1 << PB0);
// Built-in LED on PB5 (D13)
PORTB |= (1 << PB5);
int main(void)
// PB0 and PB5 as outputs
DDRB |= (1 << PB0) | (1 << PB5);
// LEDs OFF initially
PORTB &= ~((1 << PB0) | (1 << PB5));
// PD2 (INT0) as input
DDRD &= ~(1 << PD2);
// Enable internal pull-up on PD2
PORTD |= (1 << PD2);
// INT0 on falling edge
EICRA |= (1 << ISC01);
EICRA &= ~(1 << ISC00);
// Enable INT0
EIMSK |= (1 << INT0);
// Enable global interrupts
sei();
while (1)
1. Power ON the system
o ATmega328P starts execution.
o PB0 and PB5 are configured as output pins.
o PD2 (INT0) is configured as input.
o Internal pull-up on PD2 makes it HIGH initially.
o LEDs are OFF.
2. Normal State (Idle)
o No button press.
o PD2 = HIGH (logic 1).
o CPU stays in infinite loop (while(1)).
o No interrupt occurs.
3. Button Press / External Event
o Push button connects PD2 to GND.
o PD2 changes:
HIGH → LOW
o This creates a falling edge signal.
4. Interrupt Detection
o ATmega328P detects falling edge on INT0.
o Interrupt flag is set.
o CPU temporarily stops main program execution.
5. ISR Execution (Interrupt Service Routine)
o CPU jumps to INT0_vect.
o Inside ISR:
PB0 = 1 → External LED ON (D8)
PB5 = 1 → Built-in LED ON (D13)
6. Return to Main Program
o RETI executes automatically.
o CPU returns to while(1) loop.
7. After Effect
o LEDs remain ON because:
No code resets PB0 or PB5 to 0.
o Even if button is released or wire is removed:
State is stored in PORTB register.
12. Result
The external interrupt INT0 was successfully configured on the ATmega328P.
Upon pressing the push button connected to PD2, an interrupt was
generated and the Interrupt Service Routine executed successfully, causing
the LED connected to PB0 to turn ON.
13. Precautions
1. Ensure correct polarity of the LED.
2. Verify proper grounding connections.
3. Use appropriate resistor values.
4. Enable global interrupts before testing.
5. Confirm correct interrupt vector address.
6. Avoid floating inputs by using pull-up resistors.
14. Applications
Emergency Stop Systems
Industrial Automation
Security Systems
Home Automation
Smart Sensors
Alarm Systems
Real-Time Monitoring Systems
Medical Monitoring Equipment
15. Conclusion
This experiment demonstrated the implementation of external interrupts on
the ATmega328P microcontroller. The experiment provided practical
understanding of interrupt handling, interrupt configuration registers,
Interrupt Service Routines, and event-driven programming techniques used
in embedded systems.