用6位数码管显示时分秒
要C语言编的
没有其他要求
用6位数码管显示时分秒
要C语言编的
没有其他要求
下面的程序我写的时候把电路图弄丢了,你自己看程序可以知道电路图的
其实不同的电路图他的程序肯定不一样。你可以依据自己的电路图修改一下程序
希望可以帮到你
#include<reg52.h>
unsigned char t=0;
unsigned char second=0;
unsigned char tensec=0;
unsigned char minute=0;
unsigned char tenmin=0;
unsigned char hour=0;
unsigned char time[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void display();
void delay();
main()
{
TMOD=01;
TH0=0x3C;
TL0=0xB0;
ET0=1;
EA=1;
TR0=1;
while(1)
display();
}
inter0() interrupt 1 using 1
{
t++;
TH0=0x3C; TL0=0xB0;
if(t==20)
{t=0;
second++;
if(second==10)
{
second=0;
tensec++;
if(tensec==6)
{
tensec=0;
minute++;
if(minute==10)
{
minute=0;
tenmin++;
if(tenmin==6)
{
tenmin=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
}
}
}
void display()
{
P2=time[second];
P1=0x01;
delay();
P2=time[tensec];
P1=0x02;
delay();
P2=time[minute];
P1=0x04;
delay();
P2=time[tenmin];
P1=0x08;
delay();
P2=time[hour%10];
P1=0x10;
delay();
P2=time[hour/10];
P1=0x20;
delay();
}
void delay()
{
int i;
for(i=0;i<100;i++);
}
#include<reg51.h> #define uchar unsigned char #define uint unsigned int sbit key1=P1^0; sbit key2=P1^1; sbit key3=P1^2; sbit key4=P1^3; uchar miao,fen,shi; uchar miaog,miaos,feng,fens,shig,shis; uchar tt,key1num; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; uchar code table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void display(uchar shis,uchar shig,uchar fens,uchar feng,uchar miaos,uchar miaog) { miaos=miao/10; miaog=miao%10; fens=fen/10; feng=fen%10; shis=shi/10; shig=shi%10;
P3=table[shis]; P2=table1[0]; delay(1); P3=0x00;
P3=table[shig]; P2=table1[1]; delay(1); P3=0x00;//时
P3=0x40; P2=table1[2]; delay(1); P3=0x00;//-
P3=table[fens]; P2=table1[3]; delay(1); P3=0x00;
P3=table[feng]; P2=table1[4]; delay(1); P3=0x00;//分
P3=0x40; P2=table1[5]; delay(1); P3=0x00;//-
P3=table[miaos]; P2=table1[6]; delay(1); P3=0x00;
P3=table[miaog]; P2=table1[7]; delay(1); P3=0x00;//秒 } void keyscan() { if(key1==0) { delay(5); if(key1==0) { key1num++; while(!key1); if(key1num==1) { TR0=0; } if(key1num==2) { TR0=0; } if(key1num==3) { TR0=0; } if(key1num==4) { key1num=0; TR0=1;//开定时器 } }
} if(key1num!=0)//这个if的位置相当重要 { if(key2==0) //第二个键按下 { delay(5); //消陡 if(key2==0) { while(!key2); //key2断开时 if(key1num==1) { miao++; if(miao==60) miao=0; display(shis,shig,fens,feng,miaos,miaog); } if(key1num==2) { fen++; if(fen==60) fen=0; display(shis,shig,fens,feng,miaos,miaog); } if(key1num==3) { shi++; if(shi==24) shi=0; display(shis,shig,fens,feng,miaos,miaog); } } } if(key3==0) //第二个键按下 { delay(5); //消陡 if(key3==0) { while(!key3); //key2断开时 if(key1num==1) { miao--; if(miao==-1) miao=59; display(shis,shig,fens,feng,miaos,miaog); } if(key1num==2) { fen--; if(fen==-1) fen=59; display(shis,shig,fens,feng,miaos,miaog); } if(key1num==3) { shi--; if(shi==-1) shi=23; display(shis,shig,fens,feng,miaos,miaog); } } } } } void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; tt++; if(tt>=20) { tt=0; miao++; if(miao==60) { miao=0; fen++; if(fen==60) { fen=0; shi++; if(shi==24) shi=0; } } } } void init() { TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; shi=0; fen=0; miao=0; tt=0; } void main() { init(); while(1) { keyscan(); display(shis,shig,fens,feng,miaos,miaog);
} }
要用的话可以完全把显示“-”的删掉,程序里面,下面贴个图看看