ATmega Chap9 Interrupt
ATmega Chap9 Interrupt
Chapter 9
Interrupts
(Source: http://www.6502.org/tutorials/interrupts.html)
Hardware
Event
When an interrupt occurs, the main program temporarily suspends
execution and branches to the interrupt service routine (ISR),
perform the operation, and terminates with a “return from
interrupt” instruction (RETI).
Nguyễn Trung Hiếu
ISR vs. Subroutine
• Similarity: CPU executes another program and then returns to
the original program.
• Difference: It is NOT known when the main program suspends
execution.
ISR_NAME: SUBROUTINE_NAME:
… …
… …
RETI RET
I T H S V N Z C
1 = Enable
0 = Disable
Code Memory
Nguyễn Trung Hiếu
Interrupt Vectors
• When an interrupt is accepted, the value loaded into PC is
called interrupt vector. It is the address of the start of the
ISR.
No Vector Address Interrupt Source
1 $0000(*) RESET
2 $0002 INT0
3 $0004 INT1
4 $0006 INT2
5 $0008 PCINT0
6 $000A PCINT1
7 $000C PCINT2
8 $000ENguyễn Trung Hiếu PCINT3
Interrupt Vectors
9 $0010 WDT
10 $0012 TIMER2_COMPA
11 $0014 TIMER2_COMPB
12 $0016 TIMER2_OVF
13 $0018 TIMER1_CAPT
14 $001A TIMER1_COMPA
15 $001C TIMER1_COMPB
16 $001E TIMER1_OVF
17 $0020 TIMER0_COMPA
18 $0022 TIMER0_COMPB
19 $0024 TIMER0_OVF
20 $0026 SPI_STC
Nguyễn Trung Hiếu
Interrupt Vectors
21 $0028 USART0_RX
22 $002A USART0_UDRE
23 $002C USART0_TX
24 $002E ANALOG_COMP
25 $0030 ADC
26 $0032 EE_READY
27 $0034 TWI
28 $0036 SPM_READY
29 $0038 USART1_RX
30 $003A USART1_UDRE
31 $003C USART1_TX
Nguyễn Trung Hiếu
Structure Program with ISR
.ORG 0
RJMP MAIN ISR_INT0:
.ORG $02 …
RJMP ISR_INT0 RETI
.ORG $1E
RJMP ISR_TIMER1_OVF
… ISR_TIMER1_OVF:
…
.ORG $40 RETI
MAIN:
.ORG 0 TIMER1_COMPB_ISR:
RJMP MAIN …
.ORG 0X001C RETI
RJMP TIMER1_COMPB_ISR
.ORG 0X0040
MAIN: …
When command (1) is executing,
… OCF1B is set. Describe how the
START: LDI R16,1 program runs from this time?
IN R17,PORTB (1)
EOR R17,R16
OUT PORTB,R17
RJMP START Nguyễn Trung Hiếu
Interrupt Priority
• The lower the address the higher is the priority level.
• RESET has the highest priority, and next is INT0 – the
External Interrupt Request 0, …, USART1_TX has the lowest
priority
• When an interrupt occurs, the Global Interrupt Enable (I-
bit) is cleared and all interrupts are disabled. The I-bit is
automatically set when a Return from Interrupt instruction –
RETI – is executed.
28
Interrupt Priority
29
An Example
Given the following program:
.ORG 0 TIMER1_COMPB_ISR:
RJMP MAIN …
.ORG 0X001C RETI
RJMP TIMER1_COMPB_ISR
.ORG 0X002A USART0_UDRE:
RJMP USART0_UDRE …
RETI
.ORG 0X0040
MAIN: …
When command (1) is executing,
… OCF1B and UDRE is set.
START: LDI R16,1 Describe how the program runs
IN R17,PORTB (1) from this time?
EOR R17,R16
OUT PORTB,R17
RJMP START Nguyễn Trung Hiếu
An Example
Given the following program:
.ORG 0 TIMER1_COMPB_ISR:
RJMP MAIN SEI
.ORG 0X001C …
RJMP TIMER1_COMPB_ISR RETI
.ORG 0X002A USART0_UDRE:
RJMP USART0_UDRE …
RETI
.ORG 0X0040
MAIN: … When command (1) is executing,
… OCF1B is set.
START: LDI R16,1 When TIMER1_COMPB_ISR is
IN R17,PORTB (1) executing, UDRE is set.
Describe how the program runs from
EOR R17,R16 this time?
OUT PORTB,R17
RJMP START Nguyễn Trung Hiếu
Lost Data When Execute ISR
MAIN:
INT1_ISR: Write Read
If R20 is odd, generates
When button push, R20 pulse at PA7.
increase R20
If R20 is even, PA7 = 0
INT1_ISR
Configure I/O
Interrupt
SAVE SREG
R21 R20
DELAY500US
END ISR
PA7 0
LDI R18,$01
LOOP: RJMP LOOP
clk I/O/1 clk I/O/8 clk I/O/32 clk I/O/64 clk I/O/128 clk I/O/256 clk I/O/1024
Timer0 Timer0
don’t haveNguyễn Trung Hiếu
don’t have 53
(Remember) Maximum Delay with 1 loop
Calculate the maximum delay can be created with 1 loop.
Assume that clkI/O = 8MHz. The Timer 1 is used.
16 bit → 65536 count
clk I/O/1 clk I/O/8 clk I/O/64 clk I/O/256 clk I/O/1024
LDI R18,$01
LDI R20,$5
Nguyễn Trung Hiếu
LOOP: RJMP LOOP
Practice Problem 1
Write a program using Timer and interrupt to create a 100 Hz square
wave on PA0. Assume that use Internal RC 8MHz and bit CLKDIV=1.
Use Timer1
f = 100 Hz
Mode CTC
T = 10000 μs
tH tL
tH = tL = 5000 μs
T
.ORG $40
MAIN: SBI DDRA,0 ISR_T1_COMPA:
LDI R16,00 PUSH R17
STS TCNT1H,R16 IN R17,SREG
STS TCNT1L,R16 PUSH R17
LDI R16,HIGH(624) IN R16,PORTA
STS OCR1AH,R16 EOR R16,R18
LDI R16,LOW(624) OUT PORTA,R16
STS OCR1AL,R16
LDI R16,$00 POP R17
STS TCCR1A,R16 OUT SREG,R17
LDI R16,$0B POP R17
STS TCCR1B,R16 RETI
LDI R16,(1<<OCIE1A)
STS TIMSK1,R16
Nguyễn Trung Hiếu
SEI
Practice Problem 2
Write a program using Timer and interrupt to send data from PortA to
PortC every 1 ms. Assume that use Internal RC 8MHz and bit CLKDIV=1.
Use Timer1
Mode CTC
Choose Clock Selector: clkI/O / 8 TCCR1A 0000 0000
TCCR1B 0000 1010
→ CS[2:0] = 010
clkTimer = 1 MHz → 1 count = 1 μs
→ tDelay = 1000 μs = 1000 count
→ Use Timer1 to count from 0 to 999
• Initial count TCNT1H:TCNT1L = 0
OCR1AH:OCR1AL = 999
POP R17
OUT SREG,R17
POP R17
Nguyễn Trung Hiếu RETI
Practice Problem 3
USART_INIT:
LDI R16,(1<<TXEN0) ;Enable Transmitter
STS UCSR0B, R16
LDI R16,(1<<UCSZ01)|(1<<UCSZ00) ;8-bit data
STS UCSR0C, R16 ;no parity, 1 stop bit
LDI R16,0x00
STS UBRR0H,R16
LDI R16,51 ;baud rate = 9600 and Fosc = 8 MHz
STS UBRR0L,R16
RET
USART_TRANS:
LDS R17,UCSR0A
SBRS R17,UDRE0
RJMP UART_TRANS
STS UDR0,R16
RET
Nguyễn Trung Hiếu
Practice Problem 4
Write a program using Timer and interrupt to create the pulse in this
figure to PA0. Assume that use Internal RC 8MHz and bit CLKDIV=1.
PC3
PC4
PC5
Mode Normal
Choose Clock Selector: clkI/O / 8
→ CS[2:0] = 010
clkTimer = 1 MHz → 1 count = 1 μs
PA0
PA1
73
Character Output Using Interrupts
Assume a 10-byte string of 8-bit data is stored in SRAM from the location $100.
Write a program using interrupts to continually transmit this string to the USART0
(19200 baud, 1 stop bit, no parity).
PA0= 1 : Furnace ON
PA0 = 0 : Furnace OFF