For understanding the human needs a system must be able to take input
from user. The devices which can be used to take input for a system are
keypad, touch screen, etc. In the article LED blinking, the
microcontroller drives the LED or in embedded language the
microcontroller was set to give o/p, this article gives brief
information of getting an input from user at a particular pin of
microcontroller.
In
order to take input from an external source on any of the pins of the
AVR microcontroller, the pins need to be configured as input pin. This
configuration informs the controller that the corresponding pins are
used to take input. The following steps explain how to take input from a
pin:
Step1: Set the direction of PORT
To use a port as an input port first set the direction of port. DDRx (Data Direction) Register is used to set the direction of port.
DDRA=0x00;
In DDRA, ‘A’ shows the portA and 0x00 sets all the pins of port as input pins. (Writing 0 in DDR makes it input pin and writing 1 in DDR makes it output pin.
Step2: To take input form a port
Make a variable and use PINx instruction to get the input from a port
Value=PINA;
Where Value is a variable.
How to compare a pin?
To compare a pin, the instructions bit_is_set or bit_is_clear can be used. These instructions are define in avr/sfr_defs.h library. There is no need to include this library in code if avr/io.h has been included.
// Program to take input from a particular pin in AVR Microcontroller (ATmega16)
#include<avr/io.h>
#include<util/delay.h>
int main (void)
{
unsigned int i=0;
DDRA=~_BV(PA0); // PA0 as input pin
PORTA=0x00;
DDRD=0xff;
while(1)
{
PORTD=0x01;
while(PORTD!=0)
{
if(bit_is_set(PINA,PA0)) //If PA0=1
{
_delay_ms(100);
if(bit_is_set(PINA,PA0)) //Check it again due to swithc debouncing
PORTD=(PORTD<<1); //Left shift value of PORTD by 1
}
}
}
}
#include<avr/io.h>
#include<util/delay.h>
int main (void)
{
unsigned int i=0;
DDRA=~_BV(PA0); // PA0 as input pin
PORTA=0x00;
DDRD=0xff;
while(1)
{
PORTD=0x01;
while(PORTD!=0)
{
if(bit_is_set(PINA,PA0)) //If PA0=1
{
_delay_ms(100);
if(bit_is_set(PINA,PA0)) //Check it again due to swithc debouncing
PORTD=(PORTD<<1); //Left shift value of PORTD by 1
}
}
}
}
No comments:
Post a Comment