Hi buddies! 👋
Today, let’s learn how to make an LED blink using the 8051 microcontroller. This is one of the most basic and essential experiments when you’re getting started with embedded systems.
🔌 What Is LED Blinking?
Blinking an LED simply means turning it ON and OFF repeatedly, with a delay in between.
To do this, we need to understand two key concepts:
- Port Configuration
- Delay Generation
⚙️ 1. Port Configuration
The 8051 microcontroller has 4 ports: PORT0, PORT1, PORT2, and PORT3.
Each port has 8 pins, so in total, you get 32 input/output (I/O) pins.
Important Note About PORT1:
Among these, only PORT1 has internal pull-up resistors by default.
For PORT0, external pull-up resistors are required because it’s an open collector.
Also, these ports support both:
- Byte addressable access (working with full 8-bit ports)
- Bit addressable access (working with individual bits)
Examples:
1 2 3 4 5 6 7 8 | // Clear a specific bit P1 &= ~(1 << bit_position); // Example: P1 &= ~(1 << 1); clears P1.1 // Set a specific bit P1 |= (1 << bit_position); // Example: P1 |= (1 << 1); sets P1.1 // Direct bit access P1_1 = 1; // Turns ON the pin P1.1 |
We’ll cover more on bit addressable operations in upcoming tutorials!
⏱ 2. Delay – What and Why?
A delay is a pause where the system does nothing. We need this to control the timing between turning the LED ON and OFF.
How to Create a Delay?
In Assembly, there’s a NOP()
instruction.
In C, we use _nop_()
from the header file.
But how much time does one _nop_()
take?
Assume Crystal Frequency = 11.0592 MHz
- Clock Period = 1 / 11.0592 MHz ≈ 0.09042 microseconds
- One Machine Cycle = 12 × Clock Period ≈ 1.085 microseconds
So,
_nop_(); // ≈ 1.085 microseconds delay
To get a 1-second delay, you would need:
1 sec / 1.085 µs ≈ 921,600 NOPs
That’s a lot! So we use nested loops instead.
💡 Solution: Nested Loops for Delay
1 2 3 4 5 6 7 8 | void delay_1s() { unsigned int i, j; for (i = 0; i < 240; i++) { for (j = 0; j < 384; j++) { _nop_(); // 1.085 µs delay } } } |
Delay Calculation:
- Inner Loop: 384 × 1.085 µs ≈ 416.6 µs
- Outer Loop: 240 × 416.6 µs ≈ 1 second
🔁 Final Step – Blinking the LED
Now that we understand ports and delays, let’s write the complete code to blink an LED connected to pin P1.1.
Note: Make sure you’ve installed the required software and set up the hardware. Links are in the video description!
✅ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <8051.h> #include "../library/Type.h" // Include if using a custom library void delay_1s() { unsigned int i, j; for (i = 0; i < 240; i++) { for (j = 0; j < 384; j++) { _nop_(); } } } void main() { while (1) { P1_1 = 1; // Turn ON the LED delay_1s(); // Wait 1 second P1_1 = 0; // Turn OFF the LED delay_1s(); // Wait again } } |
🎉 That’s It!
You’ve just written your first LED blinking program using the 8051 microcontroller!
This is a great foundation before moving on to more complex tasks like using timers, interrupts, and sensors.
Let me know in the comments if you have any questions, or if you’re curious why we specifically used the 11.0592 MHz crystal frequency!
Leave Your Reply
You must be logged in to post a comment.