Interrupts

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

MUNJURI ALAN MUCHIRI

ENM221-0043/2019

BSc Mechatronics

Introduction to Microprocessors and Microcontrollers

EMT 2437

LAB 2 REPORT
Title: Use of Interrupts to Toggle LEDs

Introduction

The code provided is an example of how to use external interrupts on the ATmega328P
microcontroller. External interrupts are a way for the microcontroller to respond to external
events or signals asynchronously, without the need for continuous polling of input pins.

In this code, the external interrupts are used to toggle two LEDs. When INT0 is triggered, LED 0
is toggled. When INT1 is triggered, LED 1 is toggled.

Initialization

First, the code includes the device-specific definitions file for the Atmega328p microcontroller
using the .include directive. This file provides definitions for the register addresses, bit masks,
and other constants used in the code.
Next, the .def directive is used to define a macro temp that is equivalent to register r16. This is a
common technique used in AVR assembly language programs to improve code readability by
referencing registers using descriptive names.
The .org directive is used to specify the memory addresses of various parts of the program. In
this case, the reset vector is set to address 0, while the interrupt vector addresses are set to
INT0addr and INT1addr.
The first few lines of code initialize the stack pointer, set Port D as output for LEDs, enable pull-
up resistors on INT0 and INT1 pins, configure external interrupt 0 and 1, and enable global
interrupts.
The stack pointer is a special register that is used to store the return address when a function is
called. The ldi instruction loads a constant into the stack pointer register.

Port D is used to control the LEDs. The out instruction writes the value of a register to a port. In
this case, the value 0x0C is written to Port D, which sets PD2 and PD3 as output.

Pull-up resistors are used to ensure that the INT0 and INT1 pins are always pulled high. This is
important because if the pins are not pulled high, they can float and cause unexpected
interrupts. The sbi instruction sets a bit in a register. In this case, the sbi instruction sets the
PD2 and PD3 bits in the PORTD register, which enables the pull-up resistors on INT0 and INT1
pins.

The external interrupts are configured using the EICRA and EIMSK registers. The EICRA register
specifies the interrupt trigger conditions, and the EIMSK register enables or disables the
interrupts. In this code, the INT0 interrupt is configured to trigger on any logical change, and
the INT1 interrupt is configured to trigger on a falling edge.

The global interrupts are enabled using the sei instruction. This ensures that the microcontroller
will respond to interrupts.

Main Loop

The main loop simply jumps back to itself, so the program will continuously run until an
interrupt is triggered.

Interrupt Service Routines

When an interrupt is triggered, the corresponding ISR (interrupt service routine) is executed.
The ISRs in this code are very simple. They simply toggle the appropriate LED and then return
from interrupt using the reti instruction.

Conclusion

This code is a simple example of how to use external interrupts on the ATmega328P
microcontroller. It can be used to toggle LEDs, read sensors, or perform other tasks in response
to external events.

You might also like