单片机:四位数码管按秒计时:0001 0002 。。。。9998 9999

#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define CF P0
uint sec=0;
sbit K1=P3^2;
sbit K2=P3^1;
sbit K3=P3^0;
sbit LED1=P2^0;
sbit LED2=P2^1;
sbit LED3=P2^2;
sbit LED4=P2^3;
uchar disbuff[]={0,0,0,0};
uchar const num[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay(int s)
{
    uint i,j;
    for(i=0;i<s;i++)
        for(j=114;j>0;j--);
    }
void DigDisplay()
{
    LED1=1;LED2=1;LED3=1;LED4=0;
    CF=disbuff[0];
    delay(5);
    CF=0X00;
    LED1=1;LED2=1;LED3=0;LED4=1;
    CF=disbuff[1];
    delay(5);
    LED1=1;LED2=0;LED3=1;LED4=1;
    CF=disbuff[2];
    delay(5);
    CF=0X00;
    LED1=0;LED2=1;LED3=1;LED4=1;
    CF=disbuff[3];
    delay(5);
    CF=0X00;
    
}
void Count()
{
        disbuff[0]=num[sec%10];
        disbuff[1]=num[sec/10%10];
        disbuff[2]=num[sec/100%10];
        disbuff[3]=num[sec/1000%10];
    
}
void KEY()
{
        if(K1==0)
            delay(10);
        if(K1==0)
        {
            TR0=1;
        }
        while(!K1)
        {
            DigDisplay();
            Count();
        }
        if(K2==0)
        {
                delay(5);
                if(K2==0)
                {    
                    TR0=0;
                    
                }
                while(!K2)
                {DigDisplay();
                Count();}
                if(K3==0)
                {
                    delay(10);
                    if(K3==0)
                    {
                            sec=0;
                            TH0=0X3C;
                            TL0=0Xb0;
                        
                    }
                        
                }
                
            
        }
    
    
}
void main()
{
        TMOD=0X11;
        TH0=0X3C;
        TL0=0xb0;
        ET0=1;
        EA=1;
        TR0=1;
      while(1)
        {
                Count();
                DigDisplay();
                KEY();
            
        }
    
    
}
void time1()interrupt 1
{
        uchar n;
        TH0=0X3C;
        TL0=0XB0;
        n++;
    if(n==20)
    {
        n=0;
        sec++;
        if(sec==10000)
        {
            sec=0;
        }
        
        
    }
    
    
}

 

posted @ 2021-07-28 21:43  myrj  阅读(346)  评论(0编辑  收藏  举报