Sunday, April 26, 2020

ATMega32 External Interrupt Example

External interrupt is an external notification to the micro-controller. Usually, an external event (sense) such as logic changing, or edge triggering.

In ATMega32, there are three external interrupt source, INT0, INT1 and INT2. Each source locate at different pins.

  1. INT0 at PD2
  2. INT1 at PD3
  3. INT2 at PB2
For events could trigger the interrupt:
  1. Low logic level
  2. Logic changing
  3. Falling edge
  4. Rising Edge

To program the interrupt, set up the code in the following step:

  1. Set the interrupt input pin as digital input
  2. Enable the interrupt for the corresponding input
  3. Enable the global interrupt control
  4. Keep your main program loop working
  5. Write the interrupt service routine to handle the corresponding interrupt

In ATMega32 the external interrupt is controlled by the GICR (General Interrupt Control Register).


General Interrupt Control Register

We enable any external interrupt by giving "1" to the source. For example, external interrupt 1 is enabled by setting INT1=1.

Interrupt flag contain the information about the state of any presenting interrupt. GIFR (General Interrupt Flage Register) store the status of external interrupts.


GIFR (General Interrupt Flag Register)

We can test any each external interrupt flag of this register to find the interrupt source. For example, we find INTF1=1, that mean that external interrupt INT1 is occur.

There are four modes of event (sense) the trigger the interrupt, as mention about. Those senses are controlled in the MCU Control Register (MCUCR).


ATMega32 External Interrupt Example
MCUCR (MCU Control Register)

The ISC11 and ISC10, are the interrupt sense control of INT1.

The ISC01 and ISC00, are the interrupt sense control of INT0

There four mode of sense as refers to the datasheet. By default, they sense Low to trigger interrupt.


The Interrupt Service Routine (ISR) is a block of source code uses for handling the interrupt. They are short because the interrupt must not take to long to complete.

The ISR locates at the interrupt vector. Each interrupt source has their own interrupt vector. For example INT0_vect is an interrupt vector for external interrupt 0.


In this example, I use all three external interrupt sources. The interrupt sense is logic low. So when I make any external interrupt pin low, it will generate the interrupt. The ISR is to toggle LED output each corresponding to the source.


Schematic and Simulation Diagram.
We have external interrupt, INT0, INT1 and INT2
connect to its individual active low push button.
Each interrupt toggles the corresponding output LEDs. 

Source code:

  1. #include <avr/io.h>
  2. #include "avr/interrupt.h"
  3.  
  4. int main(void)
  5. {
  6. //PA7.5 ARE OUTPUT
  7. DDRA=(1<<5)|(1<<6)|(1<<7);
  8. //PB2 USED FOR INT2
  9. PORTB=(1<<2);
  10. //INT0 AND INT1
  11. PORTD=(1<<2)|(1<<3);
  12. //SET EXTERNAL INTERRUPT FOR INT0, INT1 AND INT1
  13. GICR=(1<<7)|(1<<6)|(1<<5);
  14. //SET GLOBLE INTERRUPT
  15. sei();
  16. while (1)
  17. {
  18. }
  19. }
  20.  
  21. /*
  22.  Interrupt Service Routine for
  23.  Interrupt0
  24.  */
  25. ISR(INT0_vect){
  26. //Toggle PA5
  27. PORTA^=(1<<5);
  28. }
  29.  
  30. /*
  31.  Interrupt Service Routine for
  32.  Interrupt1
  33.  */
  34. ISR(INT1_vect){
  35. //Toggle PA6
  36. PORTA^=(1<<6);
  37. }
  38.  
  39. /*
  40.  Interrupt Service Routine for
  41.  Interrupt2
  42.  */
  43. ISR(INT2_vect){
  44. //Toggle PA7
  45. PORTA^=(1<<7);
  46. }

Back to main tutorial page ATMega32 tutorials in C with Atmel Studio 7.

Click here to download this example.

If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.

Interfacing ATMega32 to 74HC595 shift register
ATMega16 ATMega32 Experiment Board PCB from PCBWay

 

3 comments:

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)