Interrupt in AVR Atmega32A Microcontroller

Summary of Interrupt in AVR Atmega32A Microcontroller


This tutorial explains using interrupts on the AVR Atmega32A microcontroller, focusing on software interrupts and a simple example with Timer/Counter1. It describes how interrupts temporarily suspend normal program flow to run an ISR, then resume execution. The project toggles a green LED on PB0 each second using the 16-bit Timer/Counter1 to generate interrupts. External interrupts INT0/INT1/INT2 and 21 interrupt vectors are mentioned; the tutorial centers on timer-driven software interrupt implementation and wiring a 330 Ω resistor to PB0 with a green LED.

Parts used in the Timer/Counter1 LED Toggle Project:

  • AVR Atmega32A microcontroller
  • Green LED
  • 330Ω current limiting resistor
  • Connection wires
  • Power supply for microcontroller
  • Programmer or development board to upload code

An interrupt lets hardware call your code the moment something happens, so the main program does not have to keep checking. This page shows the two kinds most projects use on the ATmega32A: an external interrupt on INT0 (a button) and a timer compare interrupt that fires once a second. Both run from one small program with a main loop that does nothing.

IN SHORT
  • INT0 is on PD2. Set ISC01 and clear ISC00 in MCUCR for a falling edge, then enable INT0 in GICR.
  • Timer1 in CTC mode with a prescaler of 256 counts 31,250 times a second, so OCR1A = 31249 gives one interrupt per second.
  • Call sei() once to enable interrupts globally, and keep each handler short.
  • The program compiles for the ATmega32 to 232 bytes; we did not run it on hardware.
PD2
INT0 pin
1 s
timer period
31,249
OCR1A value
232 B
program size
Chain: event from button or timer, interrupt flag set, interrupt service routine runs, main loop continues
What happens when an interrupt fires

What an interrupt does

When the event happens the hardware sets a flag. If the interrupt is enabled and the global interrupt flag is set, the chip finishes the current instruction, saves the return address, and jumps to the vector for that source. The compiler turns your ISR() function into that handler and saves the registers it uses. When it ends, the main program carries on where it was.

ISC01 ISC00 INT0 triggers on
0 0 Low level
0 1 Any logical change
1 0 Falling edge
1 1 Rising edge

The one second timer

Step Value
Clock 8 MHz (set the fuses accordingly)
Prescaler 256 8,000,000 / 256 = 31,250 Hz
Compare value 31,250 counts is one second, so OCR1A = 31,250 – 1 = 31,249
Mode CTC, WGM12 set: the counter resets on the match

The program

/* Interrupts on an ATmega32A: external interrupt INT0 (PD2, button to ground, falling edge) toggles the LED on PB0,
   and a Timer1 compare match interrupt toggles the LED on PB1 once a second (8 MHz clock, prescaler 256, OCR1A = 31249).
   Build: avr-gcc -Os -mmcu=atmega32 -DF_CPU=8000000UL -o irq.elf int0_interrupt.c  (compiled only, not run) */
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <avr/io.h>
#include <avr/interrupt.h>

ISR(INT0_vect)
{
    PORTB ^= (1 << PB0);                         /* keep ISRs short: no delays inside */
}

ISR(TIMER1_COMPA_vect)
{
    PORTB ^= (1 << PB1);
}

int main(void)
{
    DDRB |= (1 << PB0) | (1 << PB1);
    PORTD |= (1 << PD2);                         /* pull-up on INT0 */
    MCUCR |= (1 << ISC01);                       /* falling edge of INT0 */
    MCUCR &= (uint8_t)~(1 << ISC00);
    GICR |= (1 << INT0);                         /* enable INT0 */
    TCCR1B = (1 << WGM12) | (1 << CS12);         /* CTC, prescaler 256: 8,000,000 / 256 = 31,250 Hz */
    OCR1A = 31249;                               /* 31,250 counts = 1 s */
    TIMSK |= (1 << OCIE1A);
    sei();                                       /* global interrupt enable */
    for (;;) { }
}

Each ISR toggles an LED with an exclusive-or, which is quick and needs no flag clearing in the code (the hardware clears the flag when the handler starts). If an ISR and the main loop share a variable that is more than one byte wide, declare it volatile and disable interrupts while the main loop reads it.

Keep interrupt handlers short. Do not call delays or long functions inside an ISR; set a flag or update a counter, and let main do the work.

A mechanical button bounces: one press can produce several falling edges within a few milliseconds, so the LED may change more than once. Add a small capacitor across the button, or ignore further edges for 20 ms in software.

If it does not work

What you see What to check
Interrupt never fires Did you call sei()? Is the enable bit set (INT0 in GICR, OCIE1A in TIMSK)?
Fires on every bounce Debounce the button with a capacitor or a short software lockout.
Timer period is wrong Check the clock fuses and that CS12 is the only clock bit set.
Reset loop An enabled interrupt with no ISR for it sends the chip to the reset vector.

Quick answers

Can an ISR be interrupted?
Not by default: the global flag is cleared on entry. You can re-enable it, but that is rarely a good idea.
Which interrupt has priority?
The one with the lower vector address; see the vector table in the datasheet.
Does it wake the chip from sleep?
Some do, depending on the sleep mode; check the datasheet table.

Downloads and references

Share this project
X
LinkedIn
Reddit
WhatsApp

Quick Solutions to Questions related to Timer/Counter1 LED Toggle Project:

  • What is an interrupt in AVR Atmega32A?
    An interrupt temporarily suspends normal program flow to execute an interrupt service routine and then resumes the program.
  • How many interrupt vectors are available in Atmega32A?
    There are 21 different interrupt vectors available.
  • Which pins trigger external interrupts?
    External interrupts are triggered by INT0, INT1, and INT2 pins.
  • Which timer is used in this tutorial for software interrupt?
    The tutorial uses the 16-bit Timer/Counter1 register.
  • What does the example project do with the LED?
    The project toggles a green LED on PB0 every second using a timer-generated interrupt.
  • What resistor value is used with the LED on PB0?
    A 330Ω current limiting resistor is used with the green LED on PB0.
  • Is this tutorial covering hardware or software interrupts?
    This tutorial covers software interrupts using Timer/Counter1.
  • Where can I find more details about interrupt vectors and addresses?
    More details are available in the Atmega32A datasheet (referenced page 43 in the article).

About The Author

Author avatar

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Stay in the loop

New AVR projects, tutorials and schematics, every week.

Subscribe Browse projects

Scroll to Top