Professional Documents
Culture Documents
Government Engineering College, Gandhinagar
Government Engineering College, Gandhinagar
Experiment No.: 8
Vector
Address Source Interrupt Definition
No.
External Pin, Power-on Reset, Brown-out
1 $000 Reset Reset, Watchdog Reset, and JTAG AVR
Reset
2 $002 INT0 External Interrupt Request 0
3 $004 INT1 External Interrupt Request 1
4 $006 TIMER2 COMP Timer/Counter2 Compare Match
5 $008 TIMER2 OVF Timer/Counter2 Overflow
6 $00A TIMER1 CAPT Timer/Counter1 Capture Event
7 $00C TIMER1 COMPA Timer/Counter1 Compare Match A
8 $00E TIMER1 COMPB Timer/Counter1 Compare Match B
9 $010 TIMER1 OVF TIMER1 OVF Timer/Counter1 Overflow
10 $012 TIMER0 OVF Timer/Counter0 Overflow
11 $014 SPI, STC Serial Transfer Complete
12 $016 USART, RXC Rx Complete
13 $018 USART, UDRE USART Data Register Empty
14 $01A USART, TXC USART, Tx Complete
15 $01C ADC ADC Conversion Complete
16 $01E EE_RDY EEPROM Ready
Above table shows the interrupt sources and their interrupt vectors for AVR ATmega16.
Memory locations from 0002 to 0028 locations are reserve for interrupt vectors. Each
interrupt has 2 words (4 bytes) of memory space for its ISR. For example, 0012 to 0014
memory space is set aside for Timer0 overflow ISR.
Usually ISR cannot fit into 4-bytes memory space. So a JMP instruction is kept at the
vector address from where ISR jumps to another location where rest of the code of ISR
can be written.
At the end of each ISR, RETI (Return from Interrupt) instruction is placed which gives
the control back to the location from where it was interrupted.
Steps to enable an Interrupt:
To enable any interrupt of AVR, we need to take the following steps:
1 Bit D7 (I) of SREG (Status Register) must be set in order to enable the global
interrupt. Without enabling global interrupt, no interrupt can happen. This can
be done by using SEI (assembly instruction) or sei(); (C instruction).
2 After enabling global interrupt, by setting the IE (Interrupt Enable) bit of each
interrupt, that specific interrupt can be enabled. For example, to enable Timer0
overflow interrupt, we need to set TOIE0 (Bit0 of TIMSK Register).
Bit # 7 6 5 4 3 2 1 0
Bit OCIE1
OCIE2 TOIE2 TICIE1 OCIE1A TOIE1 OCIE0 TOIE0
Name B
B
OCIE1
Timer1 output compare A match interrupt enable
A
TICIE1 Timer1 input compare interrupt enable
TOIE2 Timer2 overflow interrupt enable
OCIE2 Timer2 output compare match interrupt enable
These bits, along with D7 of SREG, when set, enable the corresponding interrupt.
Upon execution of interrupt, each flag is cleared by AVR itself and D7 of SREG is
also disabled to avoid further interrupt when ISR of an interrupt is being executed.
Bit # 7 6 5 4 3 2 1 0
Bit
INT1 INT0 INT2 - - - IVSEL IVCE
Name
Bit # 7 6 5 4 3 2 1 0
Bit
SE SM2 SM1 SM0 ISC11 ISC10 ISC01 ISC00
Name
Bit # 7 6 5 4 3 2 1 0
Bit
JTD ISC2 - JTRF WDRF BORF EXTRF PORF
Name
ISC2 Description
0 The falling edge of INT2 generates an interrupt request
1 The rising edge of INT2 generates an interrupt request
Bit # 7 6 5 4 3 2 1 0
Bit
INTF1 INTF0 INTF2 - - - - -
Name
INTF1 When an external interrupt occurs, its corresponding flag bit in GIFR
INTF0 is set. When AVR jumps to its ISR, this flag is cleared by AVR. For level
triggering, pin must be hold for at least 5 instruction cycles to be
INTF2
recognized.
Simulation:
U1
9 22
RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
26
PC4/TDO
40 27
A PA0/ADC0 PC5/TDI
39 28
PA1/ADC1 PC6/TOSC1
38 29
B PA2/ADC2 PC7/TOSC2
37
PA3/ADC3
36 14
C PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
D PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32
Exercise: LOAD ABOVE PROGRAM IN EASY AVR 7 AND VERIFY THE OUTPUT.
HOMEWORK
1. Using Timer0, Write a Program that toggles pin PORTB.5 every 40us ,while at the
same time transferring data from PORTC TO PORTD. Assume XTAL-1 MHz.
Solution :
2. Implement Project in Proteus, Assume that the INT0 pin is connected to a switch that
is normally high. Write a Program that toggles PORTC.3 whenever the INT0 pin goes
low.
Solution :
.INCLUDE "M32DEF.INC"
.ORG 0 ; location for reset
JMP MAIN
.ORG 0x02 ; vector location for external interrupt
JMP EX0_ISR
MAIN: LDI R20,HIGH(RAMEND)
OUT SPH,R20
LDI R20,LOW(RAMEND)
OUT SPL,R20 ; initialize stack
SBI DDRC,3 ; PORTC.3 = output
SBI PORTD,2 ; pull up activated
LDI R20,1<<INT0 ; enable INT0
OUT GICR,R20
SEI ; enable interrupts
HERE: JMP HERE ; stay here forever
EX0_ISR:
IN R21,PINC ; read PINC
LDI R22,0x08 ; 00001000
EOR R21,R22
OUT PORTC,R21
RET
Conclusion : Commonly used DC motors requires 12V supply and 300 mA current ,
moreover interfacing DC motors directly with Microcontrollers may affect the working
of Microcontroller due to the back EMF of the DC motor. Thus it is clear that it ‘s not a
good idea to interface DC motor directly with Microcontrollers.