0% found this document useful (0 votes)
124 views

Interrupts in Atmega16

This document discusses interrupts in the Atmega16 microcontroller. It describes that interrupts allow the microcontroller to run interrupt service routines in response to events, then resume the main program. There are 21 total interrupts, including external interrupts from pins and internal interrupts for timers, ADC, and serial interrupts. When an interrupt occurs, the program execution is suspended to run the ISR, then resumes where it left off. An example is provided of using timer0 interrupts to blink an LED every 1 second when the microcontroller frequency is 4MHz.

Uploaded by

Anoop S Pillai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

Interrupts in Atmega16

This document discusses interrupts in the Atmega16 microcontroller. It describes that interrupts allow the microcontroller to run interrupt service routines in response to events, then resume the main program. There are 21 total interrupts, including external interrupts from pins and internal interrupts for timers, ADC, and serial interrupts. When an interrupt occurs, the program execution is suspended to run the ISR, then resumes where it left off. An example is provided of using timer0 interrupts to blink an LED every 1 second when the microcontroller frequency is 4MHz.

Uploaded by

Anoop S Pillai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Interrupts in Atmega16-AVR

The interrupt is another method to program the microcontroller. In the interrupt method, the
microcontroller gets interrupted by the device connected to it. Once interrupted, it is going to
run the piece of code written for particular interrupt called Interrupt Service Routine(ISR).
Once it has done executing, it’s going to resumes the operation from where it’s gets
interrupted.
Types of Interrupts
There are total 21 Interrupts in Atmega16 of AVR family, which can be divided into two
basic types-The External and the Internal Interrupt. As the name suggests, external interrupts
are triggered by hardwares(External Devices). Whereas the internal interrupts are triggered
by the events occur in the program itself. Timer interrupt, Serial interrupts, ADC(Analog to
Digital Converters) interrupt etc are examples of internal interrupts in Atmega16.
External Interrupts
There are four pins in Atmega16 that are assigned to handle interrupts from the external
sources. These pins are PIND0 (PIN16), PIND1 (PIN17), PINB2 (PIN3) and RESET (PIN9).
Out of these interrupts, RESET pin is non-maskable. It means that rest of the interrupts can
be activated or deactivated by you(maskable) but if the RESET interrupt is triggered, the
microcontroller has to execute it. Basically, RESET Interrupt resets the program loaded on
the microcontroller.

Internal Interrupts
There are 17 internal interrupts in Atmega16. These interrupts support the various operations
in the microcontroller which include-Analog to Digital Converter(ADC), Timer Interrupts,
Serial Interrupts etc.
Events occur during Interrupt
When an interrupt is triggered, the normal execution of the program gets suspended and the
instructions written for the interrupt is executed(Interrupt Service Routine). Once, the
instructions written for the interrupt gets executed, the program gets started from the same
point where it gets interrupted.

Problem Statement
Suppose we have to generate a blink the LED in 1 sec interval. The operating frequency of
the microcontroller is 4 MHz. I have connected the LED in PIN0 of Port D.
Solution using Timer0
We need a bit of calculation before writing the code. See, we know that the delay is very
large so, I am choosing 64 as a prescaler. After that, I am calculating the number of times it
should overflow to generate the delay of 1sec.
How am I going to calculate the value?
It’s easy to find out the value. As I have already stated above, the prescaler I am choosing is
64. This results the frequency of the controller is 64.5 KHz. As we know, the frequency is an
inverse of the Time period (T=1/F). Therefore, the time period of the controller should be 16
microseconds. Now, because the timer here I am using is 8 bit, therefore to find out the total
time it takes to count till 8 bit multiplies the time period with 256(0-255 total 256 counts)
which is 4096 microsecond.

Now, we know how much time the controller takes to count the value till 8 bit. But, if we
divide the value to the delay required, we can know how many time the timer have to
overflow to generate the delay of 1sec. On dividing, the value is 244.14 which is
approximately equal to 244.
Therefore, the microcontroller has to overflow 244 times for the delay generation of 1sec.
Now we just need to know how to initialize the timer interrupt.
TIMSK or Timer/Counter Interrupt Mask Register is a control register used to mask or
unmask the timer interrupts. The bit 0 of TIMSK Register controls the overflow interrupt of
Timer 0. Setting this bit to ‘1’ enables the overflow interrupt of timer 0.
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

char a;
int main(void)
{
DDRD=0xff;
PORTD=0x00; // LED is connected in this port
TIMSK=(1<<TOIE0); // enabling the interrupt
TCCR0=(1<<CS01)|(1<<CS00); // setting the prescaler-64
sei();
while(1)
{

}
}
ISR(TIMER0_OVF_vect)
{
a++;
if(a>244)
{
PORTD=~PORTD;
a=0;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy