找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 11163|回复: 8
收起左侧

8个按键分别控制8个LED灯的亮灭,按一次松开常亮,按一次松开常灭,求助大神看看问题

[复制链接]
ID:405279 发表于 2018-10-24 00:14 | 显示全部楼层 |阅读模式
#include<reg51.h>
#define uchar unsigned char
#define uint  unsigned int
void delayms(uint y)
{
        uint i,j;
        for(i=y;i>0;i--)
                for(j=110;j>0;j--);
}
main(void)
{
while(1)
{       
      
            if(P1==0xfe)         
            {
                  delayms(1);
               if(P1==0xfe)
                         {
                           while(P1==0xfe)
                           delayms(1);
                        P2=0xfe;
                      }          
                }
                 if(P1==0xfe&&P2==0Xfe)
                 {
                       delayms(1);
                     if(P1==0xfe&&P2==0Xfe)
                         {         delayms(1);
                             while(P1==0xfe)
                                 
                             P2=0xff;
                                 
                         }
                 }
                if(P1==0xfd)         
            {
                   delayms(10);
               if(P1==0xfd)
                        {
                           while(P1==0xfd)
                        P2=0xfd;
                     }          
                }
                 if(P1==0xfd&&P2==0Xfd)
                 {
                     delayms(10);
                     if(P1==0xfd&&P2==0Xfd)
                         {       
                             while(P1==0xfd)
                          P2=0xff;
                         }
                 }
                 if(P1==0Xfb)
                 {
                    delayms(10);
                    if(P1==0xfb)
                        {
                           while(P1==0xfb)
                           P2=0xfb;
                        }
                 }
                 if(P1==0xfb&&P2==0xfb)
                 {
                    delayms(10);
                        if(P1==0xfb&&P2==0xfb)
                        {
                              while(P1==0xfb)
                                  P2=0xff;


                        }
                   }
                 if(P1==0xf7)
                 {
                     delayms(10);
                         if(P1==0xf7)
                         {
                            while(P1==0xf7)
                                P2=0xf7;
                         }
                 }
                 if(P1==0xf7&&P2==0xf7)
                 {
                     delayms(10);
                         if(P1==0xf7&&P2==0xf7)
                         {
                         while(P1==0xf7)
                         P2=0xff;
                         }
                 }
                 if(P1==0xef)
                 {
                    delayms(10);
                        if(P1==0xef)
                        {
                           while(P1==0xef)
                           P2=0xef;
                        }
                 }
                 if(P1==0xef&&P2==0xef)
                 {
                    delayms(10);
                        if(P1==0xef&&P2==0xef)
                        {
                        while(P1==0xef)
                        P2=0xff;
                        }
                 }
       if(P1==0xdf)
           {
              delayms(10);
                    if(P1==0xdf)
                    {
                      while(P1==0xdf)
                      P2=0xdf;
                    }
           }
           if(P1==0xdf&&P2==0xdf)
           {
              delayms(10);
                    if(P1==0xdf&&P2==0xdf)
                    {
                    while(P1==0xdf)
                    P2=0xff;
                    }
           }
           if(P1==0xbf)
           {
              delayms(10);
                    if(P1==0xbf)
                    {
                  while(P1==0xbf)
          P2=0xbf;
         }
       }
       if(P1==0xbf&P2==0xbf)
       {
          delayms(10);
          if(P1==0xbf&&P2==0xbf)
          {
          while(P1==0xbf)
          P2=0xff;
          }
       }
       if(P1==0x7f)
       {
          delayms(10);
          if(P1==0x7f)
          {
            while(P1==0x7f)
            P2=0x7f;
          }
       }
       if(P1==0x7f&&P2==0x7f)
       {
           delayms(10);
                   if(P1==0x7f&&P2==0x7f)
           {
           while(P1==0x7f)
           P2=0xff;
                     }
       }
}
}

回复

使用道具 举报

ID:164602 发表于 2018-10-24 08:41 | 显示全部楼层
没有电路图,我猜你是用的矩阵键盘吧。
矩阵键盘的键选择,可不是你这样搞的哦。
我在我的板子上试验了一个,你看看:
#include<reg51.h>

#define GPIO_KEY P1

sbit LED0=P2^0;
sbit LED1=P2^1;
sbit LED2=P2^2;
sbit LED3=P2^3;
sbit LED4=P2^4;
sbit LED5=P2^5;
sbit LED6=P2^6;
sbit LED7=P2^7;

unsigned char KeyValue=9;

void Delay10ms();
void KeyDown();

