C51 编程,要求按照指定的次序按键

//有个题目,要求顺序按下四个按键。

//如果不按照指定的次序来按,将显示出错误提示。

//题目链接:http://zhidao.baidu.com/question/572146978.html


//要求如下:

//第一次、SW1 按住时 D1 亮,松开时 D1 灭(如第一次按的不是 SW1,是SW2、3、4的话,D5 常亮);

//第二次、SW2 按住时 D2 亮,松开时 D2 灭(如第二次按的不是 SW2,是SW1、3、4的话,D5 常亮);

//第三次、SW3 按住时 D3 亮,松开时 D3 灭(如第三次按的不是 SW3,是SW1、2、4的话,D5 常亮);

//第四次、SW4 按住时 D4 亮,松开时 D4 灭(如第四次按的不是 SW4,是SW1、2、3的话,D5 常亮)。

//第四次、当 SW4 松开后 D6 常亮。


//这要求,有些像输入密码。

//但是,不是很严格,显示出错了也不禁止按键。


//本题目的仿真电路如下:

C51 编程,要求按照指定的次序按键 - 非著名博主 - 电子信息角落

//图片链接:

//http://xiangce.baidu.com/picture/detail/0d1e899900dfde5d2d8410c3670772bfcb4035d4


//做而论道编写的程序如下:


#include<reg52.h>


sbit D1 = P0^0;

sbit D2 = P0^1;

sbit D3 = P0^2;

sbit D4 = P0^3;


sbit D5 = P2^0;

sbit D6 = P2^1;


sbit SW1 = P1^0;

sbit SW2 = P1^1;

sbit SW3 = P1^2;

sbit SW4 = P1^3;


//--------------------------------------------

void delay(int x)

{

    int i;

    while(x--)  for(i = 115; i > 0; i--);

}

//--------------------------------------------


main()

{

    D1 = D2 = D3 = D4 = D5 = D6 = 1;    //关闭


    while(1) {

//------------------------------------------------------------------------

      D1 = D2 = D3 = D4 = 1;       //关闭


      while(1) {                                 //循环

        while((SW1) && (SW2) && (SW3) && (SW4)); //等待按键

        delay(10);                               //延时

        if((!SW1) || (!SW2) || (!SW3) || (!SW4))  break; //有任意一个按键,就跳出循环

      }


      if (!SW1) { D1 = 0; while (!SW1);}         //D1亮、等待释放


      else { D5 = 0; while((!SW2) || (!SW3) || (!SW4));} //D5亮、等待释放

//------------------------------------------------------------------------

      D1 = D2 = D3 = D4 = 1;       //关闭


      while(1) {                                 //循环

        while((SW1) && (SW2) && (SW3) && (SW4)); //等待按键

        delay(10);                               //延时

        if((!SW1) || (!SW2) || (!SW3) || (!SW4))  break; //有任意一个按键,就跳出循环

      }


      if (!SW2) { D2 = 0; while (!SW2);}         //D2亮、等待释放


      else { D5 = 0; while((!SW1) || (!SW3) || (!SW4));} //D5亮、等待释放

//------------------------------------------------------------------------

      D1 = D2 = D3 = D4 = 1;       //关闭


      while(1) {                                 //循环

        while((SW1) && (SW2) && (SW3) && (SW4)); //等待按键

        delay(10);                               //延时

        if((!SW1) || (!SW2) || (!SW3) || (!SW4))  break; //有任意一个按键,就跳出循环

      }


      if (!SW3) { D3 = 0; while (!SW3);}         //D3亮、等待释放


      else { D5 = 0; while((!SW1) || (!SW2) || (!SW4));} //D5亮、等待释放

//------------------------------------------------------------------------

      D1 = D2 = D3 = D4 = 1;       //关闭


      while(1) {                                 //循环

        while((SW1) && (SW2) && (SW3) && (SW4)); //等待按键

        delay(10);                               //延时

        if((!SW1) || (!SW2) || (!SW3) || (!SW4))  break; //有任意一个按键,就跳出循环

      }


      if (!SW4) { D4 = 0; while (!SW4); D4 = 1; D6 = 0;} //D4亮、等待释放、D6亮


      else { D5 = 0; while((!SW1) || (!SW2) || (!SW3));} //D5亮、等待释放

//------------------------------------------------------------------------

      while(1);                                 //按键到此为止

    }

}

//追加提问:http://zhidao.baidu.com/question/589729659.html

//按照提问者追加的要求,本程序,增加了按键消抖的部分,便于用硬件实验。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值