# 单个按键控制多种流水灯状态

5 篇文章 10 订阅

STC89C52RC芯片 *1
10K电阻 *2
100Ω电阻 *8
10uF 电解电容 *1
22pF 瓷片电容 *2
12MHz晶振 *1

LED灯 *8

#include"reg52.h"
#define uchar unsigned char
#define uint unsigned int
#define LED P1
sbit KEY=P3^0;

unsigned char code led_1[8]=
{0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//定义第一种流水灯状态
unsigned char code led_2[8]=
{0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//定义第二种流水灯状态
unsigned char code led_3[5]=
{0x7e,0xbd,0xdb,0xe7,0xff};//定义第三种流水灯状态
unsigned char code led_4[5]=
{0xe7,0xc3,0x81,0x00,0xff};//定义第四种流水灯状态

unsigned int k,z;   //定义两个无符号整型变量

void delayms(uint ms)  //延时
{
uint i,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
void key()
{
if(KEY==0)
{
delayms(5);
if(KEY==0)
{
k++;
delayms(5);
if(k>4)
k=1;
}
}
}
void main()
{
k=1;    //初始化为开机第一种流水灯状态
while(1)
{
switch(k)
{
case 1:   //第一种流水灯状态
for(z=0;z<8;z++)
{
LED=led_1[z];
delayms(300);
key();
if(KEY==0)break;
}
break;
case 2:   //第二种流水灯状态
for(z=0;z<8;z++)
{
LED=led_2[z];
delayms(300);
key();
if(KEY==0)break;
}
break;
case 3:   //第三种流水灯状态
for(z=0;z<5;z++)
{
LED=led_3[z];
delayms(300);
key();
if(KEY==0)break;
}
break;
case 4:   //第四种流水灯状态
for(z=0;z<5;z++)
{
LED=led_4[z];
delayms(300);
key();
if(KEY==0)break;
}
break;
}
}
}


#include"reg52.h"

#define uchar unsigned char
#define uint unsigned int
#define LED P2

sbit KEY1=P1^0;
sbit KEY2=P1^1;

unsigned char code led_1[8]=
{0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//定义第一种流水灯状态
unsigned char code led_2[8]=
{0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//定义第二种流水灯状态
unsigned char code led_3[5]=
{0x7e,0xbd,0xdb,0xe7,0xff};//定义第三种流水灯状态
unsigned char code led_4[5]=
{0xe7,0xc3,0x81,0x00,0xff};//定义第四种流水灯状态

unsigned int k,z,fla=1;   //定义两个无符号整型变量

void delayms(uint ms);  //延时
void key();    //按键检测函数

void main()
{
k=1;    //初始化为开机第一种流水灯状态
while(1)
{
key();
switch(k)
{
case 1:   //第一种流水灯状态
if(fla){LED=led_1[z];z++;z%=8;}
break;
case 2:   //第二种流水灯状态
if(fla){LED=led_2[z];z++;z%=8;}
break;
case 3:   //第三种流水灯状态
if(fla){LED=led_3[z];z++;z%=5;}
break;
case 4:   //第四种流水灯状态
if(fla){LED=led_4[z];z++;z%=5;}
break;
}
delayms(100);
}
}

void delayms(uint ms)  //延时
{
uint i,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
void key()
{
if(KEY1==0)
{
delayms(5);
if(KEY1==0)
{
z=0;k++;
if(k>4)k=1;
while(KEY1==0);
}
}
else if(KEY2==0)
{
delayms(5);
if(KEY2==0)
{
fla++;
fla%=2;
while(KEY2==0);
}
}
}



10-21
10-19 1万+
04-19
10-12 3万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

©️2022 CSDN 皮肤主题：技术黑板 设计师：CSDN官方博客

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。