請各位大神幫忙看下,為什么這個程序在運行的時候,預計截斷的位置總是變換,即計數完成周期不定(預計11步,實際在第8、9、10都可能會提前計數)請大神幫忙看下程序是不是哪邊不完善
#include <REGX52.H>
sbit k1=P3^0;
sbit LED1=P0^1;
unsigned char count=0;
unsigned char flag=0;
unsigned char cs=0;
void delay(unsigned char i)//延時i個機器周期,有誤差
{
while(i--);
}
void tim0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
cs++;//每50ms加1
}
void main()
{
TMOD=0x01;//設置定時器0工作方式1
TH0=0x3c;
TL0=0xb0;//50ms
TR0=1;
ET0=1;//開定時器0
EA=1;//開中斷
//設置定時器0工作方式1,50ms進一次
while(1)
{
if(k1==0)//判斷按鍵按下
{
delay(500);//延時防抖
if(k1==0)flag=1;//標志位置1
}
if(k1==1&&flag==1)//判斷按鍵松開
{
delay(500);//延時防抖
if(k1==1)
{
flag=0;count++;//標志位置0,次數+1
if(count==1)
{
cs=0;//清空時間標志位
while(cs!=10);//等待500ms
LED1=0;
}
if(count==2)
{
cs=0;//清空時間標志位
while(cs!=10);//等待750s
LED1=1;
}
if(count==3)
{
cs=0;//清空時間標志位
LED1=1;
}
if(count==4)
{
cs=0;//清空時間標志位
LED1=1;
}
if(count==5)
{
cs=0;//清空時間標志位
LED1=1;
}
if(count==6)
{
cs=0;//清空時間標志位
LED1=1;
}
if(count==7)
{
cs=0;//清空時間標志位
LED1=1;
}
if(count==8)
{
cs=0;//清空時間標志位
LED1=1;
}
if(count==9)
{
cs=0;//清空時間標志位
LED1=1;
}
if(count==10)
{
cs=0;//清空時間標志位
LED1=1;
}
if(count==11)
{
cs=0;//清空時間標志位
while(cs!=10);//等待0.5s
LED1=1;
count=0;//重新計數
}
}
}
}
}
|