This guide will demonstrate how to interface switches with the ATmega2560 development board. The board has six built-in switches connected to pins on ports "D" and "E" of the ATmega2560 microcontroller. By following the steps outlined in this guide, you will be able to read the state of the switches and incorporate them into your projects.
The goal is to read the state of the switches connected to pins on ports "D" and "E" of the ATmega2560 and perform actions based on their states. This will allow you to take user input from the switches and utilize it in your embedded systems projects.
To successfully complete this project, you will need the following components:
ATmgea2560 development board.
Micro USB cable
Jumper wires
Push button switches (built-in)
LED (built-in)
Six switches are connected to the development board as follows:
8 LED are connected to development board as follows:
When the push button "S1" is pressed, the LED on port "C" is toggled. If it was initially in OFF state then currently LED will turn ON and vice-versa.
Open VS Code and paste the code into the file named external_interrupt.c.c
To compile and convert the source code into hex, run the following command in your terminal-
avr-gcc -Wall -g -Os -mmcu=atmega2560 -o switch_interrupt.hex external_interrupt.c.c
You can download the code from here.
#ifndef F_CPU
#define F_CPU 16000000UL // set the CPU clock
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#define LED_PIN 1 // Define the pin for the LED (PORTC.1)
void initExternalInterrupt() {
EICRB |= (1 << ISC41); // Falling edge triggers the interrupt for INT4
EIMSK |= (1 << INT4); // Enable external interrupt INT4
sei(); // Enable global interrupts
}
void initLED() {
DDRC |= (1 << LED_PIN); // Set LED pin as an output
}
int main(void) {
initLED();
initExternalInterrupt();
while (1) {
// Your main program logic here (if required)
}
return 0;
}
// External interrupt service routine for INT4
ISR(INT4_vect) {
// Toggle the LED state
PORTC ^= (1 << LED_PIN); // Toggle the LED at PORTC.1
}
Now open AVRDUDES and from the Programmer (-c) dropdown select Any usbasp clone with the correct VID/PID
In the Flash section, click on three dots and navigate to the desired hex file, and select it. Finally, click on Program to burn the hex file in the micro-controller
Upon successful upload and execution of the code, pressing the switch on the development board will toggle the LED. When the button connected to PORTE.4 (INT4) is pressed, it triggers an external interrupt, which causes the execution of the ISR(INT4_vect) function. In this ISR, the code toggles the state of the LED connected to PORTC.1 (LED_PIN). You can verify this by observing the LEDs connected to the switches on the circuit.
Interfacing switches with the ATmega2560 development board allows you to control LEDs based on switch inputs. By following the steps outlined in this guide and using the provided code, you should be able to successfully interface switches with the ATmega2560 board and control LEDs accordingly. Feel free to modify the code and circuit to suit your specific requirements and explore further possibilities with switch and LED interfacing.