Reading AVR button status using WinAVR
If you want to bring some interactivity to your embedded projects, one option is to add buttons. This allows you to control program flow, set parameters and much more.
Few words about AVR ports. AVR Port pins can be configured as input or output. See table for all general pin configurations:

DDRx register is so called direction register;
PORTx – is port output register;
PINx – is pin input regsiter;
So there can be three options for input and two for output. If you are doing some simple routines with AVR microcontrollers, you maybe are familiar with configuring output from port. Just write „1“ to DDRx register and then send data to PORTx register:
For instance:
DDRD=0×0F; //sets lower nibble as output;
PORTD=0×05 //output „1“ to PORTD pins 0 and 2;
If you want to read input signals there are normally couple ways to do this. For input DDRx register always should be set as input „0“ pin values. PORTx register can be set in two ways:
If PORTx is set to „1“, then internal pull-up resistor is enabled depending on PUD bit in SFIOR register. If PORTx pin is set to „0“ then internal pull-up is disabled despite PUD settings.
Normally PUD bit is set to „0“ what means that this enables internal pull-ups.

If you do not touch SFIOR register then you have to write:
DDRD=0×0F; //sets higher nibble as input;
PORTD=0×50 //sets port pins 4 and 6 as input with pull-ups and pins 5 and 7 without pull-ups (tristate);
Enough theories. Couple examples will make it more clear.
- Simplest way to connect button to AVR microcontroller is connect it directly to ground:

In this case internal pull-up is enabled by writing „1“ to PORTD.
- Another way is to use external Pull-Up:

Internal pull-up is disabled by writing „0“ to PORTD or Setting PUD bit to „1“.
- Similar situation may be when using Pull-down resistor:
Lets take simplest way (internal pull-up enabled and button connected to ground) and write simple routine using WinAVR:
//————————————————-
#include “avr\io.h”
#include “avr\iom8.h”
int main(void) {
DDRD&=~_BV(0);//set PORTD pin0 to zero as input
PORTD|=_BV(0);//Enable pull up
PORTD|=_BV(1);//led OFF
DDRD|=_BV(1);//set PORTD pin1 to one as output
while(1) {
if (bit_is_clear(PIND, 0))//if button is pressed
{
PORTD&=~_BV(1);//led ON
loop_until_bit_is_set(PIND, 0);//LED ON while Button is pressd
PORTD|=_BV(1);//led OFF
}
}
}
//————————————————–
Sample project fiels ready to go with VMLAB
Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post | TrackBack URI for this post
New on WinAVR Tutorial
Running TX433 and RX433 RF modules with AVR microcontrollers,Sometimes in embedded design you may want to go wireless. Might be you will want to log various readi …Programming AVR ADC module with WinAVR,Most of AVR microcontrollers have Analog to Digital Converter (ADC) integrated in to chip. Such solut … |
New on WinARM Tutorial
What are differences between WinARM and WinAVR,Everyone who is working with AVR microcontrollers knows this powerful tool – WinAVR (http://win …LPC2000 watchdog timer,As in all microcontrollers watchdog timers purpose isto reset microcontroller after reasonable amount … |
