Thứ Sáu, 14 tháng 12, 2018

How to use External (Hardware) Interrupts of AVR Microcontroller (ATmega16)

Developed By: 

Akshay Daga
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.
 
Configuring External Interrupts of AVR Microcontroller (ATmega16) Prototype


 
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);
Circuit:

Circuit Diagram of How to use External (Hardware) Interrupts of AVR Microcontroller (ATmega16)
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();
}
}
 

Không có nhận xét nào:

Đăng nhận xét

Bài đăng mới nhất

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01 # candientu ,  # fujihatsu ,  # candientufujihatsu  #candientu,  # candientufujhatsu , #fuji...

Bài đăng phổ biến