8051 – Digital Output

Photo of author

By Jackson Taylor

Home » Hardware » 8051 – Digital Output

As an Amazon Associate we earn from qualifying purchases.

Digital output is the first thing most people learn on the 8051 microcontroller. You write a 1 or a 0 to a port pin, and the pin goes high (close to the supply voltage) or low (close to ground). With an LED, a transistor or a relay driver on that pin, you can switch real hardware on and off from code.

This article shows how digital output works on the AT89S52, how to wire an LED, and how a simple Port 1 blink program written in mikroC PRO works, line by line.

Setup used in this example

  • Compiler: mikroC PRO for 8051
  • Microcontroller: AT89S52
  • Crystal frequency: 11.0592 MHz

In the mikroC PRO project settings, select the AT89S52 and set the clock to 11.0592 MHz. The compiler uses this value for its delay routines, so a wrong setting makes the delays too long or too short.

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

How digital output works on the 8051

The AT89S52 is a classic 8051-family chip with four 8-bit ports: P0, P1, P2 and P3. Each port has eight pins, which gives up to 32 I/O lines. Each port is also available as a byte-wide register in your code, so writing P1 = 0xFF; sets all eight pins of Port 1 at once.

If you are new to this chip family, the overview of the 8051 microcontroller covers the basic architecture.

Port 1 is the easiest port to start with

P1, P2 and P3 have internal pull-up resistors, so they can drive a pin high without extra parts. P1 has no special function in the basic 8051 role, so it is a good choice for a first output test. On the AT89S52, P1.0 and P1.1 also have alternate functions (timer 2 pins), and P1.5 to P1.7 are used for in-system programming. They still work as ordinary outputs, but this can matter if you program the chip in-circuit.

See also
UART - 8051

Port 0 needs pull-up resistors

Port 0 is different. Its pins are open-drain, which means they can pull a line low but cannot drive it high on their own. If you use P0 for output, add external pull-up resistors (commonly around 10 kΩ) to the supply. This is a common reason for an LED that refuses to light on P0.

Sinking and sourcing current

8051 pins are much better at sinking current (pulling low) than sourcing it (driving high). For that reason, many 8051 circuits connect the LED between the supply and the pin, so the LED lights when the pin is low. Wiring from the pin to ground through a resistor can work, but the high-level drive is weak. Always check the current limits in the AT89S52 datasheet before connecting anything larger than a small LED. For motors, buzzers or relays, use a transistor or driver IC.

Wiring an LED to Port 1

For a basic test you need the AT89S52 with a working power supply, the 11.0592 MHz crystal with its two small capacitors, and a reset circuit. The reset circuit is easy to get wrong, and a chip that does not reset properly will seem dead.

  1. Connect the LED anode to the supply through a current-limiting resistor (a few hundred ohms is typical for a standard LED at 5 V), or connect the resistor to the pin if you use the high-drive wiring.
  2. Connect the other LED leg to a Port 1 pin, such as P1.0 (for the sinking arrangement).
  3. Repeat for as many pins as you want, each with its own resistor. Never share one resistor between several LEDs.
  4. Make sure the EA pin is tied to the supply when you run code from the internal flash.

Note that with the active-low wiring, a 1 turns the LED off and a 0 turns it on. The program below still blinks, but the on and off phases are swapped.

See also
H-BRIDGE USING RELAY SIMULATION

Blinking LEDs on Port 1

This program flashes all eight pins of Port 1 together:

void main()
{
    while(1)
    {
        P1 = 0xFF;
        delay_ms(100);
        P1 = 0;
        delay_ms(100);
    }
}

What each line does

  • void main() is the entry point. The program starts here after reset.
  • while(1) creates an endless loop, which is how nearly all microcontroller programs run.
  • P1 = 0xFF; writes binary 11111111 to Port 1, so all eight pins go high.
  • delay_ms(100); waits 100 milliseconds using the mikroC library routine.
  • P1 = 0; writes binary 00000000, so all eight pins go low.

The LEDs stay on for 100 ms, then off for 100 ms. One full cycle takes about 200 ms, so they blink around five times per second. Increase the delay values for a slower, easier-to-see blink.

The original version of this code used a lowercase p1 and had a double semicolon after the first assignment. mikroC is case-sensitive, and its 8051 port registers are written in capitals (P1), so use that form. The extra semicolon is harmless but unnecessary.

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

Controlling a single pin

Often you only want to change one pin and leave the rest alone. Writing to the whole port overwrites all eight bits. In mikroC you can declare a name for a single pin and write to it directly:

sbit LED at P1_0_bit;

void main()
{
    while(1)
    {
        LED = 1;
        delay_ms(500);
        LED = 0;
        delay_ms(500);
    }
}

This makes the code easier to read, because LED says more than P1.0. It also lets you use other pins on the same port for different jobs without disturbing them.

Other ways to use a port as output

Once the blink works, try a few variations to learn how the port register behaves:

  • Write 0xAA and 0x55 alternately to flash every other LED.
  • Shift a single bit through the port to make a chasing light.
  • Use a counter and write its value to the port to display binary numbers on the LEDs.
See also
RFID INTERFACING WITH 8051 (HARDWARE AND SIMULATION)

These small exercises build the habit of thinking in bits, which you need for everything that follows, from blinking an LED with timers to driving displays.

Common problems

  • Nothing happens: check power, the crystal, reset and the EA pin first. A chip that is not running will never reach your code.
  • LED always on or always off: the LED may be wired the opposite way to what you expect. Check whether your circuit is active-high or active-low.
  • Blink speed is wrong: the clock setting in mikroC does not match the real crystal.
  • LEDs on P0 stay dark: P0 needs external pull-up resistors.
  • LED is dim or the pin misbehaves: the current is too high or the resistor is missing. Check the datasheet limits.

What to learn next

Digital output is half of basic I/O. The other half is input, such as reading a switch to decide when an output should change. After that, you can use the same port techniques to connect an LCD or scan a keypad. If you work with other chip families, the idea is the same, for example in MSP430 digital output, though register names and pin behavior differ.

FAQ

What does P1 = 0xFF do on the 8051?

It writes the value 255 (binary 11111111) to Port 1, which sets all eight pins P1.0 to P1.7 high. Writing 0 sets them all low.

Why does the crystal frequency matter for a delay?

The 8051 divides the crystal frequency to create its machine cycle, and delay routines count those cycles. If the frequency in your compiler settings differs from the real crystal, the delay will be wrong.

Can I drive a relay or motor directly from an 8051 pin?

No. The pin cannot supply enough current. Use a transistor, MOSFET or driver IC between the pin and the load, and add a flyback diode for inductive loads such as relays and motors.

See also
RESISTORS

Why does Port 0 need pull-up resistors?

Port 0 pins are open-drain. They can pull a line low but cannot pull it high by themselves, so an external resistor to the supply is needed to produce a high level.

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