51单片机学习2

让开发板的矩阵键盘显示1,2,3等数字并在LCD屏中显示。

模块化,LCD屏的调用代码可以下载后直接调用

以下位矩阵子函数代码

#include <REGX52.H>

#include "Delay.h"

unsigned char MatrixKey()

{

       unsigned char KeyNumber=0;

       P1=0xFF; #注:P1=0xFF表示I/O都为高电频无法输出电流

       P1_3=0; #注:P1_3=0表示1_3口为低电频可以输出电流进行调用

       if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;}

       if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;}

       if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;}

       if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;}

      

       P1=0xFF;

       P1_2=0;

       if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;}

       if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;}

       if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;}

       if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;}

      

       P1=0xFF;

       P1_1=0;

       if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;}

       if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;}

       if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;}

       if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;}

      

       P1=0xFF;

       P1_0=0;

       if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;}

       if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;}

       if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;}

       if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;}

      

       return KeyNumber;

}

以下为主函数代码

#include <REGX52.H>

#include "Delay.h"

#include "LCD1602.h"

#include "Materix.h"

unsigned char KeyNum;

void main()

{

       LCD_Init();#注:每次调用LCD都需要初始化

       LCD_ShowString(1,1,"MatrixKey"); #注:在LCD屏的第一行第一列开始打印“MatrixKey”

       while(1)

       {

              KeyNum=MatrixKey();

              if(KeyNum){   #注:当KeyNum获得一个返回值时,执行

                     LCD_ShowNum(2,1,KeyNum,2); #注:在LCD屏的第二行第一列打印KeyNum的值,其值占用两格屏幕

              }

      

       }

}

密码锁

#include <REGX52.H>

#include "Delay.h"

#include "LCD1602.h"

#include "MatrixKey.h"

unsigned char Number;

unsigned int password=5045;

unsigned int psw;

unsigned int i;

void main()

{

       LCD_Init();

       LCD_ShowString(1,1,"Password");

       while(1)

       {

              Number=MatrixKey();

            if(Number<=10 &&Number !=0 && i<4) #注:当去除条件Number!=0时 按键没有反应 原因是当没有按键时Number收到的返回值是0,0也小于10,所以很快就判断完了 i累加到了4,所以按键没反应。

              {

                     if(Number%10==0)

                     {

                            Number=0;

                     }

                     psw*=10;

                     psw+=Number;

                     i++;

              }

              LCD_ShowNum(2,1,psw,4);

              if(Number==11 && psw==password)

              {

                     LCD_ShowString(1,13,"OK ");

              }

              if(Number==11 && psw !=password)

              {

                     LCD_ShowString(1,13,"ERR");

              }

              if(Number >=12)

              {

                     LCD_Init();

                     LCD_ShowString(1,1,"Password");

                     i=0;

                     psw=0;

              }

                    

       }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值