Home > Microcontroller >Interface Circuits > How to display numbers on 7 segment display using 8051 microcontroller?

How to display numbers on 7 segment display using 8051 microcontroller?

Source: InternetPublisher:张小哥 Keywords: Display 8051 microcontroller Updated: 2025/02/14

When using a single-chip microcomputer to develop a project, a 7-segment display is often used. The 7-segment display can display letters and numbers. It actually has a light-emitting LED inside. By controlling different interfaces, the internal LED lights are lit to display numbers. At present, it is used to display information in many electronic products, such as power banks, hair straighteners, etc.

The 7-segment display is constructed of 8 LEDs connected in a sequential manner to facilitate displaying the digits from 0 to 9 when certain combinations of LEDs are turned on. When lit, it displays only one digit at a time.

Schematic diagram of displaying numbers on a 7-segment display using an 8051 microcontroller:

embedded-system-7-segment-display1.png

The program to display numbers from "0 to F" on a 7-segment display is:

#include< reg51.h > 
sbit a= P3^0; 
sbit x = P3^1;
sbit y = P3^2;
sbit z = P3^3;
void main()
{
unsigned char m[10]={0x40,0xF9,0x24,0x30,0x19,0x12,0x02,0xF8,0xE00,0x10};
unsigned int i,j;
a=x=y=z=1;
while(1)
{
for(i=0;i<10;i++)
{
P2=m[i];
for(j=0;j<60000;j++);
}
}
}
The program to display numbers from "00 to 10" on a 7-segment display is:

#include< reg51.h >
sbit x = P3^0;
sbit y = P3^1;
void display1();
void display2();
void delay();
void main()
{
unsigned char m[10]={0x40,0xF9,0x24,0x30,0x19,0x12,0x02,0xF8,0xE00,0x10};
unsigned int i,j;
ds1=ds2=0;
while(1)
{
for(i=0,i<20;i++)
display1();
display2();
}
}
void display1()
{
x=1;
y=0;
P2=m[ds1];
delay();
x=1;
y=0;
P2=m[ds1];
delay();
}
void display2()
{
ds1++;
if(ds1 >=10)
{
ds1=0;
ds2++;
if(ds2 >=10)
{
ds1=ds2=0;
}
}
}
void delay()
{
unsigned int k;
for(k=0;k<30000;k++);
}

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号