Software Section

                                                                                                                             <Back to Topics

Simple Program to Blink an LED (using embedded C language)        

Before we go in detail of how to blink an LED(light emitting diode) with microcontroller, let us have a brief  introduction on GPIO (general purpose I/O (input / Output)).

GPIO Introduction

The GPIO line of any microcontroller can be used mainly to perform two things

1. Generate a digital signal

2. Read a digital signal

The digital signal can be of TTL / CMOS logic depending up on the microcontroller .In general, the microcontrollers are provided with GPIO lines except for some microcontrollers which are very application specific and may not have GPIO lines.

 

The process of controlling GPIO lines for most of the controllers are similar in nature. There are mainly two configurations for any GPIO

  • GPIO configured as an input
  • GPIO configured as an output
  •  The GPIO which is configured as input can read a digital signal from an external device, the external device can be a battery charge indicator, signal from a sensor or any device   which you intend to use in performing certain functions. Same way  when you wish to generate a digital signal then the GPIO will be configured as an output and the GPIO output can generate a digital signal, which can be fed to an external device.

    Let us see an example to understand a practical application of a GPIO.

    Example: Design an indicator for a battery charger using GPIO

    Now let us assume that there is a circuitry which gives a digital signal to microcontroller when ever the battery is charged. So, microcontroller reads the digital signal from the battery charger  and then generates a digital signal to blink an LED indicator.

     

    Configuring GPIO

    The configuration of GPIO for various microcontrollers have almost similar procedure, but the procedure is not completely same for all microcontrollers. There may be few more steps to be considered, depending on the microcontroller. In this document the common configuration procedure is explained.

     

    Steps to generate a digital high using GPIO

    Step 1: Configure the GPIO bit direction register as output

    Step 2: Set the GPIO bit data/port register as high

     

    Steps to read  digital signal using GPIO

    Step 1: Configure the GPIO bit direction register as input

    Step 2: Read the GPIO bit data/port register

     

    Example program to blink an LED(Code implemented with  microcontroller atmega8535)

    #include <io.h>

    /* This program is compiled using  WINAVR platform*/

    /* Assumptions

       PORT B0 pin of the microcontroller atmega8535 is connected to an LED circuit */

    int  main()

    {

    unsigned int i =0;

    /* Configure the GPIO B0 bit direction register as output */

    DDRB = 0x01;

     

    while(1)

      {

       

      for (i=0;i<50000;i++)

        {

        /* Set the GPIO bit data/port  register as low */

        PORTB = 0x00;

        }

      for (i=0;i<50000;i++)

        {

        /* Set the GPIO bit data/port  register as HIGH */

        PORTB = 0x01;

        }

      }

    return 0;

    }

    /* End of Program */

     

    Result

      LED blinks continuously as long as the microcontroller is powered ON.     

    Best Viewed in Internet Explorer

    With other browsers you may experience improper view

    List of Topics --> Software

    Simple Program to Blink an LED (using embedded C language)

    Code to Control a Stepper Motor

    Code to Detect Obstacle using IR(Infrared) Sensor

    Articles on C

    Data types (under construction)

     

    List of Topics --> Electronics

    Controlling  a Stepper Motor

    Drive a Stepper Motor (using IC STA401A)

    Detecting obstacle with IR (Infrared) Sensor

    Use Motor as Dynamo, Motor as a Brake, Dynamometer Test

     

    [Home] [Robotics] [Electronics] [Software] [Contact Me]

    Special thanks to
    website design software