|
前些天看到一个定时器中断的计时问题,那里面采用的答案似乎是,中断执行以后才开始再次计时,对此不敢苟同,考虑以下程序,
#include<reg52.h>
unsigned char code DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//
unsigned char code WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char miao=0;
void DelayUs2x(unsigned char t)//大致延时(2*t+5)us
{
while(--t);
}
void DelayMs(unsigned int t)//大致延时1mS
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
显示函数,用于动态扫描数码管
------------------------------------------------*/
void Display(unsigned char dat)
{
P1=0xff;
P0=DuanMa[dat%10];
P1=0x7f;
DelayMs(3);
P1=0xff;
P0=DuanMa[(dat/10)%10]|0x80;
P1=0xbf;
DelayMs(3);
P1=0xff;
P0=DuanMa[(dat/100)%10];
P1=0xdf;
DelayMs(3);
}
/*------------------------------------------------
主函数
------------------------------------------------*/
void main (void)
{
TMOD |= 0x01;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256; //2ms定时
EA=1;
ET0=1;
TR0=1;
while (1)
{
Display(miao);
}
}
/*------------------------------------------------
定时器中断子程序
------------------------------------------------*/
void Timer0_isr(void) interrupt 1
{
static unsigned char count=0;
TH0=(65536-2000)/256; //重新赋值 2ms
TL0=(65536-2000)%256;
count++;
if(count==50)
{
miao++;
count=0;
}
// DelayMs(1);
}
0.1秒表,
|
|