Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to use External (Hardware) Interrupts of AVR Microcontroller (ATmega16)- (Part 22/46)

By Ashutosh Bhatt July 8, 2010

This article introduces the concept of interrupts and the different types of interrupts in AVR Microcontroller (ATmega16). Interrupt as the name suggests, interrupts the current routine of the microcontroller. Microcontroller executes instructions in a sequence as per the programs. Sometimes there may be a need of handling planned and higher priority events instantaneously that might occur during the normal operations. To handle such kind of events AVR microcontrollers are equipped with Interrupt Systems.
 

When an interrupt occurs, the normal flow of instructions is suspended by the microcontroller and the code corresponding to the interrupt, which has occurred, is executed. Once the code corresponding to the interrupt is executed completely the execution again begins from the same instruction where it was stopped.
Following is what happens when an interrupt occurs:
1.      Microcontroller normally completes the instruction which is being executed.
2.      The program control transfers to Interrupt Service Routine (ISR). Each interrupt have an associated ISR which is a piece of code which tells the microcontroller what to do when an interrupt has occurred.
3.      Execution of ISR is performed by loading the beginning address of the corresponding ISR into program counter.
4.      Execution of ISR continues until the return from the interrupt instruction (RETI) is encountered.
5.      When ISR is complete, the microcontroller resumes processing where it left off before the interrupt occurred, i.e., program control is reverted back to the main program.
 
The whole process can be visualized by the following flow diagram:
Block Diagram of common Interrupt process
Fig. 2: Block Diagram of common Interrupt process
Atmega16 Interrupts 
Number of available interrupts varies with different microcontrollers of AVR family.  Atmega16 in total has twenty one (21) interrupts available. The available interrupts are categorized in two classes:
1.      External Interrupts- Out of the twenty one interrupts available, four interrupts are directly present on controller pins to handle the interrupts generated by external sources, so they are called as external interrupts. The four available interrupts and their respective pins are shown in the figure below in their order of priority:
Pin Configuration of External Interrupts in AVR
Fig. 3: Pin Configuration of External Interrupts in AVR
2.      Internal Interrupts- The remaining seventeen (17) interrupts are available for internal use and support the precise and efficient operation of various peripherals like ADC, Timers, and USARTs etc. The table below describes the available internal  interrupts in the order of their priority:
S. No.
INTERRUPT
DEFINITION
1
TIMER2 COMP
Timer/Counter2 Compare match interrupt
2.
TIMER2 OVF
Timer2 Overflow interrupt
3.
TIMER1 CAPT
Timer/Counter1 Capture Event interrupt
4.
TIMER COMPA
Timer/Counter1 Compare Match A interrupt
5.
TIMER COMPB
Timer/Counter Compare Match B interrupt
6.
TIMER1 OVF
Timer/Counter1 Overflow interrupt
7.
TIMER0 OVF
Timer/Counter0 Overflow interrupt
8.
SPI, STC
Serial Transfer Complete interrupt
9.
USART,RXC
USART Receive Complete interrupt
10.
USART, UDRE
USART Data Register Empty interrupt
11.
USART, TXC
USART Transmit Complete interrupt
12.
ADC
ADC Conversion Complete interrupt
13.
EE_RDY
EEPROM Ready interrupt
14.
ANA_COMP
Analog Comparator interrupt
15.
TWI
Two-wire serial interface interrupt
16.
TIMER0 COMP
Timer/Countrt0 Compare Match interrupt
17.
SPM_RDY
Store Program Memory Read interrupt
Fig. 4: Pin Configuration of Internal Interrupts in AVR
The internal interrupts will be discussed with their respective peripherals. The external interrupts are mainly focused in this article.
External Interrupts Configuration Registers:
To configure an external interrupt INT0, INT1 or INT2,it is required to initialize the respective interrupt by doing appropriate bit settings of following 4 registers. The scope of this document is limited to the explanation of the bits corresponding to interrupts only, the detailed description about other bits of these register can be found in the datasheet of Atmega16.
1.      MCUCR (MCU Control register)
Pin Configuration of Internal Interrupts in AVR
Fig. 5: Bit Value of MCUCR Register to configure External Interrupt of AVR
The Bit0, Bit1, Bit2 and Bit3 of MCUCR register determines the nature of signal at which the interrupt 0 (INT0) and interrupt 1 (INT1) should occur.
2.      MCUCSR (MCU Control and Status Register)
Bit Value of MCUCR Register to configure External Interrupt of AVR
Fig. 6: Bit Value of MCUCSR Register to configure External Interrupt of AVR
The Bit6 of MCUCSR register determines the nature of signal at which the external interrupt 2 (INT2) should occur. INT2 is edge triggered only, it cannot be used for level triggering like INT0 and INT1.
3.      GICR (General Interrupt Control Register)
Bit Value of MCUCSR Register to configure External Interrupt of AVR
Fig. 7: Bit Value of GICR Register to disable/enable the respective interrupt in AVR
The GICR register Bit5, Bit6 and Bit7 called the interrupt masks are used to disable/enable the respective interrupt. Interrupt is disabled when bit value is set to 0 and enabled when bit value is set to 1. By default all the interrupts are disabled.
Above mentioned three registers have to be configured accordingly to initialize a particular  interrupt. Also note that in addition to the above mentioned registers, the I-bit (Bit7, Global Interrupt Enable) of SREG register must also be set to 1. If Global Interrupt enable bit is set to 0, none of the interrupts will work irrespective of the other register settings. The set and clear of I-bit is done by SEI and CLI instructions.
Programming Steps:
For programming an interrupt, the following steps must be followed:
1.      Clear Global Interrupt enable bit in SREG register.
2.      Initialize the interrupt by appropriately configuring the MCUCR, MCUCSR and GICR registers.
3.      Set Global Interrupt Enable bit in SREG register.
4.      Define the appropriate Interrupt service routine (ISR) for the interrupt.
There are two ways of writing ISR, for e.g. ISR for INT0 can be written in following two ways:
A.    ISR (INT0_vect)
B.     SIGNAL (SIG_INTERRUPT0)
Example: Let’s write a simple code to get an interrupt working. Initialize INT0 to generate interrupt at rising edge trigger. The interrupt is generated by using push button which toggle the LEDs status connected to PORTA. The connections of LEDs with controller is shown in circuit diagram.
So for enabling INT0, it is needed to set Bit6 of GICR register, i.e.,
 GICR= (1<<INT0);
