软件 :keil 5
部分所用寄存器以及原理图:
单片机采用”强下拉,弱上拉“的工作模式(准双向口)
输入1时,输出能力有限,输入0时,输出能力强。
具体是怎样可以在优快云中搜索“强下拉,弱上拉”,比我解释得更专业。
如下为完整代码展示:
mian.c 文件
#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.H"
#include "MatrixKey.h"
unsigned char KeyNum;
unsigned int Password;
int count;
void main()
{
LCD_Init();
LCD_ShowString(1,1,"Password:");
while(1)
{
KeyNum=MatrixKey();
if(KeyNum) //ÊäÈë1-10µÄÊý×Ö×÷ΪÃÜÂë
{
if(KeyNum<=10)
{
if(count<=4)
{Password*=10;
Password+=KeyNum%10;}
count++;
}
LCD_ShowNum(2,1,Password,4);
if(KeyNum==11) //È·ÈÏ
{
if(Password%10000==2345)
LCD_ShowString(1,14,"OK ");
else
LCD_ShowString(1,14,"ERR");
Password=0;
count=0;
}
if(KeyNum==12) //È¡Ïû
{
Password=0;
count=0;
LCD_ShowNum(2,1,Password,4);
LCD_ShowString(1,14," ");
}
}
}
}
以下均为头文件代码
Delay . h
#ifndef __DELAY_H__
#define __DELAY_H__
void Delay(unsigned int xms);
#endif
Delay.c
void Delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
Matrixkey.c
#include <REGX52.H>
#include "Delay.H"
unsigned char MatrixKey()
{
unsigned char KeyNumber=0;
P1=0xFF;
P1_3=0;
if(P1_7==0){Delay(20);while(P1_7==0);KeyNumber=1;}
if(P1_6==0){Delay(20);while(P1_6==0);KeyNumber=5;}
if(P1_5==0){Delay(20);while(P1_5==0);KeyNumber=9;}
if(P1_4==0){Delay(20);while(P1_4==0);KeyNumber=13;}
P1=0xFF;
P1_2=0;
if(P1_7==0){Delay(20);while(P1_7==0);KeyNumber=2;}
if(P1_6==0){Delay(20);while(P1_6==0);KeyNumber=6;}
if(P1_5==0){Delay(20);while(P1_5==0);KeyNumber=10;}
if(P1_4==0){Delay(20);while(P1_4==0);KeyNumber=14;}
P1=0xFF;
P1_1=0;
if(P1_7==0){Delay(20);while(P1_7==0);KeyNumber=3;}
if(P1_6==0){Delay(20);while(P1_6==0);KeyNumber=7;}
if(P1_5==0){Delay(20);while(P1_5==0);KeyNumber=11;}
if(P1_4==0){Delay(20);while(P1_4==0);KeyNumber=15;}
P1=0xFF;
P1_0=0;
if(P1_7==0){Delay(20);while(P1_7==0);KeyNumber=4;}
if(P1_6==0){Delay(20);while(P1_6==0);KeyNumber=8;}
if(P1_5==0){Delay(20);while(P1_5==0);KeyNumber=12;}
if(P1_4==0){Delay(20);while(P1_4==0);KeyNumber=16;}
return KeyNumber;
}
Matrixkey.h
#ifndef __MatrixKey_H__
#define __MatrixKey_H__
unsigned char MatrixKey();
#endif