我写了一程序,定时器0使LED亮灭。但我要加一个轻触开关来使LED在上电后,是不闪的,但按轻触开关,LED才开始闪,再按轻触开关,LED就不闪,这样控制LED的停闪和开始闪。下面是我的程序,但是不工作,按键没反应,请指导,谢谢~~
#include "reg51.h"
#define uchar unsigned char
#define uint unsigned int
sbit LED=P2^5;
sbit key=P0^0;
void Delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void main()
{
unsigned char counter=0;
LED=1;
TMOD=0x00; //设置定时器0为模式0
TH0=0xB8;
TL0=0x00; //定时值初值
TR0=1; //打开定时器0
while(1)
{
if(0==key)
{
{
Delay(10);
if(0==key)
{
TR0=0;
}
}
if(1==TF0) //判断定时器0是否溢出
{
TF0=0;
TH0=0xB8; //一旦溢出后,重新赋值
TL0=0x00;
counter++;
if(5==counter) //判断定时器0溢出是否达到5次
{
counter=0; //counter清0,重新计数
LED=!LED; //LED取反操作,0-->1,1-->0
}
}
}
}
} |