#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功茄掘能:利用定时器,产生秒计数器,使数码管显示时间和年月日,还有颤让核跑表。
05-02-01
*/
#include <reg51.h>
//LCD的口线
sbit E=P3^5
sbit RW=P3^6
sbit RS=P3^7
//8字滑渗LED的连接方法
//P0.7,P0.6,P0.5,P0.4,P0.3,P0.2,P0.1,P0.0
//G, F A E B H C D
sbit l1=P2^7
sbit l2=P2^6
sbit l3=P2^5
sbit l4=P2^4
sbit l5=P2^3
sbit l6=P2^2
sbit l7=P2^1
sbit l8=P2^0
sbit j1=P1^1
sbit j2=P1^2
unsigned char code ledcode[]={0x84,0xf5,0x46,0x54,0x35,0x1c,0x0c,0xd5,
0x04,0x14,0x05,0x2c,0x8e,0x64,0x0e,0x0f}//数码管段码表
unsigned char ms,s,min,hour,t0100ms,t0101ms,t0sm,s0,s1,mi0,mi1,h0,h1,dsm,dsh,dsm0,dsm1,dsh0,dsh1
unsigned int year,mouch,date,y3,y2,y1,y0,m1,m0,d1,d0,leap,bz,bz1,bz0,pb0,pb1,pb2,pb00,pb10,pb20,pb,cs
void time0(void)
void delay()
void main(void)
{
E=0 //取消LCD对LED的影响
RW=0
RS=1
year=2009, mouch=10, date=18,leap=0
y3=0, y2=0, y1=0, y0=0
m1=0, m0=0, d1=0, d0=0
bz=0,bz1=0,bz0=0
pb0=0,pb1=0,pb2=0,cs=1
ms=0,min=30,hour=12,s=50
s0=0x00,s1=0x00
mi0=0,mi1=0,h0=0,h1=0
t0100ms=0x14
t0101ms=0x14
t0sm=0x01 //从第一个LED开始显示
TH0=0xee
TL0=0x00
TMOD=0x01
TR0=1
IE=0x82
while(1)
{ if((year%4==0&&year%100!=0)||(year%400==0)) //判断是平年还是闰年
{leap=1}
else
{leap=2}
do
{
if(j1==0){delay(), bz++}
if(bz==8){bz=0}
switch(bz)
{ case 0:{ bz1=0} break
case 1:{ bz1=0if(j2==0){delay(), min++}} break
case 2:{ bz1=0if(j2==0){delay(), hour++}} break
case 3:{ bz1=1if(j2==0){delay(), date++}} break
case 4:{ bz1=1if(j2==0){delay(),mouch++}} break
case 5:{ bz1=1if(j2==0){delay(), year++}} break
case 6:{ bz1=2if(j2==0){delay(), dsm++}} break
case 7:{ bz1=2if(j2==0){delay(), dsh++}} break
default:break}
}
while(bz)
do
{
if(j2==0) {delay(),bz0++}
if(bz0==5){bz0=0}
switch(bz0)
{ case 0:{ bz1=0} break
case 1:{ bz1=1if(j1==0){delay(),bz0=0}} break
case 2:{ bz1=3,pb=0,pb0=0,pb1=0,pb2=0} break
case 3:{ bz1=3if(j1==0){cs=2,pb00=pb0,pb10=pb1,pb20=pb2}} break
case 4:{ bz1=3if(j1==0){pb=1}} break
// case 2:{ bz1=0if(j2==0){delay(), hour++}} break
// case 3:{ bz1=1if(j2==0){delay(), date++}} break
// case 4:{ bz1=1if(j2==0){delay(),mouch++}} break
// case 5:{ bz1=1if(j2==0){delay(), year++}} break
default:break}
}
while(bz0)
}
}
////////////////////////////////////
void delay()
{
unsigned int i,j,k
for( i=10i>0i--)
for( j=20j>0j--)
for(k=100k>0k--)
}
///////////////////////////////////////
void time0(void) interrupt 1
{
TH0=0xee,TL0=0x00
t0100ms--
if(t0100ms==0x00){ t0100ms=20,ms++
if(ms==10) { ms=0,s++
if(s==60){ s=0,min++,m0=6
if(min==60) { min=0,hour++
if(hour==24) { hour=0,date++
//*******************************************************************************
switch(mouch)
{ case 1:{if(date==32){date=0,mouch++}}break
case 2:{if(leap==1&&date==30){date=0,mouch++}else{if(leap==2&&date==29){date=0,mouch++}}}break
case 3:{if(date==32){date=0,mouch++}}break
case 4:{if(date==31){date=0,mouch++}}break
case 5:{if(date==32){date=0,mouch++}}break
case 6:{if(date==31){date=0,mouch++}}break
case 7:{if(date==32){date=0,mouch++}}break
case 8:{if(date==32){date=0,mouch++}}break
case 9:{if(date==31){date=0,mouch++}}break
case 10:{if(date==32){date=0,mouch++}}break
case 11:{if(date==31){date=0,mouch++}}break
case 12:{if(date==32){date=0,mouch++}}break
case 13:{mouch=1,year++}break
default: break }
}
}
}
}
}
if(min==60) {min=0}
if(hour==24){hour=0}
if((mouch==1||mouch==3||mouch==5||mouch==7||mouch==8||mouch==10||mouch==12)&&date==32){date=0}
if((mouch==4||mouch==6||mouch==9||mouch==11)&&date==31){date=0}
if(mouch==2&&leap==1&&date==30){date=0}
if(mouch==2&&leap==2&&date==29){date=0}
if(mouch==13){mouch=1}
s0=s%10,s1=s/10%10
mi0=min%10,mi1=min/10%10
h0=hour%10,h1=hour/10%10
d0=date%10,d1=date/10%10
m0=mouch%10,m1=mouch/10%10
y0=year%10,y1=year/10%10,y2=year/100%10,y3=year/1000
dsm0=dsm%10,dsm1=dsm/10%10
dsh0=dsh%10,dsh1=dsh/10%10
if(dsm==60){dsm=0}
if(dsh==24){dsh=0}
//**********************************************************************
if(bz0==4)
{}
else
if(bz0==3)
{ TH0=0xee,TL0=0x00
t0101ms--
if(t0101ms==0x00){ t0101ms=20,pb0++
if( pb0==10) {pb0=0,pb1++
if(pb1==10) {pb1=0,pb2++
if(pb2==10) {pb2=0}
}
}
}
}
/////////////////////////////////////////////////////////////////////////
t0sm++
if(t0sm==9){t0sm=1}
P0=0xff
if(bz1==0)
{switch(t0sm)
{ case 1:{P2=0xff,l1=0P0=ledcode[ms]} break
case 2:{P2=0xff,l2=0P0=ledcode[s0]&0xfb} break
case 3:{P2=0xff,l3=0P0=ledcode[s1]} break
case 4:{P2=0xff,l4=0P0=ledcode[mi0]&0xfb}break
case 5:{if(bz==1){P2=0xff,l5=0P0=ledcode[mi1]&0xfb}else{P2=0xff,l5=0P0=ledcode[mi1]}}break
case 6:{P2=0xff,l6=0P0=ledcode[h0]&0xfb} break
case 7:{if(bz==2){P2=0xff,l7=0P0=ledcode[h1]&0xfb}else{P2=0xff,l7=0P0=ledcode[h1]}}break
case 8:{P2=0xff,l8=0P0=ledcode[bz]&0xfb} break
default:break}
}
if(bz1==1)
{switch(t0sm)
{ case 1:{if(bz==3){P2=0xff,l1=0P0=ledcode[d0]&0xfb}else{P2=0xff,l1=0P0=ledcode[d0]}} break
case 2:{if(bz==3){P2=0xff,l2=0P0=ledcode[d1]&0xfb}else{P2=0xff,l2=0P0=ledcode[d1]}} break
case 3:{P2=0xff,l3=0P0=ledcode[m0]&0xfb} break
case 4:{if(bz==4){P2=0xff,l4=0P0=ledcode[m1]&0xfb}else{P2=0xff,l4=0P0=ledcode[m1]}} break
case 5:{P2=0xff,l5=0P0=ledcode[y0]&0xfb} break
case 6:{if(bz==5){P2=0xff,l6=0P0=ledcode[y1]&0xfb}else{P2=0xff,l6=0P0=ledcode[y1]}} break
case 7:{if(bz==5){P2=0xff,l7=0P0=ledcode[y2]&0xfb}else{P2=0xff,l7=0P0=ledcode[y2]}} break
case 8:{if(bz==5){P2=0xff,l8=0P0=ledcode[y3]&0xfb}else{P2=0xff,l8=0P0=ledcode[y3]}} break
default:break}
}
if(bz1==2)
{switch(t0sm)
{ case 1:{P2=0xff} break
case 2:{P2=0xff} break
case 3:{P2=0xff} break
case 4:{if(bz!=6){P2=0xff,l4=0P0=ledcode[dsm0]}else{P2=0xff,l4=0P0=ledcode[dsm0]&0xfb}}break
case 5:{if(bz!=6){P2=0xff,l5=0P0=ledcode[dsm1]}else{P2=0xff,l5=0P0=ledcode[dsm1]&0xfb}}break
case 6:{P2=0xff,l6=0P0=ledcode[dsh0]&0xfb} break
case 7:{if(bz!=7){P2=0xff,l7=0P0=ledcode[dsh1]}else{P2=0xff,l7=0P0=ledcode[dsh1]&0xfb}}break
case 8:{P2=0xff} break
default:break}
}
if(bz1==3)
{switch(t0sm)
{ case 1:{if(pb==1){P2=0xff,l1=0P0=ledcode[pb00]}else{P2=0xff,l1=0P0=ledcode[pb0]}}break
case 2:{if(pb==1){P2=0xff,l2=0P0=ledcode[pb10]&0xfb}else{P2=0xff,l2=0P0=ledcode[pb1]&0xfb}} break
case 3:{if(pb==1){P2=0xff,l3=0P0=ledcode[pb2]}else{P2=0xff,l3=0P0=ledcode[pb2]}} break
case 4:{P2=0xff}break
case 5:{P2=0xff,l5=0P0=ledcode[cs]}break
case 6:{P2=0xff}break
case 7:{P2=0xff}break
case 8:{P2=0xff}break
default:break}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)