//主程序代码
/*--------------------------------------------------------------*/
#include <reg52.h>
#include "LCD5510_V3.H"
#include "matrixkeyscan.c"
uchar count; //密码位计数
uchar pw[6]; //初始密码存储区
uchar pwbuf[6]; //输入密码存储区
uchar selfdefinebuf[6]; //自定义密码存储区
uchar error_cnt; //错误次数计数器
bit enterflag; //确认键按下与否标志
bit pwflag; //密码正确与否标志
bit last_pwflag; //前一次密码输入正确与否标志
unsigned char code pic[] = {
//40*40的图片
0xFF,0x7F,0x1F,0x07,0x03,0x01,0x00,0x03,0x03,0x0F,0x0F,0x1F,0x3F,0x7F,0xFF,0xFF,
0xFF,0xFF,0xF3,0xF7,0xC7,0xCF,0x8F,0x1F,0x3F,0x7F,0xFF,0xFF,0xF7,0xE7,0xCF,0x0F,
0x01,0x83,0x83,0x03,0xC3,0xCF,0xDF,0xFF,0xFF,0xC0,0xE8,0xE4,0xE4,0xE4,0xC4,0xC4,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x07,0x0F,0x0F,0x8F,0x9F,
0xDF,0xDE,0xFE,0xDD,0xFF,0xFF,0x3F,0x3F,0x3F,0xFC,0xFF,0x1F,0x1F,0x0F,0x0F,0x07,
0xFF,0xC1,0x1F,0x7D,0xF8,0xFD,0xFF,0xFF,0xBF,0x07,0x04,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x3F,0xFF,0xF1,0xF1,0xFF,0x7F,0x0F,0xFF,0x0F,
0x04,0x00,0x03,0xE7,0xC4,0x0E,0xAF,0xFF,0xFF,0xFF,0xFE,0xF8,0xE0,0xC0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x00,0x00,
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x80,0xC0,0x60,0xF9,0xF8,0xFC,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFC,0xF8,0xF0,0xF0,0xE0,0xE0,0xC0,0xC0,
0xC2,0x82,0x80,0x80,0x80,0x80,0xC0,0x60,0x60,0x20,0x30,0x18,0x08,0x0E,0x06,0x03,
0x01,0x00,0x00,0xAA,0xFF,0xFF,0xFF,0xFF};
//延时1000000+1us 函数定义
void delay1s(void)
{
unsigned char i,j,k;
for(i=167;i>0;i--)
for(j=171;j>0;j--)
for(k=16;k>0;k--);
}
//初始化设置函数
void init_main()
{
count=0; //初始没有输入密码,计数器设为0
error_cnt=0; //错误次数归0
enterflag=0; //没有按下确认键
pwflag=0; //密码标志先置为0
last_pwflag=0;
/* 假设内定密码为123456, 修改初始密码可在这里改*/
pw[0]=1;
pw[1]=1;
pw[2]=1;
pw[3]=1;
pw[4]=1;
pw[5]=1;
}
bit pwcmp(void) //密码比较函数
{
bit flag;
uchar i;
for(i=0;i<6;i++)
{
if(pw
==pwbuf)
flag=1;
else
{
flag=0;
i=6;
error_cnt++; //错误次数加1
}
}
return (flag);
}
/*--------------------------------------------------------------*/
//主函数
void main(void)
{
uchar j,key,tempkey;
uchar s=2; //指示*号显示的位置
LCD5510_Init(); //液晶初始化
Init(); //初始化函数
init_main(); //密码初始化
LCD_prints(0, 1, "WELCOME^_^");
delay1s(); delay1s();
LCD_clr_scr(); //清屏
while(1)
{
if(error_cnt<=3) //密码错误次数最多为3次
{
LCD_showsh(0, 0, "请输入密码"); //提示请输入密码
key=matrixkeyscan(); //调用键盘扫描函数
if(key>0 && key <=10) //用户输入密码
{
if(key==10)
key=0;
if(count<6)
{
pwbuf[count]=key;
LCD_printc(s, 1, '*'); //用*号加密密码
count++;
s++;
}
}
else if(key==11) //确认键按下
{
enterflag=1; //确认键按下标志
if(count==6) //只有输入6个密码后,按确认键才作密码比较
{
delayms(80);
pwflag=pwcmp();
}
else
{
pwflag=0; //否则直接pwflag赋0
error_cnt++; //错误次数加1
}
last_pwflag=pwflag; //前一次密码输入正确与否标志
enterflag=0; //标志位置回0
count=0; //密码为计数器清零
delayms(80);
if(pwflag==1) //密码正确时
{
LCD_clr_row(0); //清除第1行
LCD_clr_row(1);
LCD_clr_row(2); //清除第2行
LCD_clr_row(3);
LCD_pos_picture(0, 0, 40, 40, pic); //显示40*40的图片
LCD_showsh(3, 0, "密码");
LCD_showsh(3, 1, "正确");
}
else //密码错误时
{
LCD_clr_row(0); //清除第1行
LCD_clr_row(1);
LCD_clr_row(2); //清除第2行
LCD_clr_row(3);
LCD_prints(1, 0, "*WARNING*");
LCD_showsh(1, 1, "密码错误");
while(1)
{
beep();
}
}
}
else if(key==12) //取消键按下
{
count=0; //密码计数清零
}
else if(key==13) //重设密码键按下
{
LCD_clr_row(0); //清除第1行
LCD_clr_row(1);
LCD_clr_row(2); //清除第2行
LCD_clr_row(3);
LCD_prints(2, 0, "Modify"); //显示可以修改密码了
if(last_pwflag==1) //判断上次用户输入密码正确与否
{
while(last_pwflag)
{
tempkey=matrixkeyscan();
if(tempkey>0&&tempkey<=10) //用户输入密码
{
if(tempkey==10)
tempkey=0;
if(count<6)
{
selfdefinebuf[count]=tempkey; //存用户自定义密码
LCD_printc(s, 1, '*'); //用*号加密密码
count++;
s++;
}
}
else if(tempkey==11) //确认键按下
{
enterflag=1;
if(count==6)
{
for(j=0;j<6;j++)
pw[j]=selfdefinebuf[j]; //修改密码存储数组
last_pwflag=0;
enterflag=0;
count=0;
}
else //如果输入数据不足六位或大于六位,输入作废,重新输入
{
LCD_clr_row(2); //清除第2行
LCD_clr_row(3);
LCD_prints(0, 1, "Your password is too long!");
LCD_prints(2, 1, "ERROR!"); //设置密码错误
count = 0; //密码计数清零
}
}
}
}
}
}
else
{
while(1)
{
beep(); //错误次数超过三次,停止工作蜂鸣器一直叫
}
}
keyscan(); //独立按键扫描
LCD_printc(1, 2, (shi/10+0x30));
LCD_printc(2, 2, (shi%10+0x30));
LCD_printc(3, 2, ':');
LCD_printc(4, 2, (fen/10+0x30));
LCD_printc(5, 2, (fen%10+0x30));
LCD_printc(6, 2, ':');
LCD_printc(7, 2, (miao/10+0x30));
LCD_printc(8, 2, (miao%10+0x30));
}
}
[/code]
此帖出自小平头技术问答
一周热门 更多>