AVR – ATMEGA16 – EXTERNAL INTERRUPTS
Interrupts in AVR can be external (port pin sensing) or internal (Timers,ADC,UART,etc..)
When Interrupt occurs , the following things are done by AVR
1. AVR controller stops the current program execution and saves the context.
2. Control jumps to Interrupt Service Routine (ISR). (Each interrupt have an associated ISR which is a piece of code executed when an interrupt occurs.)
3. Execution of ISR is performed automatically..
4. When ISR is complete, the Controller resumes processing where it left off before the interrupt occurred.(main program resumes)
Interrupt sources of AVR are
PORT PINS INT0 (PD2), INT1 (PD3) , INT2 (PB2) & RESET
RESET is unmaskable interrupt & has the highest priority.
Other interrupt sources are Internal to AVR like -Timer , UART , ADC ,SPI , I2C ,etc..
This post is on the PORT PIN Interrupts i.e., EXTERNAL INTERRUPT
Let us connect an LED at PD0 & a switch at PD2.
PD2 is where Interrupt INT0 is sensed.
PD0 – LED
PD2 – SWITCH
Steps to configure the Interrupt:
————————————-
1. Set INT0 bit in the General Interrupt Control Register (GICR)
2. Configure MCU Control Register (MCUCR) to select interrupt type( let us configure for Falling edge).
3. Set Global Interrupt(I-bit) Enable bit in the AVR Status Register(SREG) sei()
4. Handle the interrupt in the Interrupt Service Routine code.
———————————-
General Interrupt Control Register (GICR)
In the GICR register enable the bit6 which configures INT0 Interrupt
It’s a good practice to mention the bit name itself in the code
GICR |= (1<<INT0) ;
Here we use the left shift operator to make the bit INT0 as 1
Type of INTERRUPT ( Rising Edge or Falling edge) is decided by setting bits 0 & 1of MCUCR Register
The MCUCR contains configuration bits which tells what signals will trigger INT0 and INT1
For this you need to set the ISC bits ( Interrupt Sense Control )
ISC00 & ISC01 configures the INT0 interrupt type.These are bits 0 & 1.
As seen in fig. above set this to Falling edge trigger by making ISC01 as 1.
MCUCR |= (1<< ISC01) ; (or you can also write MCUCR =0x02;)
Mentioning the bit name is always a good practice in coding
Now the Interrupt on INT0 is Falling edge-triggered (meaning that the interrupt is triggered when the signal changes from high to low)
We’ve connected a Reset type micro switch at PD2. When the switch is pressed 1 is applied to PD2 & when switch is released 0 is applied .Falling edge level triggering is from High to Low edge.
MCUCR is to configure INT0 & INT1 Interrupts.
INT2 is configured by another Register MCUCSR
INT2 can only be used as an edge-triggered interrupt.(no level trigger)
Now enable the Global Interrupt by setting the I-bit in SREG
It is the master control for all interrupts in AVR micro-controller.
This is done by calling sei();
When Interrupt occurs the ISR – Interrupt Service Routine is called automatically.Inside the ISR function we toggle the LED state.
ISR(INT0_vect)
{
PORTD ^= (1<<0);
}
The vector name used in this ISR is INT0_vect
Correct vector name to be used in the ISR function .Select it from the table below :
Complete code
—————————————————-
#include <avr/io.h>
#include <avr/interrupt.h>
int main (void)
{
DDRD = (1<< 0) ; // PORT pin PD0 is Output
PORTD | = (1<<0); // PD0 is High normally
GICR = (1<< INT0); // enable INT0 Interrupt
MCUCR |= (1<< ISC01) ; // configure Falling Edge Trigger
sei(); // Enable Global Interrupt
while(1) {}
}
ISR(INT0_vect) // Interrupt Service Routine
{
PORTD ^= (1<<0);
}
———————————————
We use Programmers Notepad of WINAVR to develop the C code.
Save the file as main.c inside a folder.
Open the MFILE utility of WINAVR to create the MFILE required for compilation.
Select the MCU type as ATMEGA16 , port as usb & programmer as usbasp.
Usbasp is not in the list of programmer.You need to select any one in the list & then Enable Editing.
Now you can edit your selection to usbasp
Save the MFILE in the same folder where you’ve stored the main.c file.
Click on Tools à MakeAll to compile & then Tools à Program to upload the hex file .
Now the LED at PD0 glows.
When switch connected to PD2 is pressed & then released , a Interrupt occurs (falling edge)
The LED at PD0 changes state to LOW.
Again press the switch & release , the LED changes state to HIGH.
VIDEO DEMO :
Links to previous posts :
https://alselectro.wordpress.com/2017/06/20/avr-self-learning-kit-getting-started/