}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;}
void lcm_setxy(uchar x,uchar y)//光标定位,x定位字符位置,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[i]!='\\0') {lcm_write_data(string[i]);i++;}} void lcm_init()//LCM初始化
{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)//I2C开始信号
{SDA=1;SCL=1;_nop_();_nop_();_nop_();_nop_();_nop_(); SDA=0;_nop_();_nop_();_nop_();_nop_();_nop_();SCL=0; _nop_();_nop_();}void I2C_stop(void)//I2C结束
{SDA=0;SCL=1;_nop_();_nop_();_nop_();_nop_();_nop_(); SDA=1;_nop_();_nop_();_nop_();_nop_();_nop_();SCL=0; _nop_();_nop_();}
void I2C_no_ackownledge(void)//发送noack信号
{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_send_string(uchar *string,uchar no,uchar address)//发送六位密码 {uchar counter;for(counter=0;counter 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 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!=0xef){P1=row_code; if(P1!=row_code){col_code=P1;while(col_code==P1); col_code=(col_code&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)//系统初始化 {uinti;ucharkey;do{lcm_write_cmd(0x01);lcm_write_cmd(0x06); lcm_setxy(5,1);lcm_write_string(\ \lcm_setxy(0,2);lcm_write_string(\ for(i=0;i<30000;i++){key=get_key();if((key==open)||(key==modify)) break;} } while((key!=open)&&(key!=modify));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('*');keysound();password[counter]=key;} if(key==backspace){if(counter>0) {lcm_setxy(--counter,2);lcm_write_data(' '); password[counter]=0x00;lcm_setxy(counter,2); counter--;keysound();}} if(key==enter){lcm_setxy(0,1);keysound(); 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--; keysound();}if(key==enter){keysound(); return(1);}}}} void lock() //主函数 {uchar key; uchar error_counter_1; uchar error_counter_2; 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==open) {keysound();I2C_receive_string(temp1_password,6,0x00); lcm_write_cmd(0x01); lcm_write_cmd(0x06); lcm_setxy(2,0); lcm_write_string(\ if(input_password(temp2_password)){convert_code(temp2_password);if(compare_string(temp1_password,temp2_password)) {lcm_setxy(0,2); lcm_write_string(\ \ led=0; longdelay(20);led=1; return; }else {lcm_setxy(0,2); lcm_write_string(\ \ if(error_counter_1<3){alarm1();}else{ while(1){ lcm_write_cmd(0x01);lcm_write_cmd(0x06);lcm_setxy(4,1);lcm_write_strig(\ \lcm_write_string(\ else{lcm_setxy(0,2); lcm_write_string(\ \longdelay(15); } } else{keysound();I2C_receive_string(temp1_password,6,0x00); lcm_write_cmd(0x01); lcm_write_cmd(0x06);lcm_setxy(0,1); lcm_write_string(\if(input_password(temp2_password)) {convert_code(temp2_password); if(compare_string(temp1_password,temp2_password)) {lcm_setxy(0,2); lcm_write_string(\ \longdelay(10); lcm_write_cmd(0x01); lcm_write_cmd(0x06); lcm_setxy(0,1); lcm_write_string(\ \if(input_password(temp3_password)) {lcm_write_cmd(0x01); lcm_write_cmd(0x06); lcm_setxy(0,1); lcm_write_string(\ 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(2,1); lcm_write_string(\lcm_write_string(\ \return; } else{lcm_write_cmd(0x01); lcm_write_cmd(0x06); lcm_setxy(2,1); lcm_write_string(\ input\ lcm_setxy(2,2); lcm_write_string(\ different! \longdelay(10); return;}} else{lcm_setxy(0,2); lcm_write_string(\ \longdelay(15); return;}} else{lcm_setxy(0,2); lcm_write_string(\ \longdelay(15); return;}} else {lcm_setxy(0,2); lcm_write_string(\ \error_counter_2++ ; if(error_counter_2<3) {alarm1();else{while(1) { lcm_write_cmd(0x01);lcm_write_cmd(0x06); lcm_setxy(4,1); lcm_write_string(\ \keysound();lcm_write_cmd(0x01);lcm_write_cmd(0x06); lcm_setxy(16,1); lcm_write_string(\ }} return;}} else{lcm_setxy(0,2); lcm_write_string(\ \longdelay(15); return; }}} void int_service(void) using 0 interrupt 0 //密码初始化中断函数 {EA=0; EX0=0; lcm_write_cmd(0x01); lcm_write_cmd(0x06); lcm_setxy(0,1); lcm_write_string(\ reset success! \longdelay(3); I2C_send_string(temp5_password,6,0x00); EA=1; EX0=1; return;}void main(void) {IT0=1; EX0=1; EA=1; led=1; lcm_init(); while(1){lock();}}