How to make ATmega GPIO work as Input or Output

Posted

in

by

Tags:

AVR Input Output Port programming means you have to program its GPIO pins.

GPIO – General Purpose Input and Output

The GPIO is a very important peripheral of the microcontroller. Using the GPIO we can use the same exact pin for Input or Output.

To make the PIN input or output we need to set a bit in the data direction register DDR

Now lets take the example of ATmega16. Atmega 16 has 4 ports.
-PORT A
-PORT B
-PORT C
-PORT D

Each PORT has 8 pin
Each pin can be addressed individually
PortA.1 = Pin 1 of Port A
PA.1 = Pin 1 of Port A

To make a PIN input/output
DDRx = 1 // make the pin output
DDRx = 0 // make the pin input

Here x is to be replaced by port number
DDRA = DDR for port A


To Set the value of the pin we use
PORTx = 1 // set all the pins of the port to HIGH
PORTx = 0// Set all the pins of the port to LOW

To Read the value of the pin we use
PINx
usage:
int read_in;
DDRx = 0;
PORTx = 1; // This will enable the internal Pull up
read_in = PINx; // read_in variable will store the value in PINx

If the internal pull-up is not enabled than then value of the pin will always be in a floating state and it will not tell the accurate result.
When the internal pull-up is not enabled then the external pull has to be enabled by the user.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *