BLINKING – 8051

Photo of author

By Jackson Taylor

Home » Electronics Topics » 8051 Microcontroller » BLINKING – 8051

As an Amazon Associate we earn from qualifying purchases.

Blinking an LED is the first program most people run on a new microcontroller. It proves that the chip, the clock, the compiler and the wiring all work together. This article shows how to blink LEDs on an 8051-family microcontroller (the AT89S52) using the MikroC compiler, and how to test the result in Proteus before building anything on a breadboard.

The example turns all the LEDs on the four ports on and off every half second. Once it works, you can change it into chasing patterns, single-LED blinkers or anything else that uses the same idea.

What you need

  • MICROCONTROLLER – AT89S52
  • COMPILER – MIKROC
  • SIMULATION – PROTEUS 8.1
  • CRYSTAL – 4MHz

You will also need LEDs and a current-limiting resistor for each one. A value in the range of a few hundred ohms is typical for a standard LED on a 5 V supply. The AT89S51 is very similar, and the same code works on it.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

DOWNLOAD SIMULATION FILES

CIRCUIT

The circuit uses the AT89S52 with a 4 MHz crystal and its two small load capacitors connected to the XTAL pins. LEDs are connected to the pins of Port 0, Port 1, Port 2 and Port 3. The video below shows the simulation running.

Proteus circuit of an AT89S52 microcontroller with 4 MHz crystal and LEDs on ports P0 to P3

Why the LEDs are active low

In this circuit the LEDs light when a pin is driven to 0 and go dark when it is driven to 1. That is why the code writes 0x00 to turn the LEDs on and 0xFF to turn them off.

This is a common arrangement on the 8051. Its port pins can sink current (pull it to ground) more strongly than they can source it, so the LED is usually connected between the supply voltage and the pin, with a resistor in series. When the pin goes low, current flows through the LED to the pin.

See also
AC LAMP BLINKING USING MSP430 AND TRIAC

If you wire your LEDs the other way round, from the pin to ground, the logic flips: 0xFF turns them on and 0x00 turns them off. Also note that Port 0 has no internal pull-ups, so it only works as a true output when the LED is connected to the supply, as described above, or when you add external pull-up resistors.

PROGRAM


/*
MICROCONTROLLER -AT89S51/52
COMPILER ---MIKROC
BY---EMERGING TECHNOLOGIES --WWW.EMERGINGTECHS.ORG
*/
void main() {
    while(1)
    {
        P0 = 0x00;        // Turn ON diodes on PORT0
        P1 = 0x00;        // Turn ON diodes on PORT1
        P2 = 0x00;        // Turn ON diodes on PORT2
        P3 = 0x00;        // Turn ON diodes on PORT3
        Delay_ms(500);   // 500 ms delay
        P0 = 0xFF;        // Turn OFF diodes on PORT0
        P1 = 0xFF;        // Turn OFF diodes on PORT1
        P2 = 0xFF;        // Turn OFF diodes on PORT2
        P3 = 0xFF;        // Turn OFF diodes on PORT3
        Delay_ms(500);   // 500 ms delay
    }               // Endless loop
}

The original comments on the delay lines said “1 second”, but each Delay_ms(500) call waits half a second. One full on-and-off cycle therefore takes one second.

How the code works

  1. void main() is where the program starts. A microcontroller program never finishes, so the main work happens inside a loop.
  2. while(1) creates an endless loop. The LEDs keep blinking for as long as the chip has power.
  3. P0, P1, P2, P3 are the port registers. Each one holds 8 bits, and each bit controls one pin. Writing a value to the register changes all eight pins at once.
  4. Delay_ms(500) is a MikroC library routine that pauses the program for 500 milliseconds. Without it the LEDs would switch too fast to see.

Setting up MikroC and Proteus

In MikroC

Create a new project, select the AT89S52 as the device and set the clock to 4.000 MHz so it matches the crystal. The delay routine is calculated from this value, so a wrong setting gives a wrong blink time. Build the project to produce a HEX file.

See also
8051 - Digital Output

In Proteus

Place the AT89S52, the LEDs and their resistors, then double-click the microcontroller. Load the HEX file into the program file field and set the clock frequency to 4 MHz. Run the simulation and the LEDs should switch every 500 ms. The simulation files linked above already contain this setup.

Moving to real hardware

Simulation hides a few details that matter on a breadboard or PCB:

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
  • Use a stable 5 V supply and connect the power pins properly.
  • Add a working reset circuit. If you are unsure how to size the parts, see how to work out the reset circuit capacitor for the 8051.
  • Fit a resistor in series with every LED. Never connect an LED directly to a pin.
  • Keep the total current drawn by all LEDs within the limits in the datasheet. Lighting all 32 pins at once draws far more current than lighting one.

Simple variations

To blink a single port, remove the lines for the other ports. To make one port toggle without writing the values twice, you can invert it on each pass:


void main() {
    P1 = 0xFF;
    while(1)
    {
        P1 = ~P1;        // Invert all pins of PORT1
        Delay_ms(500);
    }
}

To make a running light, write patterns such as 0xFE, 0xFD, 0xFB and so on to one port, with a delay between each. This is a good way to practise the basics of 8051 digital output before moving on to inputs. The page on 8051 digital output covers that further.

What to learn next

After blinking, the usual next steps are reading an input and showing text. You can connect a switch to the 8051 and change the blink pattern with it, or connect an LCD to display messages. If you also work with Arduino boards, the same idea is covered in LED blinking on an Arduino, which is a useful comparison.

FAQ

Why do the LEDs turn on when I write 0x00?

Because they are wired active low. The LED is connected to the supply, so it lights when the pin pulls to ground. If your LEDs are wired from the pin to ground, swap 0x00 and 0xFF.

See also
8051 (AT89S52/51) UART SIMULATION IN PROTEUS

Why is my blink rate different from 500 ms?

Check that the clock frequency in the MikroC project and in Proteus matches the crystal, which is 4 MHz here. A mismatch changes the delay timing.

Can I use the AT89S51 instead of the AT89S52?

Yes. For this program the two chips behave the same, and the code needs no changes.

Is Delay_ms the best way to time a blink?

It is the simplest way and fine for a first project. The processor does nothing else while it waits, so for programs that must do other work at the same time, a hardware timer is a better choice.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works