0% found this document useful (0 votes)
23 views

Microcontroller

Uploaded by

sumaya tasnim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Microcontroller

Uploaded by

sumaya tasnim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment no : 02

Name of the experiment :

I/O Port programming for Atmega32 with Proteus Simulation

Objectives :

• To understand I/O port programming using the Atmega32 microcontroller.


• To learn how to configure GPIO pins as inputs and outputs.
• To practice implementing digital input and output operations.

Theory:

The Atmega32 microcontroller is a popular 8-bit AVR microcontroller manufactured by Atmel (now
Microchip Technology). It features a wide range of peripherals and GPIO (General Purpose Input/
Output) pins that can be con gured for various digital input and output operations.

The Atmega32 microcontroller has a total of 32 GPIO pins, labeled as PORTA, PORTB, PORTC,
and PORTD. Each port consists of 8 pins, labeled as PA0-PA7, PB0-PB7, PC0-PC7, and PD0-PD7,
respectively.These GPIO pins can be individually configured as either inputs or outputs to interface
with external devices such as sensors, switches, LEDs, and other digital components[1].

For each port there are three important registers:


The Data Direction Register (DDRx) determines whether the pins operate as inputs or outputs.
The port output register (PORTx) determines the actual value set on each pin when it’s being used
as an output.

The port input register (PINx) is used for reading input values[2].

Fig 2.1 : AVR Atmega32[4]


fi
Pull-up and pull-down resistors simply have constant values connected to pins, usually + 5V or
GND.
Pull-up resistors are those placed between a pin and + 5V, and pull-down resistors are those placed
between a pin and GND[3]. By connecting a pull-down resistor between the GPIO pin and ground
(GND), we ensure that the pin is actively pulled to a logic low state when no external signal is
applied. This provides a clear and de ned logic state, preventing the pin from oating and
improving the reliability of the input reading.Connecting a pull-down resistor stabilizes the input
pin when the switch is open or the LED is turned off. It creates a path to ground, ensuring the pin
remains at a low logic state.

Required Components :

I. Proteus Simulation Software


II. LED(01)
III. Connecting wires
IV. ATMEGA 32 microcontroller
V. Switches(02)
VI. Resistor
VII.Power

Code:

#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 1000000UL


int main(void)
{
PORTC |= (1 << PC0) | (1 << PC1);
DDRC &= ~((1 << DDC0) | (1 << DDC1));
DDRD |= (1 << DDD0);

while (1)
{
if ((PINC & (1 << PC0)) && (PINC & (1 << PC1)))
{
PORTD |= (1 << PD0); // Turn on LED
}
else
{
PORTD &= ~(1 << PD0); // Turn off LED
}
}
return 0;
}
fi
fl
Working principle :

1. Atmel Studio 7 software was used to write the program in the C language.
2. To make a hex le, the code was generated and built in Atmel Studio.
3. After that, the hex le was put on the Atmega32 microcontroller in the Proteus simulation
system.
4. Port C (PC0 and PC1) was con gured as inputs to connect switches (SW1 and SW2).
5. Port D (PD0) was con gured as an output to control the LED.
6. Pull-up resistors were enabled for the input pins on Port C to ensure a de ned logic state when
the switches are open.
7. When both switches were closed simultaneously, the corresponding input pins on Port C
detected logic high signal.
8. The microcontroller read the input pins and activates the output pin on Port D to turn on the
LED.
9. If either or both switches were open, the input pins detected a logic low signal, and the
microcontroller turned off the LED by deactivating the output pin on Port D.

Output Figure:

Fig 2.2 : LED is ON when both switches are closed


fi
fi
fi
fi
fi
Fig 2.3 : LED is OFF when one switch is open and one is closed

Practical Uses:

• Controlling external devices such as LEDs, motors, and relays


• Reading input signals from sensors and switches
• Interfacing with communication modules such as UART, SPI, and I2C
• Implementing user interfaces with buttons and displays

Discussion :

During the experiment, we observed that when both switches were closed simultaneously, the
corresponding input pins on Port C detected a logic high signal. As a result, the microcontroller read
the input pins and activated the output pin on Port D, turning on the LED. Conversely, when either
or both switches were open, the input pins detected a logic low signal, and the microcontroller
deactivated the output pin on Port D, turning off the LED.

One important aspect that contributed to the stability and reliability of the circuit was the use of
pull-down resistors. These resistors were connected between the GPIO pins on Port C and ground
(GND). They ensured that the input pins remained at a logic low state when the switches were open,
preventing any floating or stray voltages that could potentially cause erratic behavior. This helped
maintain consistent and predictable operation of the circuit, enhancing its overall performance.
Conclusion:

In conclusion, the experiment gave us invaluable practical experience programming the Atmega32
microcontroller's I/O ports. Through GPIO pin con guration and the interface of external
components, including switches and LEDs, we were able to learn more about digital input and
output functions. Pull-up resistors were used to ensure consistent input readings, and the
microcontroller successfully managed the LED by monitoring the switches' states.

References:

[1]ATmega32 AVR MicroController - javatpoint,www.javatpoint.com. https://www.javatpoint.com/


atmega32-avr-microcontroller

[2]Machina, AVR basics: ports and direction registers - Machina Speculatrix,Machina Speculatrix,
Apr. 28, 2017. https://mans eld-devine.com/speculatrix/2017/04/avr-basics-ports-and-direction-
registers/

[3]David, WHAT IS A PULL-UP / PULL-DOWN RESISTOR,Soldered Electronics, Jun. 01, 2023.


https://soldered.com/learn/what-is-a-pull-up-pull-down-resistor/

[4]“AVR Atmega16 based Projects List - ATMega32 AVR | Atmega16 based Projects,” Projects
Tutorials Code Library for Atmels Atmega32 A VR, Aug. 02, 2023. https://atmega32-avr.com/avr-
atmega16-based-projects-list/#google_vignette
fi
fi

You might also like