Hi buddies!
In this tutorial, let’s learn how to create a simple 0–9 counter using a 7-segment display and an 8051 microcontroller. If you’re just getting started with embedded systems, this is a perfect project to understand digital output and counters!
🧠 What is a 7-Segment Display?
A 7-segment display contains 7 individual LEDs arranged to form numbers from 0 to 9. These LEDs are named from a
to g
, and there’s sometimes an additional dot (dp
) to show decimal points.
In our earlier tutorial, we saw how to turn a single LED on and off. Now, we’ll control 7 LEDs together in a specific pattern to display numbers!
🔌 Types of 7-Segment Displays
There are two types:
- Common Anode (CA): All anodes (+) of the LEDs are connected together. You need to provide LOW (0V) to light up individual segments.
- Common Cathode (CC): All cathodes (–) are connected together. You need to provide HIGH (5V) to light them up.
In this project, we’ll use Common Anode (CA) for better brightness, since the 8051 ports have internal pull-ups that limit brightness when using CC.
🧩 Segment Mapping & Logic
Each segment (a
to g
) must be turned on/off according to the number we want to display. For example:
- To display
0
: segmentsa, b, c, d, e, f
ON,g
OFF. - To display
1
: onlyb
andc
ON.
You can derive this using K-maps, but there’s a much easier method.
🗂 Using a Lookup Table
Instead of complex logic equations, we use a lookup table – a simple array of hex values, where each value represents the ON/OFF pattern of segments for digits 0–9.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Common Anode 7-segment display values (0–9) code unsigned char lookUpTable[10] = { 0xC0, // 0 0xF9, // 1 0xA4, // 2 0xB0, // 3 0x99, // 4 0x92, // 5 0x82, // 6 0xF8, // 7 0x80, // 8 0x90 // 9 }; |
Here, code
tells the compiler to store the data in Flash memory, saving RAM space.
🧪 Circuit Description
- Microcontroller: Buddy51 Mini
- Display: Common Anode 7-segment
- Resistors: 330Ω for current limiting
- Connections:
- Segments
a–g
connected to P0.0–P0.6 - Common pin connected to Vcc (+5V)
- Segments
Note: We avoid using CC displays here because internal pull-up resistors limit brightness.
💻 Code Explanation
Let’s walk through the main parts of the code:
- Declare a variable
count
to store the current number. - On key press (connected to a pin), increment
count
. - If
count > 9
, reset to 0. - Use the lookup table to send the segment pattern to
PORT0
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <8052.h> code unsigned char lookUpTable[10] = { 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90 }; unsigned char count = 0; void delay() { int i, j; for (i = 0; i < 100; i++) for (j = 0; j < 1275; j++); } void main() { while (1) { if (P1_0 == 0) { // Assume switch connected to P1.0 delay(); // Debounce delay count++; if (count > 9) count = 0; P0 = lookUpTable[count]; } } } |
🧯 Testing the Output
Once the code is flashed to the microcontroller:
- Press the button → the number increments.
- After 9, it resets to 0.
- The 7-segment display shows the count in real-time!
🤔 Got Questions?
If you have any doubts or want a deep dive into how lookup tables work or how to connect the hardware, feel free to comment below or check out our video tutorial on this same topic.
Thanks for reading – Happy Learning!
Team BuddyKit 🚀
Leave Your Reply
You must be logged in to post a comment.