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:
Fig. 2: Block Diagram to take input on Atmega16 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.
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
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.