EXPERIMENT 2 - GPIO Programming in AVR - Dhyana
EXPERIMENT 2 - GPIO Programming in AVR - Dhyana
Objectives: After successfully completion of this experiment students will be able to,
● Use C language for ATMega32 microcontroller programming on AVRStudio.
● Experiment with GPIO of ATMega32 on ATMega32 AVR Development Board.
Equipment required:
● Windows7 or later based host computer
● ATMega32 Development board
● USBasp Programmer
● Jumper Wires
● LED
● PUSH BUTTON
Software required:
Interaction with peripherals is central to many microcontroller applications. Although the exact
capabilities vary from one processor to the other, a set of features known as General Purpose
Input/Output (GPIO) is available in most microcontrollers for the same. In short,
microcontrollers can interact with the outside world with the help of GPIO’s. All
microcontrollers contain I/O ports to allow data to be read from switches, sensors etc. and to
write to LED’s, LCD displays, motors etc.
We will learn how to use AVR ports and program them to read or write from port pins. Now the
port has multiple pins associated with it. For example Atmega32 has 8-bit port, i.e. it has 8 pins
in a single port. Each bit represents a pin i.e. bit 0 represents pin 0 on that port and so on. As you
can see in the diagram given below, Atmega32 has 4 ports named as A, B, C & D. Each of these
ports has 8-pins (micro-controller pins).
Note that these port pins might have multiple functionality i.e. you can use them as GPIO or as
UART or ADC etc. You can use a port pin for a single functionality at a time.
I will frequently refer to ‘configuring pin’ or simply ‘pin’. Remember, a port has multiple pins.
Thus in order to change setting for one port, you have to change setting for all port pins of that
port. To change setting for one single pin of the port, you have to change a particular bit in
associated register.
All its ports are 8 bit wide. Every port has 3 registers associated with it each one with 8 bits.
Every bit in those registers configure pins of particular port. Bit0 of these registers is associated
with Pin0 of the port, Bit1 of these registers is associated with Pin1 of the port, …. and like wise
for other bits.
where
Example:
PINx register
PINx (Port IN) used to read data from port pins. In order to read the data from port pin, first you
have to change port’s data direction to input. This is done by setting bits in DDRx to zero. If port
is made output, then reading PINx register will give you data that has been output on port pins.
Now there are two input modes. Either you can use port pins as tri stated inputs or you can
activate internal pull up. It will be explained shortly.
Example :
When you set bits in DDRx to 1, corresponding pins becomes output pins. Now you can write
data into respective bits in PORTx register. This will immediately change state of output pins
according to data you have written.
In other words to output data on to port pins, you have to write it into PORTx register. However
do not forget to set data direction as output.
Example :
Making the DDRx bits to 0 will configure the PORTx as Input. Now the corresponding bits in
PORTx register can be used to enable/disable pull-up resistors associated with that pin.To enable
pull-up resistor, set bit in PORTx to 1, and to disable set it to 0.
Similar to printing ‘Hello World’ in C or C++, the very first step towards programming a
microcontroller is Blinking a LED with a delay. Atmega32 is a very popular high performance 8
bit AVR Microcontroller. For this example project we need to use two registers DDR and PORT.
DDR stands for Data Direction Register, it determines the direction (Input/Output) of each pins
on the microcontroller. HIGH at DDR register makes corresponding pin Output while LOW at
DDR register makes corresponding pin Input. PORT register is the output register which
determines the status of each pin of a particular port. HIGH at PORT register makes
corresponding pin Logic HIGH (5V) while LOW at PORT register makes corresponding pin
Logic LOW (0V).
5. Selecting Microcontroller
8. To load the hex file on the development board, use the external tool USBasp created in
Experiment 1.
C Code:
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRC = 0xFF; //Nakes PORTC as Output
while(1) //infinite loop
{
PORTC = 0xFF; //Turns ON All LEDs
_delay_ms(1000); //1 second delay
PORTC= 0x00; //Turns OFF All LEDs
_delay_ms(1000); //1 second delay
}
}
● DDRC = 0xFF makes all pins on PORTC as output pins
● PORTC = 0xFF makes all pins on PORTC Logic High (5V)
● PORTC = 0x00 makes all pins on PORTC Logic Low (0V)
● _delay_ms(1000) provides 1000 milliseconds delay.
● while(1) makes an infinite loop
You have seen that PORT registers are used to write data to ports. Similarly to read data from
ports PIN registers are used. It stand for Port Input Register. eg : PIND, PINB
NOTE:
You may like to set or reset individual pins of PORT or DDR registers or to know the status of a
specific bit of PIN register. There registers are not bit addressable, so we can’t do it directly but
we can do it through program. To make 3ed bit (PC2) of DDRC register low we can use DDRC
&= ~(1<<PC2). (1<<PC2) generates the binary number 00000100, which is complemented
11111011 and ANDed with DDRC register, which makes the 3ed bit 0. Similarly DDRC |=
(1<<PC2) can be used set the 3ed bit (PC2) of DDRC register and to read 3ed bit (PC2) we can
use PINC & (1<<PC2). Similarly we can set or reset each bit of DDR or PORT registers and
able to know the logic state of a particular bit of PIN register.
OUTPUT:
CONCLUSION:
Statement Meaning
PORTC = All pins on PORTC are set to logic 0 if configured as output, or no pull-up if
0x00 configured as input.