Switch and a LED with Atmega128 Breakout Revision as of 16:34, 5 April 2016 by Raghavendra (Talk | contribs)
Now we will control the LED depending on external input. In this tutorial we will interface a switch to one of the port pin and display its status on LED connected to other port.
Basics
For this tutorial we will a connect a switch to PORT C0 and LED to PORT B0 of Atmega128. W will configure PORT A0 as input to read the switch status and PORT C0 as outputto display the switch status on LED. Initially LED will be ON , when we press switch the LED will be OFF.
Refer the AVR I/O Register Configuration tutorial for basics of GPIO register configuration.
Hook Up
Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
#include <util/delay.h> | |
#define LED PB0 | |
#define SWITCH PC0 | |
int main() | |
{ | |
DDRB = (1 << LED); // Configure PORTB as output to connect Leds | |
DDRC = (0 << SWITCH); // Configure PORTC as Input to connect switches | |
PORTC = (1 << SWITCH); // Enable The PullUps of PORTC. | |
while(1) | |
{ | |
while(((PINC) & (1<<SWITCH)) == 1) // Check the status of the switch | |
{ | |
PORTB |= (1<<LED); | |
} | |
PORTB &= ~(1<<LED); | |
} | |
return 0; | |
} |