Introduction to Interrupts

11- AVR ATmega 16 Tutorials- Introduction to Interrupts Leave a comment

Hello everyone and welcome back to the Blog.

In this post, I am going to talk about Interrupts. I will not practically demonstrate how to use them in projects in this one rather I will save that for later posts. In this one, I will talk about what they are…what are their types…why we use them and their working. Lastly, I will also introduce how to start reading the Datasheet for the AVR ATmega 16 microcontroller.

A datasheet is a piece of document, provided by the manufacturer of the chip (in our case ATMEL), that tells us everything we need to know about the controller. So whenever we are starting with a new type of controller, we don’t need to mug up every register name for that controller. Instead, we refer to the datasheet whenever required. Download the PDF version of the datasheet and have a look at it.

Now let’s talk about Interrupts. An Interrupt is a signal to the microcontroller emitted by hardware or software indicating an event that needs immediate attention of the CPU. Whenever an interrupt occurs, the controller completes the execution of the current instruction and starts the execution of an Interrupt Service Routine (ISR) or Interrupt Handler.

Let’s try to understand this concept with a code snippet. So, in any typical program, we have our main function. We know that the program execution always starts in the main function. Inside the main function, first of all, we have all the register descriptions and declarations. Below that we have the infinite while loop. Inside the while loop, we have multiple statements that are executed one by one every clock cycle.

Now, when we use interrupts we will also have an Interrupt Service Routine outside of the main function. Inside this routine, we might have further statements that could be executed after entering the routine.
The Program Counter which is a register inside the CPU that contains the address of the current instruction being executed will start at the first line of the main function. It will first execute the register declaration statements and then move on to the while loop. One by one each statement will be executed inside the loop and after the counter gets to the last statement it will come back to the beginning of the loop. This will go on forever as we are inside an infinite loop.
Now let us assume, an interrupt is generated when the CPU is executing the second statement. The processor will complete the execution of the current statement first and then the program counter will jump to the ISR routine. The processor will now start executing the statements present inside the ISR routine. After executing the last statement the program counter will go back to the while loop where it left off from and restart executions.

I have tried to explain these concepts and more with the help of animations in the video below so take a look at it.

Video:

 

Now as mentioned in the video, there are quite a few disadvantages of the Polling methodology of programming. Polling wastes a lot of CPU cycles and makes the code inefficient. The other big disadvantage is that while doing polling for any condition we cannot parallelly do other tasks. It acts as a hindrance to multitasking. So in systems where we have multiple functionalities of the microcontroller all working at once such as multiple sensors connected to multiple ADC line, serial communication and timers. We will find this approach to create a lot of bugs and problems. In such cases, Interrupts are the better way of implementation. This will be more clear when we actually implement in a circuit. For now, let us understand their various types.

To do that we must first download the datasheet for the ATmega16 microcontroller. After downloading the PDF version have a look at it. For a beginner, all this information in the datasheet at once might be a little daunting at first. But you need not worry as we will get into them slowly step by step making the transition as comfortable as possible.

For their types, we basically have two types of Interrupts. Hardware or physical Interrupts and Software Interrupts. Hardware Interrupts have actual physical pins associated with them. If you go to the Pin Description section in the datasheet you will find that Pin 9 of the controller is the RESET pin which is a hardware interrupt. Apart from this, pins 16 and 17 are INT0 and INT1 external interrupts. What this means is that the status of these pins can be used to trigger interrupts. For example, if the RESET pin on the controller is given a Low value then the RESET interrupt is triggered and the microcontroller is reset. Software interrupts, on the other hand, are not triggered by hardware events but rather by software changes that we make in the code. One example of this is after the conversion of an Analog signal to its Digital counterpart (ADC), an interrupt could be triggered. This is the basic difference between hardware and software Interrupts.

Now, as we move on in this tutorial series we will one by one understand each and every interrupts available in the microcontroller. In the next video, we will first take a look at the external pin interrupts ie. INT0 and INT1 and use them on a practical project.

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