注册 登录
编程论坛 单片机编程

霓虹灯程序中,再使用3个按键来分别控制不同的效果。 请问大佬该怎么改以下程序

小桥流水1997 发布于 2020-11-16 09:04, 4580 次点击
#include<reg51.h>
#include<intrins.h>
Sbit K1=P3^0;
Sbit K1=P3^1;
Sbit K1=P3^2;

sbit LED1=P2^0£»
sbit LED2=P2^1£»
sbit LED3=P2^2£»
void delay(unsigned int delaytime)  
{
     unsigned char i;
     for(;delaytime>0;delaytime--)
     for(i=250;i>0;i--)
     ;
}
void LED()
{
     while(1)
     {
         unsigned char light=0xfe,i;
         if(K1==0)
        for(i=0;i<8;i++)
       {
       P2=light;
       light=_crol_(light,1); //
       delay(1000);
       }
         if(K1==1)
         for(i=0;i<8;i++)
       {
       P2=light;
       light=_cror_(light,1);
       delay(1000);
         }
      if(K2==1)
        {
       P2=light;
       P2=0xff;
       delay(1000);
       P2=0x7e;
       delay(1000);
       P2=0xbd;
         delay(1000);
       P2=0xdb;
         delay(1000);
       P2=0xe7;
         delay(1000);
       P2=0xff;
         delay(1000);
       }   
       if(K3==0)
         for(i=0;i<8;i++)
       {
        P2=light;
        light=_crol_(light,1);
        P2=light|0xff ;
            
                 }
         else
         for(i=0;i<8;i++)
                 {
                     P2=0xff;
                     P2=light;
                     light=P2<<1;
                     delay(100);
                 }
                 for(i=0;i<8;i++)
                 {
                     P2=0x00;
                 P2=light;
                 light=P2>>1|0x80;
                     delay(100);
                 }
             }            
             }
1 回复
#2
r3169994432020-11-16 20:07
给思路:

创建一个全局变量unsigned char Key_Status 或枚举 enum Key_Status{ Color_1,Color_2,Color3 };

void main()
{
  while(1)
  {
    Key_Scan();//把扫描到的按键状态赋值给Key_Status
    Led_Display();//监控Key_Status值并显示相对应的颜色
  }
}
1