#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit smg1=P2^4;
sbit smg2=P2^5;
sbit smg3=P2^6;
sbit key1=P3^2;
sbit key2=P3^3;
uchar flag;
uchar table[3];
unsigned char code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x10};
unsigned char code smg_we[]={0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78};
uchar s1num;
uchar count;
int miao;
//uchar miao; 注意miao的范围
//char miao; 定义一个有符号的字符变量
void display();
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void timer0init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void main()
{
// uchar i;
timer0init();
while(1)
{
display();
}
}
/*中断函数所用时间必须要大于数码管延时显示时间,否则定时就不准确 */
void timer0() interrupt 1
{
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
count++;
if(count==10)
{
count=0;
miao++;
if(key1==0)
{
delayms(5);
if(key1==0)
{
delayms(5);
while(!key1);
TR0=0;
}
}
if(key2==0)
{
delayms(5);
if(key2==0)
{
delayms(5);
while(!key2);
TR0=1;
}
}
}
}
void display()
{
if(miao==1000)
miao=0;
// miao=miao%60;
table[0]=miao%10;
table[1]=miao/10%10;
table[2]=miao/100;
smg1=0;
smg2=0;
smg3=0;
P1=smg_du[table[0]];
delayms(2);
smg2=0;
smg1=1;
P1=smg_du[table[1]];
delayms(2);
smg1=0;
smg2=1;
smg3=0;
P1=smg_du[table[2]];
delayms(2);
// delayms(2);
}
为什么按下key1,定时器停止计时,数码管显示无变化,但按下key2,定时器不能重新开始计数了?
|