采用中断方式,程序如下:
#include
#include
#define uchar unsigned char
sbit button = P3^2;
uchar press = 0;
uchar luishui = 0xfe;
void Delay(uchar z)
{
uchar x, y;
for(x = z; x > 0; x--) for(y = 255; y > 0; y--);
}
void main()
{
IT0 = 1;
EX0 = 1;
EA = 1;
while(1) {
if(press == 1) P2 = 0xff;
if(press == 0) {
P2 = luishui;
luishui = _crol_(luishui, 1);
Delay(255);
}
}
}
X0_INT() interrupt 0
{
press += 1; if(press == 2) press = 0;
Delay(10);
while(button == 0);
}
试试看。
#include
#include
#define uchar unsigned char
sbit button=P1^0;
uchar press=0;
void Delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=255;y>0;y--);
}
void Led_Continue()
{
P2=0xfe;
while(press==0|press==2)
{
P2=_crol_(P2,1);
Delay(255);
}
}
void Led_Stop()
{
P2=0xff;
}
void Keyscan()
{
if(button==0)
{
Delay(20);
if(button==0)
{
press+=1;
if(press==1)
{
Led_Stop();
}
if(press==2)
{
press=0;
}
}
}
}
void main()
{
while(1)
{
Keyscan();
Led_Continue();
}
}
试试这个,在mian中要一直去扫描,否则就扫描一次不能达到目的。
然后你问用中断怎么完成,告诉你中断更简单,定时器用作延时用,键盘只需要写当这个按键按下时 TRx=~TRx 就是每一次按下,定时器开关都和上一次状态不一样,这样每按一次都会停止、开始、停止、开始......
希望我的回答能帮助到你。
d
char unsigned cha