2. Index (1/2)
Polling Vs. interrupt
What is an Interrupt?
Interrupt Service Routine
Interrupt vector Table
Interrupt types
Steps in executing an interrupt
Interrupt Controller on ATMEGA32
Edge trigger Vs. Level trigger in external
interrupts
Interrupt Controller_LAB1
Interrupt questiones
2
3. Index (2/2)
What is a timer ?
Timers in ATMEGA32
Hand Writing Notes
Timer interrupt
TCC0 Registers
Normal Mode
CTC (Clear Timer on Compare match) mode
Timer Lab 1 (OVF) interrupt
Timer Lab 2 compare match interrupt
Timer2
The difference between Timer0 and Timer2
Timer 1
PWM Mode will be on the next Part
References
3
5. Interrupts v/s Polling
Interrupt Polling
An interrupt is like ashopkeeper. If
one needs a service or product, he
goes to him and apprises him of his
needs. In case of interrupts, when the
flags or signals are received, they
notify the controller that they need to
be serviced.
The polling method is like
asalesperson. The salesman goes
from door to door while requesting to
buy a product or service. Similarly,
the controller keeps monitoring the
flags or signals one by one for all
devices and provides service to
whichever component that needs its
service.
5
6. Polling Vs. Interrupt
Polling
Ties down the CPU
while (true)
{
if(PIND.2 == 0)
//do something;
}
Interrupt
Efficient CPU use
Has priority
Can be masked
main( )
{
Do your common task
}
whenever PIND.2 is 0 then
do something
6
7. What is an Interrupt?
An interrupt is a signal (an "interrupt request") generated by some event external to the CPU , which causes
the CPU to stop what it is doing (stop executing the code it is currently running) and jump to a separate piece of
code designed by the programmer to deal with the event which generated the interrupt request.
This interrupt handling code is often called an ISR (interrupt service routine). When the ISR is finished, it
returns to the code that was running prior to the interrupt
Some Common Interrupt Sources
•Input pin state change
•Timer overflow
•Timer compare/match
•Timer capture
•UART RX char ready
•UART TX ready
•UART TX complete
•SPI transfer
•I2C transfer
•ADC conversion complete
•Watchdog
7
8. Interrupt Service Routine
For every interrupt, there must be an interrupt service routine (ISR), or interrupt
handler.
When an interrupt occurs, the microcontroller runs the interrupt service routine.
For every interrupt, there is a fixed location in memory that holds the address of
its interrupt service routine, ISR.
The table of memory locations set aside to hold the addresses of ISRs is called as
the Interrupt Vector Table.
8
9. Interrupt vector Table
interrupt vector is the memory address of an
interrupt handler.
The interrupt vector for each interrupt
provided by the AVR microcontrollers can be
found in its datasheet.
Please note here that the interrupt vectors
are apart of the microcontroller's program
memory. As such when utilizing interrupts this
section of memory should be reserved to store
pointers to interrupt handlers and not to store
regular programs
9
11. External HW Interrupt sources for
ATMEGA 32
1.The RESET interrupt - Triggered from pin 9.
2.External Interrupt 0 (INT0) - Triggered from pin 16.
3.External Interrupt 1 (INT1) - Triggered from pin 17.
4.External Interrupt 2 (INT2) - Triggered from pin 3.
11
12. Interrupt types
you can distinguish three types of interrupts:
- Software/ Exceptions Interrupts
- Hardware Interrupts
12
13. Software Interrupt
A software interrupt is caused either by an exceptional condition or a special
instruction in the instruction set which causes an interrupt when it is executed by
the processor.
For example, if the processor's arithmetic logic unit runs a command to divide a
number by zero, to cause a divide-by-zero exception, thus causing the computer to
abandon the calculation or display an error message.
Software interrupt instructions work similar to subroutine calls.
13
14. Hardware Interrupt
A hardware interrupt is an electronic alerting signal sent to the processor from
an external device or internal Module, like a disk controller or an external
peripheral. For example, when we press a key on the keyboard or move the mouse,
they trigger hardware interrupts which cause the processor to read the keystroke or
mouse position.
CPU
Interrupt
Controller
Other
Modules
GPIO IO MUX
…..
…...
…..
…...
Eternal signal
can be configured
to be input
(external interrupt)
Internal interrupts from
Other Modules
Mange the interrupt Masking/priority
And send it to the CPU
14
15. Steps taken in servicing an interrupt
1. The microcontroller completes the execution of the current instruction, clears the I
bit and stores the address of the next instruction that should have been executed
(the content of the PC) on the stack.
2. The interrupt vector of the triggered interrupt is then loaded in the PC and the
microcontroller starts execution from that point up until is reaches a RETI instruction.
3. Upon the the execution of the RETI instruction the address that was stored on the
stack in step 1 is reloaded in the PC and the I bit is re-enabled.
4. The microcontroller then start executing instructions from that point. That is the
point that it left off when the interrupt was triggered.
15
49. Interrupt Controller_LAB1
Write C Code using the 3 eternal interrupts
External Interrupt 0 (INT0) - PD2. >> irq occur when “any logical change”
External Interrupt 1 (INT1) - PD3. >> irq occur when “rising edge”
External Interrupt 2 (INT2) - PB2. >> irq occur when “Falling edge”
We have also 3 leds (PD5,6,7) (led0,1,2).
Each interrupt just make the led 0N for 1 sec
The main function is always make all the leds off
49
53. Interrupt Controller_LAB1
For example
Released so IRQ0 happen
Again because
(Falling edge)
And you already configured
IRQ0 to happen if any logical change happen
The led0 is ON
for1 sec
53
57. Questions
What is interrupt?
What is interrupt latency?
How you can optimize it?
What is ISR?
What is return type of ISR?
Can we use any function inside ISR?
57
58. Questions
What is interrupt?
Hardware Software (Exception/Trap) Software (Instruction set)
Interrupt Request(IRQ) sent
from device to processor.
Exception/Trap sent from
processor to processor, caused
by an exceptional condition in
the processor itself.
A special instruction in the
instruction set which causes an
interrupt when it is executed.
In systems programming, an interrupt is a signal to the processor.
It can be emitted either by hardware or software indicating an event that needs
immediate attention.
Interrupts are a commonly used technique in real-time computing and such a
system is said to be interrupt-driven.
58
59. Questions
What is interrupt latency?
Interrupt latency is the time between the generation of interrupt and the time for
interrupt handler to process it.
How you can optimize it?
It is clear that the interrupt latency in a system can be shortened by locking parts of code into the cache. But
this will probably have negative effects on the performance of the rest of the system.
Another way
We may use polling on interrupt flag.
59
60. Questions
What is ISR?
ISR is interrupt service routine which is a short routine used to handle interrupt.
What is return type of ISR?
ISR does not return anything, it does not have parameter.
What is return type of ISR?
If a function could be reentrant, then we could use it.
60
62. What is a timer ?
A timer in simplest term is a register. Timers generally have a resolution of 8 or 16
or 32 Bits.
So a 8 bit timer is 8Bits wide so capable of holding value within 0-255. But this
register has a magical property ! Its value increases/decreases automatically at a
predefined rate (supplied by user).
This is the timer clock. And this operation does not need CPU’s intervention.
62
75. Example 1: write a program that waits 14
machine cycles in Normal mode.
DDRB = 1<<5;
PORTB &= ~(1<<5); //PB5=0
while (1)
{
TCNT0 = 0xF2;
TCCR0 = 0x01;
while((TIFR&(1<<TOV0))==0);
TCCR0 = 0;
PORTB = PORTB^(1<<5);
}
COM01WGM00FOC0 COM00 CS02 CS01 CS00 TCCR0WGM01
$100
-$0E
$F2
76. How to calculate the delay generated
by the timer?
1) Calculate how much a machine clock = 1/f
2) Calculate how many machine clocks it waits.
3) Delay = T * number of machine cycles
77. Example 1:if frequency 10 MHZ
Solution 1 (inaccurate):
1) Calculating T:
T = 1/f = 1/10M = 0.1µs
2) Calculating num of machine cycles:
$100
-$F2
$0E = 14
3) Calculating delay
14 * 0.1µs = 1.4 0µs
82. Example 2: toggle let each 5 µs if you have a
frequency = Because XTAL = 10 by compare match
mode
Because XTAL = 10 MHz, the counter counts up
every 0.1 µs. This means that we need 5 µs / 0.1
µs = 50 clocks. Therefore, we have OCR0= 49.
DDRB |= 1<<3;
PORTB &= ~(1<<3);
while (1)
{
OCR0 = 49;
TCCR0 = 0x09;
while((TIFR&(1<<OCF0))==0);
TCCR0 = 0; //stop timer0
PORTB.3 = ~PORTB.3;
}
COM01WGM00FOC0 COM00 CS02 CS01 CS00 TCCR0WGM01
82
83. Timer Lab 1 (OVF) interrupt
Write a program uses Timer0 to
generate a square wave on pin
PORTB.5, while at the same time
data is being transferred from
PORTC to PORTD. +5
PORTCPORTD
88
PORTB.5
ATmega32
83
85. using Timer0 and CTC mode generate a square wave on pin
PORTB.5, while at the same time data is being transferred from
PORTC to PORTD.
Timer Lab 2 compare match interrupt
Time (µS)
86