Blink LED using AVR Atmega32A Microcontroller

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: ControlSensingCommunication.

learn-embedded-system

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,

  1. Adjusting port pin of controller as input or output.
  2. Determine their state of output.
  3. 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).
led-blinking-atmega32a-tutorial
LED Blinking with ATmega32A

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.

Program LED Blinky:

/*
 * Project Name: Blinky.c
 * Created: 19-06-2013 22:41:35
 * Author: UMESH
 */ 

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

int main(void)
{
  DDRB |= 1 << PINB0;
  while (1)
  {
    PORTB |= 1 << PINB0;
    _delay_ms(1000);
    PORTB &= ~(1 << PINB0);
    _delay_ms(1000);
  }
}
Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

Login

Register | Lost your password?