Como usar interrupções externas (hardware) do microcontrolador AVR (ATmega16) – (Parte 22/46)

How to use external interrupts (hardware) of the AVR microcontroller (ATmega16) – (Part 22/46)

This article presents the concept of interrupts and the different types of interrupts in the AVR (ATmega16) microcontroller. Interrupt as the name suggests, interrupts the current routine of the microcontroller. The microcontroller executes instructions in a sequence according to the programs. Sometimes there may be a need to instantly deal with higher priority, planned events that may occur during normal operations. To deal with this type of event, 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 that occurred is executed. Once the code corresponding to the interrupt has been completely executed, execution resumes from the same instruction where it was interrupted.
Following is what happens when an outage occurs:
1. The microcontroller normally completes the instruction being executed.
two. Program control is transferred to the Interrupt Service Routine (ISR). Each interrupt has an associated ISR, which is a piece of code that tells the microcontroller what to do when an interrupt occurs.
3. ISR execution is performed by loading the start address of the corresponding ISR into the program counter.
4. ISR execution continues until the return interrupt instruction (RETI) is encountered.
5. When the ISR is complete, the microcontroller resumes processing where it left off before the interrupt occurred, i.e., program control reverts to the main program.
The entire process can be visualized in the following flowchart:
Diagrama de blocos do processo de interrupção comum
Fig. 2: Block diagram of the common interrupt process
Atmega16 outages
The number of interrupts available varies depending on the different microcontrollers in the AVR family. The Atmega16 has a total of twenty-one (21) interrupts available. Available interrupts are categorized into two classes:
1. External Interrupts- Out of the twenty-one available interrupts, four interrupts are present directly on the controller pins to deal with the interrupts generated by external sources, hence they are called external interrupts. The four available interrupts and their respective pins are shown in the figure below in order of priority:
Configuração de pinos de interrupções externas no AVR
Fig. 3: External interrupt pin configuration on the AVR
two. Internal Interrupts- The remaining seventeen (17) interrupts are available for internal use and support accurate and efficient operation of various peripherals like ADC, timers and USARTs, etc. The table below describes the available internal interrupts in order of priority:
S. No.
INTERRUPT
DEFINITION
1
COMP. TIMER2
Timer/Counter2 Compare start interrupt
two.
TIMER2 OVF
Timer2 overflow interrupt
3.
TIMER1 CAPT.
Timer/counter1 capture event interrupt
4.
COMPA. TIMER
Timer/Counter1 Compare Match An interrupt
5.
COMPB TIMER.
Interruption of timer/counter comparison correspondence B
6.
TIMER1 OVF
Timer/counter1 overflow interrupt
7.
TIMER0 OVF
Timer/counter overflow interrupt0
8.
SPI, STC
Complete interruption of serial transfer
9.
USART,RXC
USART receives complete outage
10.
USART, UDRE
USART data logging empty interrupt
11.
USART, TXC
Complete interruption of USART transmission
12.
ADC
Complete ADC conversion stop
13.
EE_RDY
EEPROM Ready Interrupt
14.
ANA_COMP
Analog Comparator Interruption
15.
TWI
Two-wire serial interface interrupt
16.
TIMER0 COMP.
Timer/countrt0 comparison start interrupt
17.
SPM_RDY
Store read interrupt in program memory
Fig. 4: Internal interrupt pin configuration on the AVR
Internal interrupts will be discussed with their respective peripherals. External interrupts are primarily focused on in this article.
External interrupt configuration registers:
To configure an external interrupt INT0, INT1 or INT2, it is necessary to initialize the respective interrupt by making appropriate bit settings of the following 4 registers. The scope of this document is limited to the explanation of the bits corresponding only to interrupts, the detailed description of the other bits of these registers can be found in the Atmega16 datasheet.
1. MCUCR (MCU Control Register)
Configuração de pinos de interrupções internas no AVR
Fig. 5: MCUCR register bit value to configure the AVR external interrupt
Bit0, Bit1, Bit2 and Bit3 of the MCUCR register determine the nature of the signal on which interrupt 0 (INT0) and interrupt 1 (INT1) are to occur.
two. MCUCSR (MCU Control and Status Register)
Valor de bit do registro MCUCR para configurar a interrupção externa do AVR
Fig. 6: MCUCSR register bit value to configure the AVR external interrupt
Bit6 of the MCUCSR register determines the nature of the signal on which external interrupt 2 (INT2) should occur. INT2 is only edge triggered, it cannot be used for level triggering like INT0 and INT1.
3. GICR (General Interruption Control Register)
Valor de bit do registro MCUCSR para configurar a interrupção externa do AVR
Fig. 7: Bit value of the GICR register to disable/enable the respective interrupt in the AVR
GICR registers Bit5, Bit6 and Bit7 called interrupt masks are used to disable/enable the respective interrupt. The interrupt is disabled when the bit value is set to 0 and enabled when the bit value is set to 1. By default, all interrupts are disabled.
The three registers mentioned above must be configured accordingly to initialize a specific interrupt. Also note that in addition to the registers mentioned above, the Ibit (Bit7, Global Interrupt Enable) of SREG register must also be set to 1. If the global interrupt enable bit is set to 0, none of the interrupts will work regardless other registry settings. The activation and clearing of bit I is done by SEI and CLI instructions.
Programming steps:
To schedule an interruption, the following steps must be followed:
1. Clear the global interrupt enable bit in the SREG register.
two. Initialize the interrupt by properly configuring the MCUCR, MCUCSR, and GICR registers.
3. Set the Global Interrupt Enable bit in the SREG register.
4. Define the appropriate interrupt service routine (ISR) for the interrupt.
There are two ways to write ISR, for example, ISR for INT0 can be written in the following two ways:
A. ISR (INT0_vect)
B. SIGNAL (SIG_INTERRUPT0)
Example: Let's write a simple code to make an interrupt work. Initialize INT0 to generate interrupt on rising edge trigger. The interruption is generated through buttons that alternate the status of the LEDs connected to the PORTA. The connections of the LEDs to the controller are shown in the circuit diagram.
Therefore, to enable INT0 it is necessary to set Bit6 of the GICR register, that is,
GICR= (1<
For INT0 rising edge trigger, Bit0 and Bit1 status will be-
ISC00 (Bit0) = 1
ISC01 (Bit1) = 1

So, MCUCR=(3<

Project source code

###

 
// Program to use external interrupts (hardware) from the AVR microcontroller (ATmega16)
#include
#include #include /***** Function to initialize ports*****/ void init_ports { DDRA = 0xFF; PORT = 0x55; } /***** Function to initialize interrupts*****/ empty init_interrupts { cli; //Disable global interrupts GICR =(1< //Set Bit6 of the GICR to unmask the INT0 interrupt. MCUCR =(3< //Configuring MCUCR for Rising Edge interrupt for INT0 know ; //Enable global interrupts } /***** Interrupt service routine for INT0*****/ ISR (INT0_vect) { DOOR = ~PORT; _delay_ms(100); } /***** Main function *****/ int main(empty) { init_ports; while(1) { init_interrupts; } }

###

Circuit diagrams

Circuit diagram of how to use external hardware interrupts of AVR-ATmega16 microcontroller

Project Components

  • ATmega16

Project video

Conteúdo Relacionado

A network of sensors is embedded in every vehicle,...
The motor controller is one of the most important...
ESP32-CAM is a compact camera module that combines the...
A evolução dos padrões USB foi fundamental para moldar...
A SCHURTER anuncia um aprimoramento para sua conhecida série...
A Sealevel Systems anuncia o lançamento da Interface Serial...
A STMicroelectronics introduziu Diodos retificadores Schottky de trincheira de...
Determinar uma localização precisa é necessário em várias indústrias...
O novo VIPerGaN50 da STMicroelectronics simplifica a construção de...
A Samsung Electronics, fornecedora de tecnologia de memória avançada,...
O mercado embarcado tem uma necessidade de soluções de...
You have probably come across the term ' drag...
You probably have a support insulator if you've noticed...
You've probably seen stand an insulator sit on power...
You've probably seen shackle insulators enthroned on electricity poles,...
You have probably experienced situations where controlling a circuit...
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.