Switch and LED with Starter AVR
In this tutorial we will look at how to read the status of a switch and control the led depending on the switch status. Refer the AVR I/O Register Configuration tutorial for basics of GPIO register configuration.
Basic
Starter AVR has ATmega32 micro controller and has two LED's and two user switches on board. we'll use the same for this example so no extra hardware is required. I am using switch connected to PD3 and LED connected to PD5. Dump the code and press the SW2 on board, you will find that LED2 will change state according to the switch status.
Hookup
Code
#include <avr/io.h> | |
#define LED 5 | |
#define SWITCH 3 | |
int main(void) | |
{ | |
DDRD |= (1<<LED); // Configure PD5 as output to connect Led | |
DDRD &= ~(1<<SWITCH); // Configure PD3 as input to connect switch | |
PORTD = 0x08; // Enable The PullUps of PORTC. | |
while(1) | |
{ | |
while(((PIND)&(1<<SWITCH))==0u) // Read the switch status and display it on Led | |
{ | |
PORTD |= (1<<LED); | |
} | |
PORTD &= ~(1<<LED); | |
} | |
return 0; | |
} |
Demo
Downloads
Download the complete project folder from the below link:
https://github.com/ExploreEmbedded/ATmega32_ExploreStarterAvr/archive/master.zip
Have a opinion, suggestion , question or feedback about the article let it out here!
Setting Up Starter AVR
In this tutorial we will look at setting up the starter AVR board. Once you have done with this basic set up, you can use on board peripherals as well as many other peripherals which can be connected...

Blinky with Starter AVR
After setting up starter AVR board, we will start with simple LED blinking experiment. Two user LED's are provided on starter AVR board, we will use same for this experiment. Basic ...
Interfacing LCD with Starter AVR
In this tutorial we'll look at how to interface different types of character LCD's to the starter AVR board. Basic Starter AVR board has female connector on board to connect LCD's...

Interfacing Seven Segment Display with Starter AVR
Now we will go further and will interface more complex peripherals. In this tutorial we will interface common anode 7 segment display to the starter AVR board. To get idea about basics of 7 segment...