AVR-12

12- AVR ATmega 16 Tutorials- External Pin Interrupts Leave a comment

Hello everyone and welcome back to the Blog.

In the last post, we talked about Interrupts; especially what they are…why we use them and their types. Therefore in this one, we will dive in and talk about how to use them with a practical example. As I mentioned in the last post there are various types of interrupts available with us on the AVR platform. For this post, we will be concentrating on External Pin Interrupts. As we move on in this series we will learn about the rest of them.

Atmega16 external Interrupts
External Pin Interrupts

In the last post, I also talked about the Polling methodology of programming, wherein we continuously check for something to happen and react accordingly. We also talked about its merits and de-merits and why interrupts are better in some cases. We will, therefore, take the example of the Button Interfacing post and try to modify the circuit as well as the code to work with external pin interrupts instead of polling and get the same results.

Before we do that, let’s have a look at the pin diagram of the ATmega16 microcontroller. We see that pin 16, 17 and 3 are INT0, INT1 and INT2 external interrupt pins respectively. These three pins have almost the same functionalities when it comes to external pin interrupts. Therefore we can use anyone among them as per our choice. For this post let us use the INT0 pin.

In the Button Interfacing post, we had connected PIN0 of PORTA with an LED while PIN1 of PORTA was connected to a push button. The microcontroller was sensing the press of the button and according to the results, the blinking rate of the LED was being determined. The old circuit diagram is shown below.

Old Circuit Diagram:

Screenshot (59)
Old Circuit Diagram

Now, we have to make a single circuit change for this post. As we will be using the INT0 external interrupt pin to sense the button press. So, instead of connecting the push button to PIN1 of PORTA; we will need to connect it with INT0 pin. The new circuit diagram has been shown below.

New Circuit Diagram:

 

Screenshot (77)
New Circuit Diagram

After this, we can now start with the code. To understand external pin interrupts we have to first know about the functionality of the Registers associated with it. Below is a list of all the important resistors we need for this project. The data has been taken from the ATmega16 datasheet.

Register Descriptions:

SREG – AVR Status Register

This register is located in the AVR CPU Section of the datasheet in chapter 6. The register has many important functionalities but for our purposes, we are only concerned with Bit-7 of the register. Named as the Global Interrupt Enable, this bit must be set high whenever we wish to use any of the interrupts present in the AVR platform. It acts as an On/Off switch for Interrupts. When it is high Interrupts are enabled and when it is low, they are not. Below are the screen grabs from the datasheet.

SREG
SREG Register
SREG Register Description
SREG Bit-7 Register Description from the datasheet

GICR – General Interrupt Control Register

This register is present in chapter 13 of the datasheet under the external interrupt section. Again this register has multiple functionalities but we are only concerned with Bit-6 for our purposes. This bit named as External Interrupt Request 0 Enable, has a similar functionality to that of the SREG 7th bit. When set high, this bit will enable the INT0 external pin interrupt. It acts as an On/Off switch for the specific INT0 External Pin Interrupt.

GICR Register
GICR Register
GICR Register Description
GICR Register Description taken from the datasheet

MCUCR – MCU Control Register

The MCU Control Register contains control bits for interrupt sense control and general MCU functions. For the purposes of this project, we are concerned with Bits 0 and Bit 1 of this register. Together they comprise of the Interrupt Sense Control and determine when the external interrupt is triggered. There are four possible methods (mentioned in the Table below) for triggering the interrupt and the user can choose which one according to the needs, using these two bits.

MCUCR Register
MCUCR Register
MCUCR Register Description
MCUCR Register Description taken from the datasheet

I would now ask you to go through the video below where I explain everything in details including the code and the registers.

Video:

Now you should have a clear idea of what’s happening in the code. I have also provided the entire code for your convenience below. Feel free to modify it as you see fit. I would suggest you use the INT1 external pin interrupt and modify this code to work with it. That will give you a clear idea of how to read the datasheet and work with interrupts.

Code:

/*
Title: External Pin Interrupt INT0
Author: Samiran Patra
Target: ATmega16 Microcontroller
MCU_SPEED: 1000000 Hz
 
Note: Use makefile for compilation.
*/

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

uint8_t flag=1;

ISR(INT0_vect)					//Interrupt Service routine
{
	flag!=flag;
}

int main(void)
{
	//DDR Data Direction Register
	DDRA |= (1<<0);				//Setting pin 0 for output mode
	DDRD &= ~(1<<2);			//Setting pin 2 for input mode
	PORTD |= (1<<2);			//Giving a high value to pin 2
	
	//SREG |= (1<<I);			
	sei();					//Enabling Global interrupts
	GICR |= (1<<INT0);			//Enabling External Interrupt INT0
	MCUCR |= (1<<ISC00);
	MCUCR &= ~(1<<ISC01);		//Choosing any logic change method for triggering the interrupt
	
	while(1)
	{
		if(flag==1) 
		{
			PORTA |= (1<<0);	//100 millisecond blinking
			_delay_ms(100);
			PORTA &= ~(1<<0);
			_delay_ms(100);
		}
		else
		{
			PORTA |= (1<<0);	//1000 millisecond blinking
			_delay_ms(1000);
			PORTA &= ~(1<<0);
			_delay_ms(1000);
		}	
	}
}

Download the Makefile needed for compilation and copy it into the folder where you have saved the source file. Modify the file as per your controller and C file before compilation.

Shop Smart with us for Best Prices:

If you want to continue on with this series I would ask you to visit our YouTube channel and subscribe to receive continuous notifications. You can also subscribe to our Newsletter below to get email notification of each post as and when they are posted.

You can also follow us on social media platforms such as  Facebook and Twitter.

Thanks a lot for reading through this post. Feel free to follow through this series and comment your queries in the comment box below. Also feel free to contact us regarding any out of stock product and we will try to get back to you as soon as possible.

Leave a Reply