|
|
//2009/11/12
//QL200实验板,芯片:PIC16F877A
//实现功能:简单六位密码锁,通过键盘设置把密码并存入EEPROM中,第一次进入系统时需先设置密码,以后不用;
//密码设置成功后显示Set Succeded! 密码输入成功则显示good ,错误则显示Error,连续输入3次错误则显示NO Chance!需复位系统后才能再输入。
//4X4键盘中对应为:K10-1,K11-2,K12-3,K13-4
// K14-5, K15-6,K16-7,K17-8
// K18-9, K19-0,K20-设置密码 其余按键不用;
#include<pic.h>
__CONFIG(0x1832);
unsigned char table[6];
unsigned char table1[]={' ',' ',' ',' ',' ',' '};
unsigned char result,k=0;
//****************************************
//**************lcd1602*******************
//****************************************
#define rs RA1
#define rw RA2
#define e RA3
//延时程序
void delay()
{
unsigned char i;
for(i=100;i>0;i--);
}
//****************************************
//LCD写一个字节数据
void lcd_write_byte(unsigned char code)
{
PORTD=code;
rs=1;
rw=0;
e=0;
delay();
e=1;
}
//****************************************
//lcd写命令函数
void lcd_enable(unsigned char code)
{
PORTD=code;
rs=0;
rw=0;
e=0;
delay();
e=1;
}
//*****************************************
//lcd显示设置
void lcd_init()
{
lcd_enable(0x01); //清除显示
lcd_enable(0x38); //设置16X2显示,5X7点阵
lcd_enable(0x0c); //开显示,不显示光标
lcd_enable(0x06); //光标左移
}
//*****************************************
//**************EEPROM部分*****************
//*****************************************
//写EEROM
void write_ee(unsigned char addr,unsigned char data)
{
EEADR=addr;
EEDATA=data;
EEPGD=0;
WREN=1;
EECON2=0X55;
EECON2=0XAA;
WR=1;
WREN=1;
while(WR==1);
}
//*****************************************
//读EEROM
unsigned char read_ee(unsigned char addr)
{
EEADR=addr;
EEPGD=0;
RD=1;
return(EEDATA);
}
//*****************************************
//**************按键操作部分***************
//*****************************************
//延时程序
void delay1(unsigned int x)
{
unsigned int i;
for(i=x;x--;);
}
//*****************************************
//按键扫描程序
unsigned char count=0,count1=0;
void scan()
{
unsigned char result;
PORTC=0xFF;
RC4=0;
if(RC3==0) {while(!RC3){ count=1;result='1';}}
if(RC2==0) {while(!RC2){ count=1;result='2';}}
if(RC1==0) {while(!RC1){ count=1;result='3';}}
if(RC0==0) {while(!RC0){ count=1;result='4';}}
PORTC=0xFF;
RC5=0;
if(RC3==0) {while(!RC3){ count=1;result='5';}}
if(RC2==0) {while(!RC2){ count=1;result='6';}}
if(RC1==0) {while(!RC1){ count=1;result='7';}}
if(RC0==0) {while(!RC0){ count=1;result='8';}}
PORTC=0xFF;
RC6=0;
if(RC3==0) {while(!RC3){ count=1;result='9';}}
if(RC2==0) {while(!RC2){ count=1;result='0';}}
if(count==1)
{
count=0;
table[k]=result;
table1[k]='*';
k++;
}
}
//****************************************
//检查输入的密码与设定的密码是否一致
void check_passwrod()
{
if(k==6)
{
unsigned char i,addr=0x01;
k=0;
for(i=1;i<7;i++)
{
if(read_ee(addr)!=table[i-1])
{
lcd_init();
lcd_init();
lcd_enable(0x85);
lcd_write_byte('E');
lcd_enable(0x86);
lcd_write_byte('r');
lcd_enable(0x87);
lcd_write_byte('r');
lcd_enable(0x88);
lcd_write_byte('o');
lcd_enable(0x89);
lcd_write_byte('r');
table1[0]=' '; //空格
table1[1]=' ';
table1[2]=' ';
table1[3]=' ';
table1[4]=' ';
table1[5]=' ';
delay1(40000);
delay1(40000);
count1++;
if(count1>=3)
{
while(1)
{
lcd_enable(0x82); //No Chance!
lcd_write_byte('N');
lcd_enable(0x83);
lcd_write_byte('o');
lcd_enable(0x85);
lcd_write_byte('C');
lcd_enable(0x86);
lcd_write_byte('h');
lcd_enable(0x87);
lcd_write_byte('a');
lcd_enable(0x88);
lcd_write_byte('n');
lcd_enable(0x89);
lcd_write_byte('c');
lcd_enable(0x8a);
lcd_write_byte('e');
lcd_enable(0x8b);
lcd_write_byte('!');
}
}
break;
}
else
addr++;
}
if(addr==0x07)
{
lcd_init();//为什么只初始化一次的话会在0X80位置多出现一个g?
lcd_init();
while(1)
{
count1=0;
lcd_enable(0x86); //输入密码正确,显示good
lcd_write_byte('g');
lcd_enable(0x87);
lcd_write_byte('o');
lcd_enable(0x88);
lcd_write_byte('o');
lcd_enable(0x89);
lcd_write_byte('d');
}
}
lcd_init();
}
}
//****************************
//设置密码
void set_passwrod1()
{
lcd_init();
k=0;
lcd_init();
table1[0]=' '; //显示空格
table1[1]=' ';
table1[2]=' ';
table1[3]=' ';
table1[4]=' ';
table1[5]=' ';
while(1)
{
lcd_enable(0x82); //显示Set Passwrod
lcd_write_byte('S');
lcd_enable(0x83);
lcd_write_byte('e');
lcd_enable(0x84);
lcd_write_byte('t');
lcd_enable(0x86);
lcd_write_byte('P');
lcd_enable(0x87);
lcd_write_byte('a');
lcd_enable(0x88);
lcd_write_byte('s');
lcd_enable(0x89);
lcd_write_byte('s');
lcd_enable(0x8a);
lcd_write_byte('w');
lcd_enable(0x8b);
lcd_write_byte('r');
lcd_enable(0x8c);
lcd_write_byte('o');
lcd_enable(0x8d);
lcd_write_byte('d');
lcd_enable(0x8e);
lcd_write_byte(':');
lcd_enable(0xc5);
lcd_write_byte(table1[0]);
lcd_enable(0xc6);
lcd_write_byte(table1[1]);
lcd_enable(0xc7);
lcd_write_byte(table1[2]);
lcd_enable(0xc8);
lcd_write_byte(table1[3]);
lcd_enable(0xc9);
lcd_write_byte(table1[4]);
lcd_enable(0xca);
lcd_write_byte(table1[5]);
scan();
if(k==6)
{
unsigned char i, addr=0x01;
k=0;
lcd_init();
for(i=1;i<7;i++)
{
write_ee(addr,table[i-1]);
addr++;
}
write_ee(0x00,0x55); //避免单片机复位时需要重新设置密码
lcd_enable(0x81); //显示Set Succeded!
lcd_write_byte('S');
lcd_enable(0x82);
lcd_write_byte('e');
lcd_enable(0x83);
lcd_write_byte('t');
lcd_enable(0x85);
lcd_write_byte('S');
lcd_enable(0x86);
lcd_write_byte('u');
lcd_enable(0x87);
lcd_write_byte('c');
lcd_enable(0x88);
lcd_write_byte('c');
lcd_enable(0x89);
lcd_write_byte('e');
lcd_enable(0x8a);
lcd_write_byte('e');
lcd_enable(0x8b);
lcd_write_byte('d');
lcd_enable(0x8c);
lcd_write_byte('e');
lcd_enable(0x8d);
lcd_write_byte('d');
lcd_enable(0x8e);
lcd_write_byte('!');
table1[0]=' ';
table1[1]=' ';
table1[2]=' ';
table1[3]=' ';
table1[4]=' ';
table1[5]=' ';
delay1(60000);
delay1(60000);
lcd_init();
count1=0;
break;
}
}
}
//***************************************
//设置密码
void set_passwrod()
{
PORTC=0xFF;
RC6=0;
if(RC1==0) set_passwrod1();//查看设置密码键是否按下
}
//***************************************
//显示函数
void display()
{
lcd_enable(0x80); //显示Enter Passwrod:
lcd_write_byte('E');
lcd_enable(0x81);
lcd_write_byte('n');
lcd_enable(0x82);
lcd_write_byte('t');
lcd_enable(0x83);
lcd_write_byte('e');
lcd_enable(0x84);
lcd_write_byte('r');
lcd_enable(0x86);
lcd_write_byte('P');
lcd_enable(0x87);
lcd_write_byte('a');
lcd_enable(0x88);
lcd_write_byte('s');
lcd_enable(0x89);
lcd_write_byte('s');
lcd_enable(0x8a);
lcd_write_byte('w');
lcd_enable(0x8b);
lcd_write_byte('r');
lcd_enable(0x8c);
lcd_write_byte('o');
lcd_enable(0x8d);
lcd_write_byte('d');
lcd_enable(0x8e);
lcd_write_byte(':');
lcd_enable(0xc5);
lcd_write_byte(table1[0]);
lcd_enable(0xc6);
lcd_write_byte(table1[1]);
lcd_enable(0xc7);
lcd_write_byte(table1[2]);
lcd_enable(0xc8);
lcd_write_byte(table1[3]);
lcd_enable(0xc9);
lcd_write_byte(table1[4]);
lcd_enable(0xca);
lcd_write_byte(table1[5]);
}
//****************************************
//管脚初始化
void port_init()
{
ADCON1=0x06;
TRISA=0x00;
TRISD=0x00;
TRISC=0x0F;
count1=0;
}
//****************************************
//主函数
void main()
{
port_init();
lcd_init();
if(read_ee(0x00)!=0x55) set_passwrod1(); //第一次进入时需先设置密码
lcd_init();
while(1)
{
display();
scan(); //按键扫描
check_passwrod(); //查看输入的密码是否正确
set_passwrod(); //设置密码
}
}
|
|
|
|