Categories: Firmware Guide-8051

LED interfacing with 8051 microcontroller

A microcontroller, being an integrated circuit with a processor, contains support devices like program memory, data memory, I/O ports and serial communication interface integrated together. Since all the required support devices are available with the microcontroller, external interfacing of devices is not required.

One of the most popular microcontrollers is Intel 8051. It belongs to the MCS-51 family of Intel microcontrollers. Many companies later adopted the MCS-51 core to develop their own microcontrollers and all these devices could be operated with the MCS-51 instruction sets. The basic difference between these devices is in the size of the memory or in the presence of an ADC or DAC.

Being the most popular microcontroller, Intel 8051 is used in many days to day applications. It can be found in a vehicle’s music system, in an automatic door, in clock rooms to automate the systems and various other common applications. AT89S51 is a microcontroller coming from the family of 8051 microcontrollers. This chapter deals with how AT89S51 is successfully used in an LED blinking application.

LED Blinking-AT89S51

LED blinking is the most basic project with a microcontroller to see a physical output. One can understand the concept of input-output configurations of the general-purpose I/O port in a microcontroller with the simple LED blinking project. The LEDs are simple electronic display units available. This tutorial will explain the method of interfacing LED with 8051 microcontrollers and to develop a c code for blinking the same.

The AT89S51 microcontroller has 4 general purposes I/O ports which can be configured as input or output. Configuring the port pins as output, the state of the port pins can be controlled in firmware either high or low. When the port is configured as input, reading the pins will read the voltage state of the pins.We need to configure the port pins as output for a led blinking process.

Interfacing LED with Microcontroller Pin

The above image is the simple way of interfacing an LED with a microcontroller. The anode is connected to the microcontroller and cathode is connected to ground through a resistor. When the voltage at the controller pin is high the LED will be turned ON and when the voltage is low the LED will be turned off.

Firmware

The beginning of the code should include header file reg51.h which is common for 80C51 controllers. By default, the ports of 8051 microcontrollers are configured as the output.

#include<reg51.h>           // special function register declarations
                            // for the intended 8051 derivative

In the main function, the PORT in which LEDs are connected is loaded with 0x00 and 0xFF with a small delay in between. This part of the code is enclosed within an infinite loop for the continuous execution of the program.

void main (void)
{
    while(1)                // infinite loop
    {
        P3 = 0x00;            // LED ON
        Delay();
        P3 = 0xff;            // LED OFF
        Delay();
    }
}

The delay function at the end creates a nominal delay in blinking the LEDs. The accurate value of time delay could be calculated with the known value of instruction execution time but is not done here since it is beyond the scope of this article. Each time when this function is called in main a delay is generated.

void Delay(void)
{
    int j;
    int i;
    for(i=0;i<10;i++)
    {
        for(j=0;j<10000;j++)
        {
        }
    }
}

The delay function should be declared at the top before the main.

Circuit Diagram

Here is the circuit diagram of the AT89S51 interfaced 8 LEDs to Port3 of the Microcontroller. All the LEDs are driven through resistors for limiting current. In this microcontroller, pin source current is limited to 10mA per pin (please refer datasheet, IOL). So even if the resistor is not provided with LED, there will not be any damage to the LEDs.

Share