|
請各位大神幫忙看下,為什么這個(gè)程序在運(yùn)行的時(shí)候,預(yù)計(jì)截?cái)嗟奈恢每偸亲儞Q,即計(jì)數(shù)完成周期不定(預(yù)計(jì)11步,實(shí)際在第8、9、10都可能會提前計(jì)數(shù))請大神幫忙看下程序是不是哪邊不完善
#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)//延時(shí)i個(gè)機(jī)器周期,有誤差
{
while(i--);
}
void tim0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
cs++;//每50ms加1
}
void main()
{
TMOD=0x01;//設(shè)置定時(shí)器0工作方式1
TH0=0x3c;
TL0=0xb0;//50ms
TR0=1;
ET0=1;//開定時(shí)器0
EA=1;//開中斷
//設(shè)置定時(shí)器0工作方式1,50ms進(jìn)一次
while(1)
{
if(k1==0)//判斷按鍵按下
{
delay(500);//延時(shí)防抖
if(k1==0)flag=1;//標(biāo)志位置1
}
if(k1==1&&flag==1)//判斷按鍵松開
{
delay(500);//延時(shí)防抖
if(k1==1)
{
flag=0;count++;//標(biāo)志位置0,次數(shù)+1
if(count==1)
{
cs=0;//清空時(shí)間標(biāo)志位
while(cs!=10);//等待500ms
LED1=0;
}
if(count==2)
{
cs=0;//清空時(shí)間標(biāo)志位
while(cs!=10);//等待750s
LED1=1;
}
if(count==3)
{
cs=0;//清空時(shí)間標(biāo)志位
LED1=1;
}
if(count==4)
{
cs=0;//清空時(shí)間標(biāo)志位
LED1=1;
}
if(count==5)
{
cs=0;//清空時(shí)間標(biāo)志位
LED1=1;
}
if(count==6)
{
cs=0;//清空時(shí)間標(biāo)志位
LED1=1;
}
if(count==7)
{
cs=0;//清空時(shí)間標(biāo)志位
LED1=1;
}
if(count==8)
{
cs=0;//清空時(shí)間標(biāo)志位
LED1=1;
}
if(count==9)
{
cs=0;//清空時(shí)間標(biāo)志位
LED1=1;
}
if(count==10)
{
cs=0;//清空時(shí)間標(biāo)志位
LED1=1;
}
if(count==11)
{
cs=0;//清空時(shí)間標(biāo)志位
while(cs!=10);//等待0.5s
LED1=1;
count=0;//重新計(jì)數(shù)
}
}
}
}
}
|
|