- #include "reg51.h"
- #include "intrins.h"
- #include"absacc.h"
- #include"math.h"
- #include"string.h"
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- BYTE temp;
- BYTE key;
- BYTE dis_buf;
- BYTE Userpassword[10]="123";
- BYTE inputpassword[10]="";
- int a;a=0;
- sbit led2=P2^2;
- sbit led1=P2^1;
- sbit beep=P2^0;
- /* 液晶1602口地址*/
- #define wr_com XBYTE[0xC000] //写命令
- #define wr_data XBYTE[0xC100] //写数据
- #define rd_com XBYTE[0xC200] //读命令
- #define rd_data XBYTE[0xC300] //读数据
- void Delay(WORD n);
- void Delay2(WORD n);
- void ShowResult(BYTE ch);
- void lcd_init(void); // lcd初始化
- void write_cmd(BYTE cmd); // lcd写命令
- //void write_string(unsigned char *s); // 写字符串
- void write_data(BYTE dat) ; // 写数据
- void set_display_place(BYTE line,column);
- void write_string_lcd(BYTE line,column,unsigned char *string);
- /********************************************************************
- Function name: write_cmd
- Descriptions: 向lcd输入指令
- ********************************************************************/
- void write_cmd(BYTE cmd)
- {
- BYTE dl;
- do {
- dl=rd_com;
- } while((dl&0x80)!=0); //判忙
- wr_com= cmd;
- Delay(1);
- }
- /*******************************************************************
- Function name: write_data
- Descriptions: 写入数据
- *******************************************************************/
- void write_data(BYTE dat)
- {
- BYTE dl;
- do {
- dl=rd_com;
- } while((dl&0x80)!=0); //判忙
- wr_data= dat;
- Delay(1);
- }
- /********************************************************************
- Function name: write_string
- Descriptions: 写入字符串
- ********************************************************************/
- void write_string(BYTE *s)
- {
- while(*s != '\0') //'\0'为字符串结束标志
- {
- write_data(*s);
- s++;
- }
- }
- /***************************************************************
- Function name: set_display_place
- Descriptions: 设置字符的显示位置
- **************************************************************/
- void set_display_place(BYTE line,column)
- {
- BYTE address;
- if(line == 1)
- {
- address = 0x80 + column;
- }
- else if(line == 2)
- {
- address = 0xc0 + column;
- }
- write_cmd(address);
- }
- /*****************************************************************
- Function name: 将字符串写到指定的位置
- Descriptions: 将字符串显示在lcd的特定位置
- *****************************************************************/
- void write_string_lcd(BYTE line,column,unsigned char *string)
- {
- set_display_place(line,column);
- write_string(string);
- Delay(1);
- }
- /***************************************************
- ************液晶模块初始化******************************
- ****************************************************/
- void lcd_init(void)
- {
- write_cmd(0x38);//
- write_cmd(0x38);//
- write_cmd(0x06);//
- write_cmd(0x0c);//
- write_cmd(0x01);//
- }
- void crti(unsigned long dat)
- {
- unsigned char dhi;
- unsigned char dli;
- unsigned char ddi;
- unsigned char dei;
- unsigned long datah;
- unsigned long datal;
- datah=dat;
- dhi=0;
- if (datah>=10)
- {
- do
- {
- datah/=10;
- dhi++;
- }while(datah>=10);
- datah=dat;
- ddi=dhi;
- for (dli=0;dli<dhi;dli++)
- {
- datal=1;
- for (dei=0;dei<ddi;dei++)
- {
- datal*=10;
- }
- datah=dat/datal;
- dat=dat%datal;
- datah+=0x30;
- write_data(datah);
- ddi--;
- }
- dat+=0x30;
- write_data(dat);
- }
- else
- {
- dat=dat+0x30;
- write_data(dat);
- }
- }
- void Delay(WORD n)
- {
- WORD x;
- while(n--)
- {
- x=500; while(x--);
- }
- }
- void Delay2(WORD n)
- {
- WORD x;
- while(n--)
- {
- x=5000; while(x--);
- }
- }
- /*************************************************************/
- /* */
- /* 键扫描子程序 */
- /* */
- /*************************************************************/
- void keyscan(void)
- {
- P1=0x0F; //低四位输入
- Delay(1);
- temp=P1; //读P1口
- temp=temp&0x0F;
- temp=~(temp|0xF0);
- if(temp==1)
- key=0;
- else if(temp==2)
- key=1;
- else if(temp==4)
- key=2;
- else if(temp==8)
- key=3;
- else
- key=16;
-
- P1=0xF0; //高四位输入
- Delay(1);
- temp=P1; //读P1口
- temp=temp&0xF0;
- temp=~((temp>>4)|0xF0);
- if(temp==1)
- key=key+0;
- else if(temp==2)
- key=key+4;
- else if(temp==4)
- key=key+8;
- else if(temp==8)
- key=key+12;
- else
- key=16;
-
- dis_buf = key; //键值入显示缓存
- //dis_buf = dis_buf & 0x0f;
- if(dis_buf>9) //转换为ASCII码
- dis_buf = dis_buf+0x37;
- else
- dis_buf = dis_buf+0x30;
- }
- void clear()
- {write_string_lcd(2,4, " ");
- }
- int fuwei()
- {Delay2(15);
- //
- //
- P1=0xF0;
- if(P1!=0xF0)
- { Delay(1);
- keyscan();
- if(key==15)
- return 0;
- else return 1;
- }
- }
- void main()
- {int j,w,c;
- c=0;
- w=0;
- j=4;
- SP=0x60;
- beep= 1;
- lcd_init();
- while(1)
- {
- write_string_lcd(1,0, "Hello ");
- write_string_lcd(2,0, "key:");
- write_string_lcd(1,10, "warn:");
- set_display_place(1,15);
- write_data(0x30+w);
- if(c==1) write_string_lcd(1,7, "C");
- else write_string_lcd(1,7, " ");
- write_string_lcd(2,j, "_");//光标闪烁
- Delay2(15);
- write_string_lcd(2,j," ");
- Delay2(15);
- //
- //
- P1=0xF0;
- if(P1!=0xF0)
- {led1= 1;
- led2= 1;
- Delay(1);
- keyscan();
- switch(key)
- {case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:
- {inputpassword[a]=key+'0';
- inputpassword[a+1]='\0';
- a++;
- set_display_place(2,j);
- write_data(dis_buf);
- j++;
- }break;
- case 10:
- j++;
- if(strcmp(inputpassword,Userpassword)==0)
- {
- write_string_lcd(2,0, "Unlock OK! ");
- write_string_lcd(1,0, "Welcome! ");
- c=1;
- inputpassword[0]="a";
- a=0;
- led1=0;
- j=4;
- w=0;
- Delay2(200);
- clear();
- }
- else {w++;
-
- if(w<3)
- { write_string_lcd(2,4, " error ");
- inputpassword[10]="";
- a=0;
- led2=0;
- Delay2(60);clear();j=4;c=0;}
- else{while(fuwei()!=0){ write_string_lcd(2,4, " shutdown!!! ");beep=0;}
- inputpassword[10]="";
- clear();
- beep=1;
- a=0;
- j=4;
- w=0;
- c=0;}
- //Delay2(60);
- }
- //clear();
- break;
- case 12:
- if(c==0) {write_string_lcd(2,4, "can't change ");Delay2(60);clear();j=4;a=0;}
- else
- {
-
- Userpassword[0]=inputpassword[0];
- Userpassword[1]=inputpassword[1];
- Userpassword[2]=inputpassword[2];
- Userpassword[3]=inputpassword[3];
- Userpassword[4]=inputpassword[4];
- Userpassword[5]=inputpassword[5];
- Userpassword[6]=inputpassword[6];
- Userpassword[7]=inputpassword[7];
- Userpassword[8]=inputpassword[8];
- Userpassword[9]=inputpassword[9];
- Userpassword[10]=inputpassword[10];
- beep=1;
- j=4;
- w=0;
- c=0;
- a=0;write_string_lcd(2,4, "change ok! "); Delay2(60);clear();j=4;}
- break;
-
- case 14:
- {c=0;
- clear();
- inputpassword[10]="";
- j=4;
- a=0;}
- break;
- }
- }
- }
- }
复制代码 |