Difference between revisions of "Switch and a LED with AVR"
Line 1: | Line 1: | ||
[[Category:AVR Tutorials]] | [[Category:AVR Tutorials]] | ||
+ | |||
+ | =Code= | ||
+ | <syntaxhighlight> | ||
+ | #include <avr/io.h> | ||
+ | #define SW 0 | ||
+ | #define LED 1 | ||
+ | int main(void) | ||
+ | { | ||
+ | DDRB &= ~(1<<SW); //switch as input | ||
+ | DDRB |= (1<<LED); //led as output | ||
+ | while(1) | ||
+ | { | ||
+ | if ((PINB&(1<<SW)) == 1) | ||
+ | PORTB |= (1<<LED); | ||
+ | PORTB &= ~ (1<<LED); | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
[http://exploreembedded.com/wiki/images/b/be/Schematic_AVR_Interfacing_Switches_%26_LEDS.pdf '''Schematic'''] | [http://exploreembedded.com/wiki/images/b/be/Schematic_AVR_Interfacing_Switches_%26_LEDS.pdf '''Schematic'''] | ||
[[File:Schematic AVR Interfacing Switches & LEDS.JPG|680px]] | [[File:Schematic AVR Interfacing Switches & LEDS.JPG|680px]] | ||
'''Code and Explanation will be updated soon..''' | '''Code and Explanation will be updated soon..''' |
Revision as of 15:06, 20 November 2014
Code
#include <avr/io.h> #define SW 0 #define LED 1 int main(void) { DDRB &= ~(1<<SW); //switch as input DDRB |= (1<<LED); //led as output while(1) { if ((PINB&(1<<SW)) == 1) PORTB |= (1<<LED); PORTB &= ~ (1<<LED); } }