Exp4 Hardware Interrupts in Microcontroller ATMEGA32
Exp4 Hardware Interrupts in Microcontroller ATMEGA32
(ATMEGA32)
Submitted by
Ronit Dutta, MS in IOT and Signal Processing
Department of Electrical Engineering, IIT Kharagpur
I. Hardware Interrupt:
AVR ATMEGA32 has three external hardware interrupts on pins PD2, PD3, and PB2
which are referred to as INT0, INT1, and INT2 respectively. Upon activation of these
interrupts, the ATMEGA32 controller gets interrupted in whatever task it is doing and
jumps to perform the interrupt service routine.
II. There are at least two interrupts set aside for each of the timers, one for
overflow and another for compare match.
III. Serial communication (USART) has three interrupts, one for receive and two
interrupts for transmit.
IV. The SPI (Serial Peripheral Interface) interrupts.
V. The ADC (Analog to Digital Converter) interrupts.
VI. The TWI (Two-Wire serial Interface) interrupts. It is also called as I2C or IIC
(Inter Integrated Circuit).
VII. EEPROM Ready interrupt.
VIII. Store Program Memory Ready Interrupt.
2
Embedded System on AVR ATMEGA32:Exp4
A single microcontroller can serve several devices. There are two methods by which
devices receive service from the microcontroller: interrupts or polling
3
Embedded System on AVR ATMEGA32:Exp4
4
Embedded System on AVR ATMEGA32:Exp4
the stack by popping the top bytes of the stack into the PC. Then it starts to
execute from that address.
Enabling and Disabling an Interrupt through Status
Register
The Status Register contains information about the result of the most recently
executed arithmetic instruction. This information can be used for altering program
flow in order to perform conditional operations. Note that the Status Register is
updated after all ALU operations, as specified in the Instruction Set Reference. This
will in many cases remove the need for using the dedicated compare instructions,
resulting in faster and more compact code.
The Status Register is not automatically stored when entering an interrupt routine and
restored when returning from an interrupt. This must be handled by software.
In assembly, the CLI and SEI instructions clear and set the I-bit of the SREG register
respectively. In C, the cli() and sei() macros do the same task.
INT0: When this bit is ‘1’ and global interrupt bit in SREG is ‘1’ (i.e. I bit), then
INT0 (External Interrupt 0) is enabled.
5
Embedded System on AVR ATMEGA32:Exp4
INT1: When this bit is ‘1’ and global interrupt bit in SREG is ‘1’ (i.e. I bit), then
INT1 (External Interrupt 1) is enabled.
INT2: When this bit is ‘1’ and global interrupt bit in SREG is ‘1’ (i.e. I bit), then
INT2 (External Interrupt 2) is enabled.
There are two type of activation for the external hardware interrupts.
Level Triggered
Edge Triggered
INT0 and INT1 can be edge or level triggered, but INT2 can be edge
triggered only.
The MCUCR and MCUCSR registers decide the triggering options of the external
hardware interrupts INT0, INT1 and INT2.
This register decides the triggering options of the external hardware interrupts
INT0 and INT1.
ISC01 and ISC00 (Interrupt Sense Control bits):
These bits define the level or edge that triggers the INT0 pin.
6
Embedded System on AVR ATMEGA32:Exp4
This register decides the triggering options of the external hardware interrupt
INT2.
ISC2 (Interrupt Sense Control bit):
ISC2 bit defines the INT2 interrupt edge triggering.
7
Embedded System on AVR ATMEGA32:Exp4
cleared when the interrupt routine is executed. This flag is always cleared
when INT0 is configured as a level interrupt.
Interrupts Priority:
If two interrupts are activated at the same time, the interrupt with the higher priority
is served first. The priority of each interrupt is related to the address of that interrupt
in the interrupt vector.
The interrupt that has a lower address, has a higher priority.
For example, the address of INT0 is 0x0002, the address of INT1 is 0x0004 and the
address of INT2 is 0x0006. Thus, the INT0 has a higher priority, then INT1 and then
INT2. If all of these interrupts are activated at the same time, then INT0 is served first.
Example in C-Language
A Push button is connected at INT0 to sense Edge Trigger for turning On and Off the
LED at PORTC PINC0(That is complement of the previous state for an Edge Trigger)
and continuously one LED at PORTA PINA0 is blinking.
Note: For Simulation on SimulIDE 1.0.0 always use Edge Trigger. The SimulIDE 1.0.0
does not support Level Trigger simulation. The Level Trigger simulation is possible
on SimulIDE 0.4.15.
#include <avr/io.h>
#define F_CPU 1000000L
#include <util/delay.h>
#include <avr/interrupt.h>
int main(void)
{
DDRD=0x00;
PORTD=0x04;
DDRA=DDRA|0x01;
DDRC=DDRC|0x01;
PORTC=0x00;
GICR=0x40;
MCUCR=0x03;
sei();
while (1)
{
8
Embedded System on AVR ATMEGA32:Exp4
PORTA=PORTA|0x01;
_delay_ms(500);
PORTA=PORTA&0xFE;
_delay_ms(500);
}
}
ISR(INT0_vect)
{
PORTC=!PORTC;
_delay_ms(1000);
}
After uploading the HEX file, verify the simulation by TA and Show the MCU Monitor Status
for GICR, MCUCR, GIFR etc.
9
Embedded System on AVR ATMEGA32:Exp4
SBI DDRA,PINA0
SBI DDRC,PINC0
CBI PORTC,PINC0
CBI DDRD,PIND2
SBI PORTD,PIND2
SEI
LDI R16,0x40
OUT GICR,R16
LDI R16,0x03
OUT MCUCR,R16
LDI R16,0x01;
Make the above circuit and then upload the HEX file, verify the simulation by TA and
Show the MCU Monitor Status for GICR, MCUCR, GIFR etc.
10
Embedded System on AVR ATMEGA32:Exp4
11