Home » Embedded Systems

Interrupt and Its Use | AVR

In this article, we will learn what an interrupt is and also we would learn an application/sample program to use the interrupt?
Submitted by Suryaveer Singh, on August 31, 2018

Interrupts are basically events that require intermediate attention by the microcontroller. When an interrupt event occurs the microcontroller pauses its current task and attend to the interrupt by executing an interrupt service. After this, the microcontroller returns to the initial task that it had paused and continue its normal operation.

Now we would create a program in which we would use Interrupt, now we have to write our code such that one bulb keeps blinking with the time interval of 2 sec and we would use another LED bulb such that when the button is pressed the bulb starts to glow and when the button is off stops glowing. The first LED should be keeping on blinking while performing this process.

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 1000000
#include <util/delay.h>


int main(void)
{
	DDRA =0x03;
	MCUCR = 0x01;
	GICR = 0x40;
	GIFR |= 0x40;
	sei();

	while(1)
	{
		PORTA ^= 1<<PA1;
		_delay_ms(1000);
	}

	return 0;
}

ISR(INT0_vect){
	PORTA ^= 1<<0;
}

Explanation:

  • Initially, use all the header files that we have used till now.
  • DDRA = 0x03 represents the connection of the LED bulb made to the microcontroller.
  • MCUCR = 0x01 represents the type of interrupt used.
  • GICR = 0x40 represents that the external interrupt is enabled.
  • GIFR |= 0x40 is used to make the interrupt initially Zero, by writing this we have cleared the interrupt flag.
  • sei() is the global interrupt. It is used to enable the interrupt flag. Similarly, cli() can be used to disable the interrupt flag.
  • Inside the while loop, we have done masking so that the next steps used in the programming can also be utilized. If masking not done the code execution will stop here only.
  • For PORTA ^= 1<<0, here the X-OR operation is used. Its benefit is that it would perform one time high and one time low.

Simulation:

Interrupt and Its Use | AVR

Explanation:

  • Open proteus and add:
    1. 2 LED Bulbs
    2. ATmega16
    3. Resistance
    4. Button
    5. 2 Ground Terminals
  • Arrange the components as shown in figure.
  • Double click on the ATmega16 and upload the Hex file created in it.
  • Write the value of the resistor as 10k.
  • The above simulation screenshot is at the time when the button is pressed ON and both of our LED bulbs are glowing.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.