External and Internal Interrupts in AVR Microcontrollers

 

COMSATS UNIVERSITY ISLAMABAD

MICROPROCESSOR SYSTEMS AND INTERFACING

 

LAB REPORT #08

 

SUBMITTED TO:

SIR KHIYAM IFTIKHAR

SUBMITTED BY: -

S/NO

NAME

REG NO

1

JUNAID AHMAD

CIIT/FA19-BEE-089/ISB

2

IBRAR AHMAD

CIIT/FA19-BEE-083/ISB

3

JARRAR MALIK

CIIT/FA19-BEE-087/ISB

 

 

DATE:

23-11-2021

 

External and Internal Interrupts in AVR Microcontrollers

 

Objectives:

              To learn the concepts related to interrupts in AVR microcontroller.

              To configure and use the external interrupt or user input tasks

              To configure and use the internal interrupts

 

 

Software Tools:

              Microchip Studio/AVR Studio

              Arduino

              Proteus ISIS

              AVR DUDESS

 

Hardware Tools:

 

Name

Value

Quantity

Arduino Nano

-

1

Breadboard

-

1

Switches

-

2

LEDs

 

8

Table 7.1: List of Components

Pre-Lab

·       What is Interrupt?

 

An interrupt refers to a notification, communicated to the controller, by a hardware device or software, on receipt of which controller momentarily stops and responds to the interrupt. 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. ISR is a piece of code that tells the processor or controller what to do when the interrupt occurs. After the execution of ISR, controller returns to the instruction it has jumped from (before the interrupt was received). The interrupts can be either internal interrupts or external interrupts.

 

·        Why needs interrupts?

 

An application built around microcontrollers generally has the following structure. It takes input from devices like keypad, ADC etc., processes the input using certain algorithm and generates an output which is either displayed using devices like seven segments, LCD or used further to operate other devices like motors etc. In such designs, controllers interact with the inbuilt devices like timers and other interfaced peripherals like sensors, serial port etc. The programmer needs to monitor their status regularly like whether the sensor is giving output, whether a signal has been received or transmitted, whether timer has finished counting, or if an interfaced device needs service from the controller, and so on. This state of continuous monitoring is known as polling.

In polling, the microcontroller keeps checking the status of other devices and while doing so it does no other operation and consumes all its processing time for monitoring. This problem can be addressed by using interrupts. In interrupt method, the controller responds to only when an interruption occurs. Thus, in interrupt method, controller is not required to regularly monitor the status (flags, signals etc.) of interfaced and inbuilt devices.

To understand the difference better, consider the following. The polling method is very much like a salesperson. The salesman goes door-to-door requesting to buy its product or service. Like controller keeps monitoring the flags or signals one by one for all devices and caters to whichever needs its service. Interrupt, on the other hand, is very similar to a shopkeeper. Whosoever needs a service or product goes to him and apprises him of his/her needs. In our case, when the flags or signals are received, they notify the controller that they need its service.

 

·        External Interrupts:

 

The External Interrupts are triggered by the INT pins or any of the PCINT pins. The Pin Change Interrupt Request 2 (PCI2) will trigger if any enabled PCINT[23:16] pin toggles. The Pin Change Interrupt Request 1 (PCI1) will trigger if any enabled PCINT[14:8] pin toggles. The Pin Change Interrupt Request 0 (PCI0) will trigger if any enabled PCINT[7:0] pin toggles. The PCMSK2, PCMSK1 and PCMSK0 Registers control which pins contribute to the pin change interrupts. Pin change interrupts on PCINT are detected asynchronously.

The External Interrupts can be triggered by a falling or rising edge or a low level. This is set up as indicated in the specification for the External Interrupt Control Register A (EICRA).

 

·        Internal Interrupts:

 

ATmega328p has 20 internal interrupts. These internal interrupts are generated by the internal peripherals of Microcontroller like Timer, ADC etc. The internal interrupts are used for efficient operation of the internal peripherals. They can be enabled through the registers of these peripherals.

 

 

 

 

IN-LAB:

o   How to use interrupts:

·       First, we must configure and enable the global interrupts. The most significant bit of status register called ‘I’ bit is used for this purpose. If ‘I’ is set 1 using register SREG this means interrupt is enabled otherwise disabled.

·        We write functions called Interrupt Service Routine (ISR) to handle interrupts. These functions are defined outside the main function because the event that causes interrupt is not known by the programmer; hence the function can’t be called inside the main function.

·        Enable the external interrupts locally in External Interrupt Mask Register (EIMSK). Then configure the interrupts for falling edge, rising edge, low level or any logical change by using EICRA register.

·        Enable the internal interrupts locally by writing 1 to interrupt enable bit in the registers of the peripheral under use. For example, ADC system consists of ADIE bit in ADCSRA register. ADIE bit is enabled to use ADC interrupts.

 

o   Advantages of Interrupt method:

·        Priority can be assigned.

·        Controller does not waste time checking if a device needs service or not.

 

 

IN-LAB TASKS:

 

TASK 1:

 

GENERATE BINARY COUNTING WITH THE HELP OF LED’S INTERFACED BY MCU AND CONTROLLED BY 2 SWITCHES. ONE FOR ENABLING THE CIRCUIT AND THE OTHER IS TO RESET IT. SWITCH PRESSING IS AN EXTERNAL EVENT, THAT’S WHY WE USE EXTERNAL INTERRUPTS. WRITE THE C CODE FOR INTERRUPTS AND SIMULATE IN PROTEUS.

