Lab 09
Lab 09
Lab 09
AVR Timer Interrupts
Submitted By:
Asna Maqsood
426990
BESE13 B
1
Embedded System
Lab 09
AVR Timer Interrupts
Submitted By:
Muhammad Ahsan
406267
BESE13 B
2
Embedded System
Lab 09
AVR Timer Interrupts
Submitted By:
Muhammad Owais Khan
404262
BESE13 B
3
Embedded System
Lab 09
AVR Timer Interrupts
Submitted By:
Umar Farooq
406481
BESE13 B
4
Lab 9: AVR Timer Interrupts
EE222: Microprocessor Systems
Contents
1 Acknowledgements .................................................................................................................... 1
2 Administrivia ............................................................................................................................... 1
2.1 Learning Outcomes ........................................................................................................................................... 1
2.2 Deliverables .......................................................................................................................................................... 2
3 Hardware Resources .................................................................................................................. 2
4 Interrupts....................................................................................................................................... 2
4.1 Timer Interrupts ................................................................................................................................................. 2
4.2 Understanding the merits of interrupts .................................................................................................. 3
5 Lab Tasks ...................................................................................................................................... 7
5.1 Task A........................................................................................................................................................................ 7
1 Acknowledgements
This lab exercise is prepared by Mohammad Azfar Tariq and Muhammad Usman under
the supervision of Dr. Rehan Ahmed for the course EE-222 Microprocessor Systems,
focusing on the ATmega16 microcontroller. Later on, the lab was revised for the
ATmega328p Arduino Uno-based microcontroller by Lab Engr. Shaiza. 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:
5
2.2 Deliverables
You are required to submit
3 Hardware Resources
• Arduino Uno board with ATmega328p microcontroller
• Resistances 47Ω
• Switch
• LCD 16 x 2
4 Interrupts
In previous labs we implemented delays by polling the timer overflow flag. Although
accurate, but this still is a process that consumes all the processing time (and energy) just
waiting for a flag. In this lab we are going to use interrupts. Interrupts are implemented
through special block of hardware integrated inside the processing device. Whenever
some specified event occurs, the hardware halts the normal execution of code and move
instruction pointer to a reserved block of code called interrupt service routine and the
process is called interrupting the processor. To understand how this happens and in what
basic forms the technique can be exploited, we will use the timer interrupts in this lab.
6
5. After successful execution pops back the address of instruction and continue
normal execution.
7
#include <avr/io.h>
#include <avr/interrupt.h>
#define LED01 1 // defining led 1 location
#define LED02 2 // defining led 2 location
#define SW 7 // defining switch location int
main( )
{
/*
Setting the pin number specified by macros
LED01 and LED02 of port B as output pins .
8
Value to be loaded in TCNT1=65535-15625=49910
“sei( )” function defined in “avr/interrupt.h” sets the global interrupt enable bit. If this
bit is not set, no interrupt will be responded to.
The TIMSK (Timer Interrupt Mask) registers in the ATmega328P microcontroller contain
bits to enable individual timer interrupts. There are three different TIMSK registers, each
corresponding to a specific timer:
1. TIMSK0: Related to Timer/Counter 0. 2.
TIMSK1: Related to Timer/Counter 1.
3. TIMSK2: Related to Timer/Counter 2.
Each of these registers allows you to enable or disable specific interrupts for their
respective timers.
TIMSK1
9
Interrupt Vector name
Timer2 Compare Match A TIMER2_COMPA_vect
Timer2 Compare Match B TIMER2_COMPB_vect
Timer2 Overflow TIMER2_OVF_vect
Timer1 Capture Event TIMER1_CAPT_vect
Timer1 Compare Match A TIMER1_COMPA_vect
Timer1 Compare Match B TIMER1_COMPB_vect
Timer1 Overflow TIMER1_OVF_vect
Timer0 Compare Match A TIMER0_COMPA_vect
Timer0 Compare Match B TIMER0_COMPB_vect
Timer0 Overflow TIMER0_OVF_vect
1 ISR (.......... )
2 {
3 ...
4 ...
5 }
More than one ISRs can be written in a program but for different interrupts (not for the
same interrupt). When the processor is servicing one interrupt, it ignores other
interrupts.
10
5 Lab Tasks
5.1 Task A
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <LiquidCrystal.h>
void setup() {
lcd.begin(16, 2);
DDRB = 0xFF;
DDRC = 0xFF;
DDRD &= ~(1 << PD2);
DDRB |= 0x01;
enableINT0();
setupTimer1();
}
void loop() {
if (interruptFlag) {
String text = "Ahsan Owais Umar Asna";
int textLength = text.length();
if (timerFlag) {
PORTC = SevenSegment(currentNumber);
bool v = (SevenSegment(currentNumber) >> 6) & 1;
if (v) {
PORTB = 0x01;
} else {
PORTB = 0x00;
}
void enableINT0() {
sei();
EIMSK |= (1 << INT0);
MCUCR &= ~((1 << ISC01) | (1 << ISC00));
}
void setupTimer1() {
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12) | (1 << CS10);
OCR1A = 15624;
TIMSK1 |= (1 << OCIE1A);
sei();
}
ISR(INT0_vect) {
interruptFlag = true;
}
ISR(TIMER1_COMPA_vect) {
timerFlag = true;
}
int main(void) {
setup();
while (1) {
loop();
}
}
Output:
13