打印
[单片机芯片]

关于51单片机用一个按键切换4种模式的问题?

[复制链接]
1661|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
李浩一|  楼主 | 2022-1-21 16:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

我用51单片机的按键进行led的4种模式的切换,我现在的程序只能是中断里面的led程序结束后再按按键才可以切换模式,我想知道如何在中断中的led程序执行时按按键切换到下一个模式,请求大家支援。以下是我的代码。
#include
sbit beep=P1^5;
sbit k1=P3^1;
sbit k3=P3^2;
sbit led=P2^0;
#define N P2
typedef unsigned int ui;
typedef unsigned char uc;
uc num;
void delay(ui i)
{
while(i--);
}
void fmq()
{
beep=~beep;
delay(100);
}
void led0()
{
N=0xff;

  • [color=rgb(119, 120, 136) !important]

    N=0xfe;

  • [color=rgb(119, 120, 136) !important]

    fmq();

  • [color=rgb(119, 120, 136) !important]

    delay(50000);

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

    N=0xfd;

  • [color=rgb(119, 120, 136) !important]

    fmq();

  • [color=rgb(119, 120, 136) !important]

    delay(50000);

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

    N=0xfb;

  • [color=rgb(119, 120, 136) !important]

    fmq();

  • [color=rgb(119, 120, 136) !important]

    delay(50000);

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

    N=0xf7;

  • [color=rgb(119, 120, 136) !important]

    fmq();

  • [color=rgb(119, 120, 136) !important]

    delay(50000);

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

    N=0xef;

  • [color=rgb(119, 120, 136) !important]

    fmq();

  • [color=rgb(119, 120, 136) !important]

    delay(50000);

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

    N=0xdf;

  • [color=rgb(119, 120, 136) !important]

    fmq();

  • [color=rgb(119, 120, 136) !important]

    delay(50000);

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

    N=0xbf;

  • [color=rgb(119, 120, 136) !important]

    fmq();

  • [color=rgb(119, 120, 136) !important]

    delay(50000);

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

    N=0x7f;

  • [color=rgb(119, 120, 136) !important]

    fmq();

  • [color=rgb(119, 120, 136) !important]

    delay(50000);


}
void led1()
{
N=0x7e;
fmq();
delay(35000);
delay(35000);
N=0xbd;
fmq();
delay(35000);
delay(35000);
N=0xdb;
fmq();
delay(35000);
delay(35000);
N=0xe7;
fmq();
delay(35000);
delay(35000);
N=0xe7;
fmq();
delay(35000);
delay(35000);
N=0xdb;
fmq();
delay(35000);
delay(35000);
N=0xbd;
fmq();
delay(35000);
delay(35000);
N=0x7e;
fmq();
delay(35000);
delay(35000);
}
void led2()
{
N=0xaa;
fmq();
delay(50000);
delay(50000);
N=0x55;
fmq();
delay(50000);
delay(50000);
}
void led3()
{
ui i;
N=0xfe;
while(1)
{
if(k1==0)
{

delay(1000);
if(k1==0)
{
switch(i)
{
case(0):N=0xfd;fmq();i++;break;
case(1):N=0xfb;fmq();i++;break;
case(2):N=0xf7;fmq();i++;break;
case(3):N=0xef;fmq();i++;break;
case(4):N=0xdf;fmq();i++;break;
case(5):N=0xbf;fmq();i++;break;
case(6):N=0x7f;fmq();i++;break;
case(7):N=0xff;fmq();i++;break;
}
while(!k1);
}
}
if(i==8)
{
i=0;
break;
}
}
}
void init()
{
IT0=1;
EX0=1;
EA=1;
}
void main()
{
init();
while(1);

}
void init0() interrupt 0
{
delay(1000);
{
while(1)
{

if(k3==0)
{
switch(num)
{
case(0):led0();num++;break;
case(1):led1();num++;break;
case(2):led2();num++;break;
case(3):led3();num++;break;
case(4):num++;N=0xff;break;
}
}
while(!k3);
if(num==4)
{
num=0;
break;
}
}

}
}


使用特权

评论回复
沙发
yangbeioften| | 2022-1-27 08:51 | 只看该作者
不要使用delay函数,用定时器实现,很简单

使用特权

评论回复
板凳
drer| | 2022-2-6 20:49 | 只看该作者
可以精简代码吗

使用特权

评论回复
地板
kxsi| | 2022-2-6 21:07 | 只看该作者
这个代码着实看不太明白

使用特权

评论回复
5
qcliu| | 2022-2-6 21:23 | 只看该作者
为什么要延时这么长的时间呢

使用特权

评论回复
6
wiba| | 2022-2-6 21:48 | 只看该作者
用循环该多好啊

使用特权

评论回复
7
coshi| | 2022-2-6 21:56 | 只看该作者
设置一个全局变量就可以

使用特权

评论回复
8
天意无罪| | 2022-2-7 16:19 | 只看该作者
定义一个状态机变量,在按键中断中切换状态机变量,在主程序中检测这个变量并执行相应函数。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

43

主题

47

帖子

1

粉丝