Académique Documents
Professionnel Documents
Culture Documents
Chapter 10
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Contents
Polling Vs. interrupt
Interrupt unit
Steps in executing an interrupt
Edge trigger Vs. Level trigger in external
interrupts
Timer interrupt
Interrupt priority
Interrupt inside an interrupt
Task switching and resource conflict
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Polling Vs. Interrupt
Polling Interrupt
Ties down the CPU Efficient CPU use
Has priority
Can be masked
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Interrupt unit
SREG
TIMSK
GICR
Other
OSC Interrupt Ports
Peripherals
Unit
I/O
PINS
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Steps in executing an interrupt
Address Code
PB0 1 40 VCC .INCLUDE "M32DEF.INC"
PB1 2 39 PA0 (ADC0) .ORG 0 ;location for reset
0002 0000 JMP MAIN
(INT2) PB2 3 38 PA1 (ADC1)
0002 .ORG 0x02 ;location for external INT0
(OC0/AIN0) PB3 4 37 PA2 (ADC2)
0002 JMP EX0_ISR
(SS) PB4 5 ATmega32 36 PA3 (ADC3) 0004 MAIN: LDI R20,HIGH(RAMEND)
(MOSI) PB5 6 35 PA4 (ADC4) 0005 OUT SPH,R20
(MISO) PB6 7 34 PA5 (ADC5)
0006 LDI R20,LOW(RAMEND)
(SCK) PB7 8 PC: 0016
0015
0014
0013
0012
000D
0009
000A
000B
000C
0006
0008
000F
0004
000E
0007
0005
0000 33 PA6 (ADC6)
0007 OUT SPL,R20
0008 SBI DDRC,3 ;PC.3 = output
RESET 9 32 PA7 (ADC7) 0009 SBI PORTD,2 ;pull-up activated
VCC 10 31 AGND 000A LDI R20,1<<INT0 ;Enable INT0
GND 11 30 AVCC
000B OUT GICR,R20
000C SEI ;Set I (Enable Interrupts)
XTAL2 12 29 PC7 (TOSC2)
000D LDI R30, 3
XTAL1 13 28 PC6 (TOSC1) 000E LDI R31, 4
(RXD) PD0 14 27 PC5 (TDI) 000F ADD R30, R31
(TXD) PD1 15 SP 26 PC4 (TDO)
0010 HERE:JMP HERE
(INT0) PD2 16 25 PC3 (TMS)
0012 EX0_ISR:IN R21,PORTC
(INT1) PD3 17 24 PC2 (TCK) 0013 LDI R22,0x08
0014
(OC1B) PD4 18 Stack 23 PC1 (SDA)
0015
EOR
OUT
R21,R22
PORTC,R21
(OC1A) PD5 19 22 PC0 (SCL)
0016 RETI
(ICP) PD6 20 21 PD7 (OC2)
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Edge trigger Vs. Level trigger in external interrupts
MCUCR SE SM2 SM1 SM0 ISC11 ISC10 ISC01 ISC00
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Edge trigger Vs. Level trigger (Cont.)
MCUCSR JTD ISC2 - JTRF WDRF BORF EXTRF PORF
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Using Timer0 overflow interrupt
ATmega32
PORTC to PORTD.
Lowest
priority
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Interrupt inside an interrupt
The I flag is cleared when the AVR begins
to execute an ISR. So, interrupts are
disabled.
The I flag is set when RETI is executed.
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Task switching and resource conflict
Does the following program work?
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Solution 1: different registers
Use different registers for different tasks.
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Solution 2: Context saving
Save the contents of registers on the stack before
execution of each task, and reload the registers at the
end of the task.
1 .INCLUDE "M32DEF.INC" 18 OUT TIMSK,R20
2 .ORG 0x0 ;location for reset 19 SEI
3 JMP MAIN 20 LDI R20,0xFF
4 .ORG 0x14 ;Timer0 compare match 21 OUT DDRC,R20
5 JMP T0_CM_ISR 22 OUT DDRD,R20
6 ;---------main program------------------------------ 23 LDI R20, 0
7 .ORG 0x100 24 HERE: OUT PORTC,R20
8 MAIN: LDI R20,HIGH(RAMEND) 25 INC R20
9 OUT SPH,R20 26 JMP HERE
10 LDI R20,LOW(RAMEND) 27 ;--------------------------ISR for Timer0
11 OUT SPL,R20 ;set up stack 28 T0_CM_ISR:
12 SBI DDRB,5 ;PB5 = output 29 PUSH R20 ;save R20
13 LDI R20,160 30 IN R20,PIND
14 OUT OCR0,R20 31 INC R20
15 LDI R20,0x09 32 OUT PORTD,R20
16 OUT TCCR0,R20 33 POP R20 ;restore R20
17 LDI R20,(1<<OCIE0) 34 RETI
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.
Saving SREG
We should save SREG, when we change
flags in the ISR.
PUSH R20
IN R20,SREG
PUSH R20
...
POP R20
OUT SREG,R20
POP R20
AVR Microcontroller and Embedded System Using Assembly and C: By: Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education,
Edited by : Dr. Irfan-ud Din , Prof., INHA University in Tashkent. Upper Saddle River, NJ 07458. • All Rights Reserved.