|
源文件:
- #include<reg51.h>
- #include<intrins.h>
- sbit RS=P3^1;
- sbit RW=P3^2;
- sbit E=P3^3;
- void lcd_w_cmd(unsigned char com);
- void lcd_w_dat(unsigned char dat);
- unsigned char lcd_r_start();
- void lcd_int();
- void delay(unsigned int i);
- void delay1();
- char keyscan();
- void puts(unsigned char ch[]);
- void set_pw();
- unsigned char codekey_code[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};
- unsigned char pw[6]={0,0,0,0,0,0,};
- unsigned char pwnew[10]={0,0,0,0,0,0,0,0,0,0};
- sbit P30=P3^0;
- void puts(unsigned char ch[])
- {
- unsigned char i;
- i=0;
- while(ch[i]!='\0')
- {
- lcd_w_dat(ch[i]);
- i++;
- }
- }
- void set_pw()
- {
- unsigned char key,count;
- lcd_w_cmd(0x01);
- puts("password setup:");
- lcd_w_cmd(0xc0);
- while(1)
- {
- key=keyscan();
- if(key>=0&&key<=9)
- {
- lcd_w_dat(key+0x30);
- pw[count]=key;
- count++;
- if(count==6)break;
- P2=0xf0;
- while(P2!=0xf0);}
- }
- }
- void main()
- {
- unsigned char i,count;
- bit pw_flag;
- P1=0xff;
- lcd_int();
- delay(255);
- lcd_w_cmd(0x80);
- puts("password:");
- count=0;
- while(1)
- {
- i=keyscan();
- if(i!=-1)
- {
- if(i>=0&&i<=9)
- {
- pwnew[count]=i;
- lcd_w_dat(i+0x30);
- count++;
- P2=0xf0;
- while(P2!=0xf0);}
- else if(i==10)
- {
- pw_flag=1;
- if(count!=6)
- pw_flag=0;
- else
- {
- for(i=0;i<6;i++)
- {
- if(pw[i]!=pwnew[i])
- {
- pw_flag=0;break;}
- }
- }
- if(pw_flag==1)
- {
- lcd_w_cmd(0xc0);
- puts("pass");
- P30=0;
- P2=0xf0;
- while(P2!=0xf0);
- while(1)
- {
- i=keyscan();
- if(i!=-1)
- break;
- }
- P2=0xf0;
- while(P2!=0xf0);
- if(i==11)set_pw();
- }
- else
- {
- lcd_w_cmd(0xc0);
- P30=1;
- puts("error");}
- count=0;
- delay(50000);
- lcd_w_cmd(0x01);
- puts("password:");
- }}}}
- char keyscan()
- {
- char scan1,scan2,keycode,j,key;
- key=-1;
- P2=0xf0;
- scan1=P2;
- if(scan1!=0xf0)
- {
- delay(1200);
- scan1=P2;
- if(scan1!=0xf0)
- {
- P2=0x0f;
- scan2=P2;
- keycode=scan1|scan2;
- for(j=0;j<16;j++)
- {
- if(keycode==codekey_code[j])
- {
- key=j;break;}
- }}}
- return(key);
- }
- void delay1()
- {
- _nop_();
- _nop_();
- _nop_();
- }
- void lcd_int()
- {
- lcd_w_cmd(0x3c);
- lcd_w_cmd(0x0e);
- lcd_w_cmd(0x01);
- lcd_w_cmd(0x06);
- lcd_w_cmd(0x80);
- }
- unsigned char lcd_r_start()
- {
- unsigned char s;
- RW=1;
- delay1();
- RS=0;
- delay1();
- E=1;
- delay1();
- s=P1;
- delay1();
- E=0;
- delay1();
- RW=0;
- delay1();
- return(s);
- }
- void lcd_w_cmd(unsigned char com)
- {
- unsigned char i;
- do{
- i=lcd_r_start();
- i&=0x80;
- delay(2);
- }
- while(i!=0);
- RW=0;
- delay1();
- RS=0;
- delay1();
- E=1;
- delay1();
- P1=com;
- delay1();
- E=0;
- delay1();
- RW=1;
- delay(255);
- }
- void lcd_w_dat(unsigned char dat)
- {
- unsigned char i;
- do{
- i=lcd_r_start();
- i&=0x80;
- delay(2);
- }
- while(i!=0);
- RW=0;
- delay1();
- RS=1;
- delay1();
- E=1;
- delay1();
- P1=dat;
- delay1();
- E=0;
- delay1();
- RW=1;
- delay(255);
- }
- void delay(unsigned int i)
- {unsigned int k;
- for(k=0;k<i;k++);
- }
复制代码
完整程序 电路图 仿真文件下载:
lcd 密码锁.zip
(75.46 KB, 下载次数: 101)
|
评分
-
查看全部评分
|