Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

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

By Ashutosh Bhatt

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 <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
 
/***** Function To Initialize Ports*****/
void init_ports()
{
DDRA = 0xFF;
PORTA = 0x55;
}
 
/***** Function To Initialize Interrupts*****/
void init_interrupts()
{
cli(); //Disable Global Interrupts
GICR =(1<<INT0); //Set Bit6 of GICR to unmask INT0 interrupt.
MCUCR =(3<<ISC00); //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 Microcontroller
Tagged With: atmega16, avr, interrupt, microcontroller
 

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



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

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!


Featured Tutorials

  • Introduction to Brain Waves & its Types (Part 1/13)
  • Understanding NeuroSky EEG Chip in Detail (Part 2/13)
  • Performing Experiments with Brainwaves (Part 3/13)
  • Amplification of EEG Signal and Interfacing with Arduino (Part 4/13)
  • Controlling Led brightness using Meditation and attention level (Part 5/13)
  • Control Motor’s Speed using Meditation and Attention Level of Brain (Part 6/13)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • What is a loop calibrator? 
  • What are the battery-selection criteria for low-power design?
  • Key factors to optimize power consumption in an embedded device
  • EdgeLock A5000 Secure Authenticator
  • How to interface a DS18B20 temperature sensor with MicroPython’s Onewire driver

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • 7 segment display connections
  • Passive Harmonics Filter
  • file edit
  • RCF Subwoofer Amplifier PIC16F870-I/SP please help me about hex code
  • Active Balun Design

RSS Electro-Tech-Online.com Discussions

  • Control Bare LCD With ATmega328p
  • Need a ducted soldering fan for solder smoke extraction
  • Identify a circuit.
  • Sla ir li ion
  • Question about ultrasonic mist maker
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 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 | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering