微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 前辈们帮我看看这个定时倒计时程序吧

前辈们帮我看看这个定时倒计时程序吧

时间:10-02 整理:3721RD 点击:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit kaishi=P3^4;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code tablemin[]={
0xbf,0x86,0xdb,0xcf,
0xe6,0xed,0xfd,0x87,
0xff,0xef};
uchar t0,t1,SEC,sec1,sec2,min1,min2,temp,num,a,b;
uchar keyscan();
void init();
void putin();
void display1(uchar bb,uchar aa);
void display(uchar sec1,uchar sec2,uchar min1,uchar min2);
void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}
void main()
{
   init();
   putin();
  while(1)
  {
        if(t0==3)
        {
            t0=0;
                SEC--;
            if(SEC==-1)
                {
                   SEC=59;
                   min1--;
                   if(min1==-1)
                   {
                             min2--;
                          min1=9;
                   }
                }
                  sec1=SEC%10;
                  sec2=SEC/10;

         }
          display(sec1,sec2,min1,min2);
  }
}
void init()
{
  dula=1;         
  P0=0x3f;
  dula=0;
  P0=0xff;
  wela=1;
  P0=0x0f;
  wela=0;
  TMOD=0x11;
  TH0=(65536-50000)/256;
  TL0=(65536-50000)%256;
  TH1=(65536-5000)/256;
  TL1=(65536-5000)%256;
  EA=1;
  ET0=1;
  TR0=1;
  ET1=1;
  TR1=1;
  SEC=0;

}
void display1(uchar bb,uchar aa)
{
          dula=1;
          P0=tablemin[bb];
          dula=0;
          P0=0xff;
          wela=1;
          P0=0xdf;
          wela=0;
          delay(10);
          dula=1;
          P0=table[aa];
          dula=0;
          P0=0xff;
          wela=1;
          P0=0xef;
          wela=0;
          delay(10);
}
void display(uchar sec1,uchar sec2,uchar min1,uchar min2)
{

          dula=1;
          P0=table[sec1];
          dula=0;
          P0=0xff;
          wela=1;
          P0=0x7f;
          wela=0;
          delay(2);
          dula=1;
          P0=table[sec2];
          dula=0;
          P0=0xff;
          wela=1;
          P0=0xbf;
          wela=0;
          delay(2);
          dula=1;
          P0=tablemin[min1];
          dula=0;
          P0=0xff;
          wela=1;
          P0=0xdf;
          wela=0;
          delay(2);
          dula=1;
          P0=table[min2];
          dula=0;
          P0=0xff;
          wela=1;
          P0=0xef;
          wela=0;
          delay(2);
}

void timer0() interrupt 1
{
        TH0=(65536-50000)/256;
        TL0=(65536-50000)%256;
        t0++;
}
void timer1() interrupt 3
{
        TH1=(65536-5000)/256;
        TL1=(65536-5000)%256;
        t1++;
}
uchar keyscan()
{
                        P3=0xfe;
                        temp=P3;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                                {
                                        delay(5);
                                        temp=P3;
                                        temp=temp&0xf0;
                                        while(temp!=0xf0)
                                        {
                                                temp=P3;
                                        switch(temp)
                                                {
                                                        case 0xee:num=1;
                                                                break;
                                                        case 0xde:num=2;
                                                                break;
                                                        case 0xbe:num=3;
                                                                break;
                                                }
                                        while(temp!=0xf0)
                                                {
                                                        temp=P3;
                                                        temp=temp&0xf0;
                                                }
                                        }
                                }
                        P3=0xfd;
                        temp=P3;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                                {
                                        delay(5);
                                        temp=P3;
                                        temp=temp&0xf0;
                                        while(temp!=0xf0)
                                        {
                                                temp=P3;
                                        switch(temp)
                                                {
                                                        case 0xed:num=4;
                                                                break;
                                                        case 0xdd:num=5;
                                                                break;
                                                        case 0xbd:num=6;
                                                                break;
                                                }
                                        while(temp!=0xf0)
                                                {
                                                        temp=P3;
                                                        temp=temp&0xf0;
                                                }
                                        }
                                }

                        P3=0xfb;
                        temp=P3;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                                {
                                        delay(5);
                                        temp=P3;
                                        temp=temp&0xf0;
                                        while(temp!=0xf0)
                                        {
                                                temp=P3;
                                        switch(temp)
                                                {
                                                        case 0xeb:num=7;
                                                                break;
                                                        case 0xdb:num=8;
                                                                break;
                                                        case 0xbb:num=9;
                                                                break;
                                                }
                                        while(temp!=0xf0)
                                                {
                                                        temp=P3;
                                                        temp=temp&0xf0;
                                                }
                                        }
                                }

                        P3=0xf7;
                        temp=P3;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                                {
                                        delay(5);
                                        temp=P3;
                                        temp=temp&0xf0;
                                        while(temp!=0xf0)
                                        {
                                                temp=P3;
                                                if(temp==0xd7)
                                                   num=0;
                                        while(temp!=0xf0)
                                                {
                                                        temp=P3;
                                                        temp=temp&0xf0;
                                                }
                                        }          
                                }         
return num;
}
void putin()
{
  while(kaishi==1)
  {
          a=keyscan();
      display1(0,a);
          
  }
    if(t1==3)
  {
     t1=0;
         TR1=0;
  }
      TR1=1;
      min2=a;
  while(kaishi==0);
  if(t1==3)
  {
     t1=0;
         TR1=0;
  }
  while(kaishi==1)
  {
      b=keyscan();
          display1(b,a);
  }
          min1=b;
          TR1=1;
  if(t1==3)
  {
     t1=0;
         TR1=0;
  }
}


=======================================================================
我的目的是为了
设置min的十位---->确认---->设置min的个位---->确认,并开始计时


遇到的问题:
1)在设置完min的十位按确认之后,min的个位在未确认的情况下显示的是跟十位相同的数字,我本来是想它在设置min个位前显示的是0;
2)在分的十位和个位设计完之后,按了确认键,它不能立即进行计时,会延时一段时间,这是为什么? 是不是因为按键抖动的问题?

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top