51单片机之闹钟制作

来源:互联网 发布:淘宝发安能物流好恶心 编辑:程序博客网 时间:2024/05/01 06:38
#include<reg51.h>


sbit Key0=P2^0; // 控制开关
sbit Key1=P2^1;   //控制调节的变量
sbit Key2=P2^2; // 增加
sbit Key3=P2^3;   // 减少


unsigned char code Dx[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                         0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};


 unsigned char code Wx[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};


 unsigned  char playdata[8]={0};


   char min=0;
 char ms=0;
   char h=0; 
 void main()
 {
  unsigned int i;
unsigned  char xuanzhe=0;
unsigned char j;
    EA=1;
    TMOD=0x10;
TH1=0xB8;
TL1=0x00;
ET1=1;
TR1=1;
while(1)
{
      if(!Key0)
 {
    for(i=1000;i>0;i--);
if(!Key0)
{
     EA=~EA;
 while(!Key0);
 }
 }
 if(!Key1)
 {
    for(i=1000;i>0;i--);
if(!Key1)
{
     xuanzhe++;
 while(!Key1);
 }
 if(xuanzhe>3)
 xuanzhe=0;
 }
 if(!Key2)
 {
    for(i=1000;i>0;i--);
if(!Key2)
{
     switch(xuanzhe)
 {
    case 1:
{
ms++;
if(ms>=60)
{
    ms=0;
min++;
if(min>=60)
{
   min=0;
h++;
if(h>=24)
h=0;
}
}
break;
}


 case 2:
 {
 min++;
 if(min>=60)
 {
    min=0;h++;
if(h>=24)
h=0;
 }
 break;
 }
  case 3:
  {
  h++;
  if(h>=24)
   h=0;
  break;
  }
 }


 while(!Key2);
 }
 }


 if(!Key3)
 {
    for(i=1000;i>0;i--);
if(!Key3)
{
     switch(xuanzhe)
 {
    case 1:
{
 ms--;
if(ms<=0)
{
ms=59;
min--;
if(min<=0)
{
  
  min=59;
  h--;
  if(h<=0)
  {
   h=23;
}

}

}
break;
}
 case 2:
 {
  min--;
 if(min<0)
 {
   h--;
if(h<0)
h=23;
 }
 break;
 }
  case 3:
  {
  h--;
  if(h<0)
  h=23;
  break;
  }
 }
 while(!Key3);
}
}
  playdata[0]=Dx[h/10];
playdata[1]=Dx[h%10];
playdata[2]=0x80;
playdata[3]=Dx[min/10];
playdata[4]=Dx[min%10];
playdata[5]=0x80;
playdata[6]=Dx[ms/10];
playdata[7]=Dx[ms%10];
for(i=0;i<8;i++)
{
     P0=0x00;
    P0=playdata[i];
             P1=Wx[i];
j=200;
while(j--);
 


}


}
 }
 void Timer1()   interrupt 3
 {
    static  unsigned char cnt=0;
    TH1=0xB8;
TL1=0x00;
cnt++;
if(cnt>=50)
{
  cnt=0;
  ms++;
  if(ms>=60)
  {
     ms=0;
 min++;
 if(min>=60)
 {
    min=0;
h++;
if(h>=24)
h=0;
 }
  }
}
 }


 
1 0
原创粉丝点击