作业帮 > 综合 > 作业

51单片机,开关控制LED,开始流水灯,按一下停止,再按一下继续流水灯

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/04/27 14:01:54
51单片机,开关控制LED,开始流水灯,按一下停止,再按一下继续流水灯
想问一下我的程序有什么问题,还往指教用中断怎么实现再按一下继续停止时的流水灯
#include
#include
#define uchar unsigned char
sbit button=P1^0;
uchar press=0;
void Delay(uchar z)
{
\x05\x05uchar x,y;
\x05\x05for(x=z;x>0;x--)
\x05\x05\x05for(y=255;y>0;y--);
}
\x05\x05\x05\x05
void Led_Continue()
{
\x05\x05P2=0xfe;
\x05\x05while(press==0|press==2)\x05
\x05\x05{
\x05\x05\x05\x05P2=_crol_(P2,1);
\x05\x05\x05\x05Delay(255);\x05\x05
\x05\x05}
}
void Led_Stop()
{
\x05\x05P2=0xff;
}
void Keyscan()
{
\x05\x05if(button==0)
\x05\x05\x05{
\x05\x05\x05\x05Delay(20);
\x05\x05\x05\x05if(button==0)
\x05\x05\x05\x05{
\x05\x05\x05\x05\x05press+=1;
\x05\x05\x05\x05\x05if(press==1)
\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05Led_Stop();
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05\x05\x05if(press==2)
\x05\x05\x05\x05\x05\x05{
\x05\x05\x05\x05\x05\x05
\x05\x05\x05\x05\x05\x05\x05press=0;
\x05\x05\x05\x05\x05\x05}
\x05\x05\x05\x05}
\x05\x05\x05}\x05\x05
}
void main()
{
\x05\x05Keyscan();\x05
\x05\x05Led_Continue();
}
采用中断方式,程序如下:
#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);
}
试试看.
再问: 嗯,可以实现,谢谢,
再答: 如果,用定时中断来显示流水灯,主函数不停的检测键盘,也可以。 如果不用中断,实现起来,较难。 使用软件延时,延时几百毫秒期间,并不检测按键。 延时之后,再检测键盘,可能,按键就已经松开了。 按下了,也白按,并没有检测到。 如果,在延时期间,检测键盘,有按键,就提前结束延时,也行。 但是,编程就太麻烦了。 使用中断,才是最简便的。