51单片机 编写4个LED数码管同时循环显示0—9的数字 的程序。

使用C语言即可

#include<reg51.h>
//-----------------------------------------------
void delay(unsigned int i) //延时
{
unsigned char j;
while(i--) for(j = 0; j < 115; j++);
}
//-----------------------------------------------
void main(void)
{
unsigned char n1;
unsigned char code SEG[] = {
0xc0, 0xf9, 0xa4, 0xb0, 0x99, //0-4
0x92, 0x82, 0xf8, 0x80, 0x90};//5-9
while(1) {
P0 = SEG[n1]; //P0口,外接共阳数码管的段选端
delay(2000);
n1++; if(n1 > 9) n1 = 0;
}
}

4个(任意个)共阳数码管的段选端,接在 P0,其位选,都接电源即可。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-13
#include<reg51.h>

#define uchar unsigned char
uchar code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//0-9
uchar time,scanled,tiao;
uchar disdat[6]="201803";
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void t0isr() interrupt 1 //秒计时
{
TH0=0x3c;
TL0=0xb0;
time++;
if(time==20)
{
time=0;
tiao++;
tiao%=10;
}
}

void t1isr() interrupt 3 //显示
{
TH1=0xec;
TL1=0x78;
P2=1<<scanled;
P0=~ledtab[tiao];
scanled++;
scanled%=4;
}
main()
{
TMOD=0x11;
TH0=0x3c;
TL0=0xb0;
TH1=0xec;
TL1=0x78;
TR1=1;
TR0=1;
ET0=1;
ET1=1;
EA=1;
while(1);
}

本回答被网友采纳
相似回答