Lab 10
Lab 10
Lab 10
AVR Hardware Interrupts
Submitted By:
Asna Maqsood
426990
BESE13 B
Embedded System
Lab 10
AVR Hardware Interrupts
Submitted By:
Muhammad Ahsan
406267
BESE13 B
Embedded System
Lab 10
AVR Hardware Interrupts
Submitted By:
Muhammad Owais Khan
404262
BESE13 B
Embedded System
Lab 10
AVR Hardware Interrupts
Submitted By:
Umar Farooq
406481
BESE13 B
Lab 10: AVR Hardware Interrupts
EE222: Microprocessor Systems
Contents
1. Acknowledgements........................................................................................................................ 1
2. Administrivia................................................................................................................................... 1
2.2. Deliverable........................................................................................................................................................... 2
3. Interrupts........................................................................................................................................... 2
4. Tasks.................................................................................................................................................... 6
1. Acknowledgements
This lab exercise is prepared by Lab Engr. Shaiza for the course EE-222 Microprocessor
Systems. Reporting any errors or discrepancies found in the text is appreciated.
2. Administrivia
2.1. Learning Outcomes
By the end of this lab you will be able to;
2.2. Deliverable
You are required to submit
•Issues in Developing the Solution and your Response in the beginning of next lab
• Universal Programmer
• Resistance 47Ω x2
• Switch
3. Interrupts
In this lab, hardware interrupts are utilized to enable communication between
microcontrollers and external devices. Hardware interrupts allow the microcontroller to
respond immediately to external signals, pausing its current tasks to handle the interrupt
before resuming its normal operations.
To utilize these interrupts, they must be enabled in the External Interrupt Mask Register
(EIMSK). The EIMSK includes bits for enabling each interrupt:
_ _ _ _ _ _ INT1 INT0
In addition to setting the interrupt enable bits, the global interrupt enable (I) bit must also
be set to allow the microcontroller to respond to interrupts. This can be done using the
sei() function from the avr/interrupt.h library.
ISC (Interrupt Sense Control bits) bits define the level or edge on the external pins that
activates the interrupt.
ISC01:ISC00 Description
00 The low level of INT0 generates an
interrupt request.
01 Any logical change on INT0 generates an
interrupt request
10 The falling edge of INT0 generates an
interrupt request.
11 The rising edge of INT0 generates an
interrupt request.
ISC11:ISC10 Description
00 The low level of INT1 generates an
interrupt request.
01 Any logical change on INT1 generates an
interrupt request
10 The falling edge of INT1 generates an
interrupt request.
11 The rising edge of INT1 generates an
interrupt request.
Example Code:
The code demonstrates the use of hardware interrupts to toggle two LEDs independently.
These LEDs are controlled via two external interrupts, INT0 and INT1. The INT0 interrupt
pin is connected to switch 0 (SW0), and the INT1 interrupt pin is connected to switch 1
(SW1). Whenever INT0 is triggered by a rising edge, the program toggles the LED
connected to PORTC.0, and whenever INT1 is triggered by a rising edge, the LED connected
to PORTB.0 toggles.
#include <avr/io.h>
#include <avr/interrupt.h>
int main(void)
{
DDRC=(1<<PORTC0);
DDRB=(1<<PORTB0);
EIMSK=(1<<INT0)|(1<<INT1); // Enable External Interrupt 0 and Interrupt 1
sei();
EICRA=(1<<ISC00)|(1<<ISC01)|(1<<ISC10)|(1<<ISC11);
ISR(INT0_vect)
{
PORTC ^=(1<<PORTC0);
}
ISR(INT1_vect)
{
PORTB^=(1<<PORTB0);
}
When INT0 or INT1 detect a low-to-high transition, the corresponding flag in the External
Interrupt Flag Register (EIFR) is set. This triggers an interrupt, and the AVR
microcontroller jumps to the appropriate interrupt service routine: ISR(INT0_vect) or
ISR(INT1_vect). These routines toggle the state of the LEDs connected to PORTC.0 and
PORTB.0, respectively.
While the processor is executing an interrupt service routine (ISR), the flag that caused
the interrupt (such as INTF0 and INTF1) is automatically cleared by the processor.
The GIFR register layout includes flags for each interrupt:
- - - - - - - INTF1 INTF0
4. Lab tasks:
4.1. Task1:
Code:
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <LiquidCrystal.h> // Includes the LiquidCrystal Library
// Initialize the LCD. Parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
// Global flag to indicate interrupt
volatile bool interruptFlag = false;
void setup() {
lcd.begin(16, 2); // Initialize the LCD screen with dimensions 16x2
DDRB = 0xFF; // Set PORTB as output for the seven-segment display
enableINT0(); // Enable INT0 interrupt
}
void loop() {
// Only update LCD when interrupt has been triggered
if (interruptFlag) {
String text = "Ahsan Owais Umar Asna"; // Text to scroll
int textLength = text.length(); // Length of the text
// Scroll the text horizontally
for (int position = 0; position < textLength + 16; position++) {
lcd.clear(); // Clear the display
lcd.setCursor(16 - position, 0); // Set the cursor position for scrolling
lcd.print(text); // Display the text
delay(200); // Adjust delay for scrolling speed
}
void enableINT0() {
sei(); // Enable global interrupts
EIMSK |= (1 << INT0); // Enable INT0 external interrupt
MCUCR &= ~((1 << ISC01) | (1 << ISC00)); // Clear both ISC01 and ISC00 for low-level
trigger
}
int main(void) {
// This function does nothing in this case because the main loop
// is driven by interrupts and flags
DDRC = 0xFF;
DDRB = 0x01;
// enableINT0();
while (1) {
// The `loop()` function will handle LCD display updates when needed.
for (int i=0;i<10;i++)
{
PORTC=SevenSegment(i);
bool v = (SevenSegment(i)>>6)<<7;
}
}
}
Output: