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

MSP430 GPIO Programming Reading an external switch

To read an external switch using MSP430 an external switch is connected to P1.4…We uses polling method here to read the switch

/*button_press.c
ganeshredcobra@gmail.com
GPL
An external button connected to P1.4 is used in this example...
connect P1.4 to vcc through a resistor...
*/
#include 

volatile unsigned int i;//to prevent optimization
void main(void)
{
	WDTCTL=WDTPW+WDTHOLD;
 	P1DIR|=0X01;//set all bits in P1 to input except BIT0
	for(;;)
	{
	  if((P1IN & BIT4)==0)//while pushing S1
		P1OUT=0X00;//LED ON
	  else
		P1OUT=0x01;//LED OFF		 
	}
}

When we pushes the button LED will be ON and when we releases it will be OFF…
Happy Hacking 🙂