Blinking LEDs like 8 led blinking are one of most common tasks. Once you learned how to blink single LED with 8051 microcontrollers in Assembly language. You may want to experiment with different ways to write 8 led blinking program. This is a good practice to grip your grounds in Assembly language programming and 8051 microcontrollers.

Previous Single LED Blinking Recap

Before we start diving into blinking the multiple LEDs with 8051 microcontroller which in today’s case 8 LEDs. Let’s recap our previous 8051 LED blinking program where we use delay to blink a single LED with CPL command. So we had one delay function which we called in our main loop. We will extend that program to blink 8 LEDs.

In our previous post we explained what a delay function is and how to implement a delay function in assembly language for 8051 microcontrollers. In this post we will use the same delay function but will use different patterns and algorithms to write 8 LED blinking program in assembly language for 8051 microcontrollers.

Prepare Your Hardware

Before we dive into the colorful world of LED blinking, let’s gather our squad of hardware buddies. Here’s what you’ll need for this electrifying adventure:

  1. 8051 Microcontroller: Meet the brain of our operation! This little chip is going to make the magic happen.
  2. LEDs (Light-Emitting Diodes): Get ready to shine bright like a diamond! LEDs are our stars in this show. They come in various colors, so pick your favorites and let them sparkle.
  3. Resistors: Think of resistors as the guardians of your LEDs. They make sure your LEDs don’t get too much power and burn out. Safety first, right?
  4. Breadboard: Ah, the trusty breadboard! It’s like a playground for your components. With the breadboard, you can easily connect and disconnect parts without any soldering. It’s like building a LEGO masterpiece but with electronics.

Now that we have all the basic components we need to add some power source and oscillators to give the heart beat to the microcontroller. Ready to add some serious power and magic to your LED fiesta? Buckle up because we’ve got some more awesome components joining our party. Here are the new recruits for our electronic adventure:

  1. Power Supply: Our trusty power supplier! Think of it as the energy drink for our circuit. It provides the juice that makes everything come to life. Without it, our LEDs would be as lively as a snoozing cat!
  2. 11.0592MHz Crystal: Meet the heartbeat of our microcontroller. This crystal oscillator sets the rhythm, making sure our microcontroller dances to the right tunes. It’s like the DJ of our electronic party, spinning the coolest tracks.
  3. 33pF Capacitors (with Crystal): These capacitors are like the chill pills for our crystal. They help stabilize the oscillations and keep everything in harmony. Think of them as the peacemakers in our electronic realm.
  4. 10k Resistor and 10uF Capacitor: Ah, the power on reset circuit dream team! Together, they ensure our microcontroller starts up smoothly, without any glitches. It’s like having a superhero duo guarding the entrance to our LED wonderland.
  5. Push Button (Connected to Pin Number 9): Our reset button superstar! This button gives you the power to reset your microcontroller whenever you want. It’s like having a magical ‘do-over’ button in your favorite video game.

Schematic Diagram for 8051 microcontroller

Now, picture this: you’ve got your 8051 microcontroller, your LEDs winking at you, resistors ensuring everyone behaves, and a breadboard as your creative canvas. Exciting, isn’t it?

But wait, there’s more! To make things even better, we’ve prepared a special treasure map, a.k.a. schematic diagram, to guide you through connecting these components. It’s like having GPS for your electronics journey, ensuring you won’t get lost in the sea of wires.

t’s time to unveil the secrets of our LED wonderland! Grab your magnifying glasses and let’s dissect the magical realm we’re about to create. Here’s the breakdown of our circuit diagram, where every component has a special role to play:

8051 microcontroller circuit for 8 LED blinking program
  1. 8051 Microcontroller (Our Brainy Conductor): At the heart of our circuit sits the 8051 microcontroller, ready to conduct the symphony of LEDs. It’s like the master puppeteer, orchestrating every blink and sparkle.
  2. P0 Pins (LEDs’ Home): Picture the P0 pins as the stage where our LEDs perform. Each P0 pin is connected to an LED, giving them a home to shine their brightest. It’s where the real magic happens!
  3. LEDs (Shining Stars of the Show!): Ah, our shining stars! Connected to the P0 pins, these LEDs are the soul of our circuit. Each LED adds a burst of color to our LED wonderland, lighting up the room with their vibrant glow.
  4. 10k Pull-up Resistors (The Guardians): Meet the guardians of stability! Each P0 pin is accompanied by a 10k pull-up resistor. These resistors ensure that the P0 pins stay at a known voltage level, preventing any unwanted flickering and keeping our LEDs steady and reliable.
  5. Power On Reset Circuit (The Circuit Keeper): The power on reset circuit, consisting of a 10k resistor and a 10uF capacitor, ensures our microcontroller starts up smoothly. It’s like the keeper of the gates, ensuring a grand entrance for our LED extravaganza.
  6. Crystal Oscillator and 33pF Capacitors (The Rhythm Makers): The crystal oscillator, along with its loyal 33pF capacitors, sets the heartbeat of our microcontroller. Think of it as the conductor’s baton, dictating the pace at which our LEDs blink, creating a mesmerizing rhythm.
  7. Reset Button (The Magic Resetter): Last but not least, our reset button connected to Pin Number 9! This magical button gives you the power to reset the entire show whenever you desire. It’s like a secret trapdoor to restart the LED wonderland, allowing you to enjoy the magic over and over again.

And there you have it, folks! Every component plays a vital role in our LED wonderland, creating a harmonious and dazzling display of lights. Now that you understand the magic behind the curtain.

So, grab your components, put on your creative hats, and get ready to light up the world with your LED masterpiece! 🚀✨

Reason for Choosing P0

