#include <STC8.H>//#include <string.h>
//#include <EEPROM.h>
#include <LCD1602.h>
#define MAIN_Fosc 24000000L //定义主时钟
sbit ALAM = P3^4; //报警
sbit KEY = P3^2; //开锁
//sbit dtmf_ok = P1^1; //双音频接收
sbit d0 = P1^2;
sbit d1 = P1^3;
sbit d2 = P1^4;
sbit d3 = P1^5;
unsigned char k,KeyNum,Count=0;
unsigned int Password=0;
unsigned char code initpassword[4]={5,6,7,8}; //初始密码
/***************************************************************************
函数: unsigned char MatrixKey()
描述: dtmf取值函数
参数: 将DTMF解码数据编码为数值
返回: K
版本: VER1.0
日期: 2024-4-23
备注:
***************************************************************************/
unsigned char MatrixKey()
{
switch((P1>>2)&0x0f)
{
case 0x01: k='1'; break;
case 0x02: k='2'; break;
case 0x03: k='3'; break;
case 0x04: k='4'; break;
case 0x05: k='5'; break;
case 0x06: k='6'; break;
case 0x07: k='7'; break;
case 0x08: k='8'; break;
case 0x09: k='9'; break;
case 0x0A: k='0'; break;
case 0x0B: k='*'; break;
case 0x0C: k='#'; break;
case 0x0D: k='A'; break;
case 0x0E: k='B'; break;
case 0x0F: k='C'; break;
case 0x00: k='D'; break;
default: k=0xff; break;
}
return k;
}
void main()
{
P1M0 &= ~0x3f;
P1M1 &= ~0x3f;
P3M0 &= ~0xfc;
P3M1 &= ~0xfc;
//P1=0xff;
LCD_Init();
LCD_ShowString(1,1,"Password:");
while(1)
{
if(K<=9) //如果S1~S10按键按下,输入密码
{
if(Count<4) //如果输入次数小于4
{
Password*=10; //密码左移一位
Password+=K%10; //获取一位密码
Count++; //计次加一
}
LCD_ShowNum(2,1,Password,4); //更新显示
}
if(K=='#') //如果#按键按下,确认
{
if(Password==initpassword[4]) //如果密码等于正确密码
{
LCD_ShowString(1,11,"PassOK "); //显示OK
KEY=~KEY; //打开继电器
Password=0; //密码清零
Count=0; //计次清零
LCD_ShowNum(2,1,Password,4); //更新显示
}
else //否则
{
LCD_ShowString(1,12,"error"); //显示ERR
Password=0; //密码清零
Count=0; //计次清零
LCD_ShowNum(2,1,Password,4); //更新显示
}
}
if(k=='*') //如果*按键按下,取消
{
Password=0; //密码清零
Count=0; //计次清零
LCD_ShowNum(2,1,Password,4); //更新显示
}
}
}
|