外部中断计数器

来源:互联网 发布:淘宝只能换绑不能解绑 编辑:程序博客网 时间:2024/05/16 14:44
#include <reg52.h>
#define uchar unsigned char
#define uint  unsigned  int
uchar duan[10]={0xc0,0Xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//所需的段的位码
uchar wei[4]={0X80,0X40,0X20,0X10};                                 //位的控制端
uint date; //定义数据类型
uint dispcount=0;
uint lck=0;
uint disp=0;
/******************************************************************


延时函数


******************************************************************/
void delayms(uchar i)
{
  uchar t;
while(i--)
{
for(t=0;t<120;t++);
}
}




/**********************************************************************
                数码管动态扫描
*********************************************************************/
void display()

P2=wei[0];
P0=duan[date/1000];    //求千位
delayms(1);
P2=0x00;
 
P2=wei[1];
P0=duan[date%1000/100];    //求百位
delayms(1);
P2=0x00;
   
P2=wei[2];
P0=duan[date%100/10];    //求十位
delayms(1);
P2=0x00;
   
P2=wei[3];
P0=duan[date%10];    //求个位
delayms(1);
P2=0x00;
}


/*************************************************************************
                                定时器初值50ms
**************************************************************************/
void initTimer()
{
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
}


/*************************************************************************
                                定时器函数
**************************************************************************/
void timer0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
lck++;
if(lck==20)
{
disp=dispcount;
lck=0;
dispcount=0;
}
}


/*************************************************************************
                                中断函数
**************************************************************************/
void int0() interrupt 0
{
dispcount++; //每一次中断,计数加一
}


/*************************************************************************
                                主函数
**************************************************************************/
void main()
{
IT0=1;     //INT0下降沿中断
EX0=1;     //允许INT0中断
initTimer(); //装入初值
TR0=1;
ET0=1;
EA=1;
while(1)
{
date=disp;
display();
}
}
0 0