利用中断控制秒表的开关

作者在 2017-04-11 09:15:30 发布以下内容
#include <reg52.h>
typedef  unsigned int  U16;
typedef  unsigned char  U8;
U8 num[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x7F,0xFF};
sbit LED0=P1^0;
sbit LED1=P1^1;
sbit LED2=P1^2;
    sbit LED3=P1^3;
sbit KeyIn4=P3^4;
U8 shuma[4]={0};
void delay()
  {
U16 i=1000;
while(i--);
   }
void T1_Init()
{  
     TMOD &=0x0F; 
TMOD |=0x10;         // TMOD=0x10;  0001 xxxx     为了T0和T1不互相干扰
TH1=0xFC;
TL1=0x18;
TR1=1;
}
void T0_Init()
{                     //  TMOD=0x01;  0000 0001
TMOD &=0xF0; //   TMOD =TMOD &0xF0;
TMOD |=0x01;               //           TMOD =TMOD |0x01; 
TH0=0xFC;
TL0=0x18;
TR0=1;
}
void Inter_Init()
{
ET1=1;
ET0=1;
EA=1;
}
void refresh()
  {
   
     static  U8   t=0;    
U8 j,flag=0;
for(j=3;j>1;j--)
{
  flag=flag|shuma[j];
  if(0==flag)
  shuma[j]=11;
}
 switch(t)
     {
        case 0:LED0=1;LED1=1;LED2=1;LED3=0;P0=num[shuma[0]];t++;break;
        case 1:LED0=1;LED1=1;LED2=0;LED3=1;P0=num[shuma[1]];t++;break;
        case 2:LED0=1;LED1=1;LED2=0;LED3=1;P0=num[10];t++;break;
        case 3:LED0=1;LED1=0;LED2=1;LED3=1;P0=num[shuma[2]];t++;;break;
case 4:LED0=0;LED1=1;LED2=1;LED3=1;P0=num[shuma[3]];t=0;;break;
        default:break;
          }
 
  }
void main()
  {  
      T1_Init();
 T0_Init();
 Inter_Init();
      while(1)
 {
      if(KeyIn4==0)
{delay();
 if(KeyIn4==0)
{
  TR1=~TR1;
while(KeyIn4==0);

}
}      
 }
   }
void c_t1_inter()   interrupt 3
{  
  
           static   U16 n=0,x=0;     //多次调用,n,x的值需要保存。一定是静态,为了保持x,n的正常运行。
                                    //if(1==TF1)  没有必要,因为一进这个函数,系统默认TF1==1 中断溢出;                   
                                   //TF1=0;   没有必要,因为系统自动清零。
   TH1=0xFC;
   TL1=0x18;
   n++;
       //refresh();
           if(100==n)
     {
     x++;
shuma[3]=x/1000%10;
   shuma[2]=x/100%10;
   shuma[1]=x/10%10;
shuma[0]=x%10;
    n=0;
    }


    }
void c_t0_inter()   interrupt 1 //采用另一个定时器进行刷新。不断刷新数码管,防止出现数据残缺。
{  
  
         
   TH0=0xFC;
   TL0=0x18;
   refresh();
        
    }

默认分类 | 阅读 826 次
文章评论,共0条
游客请输入验证码
文章分类
文章归档
最新评论