|
- //proteus仿真论坛
- #include<reg51.h>
- #include<intrinS.h>
- #include<absacc.h>
- #define uchar unsigned char
- #define C02_write 0xa0
- #define C02_read 0xa1
- #define no0 0x28
- #define no1 0x14
- #define no2 0x24
- #define no3 0x44
- #define no4 0x12
- #define no5 0x22
- #define no6 0x42
- #define no7 0x11
- #define no8 0x21
- #define no9 0x41
- #define enter 0x88
- #define backspace 0x81
- #define lcm_write_cmd_add XBYTE[0x80FF]
- #define lcm_write_data_add XBYTE[0x81FF]
- #define lcm_read_busy_add XBYTE[0x82FF]
- #define lcm_read_data_add XBYTE[0x83FF]
- uchar idata temp5_password[6]={0x00,0x00,0x00,0x00,0x00,0x00};
- uchar idata key_code[]={no0,no1,no3,no4,no5,no6,no7,no8,no9};
- sbit SCL=P3^0;
- sbit SDA=P3^1;
- sbit i=P3^2;
- bit ack;
- uchar int_counter_1;
- unsigned int int_counter_2;
- void delayms(uchar i)
- {
- uchar j;
- for(;i>0;i--)
- for(j=124;j>0;j--);
- }
- void longdelay(uchar i)
- {
- unsigned int j;
- for(;i>0;i--)
- for(j=10000;j>0;j--);
- }
- void lcm_wait()
- {
- while(lcm_read_busy_add&0x80);
- }
- void lcm_write_cmd(uchar lcm_cmd)
- {
- lcm_wait();
- lcm_write_cmd_add=lcm_cmd;
- }
- void lcm_write_data(uchar lcm_data)
- {
- lcm_wait();
- lcm_write_data_add=lcm_data;
- }
- uchar lcm_read_data(void)
- {
- uchar lcm_data;
- lcm_wait();
- lcm_data=lcm_read_data_add;
- return(lcm_data);
- }
- void lcm_setxy(uchar x,uchar y)
- {
- if(y==1) lcm_write_cmd(x|0x80);
- if(y==2) lcm_write_cmd(x|0xc0);
- }
- void lcm_write_string(uchar *string)
- {
- uchar i=0;
- while(string!='\0')
- {
- lcm_write_data(string);
- i++;
- }
- }
- void lcm_roll_display(uchar *string,uchar y)
- {
- uchar counter;
- lcm_write_cmd(0x06);
- lcm_setxy(0,y);
- while(string[counter]!='\0')
- {
- lcm_write_data(string[counter]);
- counter++;
- if (counter==19) lcm_setxy(19,y);
- longdelay(2);
- }
- lcm_write_cmd(0x07);
- lcm_setxy(19,y);
- while(string[counter]!='\0')
- {
- lcm_write_data(string[counter]);
- counter++;
- if (counter==19) lcm_setxy(19,y);
- longdelay(2);
- }
- }
- void lcm_init()
- {
- delayms(20);
- lcm_write_cmd_add=0x38;
- delayms(4);
- lcm_write_cmd_add=0x38;
- delayms(1);
- lcm_write_cmd_add=0x38;
- lcm_write_cmd(0x38);
- lcm_write_cmd(0x0f);
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- }
- void I2C_start(void)
- {
- SDA=1;
- SCL=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SDA=0;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SCL=0;
- _nop_();
- _nop_();
- }
- void I2C_stop(void)
- {
- SDA=0;
- SCL=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SDA=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SCL=0;
- _nop_();
- _nop_();
- }
- void I2C_ackownledge(void)
- {
- SDA=0;
- _nop_();
- _nop_();
- SCL=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SCL=0;
- _nop_();
- _nop_();
- _nop_();
- }
- void I2C_no_ackownledge(void)
- {
- SDA=1;
- _nop_();
- _nop_();
- SCL=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SCL=0;
- _nop_();
- _nop_();
- _nop_();
- }
- void I2C_sendB(uchar byte)
- {
- uchar counter;
- for(counter=0;counter<8;counter++)
- {
- if(byte&0x80) SDA=1;
- else SDA=0;
- _nop_();
- SCL=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SCL=0;
- _nop_();
- _nop_();
- byte<<=1;
- }
- _nop_();
- _nop_();
- SDA=1;
- _nop_();
- _nop_();
- _nop_();
- SCL=1;
- _nop_();
- _nop_();
- _nop_();
- if(SDA==0) ack=1;
- else ack=0;
- SCL=0;
- _nop_();
- _nop_();
- }
- uchar I2C_receiveB(void)
- {
- uchar temp;
- uchar counter;
- temp=0;
- SDA=1;
- _nop_();
- _nop_();
- for(counter=0;counter<8;counter++)
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SCL=1;
- _nop_();
- _nop_();
- if(SDA==1) temp=(temp<<1)|0x01;
- else temp=temp<<1;
- _nop_();
- _nop_();
- SCL=0;
- _nop_();
- _nop_();
- _nop_();
- }
- _nop_();
- _nop_();
- return(temp);
- }
- /*bit I2C_write_byte(uchar byte,uchar address)
- {
- I2C_sendB(address);
- if(ack=0)
- {
- I2C_stop();
- return(0);
- }
- else I2C_sendB(byte); */
- bit I2C_send_string(uchar *string,uchar no,uchar address)
- {
- uchar counter;
- for(counter=0;counter<no;counter++)
- {
- I2C_start();
- I2C_sendB(C02_write);
- if(ack==0) return(0);
- I2C_sendB(address+counter);
- if(ack==0) return(0);
- I2C_sendB(string[counter]);
- I2C_stop();
- delayms(20);
- }
- return(1);
- }
- bit I2C_receive_string(uchar *string,uchar no,uchar address)
- {
- uchar counter;
- for(counter=0;counter<no;counter++)
- {
- I2C_start();
- I2C_sendB(C02_write);
- if(ack==0) return(0);
- I2C_sendB(address+counter);
- if(ack==0) return(0);
- I2C_start();
- I2C_sendB(C02_read);
- if(ack==0) return(0);
- *(string+counter)=I2C_receiveB();
- I2C_no_ackownledge();
- I2C_stop();
- }
- }
- uchar get_key()
- {
- uchar row_code;
- uchar col_code;
- P1=0xf0;
- if(P1!=0xf0)
- {
- delayms(10);
- if(P1!=0xf0)
- {
- row_code=0xfe;
- while(row_code!=0x7f)
- {
- P1=row_code;
- if(P1!=row_code)
- {
- col_code=(P1&0xf0)|0x0f;
- return( (~col_code)|(~row_code));
- }
- row_code=((row_code<<1)|0x01);
- }
- }
- }
- return(0x00);
- }
- void convert_code(uchar *string)
- {
- uchar counter=0;
- for(counter=0;counter<6;counter++)
- {
- switch(*string)
- {
- case no0:
- *string=0x00;
- break;
- case no1:
- *string=0x01;
- break;
- case no2:
- *string=0x02;
- break;
- case no3:
- *string=0x03;
- break;
- case no4:
- *string=0x04;
- break;
- case no5:
- *string=0x05;
- break;
- case no6:
- *string=0x06;
- break;
- case no7:
- *string=0x07;
- break;
- case no8:
- *string=0x08;
- break;
- case no9:
- *string=0x09;
- break;
- default:
- break;
- }
- string++;
- }
- }
- bit compare_string(uchar *string1,uchar *string2)
- {
- uchar counter;
- for(counter=0;counter<6;counter++)
- {
- if(string1[counter]!=string2[counter]) return(0);
- }
- return(1);
- }
- uchar step_choose(void)
- {
- unsigned int i;
- uchar key;
- do{
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- lcm_setxy(0,1);
- lcm_write_string("input password");
- lcm_setxy(0,2);
- lcm_write_string("please press 1");
- for(i=0;i<30000;i++)
- {
- key=get_key();
- if((key==no1)||(key==no2)) break;
- }
- if((key!=no1)&&(key!=no2))
- {
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- lcm_setxy(0,1);
- lcm_write_string("change password");
- lcm_setxy(0,2);
- lcm_write_string("please press 2");
- for(i=0;i<30000;i++)
- {
- key=get_key();
- if((key==no1)||(key==no2)) break;
- }
- }
- }
- while((key!=no1)&&(key!=no2));
- return(key);
- }
- bit input_password(uchar *password)
- {
- uchar counter;
- uchar key;
- lcm_setxy(0,2);
- for(counter=0;counter<7;counter++)
- {
- longdelay(3);
- if(counter<6)
- {
- do{
- key=get_key();
- }
- while(key==0x00);
- if((key!=backspace)&&(key!=enter))
- {
- lcm_write_data('*');
- password[counter]=key;
- }
- if(key==backspace)
- {
- if(counter>0)
- {
- lcm_setxy(--counter,2);
- lcm_write_data(' ');
- password[counter]=0x00;
- lcm_setxy(counter,2);
- counter--;
- }
- }
- if(key==enter)
- {
- lcm_setxy(0,1);
- return( 0);
- }
- }
- if(counter==6)
- {
- do{
- key=get_key();
- }
- while((key!=backspace)&&(key!=enter));
- if(key==backspace)
- {
- lcm_setxy(--counter,2);
- lcm_write_data(' ');
- password[counter]=0x00;
- lcm_setxy(counter,2);
- counter--;
- }
- if(key==enter)
- {
- return(1);
- }
- }
- }
- }
- void mima()
- {
- uchar key;
- uchar idata temp1_password[6]={0,0,0,0,0,0};
- uchar idata temp3_password[6]={0,0,0,0,0,0};
- uchar idata temp2_password[6]={0,0,0,0,0,0};
- uchar idata temp4_password[6]={0,0,0,0,0,0};
- key=step_choose();
- if(key==no1)
- {
- I2C_receive_string(temp1_password,6,0x00);
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- lcm_setxy(2,0);
- lcm_write_string("input password");
- if(input_password(temp2_password))
- {
- convert_code(temp2_password);
- if(compare_string(temp1_password,temp2_password))
- {
- lcm_setxy(0,2);
- lcm_write_string("correct!");
- longdelay(6);
- return;
- }
- else
- {
- lcm_setxy(0,2);
- lcm_write_string("wrong password");
- longdelay(6);
- return;
- }
- }
- else
- {
- lcm_setxy(0,2);
- lcm_write_string("error! ");
- longdelay(6);
- }
- }
- else
- {
- I2C_receive_string(temp1_password,6,0x00);
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- lcm_setxy(0,0);
- lcm_write_string("input old password");
- if(input_password(temp2_password))
- {
- convert_code(temp2_password);
- if(compare_string(temp1_password,temp2_password))
- {
- lcm_setxy(0,2);
- lcm_write_string("password correct!");
- longdelay(6);
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- lcm_setxy(0,1);
- lcm_write_string("input new password");
- if(input_password(temp3_password))
- {
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- lcm_setxy(0,1);
- lcm_write_string("input password again");
- if(input_password(temp4_password))
- {
- if(compare_string(temp3_password,temp4_password))
- {
- convert_code(temp3_password);
- I2C_send_string(temp3_password,6,0x00);
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- lcm_setxy(0,1);
- lcm_write_string("password has");
- lcm_setxy(0,2);
- lcm_write_string("been changed");
- longdelay(10);
- return;
- }
- else
- {
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- lcm_setxy(0,1);
- lcm_write_string("twice input");
- lcm_setxy(0,2);
- lcm_write_string("is different");
- longdelay(10);
- return;
- }
- }
- else
- {
- lcm_setxy(0,2);
- lcm_write_string("error! ");
- longdelay(10);
- return;
- }
- }
- else
- {
- lcm_setxy(0,2);
- lcm_write_string("error! ");
- longdelay(10);
- return;
- }
- }
- else
- {
- lcm_setxy(0,2);
- lcm_write_string("wrong password");
- longdelay(6);
- return;
- }
- }
- else
- {
- lcm_setxy(0,2);
- lcm_write_string("error! ");
- longdelay(6);
- return;
- }
- }
- }
- void int_service(void) using 0 interrupt 0
- {
- EA=0;
- EX0=0;
- for(int_counter_1=10;int_counter_1>0;int_counter_1--)
- {
- for(int_counter_2=0xffff;int_counter_2>0;int_counter_2--)
- {
- if(i)
- {
- EA=1;
- EX0=1;
- return;
- }
- }
- }
- lcm_write_cmd(0x01);
- lcm_write_cmd(0x06);
- lcm_write_string("rest success!");
- longdelay(12);
- I2C_send_string(temp5_password,6,0x00);
- (*(void(*)())0)();
- }
- void main(void)
- {
- IT0=1;
- EX0=1;
- EA=1;
- lcm_init();
- while(1)
- {
- mima();
- }
- }
|
|