control7 blog

~Electronics & Programming~

Skip to: Content | Sidebar | Footer

Pages

AVR tutorial – Basic I/O Operation. Input signal from push button and drive LED

30 March, 2011 (23:40) | AVR Microcontroller, Electronics | By: xenon ark

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.

Comments

Comment from jojo
Time May 1, 2011 at 5:06 pm

thanks wat infonya…

Comment from xenon ark
Time May 2, 2011 at 4:43 am

thank you jojo

Comment from Maribeth
Time June 12, 2011 at 12:56 am

Thank God! Soemone with brains speaks!

Write a comment





*
= 5 + 7