This tutorial is about accessing IO (Input/Output) ports of AVR ATmega32A Microcontroller.This tutorial also covers how to blink LED using AVR Atmega32A Microcontroller. From previous tutorial, we know that our chip has 40 physical pins and these pins are categorises into four ports so each pin perform multiple function, thus before we make use of microcontroller let’s try to understand how microcontroller allows us to access its pins to perform certain task. Basically, functions of microcontroller pins divided into three major categories: Control, Sensing, Communication.
There are different ways to program these functions for better understanding and to keep simplicity we are going to control PB0 pin of PORTB. Lets add a green LED to PB0 pin along with current limiting resistor of (360Ω). Now let’s look at how to make it work,
- Adjusting port pin of controller as input or output.
- Determine their state of output.
- Detect their state of input.
To map the physical pins into program code. We need to understand ports and their respective register and also naming convention to instruct microcontroller to perform certain action by accessing those register through C Program.
|DDRx||Data Direction Register for Port x|
x- corresponds to port A, B, C or D. Bit is set to 1 for output & bit is set to ‘0’ (cleared) for Input
|PINx||Input address for Port x|
x- corresponds to ports A, B, C or D
Represent state of a port, bit ‘1’ if pin is HIGH and bit ‘0’ if pin is LOW
|PORTx||Data Register for Port x|
This register is used to control the output of a port for pin that was connected to the input means DDRx. The internal pull up resistor can be enables(1) or disabled(0).
The circuit diagram is shown in the picture (above) and for further details follow a video provided down below. The short description is provided below code snippet.
* Project Name: Blinky.c
* Created: 19-06-2013 22:41:35
* Author: UMESH
DDRB |= 1 << PINB0;
PORTB |= 1 << PINB0;
PORTB &= ~(1 << PINB0);