Avr interfacing a switch

In this tutorial we check how to interface a push button to an Atmega8.

Here we will connect an external button to give input to a AVR. Button is connected to 5v if button pressed then delay of led lights changes if released then.here the program is configured in such a way that one pin B1 of Port B is configured as input and pin B0 as output.The input pin checks whether high or low is received and toggles the output pin.

Circuit Diagram

switch

Program

#include <avr/io.h>
#include <avr/delay.h>

int main()
{
	DDRB |= 1<<PINB0;
	DDRB &= ~(1<<PINB1);
	PORTB |= 1<<PINB1;
	while(1)
	{
		PORTB ^= 1<<PINB0;
		if(bit_is_set(PINB,1))
		{
			_delay_ms(100);
		}
		else
		{
			_delay_ms(500);
		}
	}
}

Use  the  same  Makefile
Change the TARGET name with the file name of the above program
Use the  commands  in  sequence

make elf

make hex

make flash

Switch in action

Happy Hacking 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s