AVR tutorial – Basic I/O Operation. Input signal from push button and drive LED
This small tutorial will show how to read signal from a push button and on/off two led by the input signal. I have used Atmega48 to write and test the program but you can use any AVR which supports AVR GCC.
Before pressing the push button it is like:
After pressing the push button:
Sketch of the schematic:
Here is the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <avr/io.h> #include <stdio.h> //Method to read pin status uint8_t GetKeyStatus(uint8_t key) { return (!(PINB & (1<<key))); } void main() { DDRB = 0b00000111; //set B0, B1 and B2 as output while(1) { if(GetKeyStatus(0)) //reading B0 status { PORTB = 0b00000010; //set B1 as HIGH } else { PORTB = 0b00000100; //set B2 as HIGH } } } |
We can read PB0. When the button is not pressed, PB1 is high otherwise PB2 is high.











Recent Comments