0% found this document useful (0 votes)
18 views12 pages

EXPERIMENT 2 - GPIO Programming in AVR - Dhyana

The document outlines an experiment focused on programming the ATMega32 microcontroller's GPIO using C language in AVRStudio. It details the necessary equipment, software, and theoretical background on GPIO functionality, including the configuration of data direction and port registers. Additionally, it provides a practical example of blinking an LED and includes a post-lab exercise with questions related to GPIO programming concepts.

Uploaded by

dhyanakothari94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

EXPERIMENT 2 - GPIO Programming in AVR - Dhyana

The document outlines an experiment focused on programming the ATMega32 microcontroller's GPIO using C language in AVRStudio. It details the necessary equipment, software, and theoretical background on GPIO functionality, including the configuration of data direction and port registers. Additionally, it provides a practical example of blinking an LED and includes a post-lab exercise with questions related to GPIO programming concepts.

Uploaded by

dhyanakothari94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Experiment-2

Aim: Hands-on experimentation of ATMega32 GPIO programming in C.

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:

●​ AVR Studio7 installation setup


●​ USBasp driver installation setup
Theory:
Introduction of GPIO:

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.

These three registers are as follows :


(x can be replaced by A,B,C,D as per the AVR you are using)
– DDRx register
– PORTx register
– PINx register

where

DDRn – Data Direction Register

PORTn – Port Output data Register

PINn – Port Input Register


DDRx register
DDRx (Data Direction Register) configures data direction of port pins. Means its setting
determines whether port pins will be used for input or output. Writing 0 to a bit in DDRx makes
corresponding port pin as input, while writing 1 to a bit in DDRx makes corresponding port pin
as output.

Example:

●​ to make all pins of port A as input pins :​


DDRA = 0b00000000;
●​ to make all pins of port A as output pins :​
DDRA = 0b11111111;
●​ to make lower nibble of port B as output and higher nibble as input :​
DDRB = 0b00001111;

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 :

to read data from port A.​


DDRA = 0x00; //Set port a as input
●​ x = PINA; //Read contents of port a
PORTx register
PORTx is used for two purposes.

1) To output data : when port is configured as output

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 :

to output 0xFF data on port b​


DDRB = 0b11111111; //set all pins of port b as outputs
PORTB = 0xFF; //write data on port
●​

to output data in variable x on port a​


DDRA = 0xFF; //make port a as output
PORTA = x; ​ //output variable on port
●​

to output data on only 0th bit of port c​


DDRC |= 0b00000001; ​ //set only 0th pin of port c as output
●​ PORTC |= 0b00000001; //make it high.

Enabling Internal Pull Up Resistors:

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.

1.​ DDRB = 0x00; // Configure the PORTB as Input.


2.​ PORTB = 0xFF; // Enable the internal Pull Up resistor of PORTB.
3.​ DDRD = 0xff; // Configure PORTD as Output
4.​ PORTD = PINB; // Read the data from PORTB and send it to PORTD.
Procedure:

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).

Getting started with Microchip studio 7

1. Download and Install Atmel Studio.

2. Open Atmel/Microchip Studio

After Opening Atmel Studio


3. Select New Project from File
4. Select GCC C Executable Project, give a project name, solution name, location in which
project is to be saved and click OK.

5. Selecting Microcontroller

Search ATMEGA32 on the screen and select that microcontroller.


6. Enter the Program
7. Then click on Build >> Build Solution or Press F7 to generate the hex file.

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:

Experiment-1 ​ Post Lab Exercise


Student Name: Dhyana Kothari
Enrollment
No:92301733031____________________________________________________________________________________

Answer the following questions:

1) Find the contents of PORTC after execution of the following code:


●​ PORTC = 0;
●​ PORTC = PORTC | 0x99;
●​ PORTC = ~PORTC;

2) Find the contents of PORTC after execution of the following code:


●​ PORTC = ~(0<<3);

3) To configure the output at pin 3 of PORTB, which register should I use?


You should use the Data Direction Register for PORTB, which is:
●​ DDRB |= (1 << PB3); // Set bit 3 of DDRB to 1 (output)

4) When the PORTx register has 1 value, what does it mean?


It depends on the data direction (input/output):

If the pin is configured as output (DDRx = 1):


○​ PORTx = 1 → The pin is set to high (logic 1).​

○​ PORTx = 0 → The pin is set to low (logic 0).​

If the pin is configured as input (DDRx = 0):

○​ PORTx = 1 → The pull-up resistor is enabled.​

○​ PORTx = 0 → The pin is in high-impedance state (no pull-up).

5) What is the difference between PORTC = 0x00 and DDRC = 0x00?

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.

DDRC = All pins on PORTC are configured as input.


0x00
6) At port A, one push button has been connected and at port D, one led has been connected, now
I am writing a program in such a way that when push button is pressed, led is on and when push
button is not pressed, led is off. For this, which GPIO registers am I going to use?

We need these three registers:

●​ DDRA – Set the push button pin as input


●​ PORTA – Optionally enable pull-up resistor on the input pin
●​ PINA – To read the input from the push button
●​ DDRD – Set the LED pin as output
●​ PORTD – To turn LED on/off​

You might also like