void main(void)
{
        while(1)
        {
                KeyDown();
                if (KeyValue==0)
                {
                        LED0=~LED0;
                        KeyValue=9;
                }
                if (KeyValue==1)
                {
                        LED1=~LED1;
                        KeyValue=9;
                }
                if (KeyValue==2)
                {
                        LED2=~LED2;
                        KeyValue=9;
                }
                if (KeyValue==3)
                {
                        LED3=~LED3;
                        KeyValue=9;
                }
                if (KeyValue==4)
                {
                        LED4=~LED4;
                        KeyValue=9;
                }
                if (KeyValue==5)
                {
                        LED5=~LED5;
                        KeyValue=9;
                }
                if (KeyValue==6)
                {
                        LED6=~LED6;
                        KeyValue=9;
                }
                if (KeyValue==7)
                {
                        LED7=~LED7;
                        KeyValue=9;
                }
                if (KeyValue==8)
                {
                        LED0=1;
                        LED1=1;
                        LED2=1;
                        LED3=1;
                        LED4=1;
                        LED5=1;
                        LED6=1;
                        LED7=1;
                }
        }                               
}

void KeyDown(void)
{
        char a=0;
        GPIO_KEY=0x0f;
        if(GPIO_KEY!=0x0f)
        {
                Delay10ms();
                if(GPIO_KEY!=0x0f)
                {
                        GPIO_KEY=0X0F;
                        switch(GPIO_KEY)
                        {
                                case(0X07):        KeyValue=0;break;
                                case(0X0b):        KeyValue=1;break;
                                case(0X0d): KeyValue=2;break;
                                case(0X0e):        KeyValue=3;break;
                        }
                        GPIO_KEY=0XF0;
                        switch(GPIO_KEY)
                        {
                                case(0X70):        KeyValue=KeyValue;break;
                                case(0Xb0):        KeyValue=KeyValue+4;break;
                                case(0Xd0): KeyValue=KeyValue+8;break;
                                case(0Xe0):        KeyValue=KeyValue+12;break;
                        }
                        while((a<50)&&(GPIO_KEY!=0xf0))
                        {
                                Delay10ms();
                                a++;
                        }
                }
        }
}

void Delay10ms(void)
{
    unsigned char a,b,c;
    for(c=1;c>0;c--)
        for(b=38;b>0;b--)
            for(a=130;a>0;a--);
}
电路图也发出来,程序都是与电路对应的。
1.jpg

1.jpg

回复

使用道具 举报

ID:23606 发表于 2018-10-24 08:49 | 显示全部楼层
感觉太复杂了,一个按键按下对相应的灯取反一下就行了
回复

使用道具 举报

ID:357401 发表于 2018-10-24 09:14 | 显示全部楼层
你这个if判断这里:if(P1==0xfe)         
            {
                  delayms(1);
               if(P1==0xfe)
                         {
                           while(P1==0xfe)
                           delayms(1);
                        P2=0xfe;
                      }         
                }判断有按键按下就亮灯,亮灯后并没有其他后续可执行任务,并且你是放在while(1)的死循环里的。所以你的程序实际表现就是按一下就一直亮,再按一下就灭。
回复

使用道具 举报

ID:111634 发表于 2018-10-24 09:27 | 显示全部楼层
本帖最后由 zl2168 于 2018-10-24 10:48 编辑

介绍你一个按键操作案例。
实例5  4种不同方式键控信号灯
Proteus仿真一下,确认有效。
实例5 不同方式键控灯.rar (31.72 KB, 下载次数: 27)
回复

使用道具 举报

ID:313336 发表于 2018-10-24 09:40 来自手机 | 显示全部楼层
qwersdfgfg 发表于 2018-10-24 09:14
你这个if判断这里:if(P1==0xfe)         
            {
                  delayms(1);

按键扫描出现问题了吧
回复

使用道具 举报

ID:89515 发表于 2018-10-24 09:52 | 显示全部楼层
#include<reg51.h>
#define uchar unsigned char
#define uint  unsigned int
void delayms(uint y)
{
        uint i,j;
        for(i=y;i>0;i--)
                for(j=110;j>0;j--);
}
void main(void)
{
while(1)
{            
     if(P1==0xfe)         
     {
   delayms(10);
   if(P1==0xfe)
   {
          while(P1==0xfe) ;
          delayms(10);
         ( P2&=0xfe)?(P2|=0x01):(P2&=0xfe);
   }         
     }
  //类似上……
}
}

回复

使用道具 举报

ID:414462 发表于 2018-10-24 09:58 | 显示全部楼层
程序搞复杂了
回复

使用道具 举报

ID:839984 发表于 2020-11-6 22:37 来自手机 | 显示全部楼层
能不能帮忙用if语句写一个开始八个全亮,之后按一个按键熄灭一个,在按一下,熄灭两个,直到全灭为止
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表