按一下按鍵,跑馬燈就開始跑動,按兩下就熄滅,再按再亮,再按再滅,如此循環.
但是我現在的效果是按一下以后,跑馬燈一直在工作,按第二下三下四下……都不起作用了,與我寫程序的初衷不合,請高人幫忙.
程序如下:
#include "reg51.h"
#include
sbit A=P3^5;
void delayms(unsigned char time) // 延時子程序
{
unsigned char i;
while(time--)
{
for(i = 0; i < 120; i++);
}
}
main()
{
unsigned int i;
i=0;
if(!A)
{
unsigned int j;
for(j=0;j<1000;j++) //延時去抖
{
if(!A) //延時以后按鍵依然按下.
i=i++; //變量加1.
}
}
while(i%2) //奇數次就開始運行,偶數次就跳出去.
{
unsigned char LED;
LED = 0xfe;
P0 = LED;
while(1)
{
delayms(1000);
LED = _cror_(LED,1); //循環右移1位,點亮下一個LED
P0 = LED;
}
}
}