Skip to main content

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

Segment8051 Pin
aP2.0
bP2.1
cP2.2
dP2.3
eP2.4
fP2.5
gP2.6
dpP2.7

Common Cathode

  • Connect to GND

⚠️ Each segment must have a 220Ω resistor


🧠 Segment Code Table (Common Cathode)

DigitBinaryHex
0001111110x3F
1000001100x06
2010110110x5B
3010011110x4F
4011001100x66
5011011010x6D
6011111010x7D
7000001110x07
8011111110x7F
9011011110x6F

⚙️ 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();
}
}
}