The reason for choosing the P0 over the other ports is pretty simple. If you look on the datasheet of the 8051 microcontroller and go down to the Port Pins circuit diagram you will see the reason why P0 is a good choice to drive high current loads. Here is the diagram below for the reference.

And that is the reason for adding a 10k pull up resistors to every pin of the P0 in 8051 microcontrollers. You can also use 10K resistors pack SIP which I am using as well.

Blinking Algorithms in Assembly Language

Now the fun part of writing assembly language program begins. We have to write a code to blink the LEDs interfaced with 8051 microcontrollers in assembly language. There could be many combinations and patterns to blink are possible. We will take care of few most basic and necessary patterns and algorithms for blinking the LEDs.

Binary Counting

In this method, you represent the LED states in binary and count through the binary numbers. Each LED corresponds to a bit in the binary number. For example, you can count from 0000 0001 to 1000 0000 and repeat the sequence.

START: MOV A, P0      ; Read from Port 1 (get current LED status)
       INC A          ; Increment A-register (binary counting)
       MOV P0, A      ; Output A back to Port 1
       ACALL DELAY3    ; Call delay subroutine
       SJMP START     ; Jump back to START (loop)
DELAY3:
 MOV  R7,#45
D3L1: MOV  R6,#255
D3L2: MOV  R5,#255
 
 DJNZ R5,$
      DJNZ R6,D3L2
 DJNZ R7,D3L1
 RET
;-----------------------------------------------
END
       <a href="DELAY3:
 MOV  R7,#45
D3L1: MOV  R6,#255
D3L2: MOV  R5,#255
 
 DJNZ R5,$
      DJNZ R6,D3L2
 DJNZ R7,D3L1
 RET
;-----------------------------------------------
END">
</a>Code language: PHP (php)

Sequential 8 LED Blinking

In this algorithm we blink the Leds in sequence. We can accomplish this by different ways but I am going to show you the simple technique which is to write a sequence on the port and then take it back into the Accumulator register (ACC) register of 8051 and simply apply the instruction of rotate. Here is the example code which will do this task.

ORG 00H
;--------------------------------------------------------
START: MOV A, #01H    ; Initialize A-register with 0000 0001
       MOV P0, A      ; Output A to Port 1 (turn on LED 1)
       ACALL DELAY3    ; Call delay subroutine
       MOV A, P0      ; Read from Port 1 (get current LED status)
       RLC A          ; Rotate A-register left through carry
       MOV P0, A      ; Output A back to Port 1
       ACALL DELAY3    ; Call delay subroutine
       SJMP START     ; Jump back to START (loop)
       
;-------------------------------------------

DELAY3:
 MOV  R7,#45
D3L1: MOV  R6,#255
D3L2: MOV  R5,#255
 
 DJNZ R5,$
      DJNZ R6,D3L2
 DJNZ R7,D3L1
 RET
;-----------------------------------------------
END
;-------------------------------------------------
Code language: PHP (php)

Alternate Blinking

Make LEDs on even and odd pins blink alternately, creating a rhythmic and symmetrical pattern.

START: MOV A, #0AAH    ; Initialize A-register with 1010 1010 (even pins)
       MOV P1, A       ; Output A to Port 1 (turn on even LEDs)
       ACALL DELAY     ; Call delay subroutine
       MOV A, #055H    ; Initialize A-register with 0101 0101 (odd pins)
       MOV P1, A       ; Output A to Port 1 (turn on odd LEDs)
       ACALL DELAY     ; Call delay subroutine
       SJMP START      ; Jump back to START (loop)
Code language: PHP (php)

Chasing Pattern

Create a chasing effect where LEDs turn on and off one after the other, resembling lights moving along a line.

START: MOV A, P1       ; Read from Port 1 (get current LED status)
       RLC A           ; Rotate A-register left through carry
       MOV P1, A       ; Output A back to Port 1
       ACALL DELAY     ; Call delay subroutine
       SJMP START      ; Jump back to START (loop)
Code language: JavaScript (javascript)

Random Blinking

Generate random patterns by toggling LEDs in a random sequence. Use a random number generator to select LEDs to turn on and off, creating a dynamic and unpredictable blinking effect.

START: ACALL RANDOM     ; Call random number generator subroutine
       MOV A, P0       ; Read from Port 1 (get current LED status)
       XOR A, R1       ; XOR with a random number to toggle specific LED
       MOV P0, A       ; Output A back to Port 1
       ACALL DELAY3     ; Call delay subroutine
       SJMP START      ; Jump back to START (loop)

RANDOM: RLC R2         ; Rotate R2 register left through carry
        MOV A, R2      ; Move R2 to A-register (contains random bit)
        JNB ACC.7, RANDOM ; If ACC.7 is not set, jump back (to rotate more)
        RET            ; Return from subroutine (ACC.7 is set, return)
Code language: JavaScript (javascript)

Random Number Generation in Assembly

Creating a random number generator routine in assembly language for an 8051 microcontroller involves utilizing some sort of entropy to introduce randomness. One common approach is to use an XOR shift random number generator, which involves shifting bits and performing XOR operations to generate random-like sequences. Here’s an example of how you could implement an XOR shift random number generator subroutine:

RANDOM: MOV A, R2     ; Move R2 to A-register
        XOR A, R3     ; XOR A with R3 (or any other register for randomness)
        MOV R3, R2    ; Move R2 back to R3 for the next iteration
        RLC A         ; Rotate A left through carry
        MOV R2, A     ; Move A back to R2 for the next iteration
        RET           ; Return from subroutine
Code language: PHP (php)

In this example, R2 and R3 are used as working registers. Before calling the RANDOM subroutine, make sure to initialize R3 with a non-zero value to introduce some initial entropy.

Further Readings

Here are some more tutorials links which you may like to consider for further readings about the topic.

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.