void init_main() {
Lcd_Initial(); //对12864液晶显示屏的初始化作用
number = 0; //密码存储,显示位置,各方面清零
P32 = 0; //进行电磁铁的初始关闭作用
//显示居民小区:
Lcd_Character_16X16( 1, 0, 0,data_shuju[0]);
Lcd_Character_16X16( 1, 0, 16,data_shuju[1]);
Lcd_Character_16X16( 1, 0, 32,data_shuju[2]);
Lcd_Character_16X16( 1, 0, 48,data_shuju[3]);
Lcd_Character_16X16( 1, 0, 64,data_shuju[4]);
//显示请输入密码:
Lcd_Character_16X16( 1, 4, 0,data_shuju[5]);
Lcd_Character_16X16( 1, 4, 16,data_shuju[6]);
Lcd_Character_16X16( 1, 4, 32,data_shuju[7]);
Lcd_Character_16X16( 1, 4, 48,data_shuju[8]);
Lcd_Character_16X16( 1, 4, 64,data_shuju[9]);
Lcd_Character_16X16( 1, 4, 80,data_shuju[10]);
EA = 1; //总中断打开
EX1 = 1; //外部中断1打开
IT1 = 0; //外部中断1工作方式,为低电平触发,IT1 = 1为下降沿触发
}
/***************************************************************
函数:主函数
功能:综合各个函数的作用
注意:进入主函数的时候,首先开启初始化函数功能
***************************************************************/
void main() {
init_main(); //初始化主函数之前的所有操作
while(1) { //进入无限循环当中
key_scan(); //进行键盘扫描函数
}
//clear_screen();
}
/*********************************************************************
函数:键盘扫描函数
功能:集成了输入密码,显示密码个数,清除密码,
显示密码正确与错误各种功能的集成函数
可称为本函数的核心函数之一
键盘扫描方式:采用的翻转法的键盘扫描,可以减少扫描代码的数量
注意:本函数采用的是带有返回值,不带参数的函数,所以在函数的
末尾一定要加入return keyscan;这个语句
**********************************************************************/
uchar key_scan()
{ //函数开始
unsigned char k=0,p1_h=0,p1_l=0; //翻转法的定义k为选择分支作用,
uchar Mark; //p1_h是选择分支的高四位,p1_l是选择分支的低四位
char keyscan = 0, i = 0, j = 0; //记录输入的数据是数值,然后将其送入到shuru所代表的数组当中去
//i的作用记录输入的数据是否是6位数据,防止超出所存储的数组的空间
//j的作用是用来加强功能键“确认”的作用
/*************************/
P1 = 0xf0; //翻转法先给P1口送数据
k = P1; //然后赋值给k
if(k != 0xf0) //判断k是否是0xf0,如果不是,进入内部执行相应的程序
{
delayms(50); //延时,防抖动
k = P1; //重新赋值
if(k != 0xf0) //确定是否k不等于0xf0
{
number++; //number用于确定后面的在12864中的输出位置和密码存储的位置
p1_h = P1; //将P1的高四位赋值给p1_h
P1 = 0x0f; //然后将0x0f赋值给P1口,翻转,确定低四位,赋值给p1_l
p1_l = P1; //将P1口的值赋给p1_l(是小写的字母l,不是数字1)
k = p1_h|p1_l; //将高四位和低四位相或,然后赋值给k
if (k == 0XE7) { //此判断是为了确定是否是确认按键,如果是,直接进入确定程序
j = 1;
}
if(number == 7) //此判断是为了确定输入的是否是6个数字,防止多输入或者少输入
{
if (k != 0XB7) { //这个是判断是否是清除按键,
number = 0;
}
}
while (p1_l == P1); //这个是松手检测,防止长时间按在按键上,使输入的数据重复
if (number > 0||j == 1){
switch(k) //条件分支语句
{
case 0XD7: keyscan = 0; Lcd_Character_16X8( 1, 6, 8*number,shuzi[0]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XEE: keyscan = 1; Lcd_Character_16X8( 1, 6, 8*number,shuzi[1]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XDE: keyscan = 2; Lcd_Character_16X8( 1, 6, 8*number,shuzi[2]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XBE: keyscan = 3; Lcd_Character_16X8( 1, 6, 8*number,shuzi[3]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XED: keyscan = 4; Lcd_Character_16X8( 1, 6, 8*number,shuzi[4]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XDD: keyscan = 5; Lcd_Character_16X8( 1, 6, 8*number,shuzi[5]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XBD: keyscan = 6; Lcd_Character_16X8( 1, 6, 8*number,shuzi[6]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XEB: keyscan = 7; Lcd_Character_16X8( 1, 6, 8*number,shuzi[7]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XDB: keyscan = 8; Lcd_Character_16X8( 1, 6, 8*number,shuzi[8]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XBB: keyscan = 9; Lcd_Character_16X8( 1, 6, 8*number,shuzi[9]);delayms(1000);Lcd_Character_16X8( 1, 6, 8*number,shuzi[10]);shuru[number-1] = keyscan;break; //keyscan是用来确定输入的数据,number是用来确定输入的位置,shuru【】是用来放置密码
case 0XE7: number = 0;
for(i = 0; i < 6; i++)
if (mima[i] != shuru[i])//对比原始密码和输入密码
{
Mark++;
//该if语句是用来判断密码Mark的错误次数,一旦达到3次,
//就进入到while循环当中,并且P30口 = 0;蜂鸣器响,直至单片机复位才可消失
if (3 == Mark) {
P30 = 0;
Lcd_Character_16X8( 1, 6, 8*1,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*2,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*3,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*4,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*5,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*6,disappear_char[0]);
while(1);
}
//密码错误
Lcd_Character_16X16( 1, 6, 64,mistake[0]);
Lcd_Character_16X16( 1, 6, 80,mistake[1]);
Lcd_Character_16X16( 1, 6, 96,mistake[2]);
Lcd_Character_16X16( 1, 6, 112,mistake[3]);
//消除密码显示
delayms(1000);
Lcd_Character_16X8( 1, 6, 8*1,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*2,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*3,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*4,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*5,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*6,disappear_char[0]);
//消除密码错误汉字显示
Lcd_Character_16X16( 1, 6, 64,disappear_hanzi[0]);
Lcd_Character_16X16( 1, 6, 80,disappear_hanzi[0]);
Lcd_Character_16X16( 1, 6, 96,disappear_hanzi[0]);
Lcd_Character_16X16( 1, 6, 112,disappear_hanzi[0]);
break;
}
if(i == 6) //确认密码是否是正确的,通过i值
{
//输入密码正确
Lcd_Character_16X16( 1, 6, 64,mistake[0]);
Lcd_Character_16X16( 1, 6, 80,mistake[1]);
Lcd_Character_16X16( 1, 6, 96,mistake[4]);
Lcd_Character_16X16( 1, 6, 112,mistake[5]);
delayms(1000); //延时
//消除密码显示位
Lcd_Character_16X8( 1, 6, 8*1,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*2,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*3,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*4,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*5,disappear_char[0]);
Lcd_Character_16X8( 1, 6, 8*6,disappear_char[0]);
//消除密码正确显示位
Lcd_Character_16X16( 1, 6, 64,disappear_hanzi[0]);
Lcd_Character_16X16( 1, 6, 80,disappear_hanzi[0]);
Lcd_Character_16X16( 1, 6, 96,disappear_hanzi[0]);
Lcd_Character_16X16( 1, 6, 112,disappear_hanzi[0]);
P32 = 1;
delayms(2000);
P32 = 0;
}
break;
case 0XB7: keyscan = 10;number--;Lcd_Character_16X8( 1, 6, 8*number,disappear_char[0]);number--;break;
//这个是用来清除输错的密码,然后重新输入新的位数的密码
default: break;
/* case 0XE7: return(4);break;
case 0XDE: return(5);break;
case 0XDD: return(6);break;
case 0XDB: return(7);break;
case 0XD7: return(8);break;
case 0XBE: return(9);break;
case 0XBD: return(10);break;
case 0XBB: return(11);break;
case 0XB7: return(12);break;
case 0X7E: return(13);break;
case 0X7D: return(14);break;
case 0X7B: return(15);break;
case 0X77: return(16);break;
default: return(0);break; */
}
}
}
}
return keyscan; //函数返回的参数
|