51单片机时钟程序

51单片机时钟程序
51单片机时钟程序

#include

#include

sbit P32=P3^2;

sbit P23=P2^3;

sbit P22=P2^2;

sbit P21=P2^1;

sbit P20=P2^0;

unsigned char x;

unsigned char DisBuf[8]={0,0,0,0,0,0,0,0};

unsigned char DisBuf1[8]={0,0,0,0,0,0,0,0};

code unsigned char Tab[10]={0x81,0xed,0x43,0x49,0x2d,0x19,0x11,0xcd,0x01,0x09};

unsigned char timer0_count1=0,t1=0;

//unsigned char timer0_count3=0,t3=0;

unsigned char timer0_count2=0;

unsigned char Second=0,Minute=0;

unsigned int Year=17;

unsigned int Month=01;

unsigned int Day=02;

unsigned int Hour=14;

unsigned char a1=0;

unsigned int mond=1;//显示模式

unsigned int a=9;//倒计时初试时间

unsigned int b=60;

void mDelay(unsigned int delay)

{

unsigned char i;

for(;delay>0;delay--)

{

for(i=0;i<124;i++)

{

_nop_();

_nop_();

_nop_();

}

}

}

void timer0(void) interrupt 1

{

TH0=0xfc;TL0=0x66;

timer0_count2++;

if(timer0_count2>1000)

{

P32=!P32;

timer0_count2=0;

if(mond==4)

{

b--;

if(b<1)

{

b=60;

a--;

}

if(a==0&b==0)

{

mond=5;

}

}

Second++;

if(Second>59)

{

Second=0;

Minute++;

if(Minute>59)

{

Minute=0;

Hour++;

if(Hour>24)

{

Hour=0;

Day++;

if(Day>30)

{

Day=0;

Month++;

if(Month>12)

{

Month=0;

Year++;

}

}

}

}

}

DisBuf[0]=Minute/10; DisBuf[1]=Minute%10; DisBuf[2]=Second/10;

DisBuf[3]=Second%10;

DisBuf[4]=Day/10;

DisBuf[5]=Day%10;

DisBuf[6]=Hour/10;

DisBuf[7]=Hour%10;

DisBuf1[0]=Year/10;

DisBuf1[1]=Year%10;

DisBuf1[2]=Month/10;

DisBuf1[3]=Month%10;

DisBuf1[4]=a/10;

DisBuf1[5]=a%10;

DisBuf1[6]=b/10;

DisBuf1[7]=b%10;

}

if(mond==1)//分秒显示

{

timer0_count1++;

if(timer0_count1>5)

{

timer0_count1=0;

t1++;

if(t1>3) t1=0;

switch(t1)

{

case0:P0=Tab[DisBuf[0]];

P2=P2|0x80;P2=P2&0x8f;

break;

case1:P0=Tab[DisBuf[1]];

P2=P2|0x40;P2=P2&0x4f;

break;

case2:P0=Tab[DisBuf[2]];

P2=P2|0x20;P2=P2&0x2f;

break;

case3:P0=Tab[DisBuf[3]];

P2=P2|0x10;P2=P2&0x1f;

break;

default: break;

}

}

}

else

{

if(mond==2)//天时显示

{

timer0_count1++;

if(timer0_count1>5)

{

timer0_count1=0;

t1++;

if(t1>3) t1=0;

switch(t1)

{

case0: P0=Tab[DisBuf[4]];

P2=P2|0x80;P2=P2&0x8f;

break;

case1:P0=Tab[DisBuf[5]];

P2=P2|0x40;P2=P2&0x4f;

break;

case2:P0=Tab[DisBuf[6]];

P2=P2|0x20;P2=P2&0x2f;

break;

case3:P0=Tab[DisBuf[7]];

P2=P2|0x10;P2=P2&0x1f;

break;

default: break;

}

}

}

else

{

if(mond==3)//年月显示

{

timer0_count1++;

if(timer0_count1>5)

{

timer0_count1=0;

t1++;

if(t1>3) t1=0;

switch(t1)

{

case0:P0=Tab[DisBuf1[0]];

P2=P2|0x80;P2=P2&0x8f;

break;

case1:P0=Tab[DisBuf1[1]];

P2=P2|0x40;P2=P2&0x4f;

break;

case2:P0=Tab[DisBuf1[2]];

P2=P2|0x20;P2=P2&0x2f;

break;

case3:P0=Tab[DisBuf1[3]];

P2=P2|0x10;P2=P2&0x1f;

break;

default: break;

}

}

}

else

{

if(mond==4)//倒计时

{

timer0_count1++;

if(timer0_count1>5)

{

timer0_count1=0;

t1++;

if(t1>3) t1=0;

switch(t1)

{

case0:P0=Tab[DisBuf1[4]];

P2=P2|0x80;P2=P2&0x8f;

break;

case1:P0=Tab[DisBuf1[5]];

P2=P2|0x40;P2=P2&0x4f;

break;

case2:P0=Tab[DisBuf1[6]];

P2=P2|0x20;P2=P2&0x2f;

break;

case3:P0=Tab[DisBuf1[7]];

P2=P2|0x10;P2=P2&0x1f;

break;

default: break;

}

}

}

}

}

}

}

void main()

{

unsigned char Keynumber=0,Keytemp=0; TMOD=0x01;

TH0=0xfc;TL0=0x66;

TR0=1;

IE=0x82;

while(1)

{

P1&=0x0f;

if((P2&0x0f)!=0x0f) //有键按下

{

mDelay(5);

if(!P23)

{

P1&=0x7f;P1|=0x70;

if(!P23) Keynumber=1;

else

{

P1&=0xbf;P1|=0xb0;

if(!P23) Keynumber=2;

else

{

P1&=0xdf;P1|=0xd0;

if(!P23) Keynumber=3;

else

{

P1&=0xef;P1|=0xe0;

if(!P23) Keynumber=4;

else Keynumber=20;

}

}

}

}

else if(!P22)

{

P1&=0x7f;P1|=0x70;

if(!P22) Keynumber=5;

else

{

P1&=0xbf;P1|=0xb0;

if(!P22) Keynumber=6;

else

{

P1&=0xdf;P1|=0xd0;

if(!P22) Keynumber=7;

else

{

P1&=0xef;P1|=0xe0;

if(!P22) Keynumber=8;

else Keynumber=20;

}

}

}

}

else if(!P21)

{

P1&=0x7f;P1|=0x70;

if(!P21) Keynumber=9;

else

{

P1&=0xbf;P1|=0xb0;

if(!P21) Keynumber=10;

else

{

P1&=0xdf;P1|=0xd0;

if(!P21) Keynumber=11;

else

{

P1&=0xef;P1|=0xe0;

if(!P21) Keynumber=12;

else Keynumber=20;

}

}

}

}

else if(!P20)

{

P1&=0x7f;P1|=0x70;

if(!P20) Keynumber=13;

else

P1&=0xbf;P1|=0xb0;

if(!P20) Keynumber=14;

else

{

P1&=0xdf;P1|=0xd0;

if(!P20) Keynumber=15;

else

{

P1&=0xef;P1|=0xe0;

if(!P20) Keynumber=16;

else Keynumber=0;

}

}

}

}

else Keynumber=0;

}

else {Keynumber=Keytemp;}//无键按下if(Keynumber!=Keytemp)

{

Keytemp=Keynumber;

switch(Keynumber)

{

}

}

}

}

相关主题
相关文档
最新文档