立即注册 登录
返回首页

uid:223372的个人空间

日志

给大家分享一个数码时钟程序

已有 773 次阅读2017-7-30 13:36 | 数字时钟

#include<reg51.h>

#define uchar unsigned char

#define uint unsigned int         

sbit s1=P2^0;                 

sbit s2=P2^1;

sbit s3=P2^2;

sbit s4=P2^3;

sbit s5=P2^4;

sbit s6=P2^5;

sbit s7=P2^6;

sbit k1=P1^0;                      //时间设置

sbit k2=P1^1;                       // 闹钟设置

sbit k3=P1^2;                     //定时开关设置

sbit k4=P1^3;                   //倒计时设置

sbit k5=P1^4;                   //时分秒

sbit k6=P1^5;                  //

sbit k7=P1^6;                   //

sbit k8=P1^7;                  //倒计时开关

 

 

uchar count=0,count1=0,a,b,c,sec,min,hour,x,y,z;flag=0,s=60,m=60,h=24;

uchar code mum[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

/*******延时程序*************/

void display();

void delayms(uint k)

{

 uint i,j;

 for(i=k;i>0;i--)

 for(j=110;j>0;j--);

}

/********显示子程序*******/

void display(uchar sz,uchar fz,uchar xh)

{

 if(((sec>=a)&&(min>=b)&&(hour>=c))||(k3==0))

 {

 s1=1;

 P0=mum[xh/10];

 delayms(5);

 s1=0;

 

 s2=1;

 P0=mum[xh%10]&0x7f;

 delayms(5);

 s2=0;

 

 s3=1;

 P0=mum[fz/10];

 delayms(5);

 s3=0;

 

 s4=1;

 P0=mum[fz%10]&0x7f;

 delayms(5);

 s4=0;

 

 s5=1;

 P0=mum[sz/10];

 delayms(5);

 s5=0;

 

 s6=1;

 P0=mum[sz%10];

 delayms(5);

 s6=0;

  }

}

/*******倒计时显示子程序**********/

void display1()

{ s1=1;

 P0=mum[z/10];

 delayms(5);

 s1=0;

 

 s2=1;

 P0=mum[z%10]&0x7f;

 delayms(5);

 s2=0;

 

 s3=1;

 P0=mum[y/10];

 delayms(5);

 s3=0;

 

 s4=1;

 P0=mum[y%10]&0x7f;

 delayms(5);

 s4=0;

 

 s5=1;

 P0=mum[x/10];

 delayms(5);

 s5=0;

 

 s6=1;

 P0=mum[x%10];

 delayms(5);

 s6=0;

}

/*********定时开关***************/

void time()

{

        if(k3==0)

        {

                delayms(10);              

                if(k3==0)

                {

                a=sec;b=min;c=hour;

                while(k3==0)         //开始定时开关设置

                {

                        display(a,b,c);

                        if(k5==0)

                        {

                                delayms(10);

                                if(k5==0)         

                                {

                                while(!k5);

                                flag++;

                                if(flag==3)        flag=0;

                                }

                        }

                        if(k6==0)                //加数

                                {

                                         delayms(10);

                                        if(k6==0)

                                        {

                                        while(!k6);

                                        switch(flag)

                                        {

                                        case 0:a++;if(a==60)a=0;break;

                                        case 1:b++;if(b==60)b=0;break;

                                        case 2:c++;if(c==24)c=0;break;      

                                        default :;                                              

                                        }

                                        }      

                                }

                        if(k7==0)                //减数

                                {

                                         delayms(10);

                                        if(k7==0)

                                        {

                                        while(!k7);

                                        switch(flag)

                                        {

                                        case 0:if(a>0)a--;

                                                        else        a=0;break;

                                        case 1:if(b>0)b--;

                                                        else        b=0;break;

                                        case 2:if(c>0)c--;

                                                        else        c=0;break;      

                                        default : ;

                                        }                                                                                                                                                }      

                                        }

                                }                                              

                        }

        }

 

}

/*******倒计时***********/

void dtime()

{

        if(k4==0)

        {

                delayms(10);              

                if(k4==0)

                {

                x=sec;y=min;z=hour;

                while(k4==0)         //开倒计时设置

                {

                        display1();

                        if(k5==0)

                        {

                                delayms(10);

                                if(k5==0)         

                                {

                                while(!k5);

                                flag++;

                                if(flag==3)        flag=0;

                                }

                        }

                        if(k6==0)                //加数

                                {

                                         delayms(10);

                                        if(k6==0)

                                        {

                                        while(!k6);

                                        switch(flag)

                                        {

                                        case 0:x++;if(x==60)x=0;break;

                                        case 1:y++;if(y==60)y=0;break;

                                        case 2:z++;if(z==24)z=0;break;      

                                        default :;                                              

                                        }

                                        }      

                                }

                        if(k7==0)                //减数

                                {

                                         delayms(10);

                                        if(k7==0)

                                        {

                                        while(!k7);

                                        switch(flag)

                                        {

                                        case 0:if(x>0)x--;

                                                        else        x=0;break;

                                        case 1:if(y>0)y--;

                                                        else        y=0;break;

                                        case 2:if(z>0)z--;

                                                        else        z=0;break;      

                                        default : ;

                                        }                                                                                                                                                }      

                                        }

                                }                                              

                        }

        }

 

}

void key()           //开始倒计时

{

    if(k8==0)

    {

    delayms(10);

    if(k8==0)

       {

       while(k8==0)

           {

           display1();

           TR1=1;

           if((x==0)&&(y==0)&&(z==0))

              {

              TR1=0;

              }

           }

       }

    }

}

/*****时间设置******/

void shijian_settime()

{

 uchar st,mt,ht;

 display(sec,min,hour);

        if(k1==0)

        {

                delayms(10);

                if(k1==0)

                {

                st=sec,mt=min,ht=hour;

                TR0=0;                 //关闭定时器

                while(k1==0)         //开始设置时间

                {

                        display(st,mt,ht);                      

                        if(k5==0)

                        {

                                delayms(10);

                                if(k5==0)         

                                {

                                while(!k5);

                                flag++;

                                if(flag==3)        flag=0;

                                }

                        }

                        if(k6==0)                //加数

                                {

                                         delayms(10);

                                        if(k6==0)

                                        {

                                        while(!k6);

                                        switch(flag)

                                        {

                                        case 0:st++;if(st==60)st=0;break;

                                        case 1:mt++;if(mt==60)mt=0;break;

                                        case 2:ht++;if(ht==24)ht=0;break;      

                                        default :;                                               

                                        }

                                        }      

                                }

                        if(k7==0)                //减数

                                {

                                         delayms(10);

                                        if(k7==0)

                                        {

                                        while(!k7);

                                        switch(flag)

                                        {

                                        case 0:if(st>0)st--;

                                                        else        st=0;break;

                                        case 1:if(mt>0)mt--;

                                                        else        mt=0;break;

                                        case 2:if(ht>0)ht--;

                                                        else        ht=0;break;      

                                        default : ;

                                        }                                                                                                                                      

                                        }      

                                }

                                }

                        TR0=1;

                        sec=st,min=mt,hour=ht;                                                      

                        }                                              

                }               

                display(sec,min,hour);

}

/********闹钟**********/

void naozhong_time()               

{

        if(k2==0)

        {

                delayms(10);              

                if(k2==0)

                {

                s=sec;m=min;h=hour;

                while(k2==0)         //开始设置闹钟

                {

                        display(s,m,h);

                        if(k5==0)

                        {

                                delayms(10);

                                if(k5==0)         

                                {

                                while(!k5);

                                flag++;

                                if(flag==3)        flag=0;

                                }

                        }

                        if(k6==0)                //加数

                                {

                                         delayms(10);

                                        if(k6==0)

                                        {

                                        while(!k6);

                                        switch(flag)

                                        {

                                        case 0:s++;if(s==60)s=0;break;

                                        case 1:m++;if(m==60)m=0;break;

                                        case 2:h++;if(h==24)h=0;break;      

                                        default :;                                              

                                        }

                                        }      

                                }

                        if(k7==0)                //减数

                                {

                                         delayms(10);

                                        if(k7==0)

                                        {

                                        while(!k7);

                                        switch(flag)

                                        {

                                        case 0:if(s>0)s--;

                                                        else        s=0;break;

                                        case 1:if(m>0)m--;

                                                        else        m=0;break;

                                        case 2:if(h>0)h--;

                                                        else        h=0;break;      

                                        default : ;

                                        }                                                                                                                                                }      

                                        }

                                }                                              

                        }

        }

}

void naozhong()

{

 if((hour==h)&&(min==m)&&(sec==s))    //判断时间是否达到

 {

  s7=1;

  delayms(5000);

  s7=0;

  }

}

/******主程序*******/                                            

void main()

  TMOD=0x11;

  TH0=(65536-45872)/256;

  TL0=(65536-45872)/256;

  TH1=(65536-45872)/256;               

  TL1=(65536-45872)%256;

  EA=1;

  ET0=1;

  TR0=1;

  ET1=1;

  TR1=0;

  s7=0;

  while(1)

  {

  shijian_settime();     //设置时间

  naozhong_time();          //闹钟设置

  naozhong();            //闹钟程序

  time();                  //定时开关设置时间

  dtime();                 //倒计时设置时间

  key();                 //倒计时开关

  }

}

/*******中断子程序*********/

void T0_ms() interrupt 1          //中断入口程序

{

        TH0=(65536-45872)/256;                //重新装初值

        TL0=(65536-45872)%256;

        count++;

        if(count==20)                 //判定1s时间是否已到

        {

                count=0;

                sec++;

                if(sec==60)

                {

                 sec=0;

                 min++;

                if(min==60)

                {

                 min=0;

                 hour++;

                if(hour==24)

                 {

                  hour=0;

 }

 }

 }

        }

}

void T1_ms() interrupt 3          //中断入口程序

{

        TH1=(65536-45872)/256;                //重新装初值

        TL1=(65536-45872)%256;

        count1++;

        if(count1==20)                 //判定1s时间是否已到

        {

                count1=0;

 

 if(x!=0)

 {

 x--;

 }

 if((y!=0)&&(x==0))

 {

 x=59;

 y--;

 }

     if((z!=0)&&(y==0)&&(x==0))

 {

 z--;

 y=59;

 x=59;

 }

  display1();

   }    

}


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

返回顶部