单片机定时计数器。求教 为何此程序按键按下后定时计数器并不开始工作?数码管显示的数并没有变化?

#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit start=P3^2;
u8 n_ms=0;
u8 n_s=0;
u8 n_m=0; //定义3个变量分别用于代表毫秒,秒,分钟数
u8 run_flag; //定义一个变量用于判断是否处于计数状态
void delay(u16 z)
{
u8 x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
u8 code seg_tab[]={ 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F };
u8 code bit_tab[]={ 0x00, 0x01, 0x02, 0x03};
void display_led(u8 w_bit,u8 w_num) //让第w_bit位显示数字w_num
{
P1 = bit_tab[w_bit];
P2 = seg_tab[w_num];
}
void display_num(u8 which_bit,u8 which_num) //在第which_bit位,显示一个两位数
//which_num 设计秒表第一位由1位数码管显示),仅仅显示10位上的数(0~9);
{ //第二位由两位数码管显示,显示10位及个位的数
//(00~99);第三位由一位数码管显示,仅仅显示个位上的数(0~9);
u8 x,y;
x=which_num/10; //显示10位上的数字
y=which_num%10; //显示个位上的数字
if(which_bit==1)
{
display_led(0,x) ;
delay(3);
}
if(which_bit==2)
{
display_led(2,x) ;
delay(3);
display_led(1,y) ;
delay(3);
}
if(which_bit==3)
{
display_led(3,y) ;
delay(3);
}
}
void reflash(void)
{
while(1)
{
display_num(1,n_ms);
display_num(2,n_s);
display_num(3,n_m);
}
}
int main(void)
{
TMOD=0x01; //将T0设置成为以工作方式1(16位)工作的计数器模式;
TH0=(65535-10000) / 256; //计数10000次(延时10ms)
TL0=(65535-10000) % 256; //计数10000次(延时10ms);
EA=1; //开放总中断;
ET0=1; //开放定时计数器0中断;
// TR0=1;
while(1)
{
if(start==0)
{
delay(5);
if(start==0)
{
run_flag=~run_flag;
}
}
if(run_flag)
{
TR0=1; //开始计数
}
else if(!run_flag)
{
TR0=0;
}
reflash();
}
}
void timer_service(void) interrupt 1
{
TH0=(65535-10000) / 256;
TL0=(65535-10000) % 256;
n_ms++;
if(n_ms>=100)
{
n_ms=0;
n_s++;
if(n_s>=60)
{
n_s=0;
n_m++;
if(n_m>=60)
{
n_m=0;
}
}
}
}

消抖5ms?你这样按一次按键鬼知道会被执行多少次,run_flag的状态根本不可控……追问

if(!start)
{
delay(200);
if(!start)
{
while(!start);
run_flag=~run_flag;
}
}
if(run_flag)
{
TR0=1; //开始计数
}
else if(!run_flag)
{
TR0=0;
};
我把那段这样改了 还是不行,不知道问题在哪里....

温馨提示:答案为网友推荐,仅供参考
相似回答