REGISTERS USED IN THIS TASK:

·         EICRA

·         EIMSK

·         SREG (STATUS REGISTER)

 

 

CODE

 

* Created: 12/11/2020 9:10:39 PM

 * Author : Aegon

 */

 

#include<avr/interrupt.h>

#include<avr/io.h>

#define F_CPU 16000000UL

unsigned char counter=0;

ISR(INT0_vect)

{

       counter++; //Sw1 pressed LED on

       PORTB = counter; //counter value displayed on portB

}

ISR(INT1_vect)

{

       counter= 0x00; //reset counter

       PORTB = counter; //counter value displayed on portB

}

int main()

{

       DDRB= 0XFF;

       counter=0;

       SREG |=(1<<7) ;              //Enable interrupts globally

       EIMSK|=(1<<INT0) ;           //Enable INT0 and INT1 interrupt locally

       EIMSK|=(1<<INT1);

       EICRA|= (1<<ISC01)  ;         //configure EICRA for falling edge INT0 and INT1

       EICRA|= (1<<ISC11);

       while(1)

       {

       }

}

 

PROTEUS -SIMULATION:





 HARD-WARE:



TASK 2:

ASSUME THAT INT1 PIN IS CONNECTED TO A SWITCH THAT IS NORMALLY LOW. WRITE A PROGRAM THAT TOGGLES PORTC 20 TIMES WITH SOME DELAY WHENEVER INT1 PIN GOES HIGH.

 

CODE

 

* Created: 12/11/2020 9:25:05 PM

 * Author : Aegon

 */

 

#include<avr/interrupt.h>

#include<avr/io.h>

#include<util/delay.h>

#define F_CPU 16000000UL

unsigned char counter=0;

ISR(INT1_vect)

{

       for (int i=0;i<20;i++)

       {

              PORTC=0x00;

              _delay_ms(500);

              PORTC=0xFF;

              _delay_ms(500);

       }

}

int main()

{

       DDRC= 0XFF;

       PORTC=0;

       SREG |=(1<<7) ;              //Enable interrupts globally

       EIMSK|=(1<<INT1);            //Enable INT1 interrupt locally

       EICRA|= (1<<ISC10)  ;         //configure EICRA for falling edge INT0 and INT1

       EICRA|= (1<<ISC11);

       while(1)

       {

       }

}


PROTEUS -SIMULATION:



Post Lab Task:

 

What is switch debouncing? Explain software and hardware methods for switch debouncing.

 

A Switch Debouncer Circuit

There are many different approaches to cleaning up switch bounce. Below is a debouncing circuit. The basic idea is to use a capacitor to filter out any quick changes in the switch signal.



A Switch debouncing circuit

The circuit's operation can be explained by looking at the equivalent circuits formed in the two switch states, open and closed.



Debouncing circuit in switch open and closed states

Starting with the switch open.

  • The capacitor C1 will charge via R1 and D1.
  • In time, C1 will charge and Vb will reach within 0.7V of Vcc.
  • Therefore, the output of the inverting Schmitt trigger will be a logic 0.

Now close the switch

  • The capacitor will discharge via R2.
  • In time, C1 will discharge and Vb will reach 0V.

 

 

Software Debounce

 

Debouncing a switch in software is very simple. The basic idea is to sample the switch signal at a regular interval and filter out any glitches. There are a couple of approaches to achieving this listed below. Both approaches assume a switch circuit like that shown in the explanation of switch bounce: a simple push switch with a pull-up resistor.

Approach 1

The first approach uses a counter to time how long the switch signal has been low. If the signal has been low continuously for a set amount of time, then it is considered pressed and stable.

 

 

 1 Setup a counter variable, initialize to zero.
 2 Setup a regular sampling event, perhaps using a timer. Use a period of about 1ms.
 
 3 On a sample event:
 4   if switch signal is high then
 5     Reset the counter variable to zero
 6     Set internal switch state to released
 7   else
 8     Increment the counter variable to a maximum of 10
 9   end if
10   if counter=10 then
11     Set internal switch state to pressed
12   end if

 

 

 

 

Approach 2

The second approach is like the first, but uses a shift register instead of a counter. The algorithm assumes an unsigned 8bit register value, such as that found it 8-bit microcontrollers.

 1 Setup a variable to act as a shift register, initialize it to xFF.
 2 Setup a regular sampling event, perhaps using a timer. Use a period of about 1ms.
 
 3 On a sample event:
 4   Shift the variable towards the most significant bit
 5   Set the least significant bit to the current switch value
 6   if shift register val=0 then
 7     Set internal switch state to pressed
 8   else
 9     Set internal switch state to released
10   end if

 

 

 

 

Critical Analysis / Conclusion:

 

The objectives of this lab were to learn about external and internal interrupts, how to configure and use them and interrupt related concepts for AVR microcontroller. Whenever an interrupt occurs the controller completes the execution of the current instruction and starts the execution of an Interrupt Service Routine (ISR).

 

Steps to enable interrupts are:

1.     Enable I bit in SREG.

2.     Write function called interrupt service routine that is ISR(ISR_vector).

3.     Enable interrupts locally using EIMSK register.

4.     Enable interrupt locally.

5.      

TASK1 OUTCOME: in this task binary counting was shown on LED bar. Switch one would start the counting and continue it if pressed again and again. The second switch would reset the counting to 0.

 

TASK2 OUTCOME:  port C was toggled 20 times whenever INT1 would go high.

 

 

Post a Comment

Post a Comment (0)

Previous Post Next Post