Microcontroller
Microcontroller
Objectives :
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].
The port input register (PINx) is used for reading input values[2].
Required Components :
Code:
#include <avr/io.h>
#include <util/delay.h>
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:
Practical Uses:
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:
[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/
[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