For Rising Edge trigger of INT0 the Bit0 and Bit1 status will be-
ISC00 (Bit0) = 1
ISC01 (Bit1) = 1

So,  MCUCR=(3<<ISC00);

Project Source Code

###


// Program to use External (Hardware) Interrupts of AVR Microcontroller (ATmega16)
#include
#include
#include
 
/***** Function To Initialize Ports*****/
void init_ports()
{
DDRA = 0xFF;
PORTA = 0x55;
}
 
/***** Function To Initialize Interrupts*****/
void init_interrupts()
{
cli(); //Disable Global Interrupts
GICR =(1< //Set Bit6 of GICR to unmask INT0 interrupt.
MCUCR =(3< //Configuring MCUCR for Rising Edge interrupt for INT0
sei(); //Enable Global Interrupts
 
}
 
/***** Interrupt Service Routine For INT0*****/
ISR (INT0_vect)
{
PORTA = ~PORTA;
_delay_ms(100);
}
 
 
/***** Main Function *****/
int main(void)
{
init_ports();
while(1)
{
init_interrupts();
}
}
 

###

 


Circuit Diagrams

Circuit-Diagram-of-How-to-use-External-Hardware-Interrupts-of-AVR-Microcontroller-ATmega16

Project Components

  • ATmega16

Project Video


Filed Under: AVR, Electronic Projects
Tagged With: atmega16, avr, interrupt, microcontroller
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Voltage mode pushpull is a nonsense SMPS?
  • Input impedance matching network
  • High Side current sensing
  • The comparison of different Tcl script checkers
  • Reducing "shoot-through" in offline Full Bridge SMPS?

RSS Electro-Tech-Online.com Discussions

  • Is AI making embedded software developers more productive?
  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe