This tutorial is about how to use button switch with AVR Atmega32A Microcontroller. In previous tutorial we have seen, how to drive LED connected to Pin “PB0” and in this tutorial lets have a look at, how to connect button to PB1 pin of microcontroller so that will control LEDs blinking by pressing button switch (tactile switch). To keep simplicity in example we’ll blink LED by 10 ms when button pressed and blink LED by 100 ms when button switch is released.
In general to control hardware we often need to provide some sort of input to system through human interaction. As an example, if we take our personal computer we use mouse and keyboard to provide input to a system and then system react accordingly. If you look at keyboard, which is nothing but the bunch of buttons. I believe this tutorial will help you to understand concept. Let’s have a look at circuit connection for this experiment.
* Example: ButtonSwitch
* Created: 07-06-2013 00:05:44
* Author: UMESH
#define F_CPU 10000000
DDRB |= 1 << PINB0;
DDRB &= ~(1 << PINB1);
PORTB |= 1 << PINB1;
PORTB ^= 1 << PINB0;
if (bit_is_clear(PINB, 1))
_delay_ms(100); //Slow, from previous
In code snippet you can see in main loop first two lines DDRB |= 1 << PBo assign PB0 pin as output for LED and DDRB &= ~(1<< PB1) assign pin PB1 as input for button switch to read input though button.
In while loop “bit_is_clear” function takes two argument (PINB,1) which used to continuously monitor state of button either pressed or un-pressed and if its un-pressed then turn LED off. This way one can give input from button switch with ATmega32A at very basic level. I hope you will find this tutorial educational and entertaining if you have question then post comment below.