7-Segment Display with 8051 (Auto Increment Every 1 Second)
This tutorial provides a detailed explanation of interfacing a common cathode 7-segment display with the 8051 microcontroller, including both:
- ✅ Software delay method
- ✅ Accurate Timer-based method (recommended)
📌 Learning Objectives
After this tutorial, you will understand:
- How a 7-segment display works
- Difference between common cathode vs anode
- How to generate digit patterns
- How to create 1-second delay
- How to use Timer0 for precise timing
🔌 What is a 7-Segment Display?
A 7-segment display consists of 7 LEDs (a–g) used to display numbers.
🖼️ Segment Layout
oaicite:0
⚙️ Types of 7-Segment Displays
1. Common Cathode (Used here)
- All cathodes connected to GND
- Segment ON → Logic 1
2. Common Anode
- All anodes connected to VCC
- Segment ON → Logic 0
🔗 Circuit Connections
Port Mapping
| Segment | 8051 Pin |
|---|---|
| a | P2.0 |
| b | P2.1 |
| c | P2.2 |
| d | P2.3 |
| e | P2.4 |
| f | P2.5 |
| g | P2.6 |
| dp | P2.7 |
Common Cathode
- Connect to GND
⚠️ Each segment must have a 220Ω resistor
🧠 Segment Code Table (Common Cathode)
| Digit | Binary | Hex |
|---|---|---|
| 0 | 00111111 | 0x3F |
| 1 | 00000110 | 0x06 |
| 2 | 01011011 | 0x5B |
| 3 | 01001111 | 0x4F |
| 4 | 01100110 | 0x66 |
| 5 | 01101101 | 0x6D |
| 6 | 01111101 | 0x7D |
| 7 | 00000111 | 0x07 |
| 8 | 01111111 | 0x7F |
| 9 | 01101111 | 0x6F |
⚙️ Method 1: Software Delay (Simple)
💻 Code
#include <reg51.h>
// Approx 1 second delay
void delay_1s()
{
int i, j;
for(i = 0; i < 1000; i++)
for(j = 0; j < 1275; j++);
}
void main()
{
unsigned char num[] = {0x3F,0x06,0x5B,0x4F,0x66,
0x6D,0x7D,0x07,0x7F,0x6F};
int i;
while(1)
{
for(i = 0; i < 10; i++)
{
P2 = num[i];
delay_1s();
}
}
}