Line 8: Line 8:
 
=Hook Up=
 
=Hook Up=
 
=Code=
 
=Code=
 +
<html>
 +
<script src="https://gist.github.com/nitishshete/7252ffb1af5e6f29767ac68dc0123a67.js"></script>
 +
</html>
 +
 
=Demo=
 
=Demo=
 
=Downloads=
 
=Downloads=

Revision as of 17:15, 4 April 2016

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 Atmega32. 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

#include <avr/io.h>
#define LED 0
#define SWITCH 0
int main(void)
{
DDRB |= (1<<LED); // Configure PORTB as output to connect Led
DDRC &= ~(1<<SWITCH); // Configure PORTC as input to connect switch
PORTC |= 0x01; // Enable The PullUps of PORTC.
while(1)
{
if(!PINC)
PORTB &= ~(1<<LED);
else
PORTB |= (1<<LED);
}
return 0;
}

Demo

Downloads