#define uchar unsigned char
uchar code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}//0-9
unsigned char sec=0,min=0,hour=12,scanled
unsigned char key,flashbit,mode,time
unsigned char disdat[8]
sbit led=P1^0
void delay(unsigned int x)
{
unsigned int i,j
for(i=0i<xi++)
for(j=0j<120j++)
}
void dischg()
{
disdat[0]=sec%10
disdat[1]=sec/10
disdat[2]=10
disdat[3]=min%10
disdat[4]=min/10
disdat[5]=10
disdat[6]=hour%10
disdat[7]=hour/10
}
void flash()
{
switch(flashbit)
{
case 0:break
case 1:
disdat[6]=0x10
disdat[7]=0x10
delay(50)
dischg()
delay(80)
break
case 2:
disdat[3]=0x10
disdat[4]=0x10
delay(50)
dischg()
delay(80)
break
case 3:
disdat[0]=0x10
disdat[1]=0x10
delay(50)
dischg()
delay(80)
break
default:break
}
}
void t0isr() interrupt 1 //秒计时
{
TH0=0x3c
TL0=0xb0
time++
switch(mode)
{
case 0:
if(time==20)
{
time=0
sec++
if(sec>59)
{
sec=0
min++
if(min>59)
{
min=0
hour++
if(hour>23)hour=0
}
}
}
break
case 1:
if(time==20)
{
time=0
if(sec>0)sec--
else if(min>0){sec=59min--}
else if(hour>0){sec=59min=59hour--}
else {hour=0min=0sec=0}
}
break
}
dischg()
}
void t1isr() interrupt 3 //显示
{
TH1=0xec
TL1=0x78
switch(scanled)
{
case 0:
P2=0x01
P0=~ledtab[disdat[7]]
break
case 1:
P2=0x02
P0=~ledtab[disdat[6]]
break
case 2:
P2=0x04
P0=~ledtab[disdat[5]]
break
case 3:
P2=0x08
P0=~ledtab[disdat[4]]
break
case 4:
P2=0x10
P0=~ledtab[disdat[3]]
break
case 5:
P2=0x20
P0=~ledtab[disdat[2]]
break
case 6:
P2=0x40
P0=~ledtab[disdat[1]]
break
case 7:
P2=0x80
P0=~ledtab[disdat[0]]
break
default:break
}
scanled++
scanled%=8
}
main()
{
TMOD=0x11
TH0=0x3c
TL0=0xb0
TH1=0xec
TL1=0x78
TR1=1
TR0=1
ET0=1
ET1=1
EA=1
sec=55
min=59
hour=23
flashbit=0
scanled=0
time=0
mode=0
dischg()
while(1)
{
flash()//闪烁
if((P3&0x0f)!=0x0f){
key=P3&0x0f
while((P3&0x0f)!=0x0f)
led=0
delay(10)
key|=0xf0
switch(~key)
{
case 0x01: //p3.1选择调时、分、秒
TR0=0
flashbit+=1
if(flashbit>3){flashbit=0TR0=1}
break
case 0x02: //p3.2调数
if(flashbit==0)break
if(flashbit==1)
{
hour++
if(hour>99)hour=0
}
if(flashbit==2)
{
min++
if(min>59)min=0
}
if(flashbit==3)
{
sec++
if(sec>59)sec=0
}
break
case 0x04: //选择正/倒
TR0=0
mode++
mode&=0x01
if(mode==0){sec=0min=0hour=0}
dischg()
break
case 0x08: //启动/暂停
TR0=~TR0
break
default:break
}
}
}
}
这个是简单的数码管时钟显示如果有需要,我写过比较完善的1602显示时钟.显示控制年月日等等.#include<reg51.h>sbit Begin=P2^0
sbit Hour=P2^1
sbit Mus=P2^2
sbit End=P2^3
unsigned char code Tab[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40}
unsigned char code num[]={ 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}
unsigned char Time[]={0,0,16,0,0,16,0,0}
unsigned char a
unsigned int x,m,th
void init()
{
TMOD=0x01
TH0=(65535/50000)/256
TL0=(65535/50000)%256
EA=1
ET0=1
TR0=1
}
void delay(unsigned int z)
{
unsigned int x,y
for(x=zx>0x--)
for(y=110y>0y--)
}
unsigned char keyboard()
{
if(Begin==0){
delay(5)
if(Begin==0)
return 1
}
if(Hour==0){
delay(30)
if(Hour==0)
return 2
}
if(Mus==0)
return 3
if(End==0)
return 4
}
void display()
{
unsigned char i for(i=0i<8i++)
{
P3=num[i]
P0=Tab[Time[i]]
delay(5)
}
}
void main()
{
unsigned char Key
init()
Key=keyboard()
if(Key==1)
{
while(1){
Key=keyboard()
display()
if(Key==2)
x+=3600
if(Key==3)
x+=1
if(Key==4)
return
}
}}
void holes() interrupt 1 using 2
{
TH0=(65535/50000)/256
TL0=(65535/50000)%256
a++
if(a>=20)
{
x++
m=x
th=m/3600//设置小时
Time[0]=th/10
Time[1]=th%10
m=m%3600 Time[2]=16 th=m/60//设置分钟
Time[3]=th/10
Time[4]=th%10
m=m%60 Time[5]=16 th=m//设置秒
Time[6]=th/10
Time[7]=th%10 a=0
}
}
#include "reg52.h"#define uint unsigned int
#define uchar unsigned char
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}
uchar shi,fen,miao
uchar time
void delay(uint x)
{
uint y
for(x>0x--)
{
for(y=0y<124y++)
}
}
void display(uchar shi,uchar fen,uchar miao)
{
P2=0 //位码
P0=(tab[shi/10]) //段码
delay(2)
P2=1
P0=(tab[shi%10])
delay(2)
P2=2 //位码
P0=0x40 //段码
delay(2)
P2=3 //位码
P0=(tab[fen/10]) //段码
delay(2)
P2=4
P0=(tab[fen%10])
delay(2)
P2=5 //位码
P0=0x40 //段码
delay(2)
P2=6 //位码
P0=(tab[miao/10]) //段码
delay(2)
P2=7
P0=(tab[miao%10])
delay(2)
}
void main()
{
TMOD=0x01
TH0=(65536-50000)/256
TL0=(65536-50000)%256
EA=1
ET0=1
TR0=1
while(1)
{
if(time==20)
{
time=0
miao++
if(miao==60)
{
miao=0
fen++
if(fen==60)
{
fen=0
shi++
if(shi==24)
shi=0
}
}
}
display(shi,fen,miao)
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256
TL0=(65536-50000)%256
time++
}
/*还有什么不明白继续追加*/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)