找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3412|回复: 6
打印 上一主题 下一主题
收起左侧

可修改密码的单片机密码锁设计 LCD1602显示带程序和仿真

[复制链接]
跳转到指定楼层
楼主
LCD1602显示  6位密码  可修改 有程序 有仿真



仿真工程文件及程序等所有资料下载:
密码锁.zip (89.12 KB, 下载次数: 49)


源程序:
  1. //proteus仿真论坛
  2. #include<reg51.h>
  3. #include<intrinS.h>
  4. #include<absacc.h>

  5. #define uchar unsigned char
  6. #define C02_write 0xa0
  7. #define C02_read  0xa1

  8. #define no0 0x28
  9. #define no1 0x14
  10. #define no2 0x24
  11. #define no3 0x44
  12. #define no4 0x12
  13. #define no5 0x22
  14. #define no6 0x42
  15. #define no7 0x11
  16. #define no8 0x21
  17. #define no9 0x41
  18. #define enter 0x88
  19. #define backspace 0x81

  20. #define lcm_write_cmd_add XBYTE[0x80FF]
  21. #define lcm_write_data_add XBYTE[0x81FF]
  22. #define lcm_read_busy_add XBYTE[0x82FF]
  23. #define lcm_read_data_add XBYTE[0x83FF]

  24. uchar idata temp5_password[6]={0x00,0x00,0x00,0x00,0x00,0x00};
  25. uchar idata key_code[]={no0,no1,no3,no4,no5,no6,no7,no8,no9};

  26. sbit SCL=P3^0;
  27. sbit SDA=P3^1;
  28. sbit i=P3^2;
  29. bit ack;

  30. uchar        int_counter_1;
  31. unsigned int int_counter_2;

  32. void delayms(uchar i)
  33. {
  34.         uchar j;
  35.         for(;i>0;i--)
  36.         for(j=124;j>0;j--);
  37. }

  38. void longdelay(uchar i)
  39. {
  40.         unsigned int j;
  41.         for(;i>0;i--)
  42.         for(j=10000;j>0;j--);
  43. }

  44. void lcm_wait()
  45. {
  46.         while(lcm_read_busy_add&0x80);
  47. }

  48. void lcm_write_cmd(uchar lcm_cmd)
  49. {
  50.         lcm_wait();
  51.         lcm_write_cmd_add=lcm_cmd;
  52. }

  53. void lcm_write_data(uchar lcm_data)
  54. {
  55.         lcm_wait();
  56.         lcm_write_data_add=lcm_data;
  57. }

  58. uchar lcm_read_data(void)
  59. {
  60.         uchar lcm_data;
  61.         lcm_wait();
  62.         lcm_data=lcm_read_data_add;
  63.         return(lcm_data);
  64. }

  65. void lcm_setxy(uchar x,uchar y)
  66. {
  67.         if(y==1) lcm_write_cmd(x|0x80);
  68.         if(y==2) lcm_write_cmd(x|0xc0);
  69. }

  70. void lcm_write_string(uchar *string)
  71. {
  72.         uchar i=0;
  73.         while(string[i]!='\0')
  74.         {
  75.                 lcm_write_data(string[i]);
  76.                 i++;
  77.         }
  78. }

  79. void lcm_roll_display(uchar *string,uchar y)
  80. {
  81.         uchar counter;
  82.         lcm_write_cmd(0x06);
  83.         lcm_setxy(0,y);
  84.         while(string[counter]!='\0')
  85.         {
  86.                 lcm_write_data(string[counter]);
  87.                 counter++;
  88.                 if (counter==19) lcm_setxy(19,y);
  89.                 longdelay(2);

  90.         }
  91.         lcm_write_cmd(0x07);
  92.         lcm_setxy(19,y);
  93.                 while(string[counter]!='\0')
  94.         {
  95.                 lcm_write_data(string[counter]);
  96.                 counter++;
  97.                 if (counter==19) lcm_setxy(19,y);
  98.                 longdelay(2);

  99.         }
  100. }

  101. void lcm_init()
  102. {
  103.         delayms(20);
  104.         lcm_write_cmd_add=0x38;
  105.         delayms(4);
  106.         lcm_write_cmd_add=0x38;
  107.         delayms(1);
  108.         lcm_write_cmd_add=0x38;
  109.         lcm_write_cmd(0x38);
  110.         lcm_write_cmd(0x0f);       
  111.         lcm_write_cmd(0x01);
  112.         lcm_write_cmd(0x06);
  113. }

  114. void I2C_start(void)
  115. {
  116.         SDA=1;
  117.         SCL=1;
  118.         _nop_();
  119.         _nop_();
  120.         _nop_();
  121.         _nop_();
  122.         _nop_();
  123.         SDA=0;
  124.         _nop_();
  125.         _nop_();
  126.         _nop_();
  127.         _nop_();
  128.         _nop_();
  129.         SCL=0;
  130.         _nop_();
  131.         _nop_();
  132. }

  133. void I2C_stop(void)
  134. {
  135.         SDA=0;
  136.         SCL=1;
  137.         _nop_();
  138.         _nop_();
  139.         _nop_();
  140.         _nop_();
  141.         _nop_();
  142.         SDA=1;
  143.         _nop_();
  144.         _nop_();
  145.         _nop_();
  146.         _nop_();
  147.         _nop_();
  148.         SCL=0;
  149.         _nop_();
  150.         _nop_();
  151. }

  152. void I2C_ackownledge(void)
  153. {
  154.         SDA=0;
  155.         _nop_();
  156.         _nop_();
  157.         SCL=1;
  158.         _nop_();
  159.         _nop_();
  160.         _nop_();
  161.         _nop_();
  162.         _nop_();
  163.         SCL=0;
  164.         _nop_();
  165.         _nop_();
  166.         _nop_();
  167. }

  168. void I2C_no_ackownledge(void)
  169. {
  170.         SDA=1;
  171.         _nop_();
  172.         _nop_();
  173.         SCL=1;
  174.         _nop_();
  175.         _nop_();
  176.         _nop_();
  177.         _nop_();
  178.         _nop_();
  179.         SCL=0;
  180.         _nop_();
  181.         _nop_();
  182.         _nop_();
  183. }

  184. void I2C_sendB(uchar byte)
  185. {
  186.         uchar counter;
  187.         for(counter=0;counter<8;counter++)
  188.         {
  189.                 if(byte&0x80)        SDA=1;
  190.                 else        SDA=0;
  191.                 _nop_();
  192.                 SCL=1;
  193.                 _nop_();
  194.                 _nop_();
  195.                 _nop_();
  196.                 _nop_();
  197.                 _nop_();
  198.                 SCL=0;
  199.                 _nop_();
  200.                 _nop_();
  201.                 byte<<=1;
  202.         }
  203.         _nop_();
  204.         _nop_();
  205.         SDA=1;
  206.         _nop_();
  207.         _nop_();
  208.         _nop_();
  209.         SCL=1;
  210.         _nop_();
  211.         _nop_();
  212.         _nop_();
  213.         if(SDA==0)        ack=1;
  214.         else         ack=0;
  215.         SCL=0;
  216.         _nop_();
  217.         _nop_();
  218. }
  219.        
  220. uchar I2C_receiveB(void)
  221. {
  222.         uchar temp;
  223.         uchar counter;
  224.         temp=0;
  225.         SDA=1;
  226.         _nop_();
  227.         _nop_();
  228.         for(counter=0;counter<8;counter++)
  229.         {
  230.                  _nop_();
  231.                  _nop_();
  232.                  _nop_();
  233.                  _nop_();
  234.                  _nop_();
  235.                  SCL=1;
  236.                  _nop_();
  237.                  _nop_();
  238.                  if(SDA==1)        temp=(temp<<1)|0x01;
  239.                  else        temp=temp<<1;
  240.                  _nop_();
  241.                  _nop_();
  242.                  SCL=0;
  243.                  _nop_();
  244.                  _nop_();
  245.                  _nop_();
  246.         }
  247.         _nop_();
  248.         _nop_();
  249.         return(temp);
  250. }

  251. /*bit I2C_write_byte(uchar byte,uchar address)
  252. {
  253.         I2C_sendB(address);
  254.         if(ack=0)       
  255.         {
  256.                 I2C_stop();
  257.                 return(0);
  258.         }
  259.         else        I2C_sendB(byte);  */
  260.        

  261. bit I2C_send_string(uchar *string,uchar no,uchar address)
  262. {
  263.         uchar counter;
  264.         for(counter=0;counter<no;counter++)
  265.         {
  266.                 I2C_start();
  267.                 I2C_sendB(C02_write);
  268.                 if(ack==0) return(0);
  269.                 I2C_sendB(address+counter);
  270.                 if(ack==0) return(0);
  271.                 I2C_sendB(string[counter]);
  272.                 I2C_stop();
  273.                 delayms(20);               
  274.         }
  275.         return(1);
  276. }

  277. bit I2C_receive_string(uchar *string,uchar no,uchar address)
  278. {
  279.         uchar counter;
  280.         for(counter=0;counter<no;counter++)
  281.         {
  282.                 I2C_start();
  283.                 I2C_sendB(C02_write);
  284.                 if(ack==0) return(0);
  285.                 I2C_sendB(address+counter);
  286.                 if(ack==0) return(0);
  287.                 I2C_start();
  288.                 I2C_sendB(C02_read);
  289.                 if(ack==0) return(0);
  290.                 *(string+counter)=I2C_receiveB();
  291.                 I2C_no_ackownledge();
  292.                 I2C_stop();
  293.         }
  294. }

  295. uchar get_key()
  296. {
  297.         uchar row_code;
  298.         uchar col_code;
  299.         P1=0xf0;
  300.         if(P1!=0xf0)
  301.         {
  302.                 delayms(10);
  303.                 if(P1!=0xf0)
  304.                 {
  305.                         row_code=0xfe;
  306.                         while(row_code!=0x7f)
  307.                         {       
  308.                                 P1=row_code;
  309.                                 if(P1!=row_code)
  310.                                 {
  311.                                         col_code=(P1&0xf0)|0x0f;
  312.                                         return(        (~col_code)|(~row_code));
  313.                                 }
  314.                                 row_code=((row_code<<1)|0x01);
  315.                         }
  316.                 }
  317.         }
  318.         return(0x00);
  319. }

  320. void convert_code(uchar *string)
  321. {
  322.         uchar counter=0;
  323.         for(counter=0;counter<6;counter++)
  324.         {
  325.                 switch(*string)
  326.                 {
  327.                         case no0:
  328.                                                 *string=0x00;
  329.                                                 break;
  330.                         case no1:
  331.                                                 *string=0x01;
  332.                                                 break;
  333.                         case no2:
  334.                                                 *string=0x02;
  335.                                                 break;
  336.                         case no3:
  337.                                                 *string=0x03;
  338.                                                 break;
  339.                         case no4:
  340.                                                 *string=0x04;
  341.                                                 break;
  342.                         case no5:
  343.                                                 *string=0x05;
  344.                                                 break;
  345.                         case no6:
  346.                                                 *string=0x06;
  347.                                                 break;
  348.                         case no7:
  349.                                                 *string=0x07;
  350.                                                 break;
  351.                         case no8:
  352.                                                 *string=0x08;
  353.                                                 break;
  354.                         case no9:
  355.                                                 *string=0x09;
  356.                                                 break;
  357.                         default:
  358.                                                 break;
  359.                 }
  360.                 string++;                               
  361.         }
  362. }

  363. bit compare_string(uchar *string1,uchar *string2)
  364. {
  365.         uchar counter;
  366.         for(counter=0;counter<6;counter++)
  367.         {
  368.                 if(string1[counter]!=string2[counter]) return(0);
  369.         }
  370.         return(1);
  371. }

  372. uchar step_choose(void)
  373. {
  374.         unsigned int i;
  375.         uchar key;
  376.         do{
  377.                 lcm_write_cmd(0x01);
  378.                 lcm_write_cmd(0x06);
  379.                 lcm_setxy(0,1);
  380.                 lcm_write_string("input password");
  381.                 lcm_setxy(0,2);
  382.                 lcm_write_string("please press 1");
  383.                 for(i=0;i<30000;i++)
  384.                 {
  385.                         key=get_key();
  386.                         if((key==no1)||(key==no2))        break;
  387.                 }
  388.                 if((key!=no1)&&(key!=no2))
  389.                 {
  390.                         lcm_write_cmd(0x01);
  391.                         lcm_write_cmd(0x06);
  392.                         lcm_setxy(0,1);
  393.                         lcm_write_string("change password");
  394.                         lcm_setxy(0,2);
  395.                         lcm_write_string("please press 2");
  396.                         for(i=0;i<30000;i++)
  397.                         {
  398.                                 key=get_key();
  399.                                 if((key==no1)||(key==no2))        break;
  400.                         }
  401.                 }
  402.                 }
  403.         while((key!=no1)&&(key!=no2));
  404.         return(key);
  405. }

  406. bit input_password(uchar *password)
  407. {
  408.         uchar counter;
  409.         uchar key;
  410.         lcm_setxy(0,2);
  411.         for(counter=0;counter<7;counter++)
  412.         {
  413.                 longdelay(3);
  414.                 if(counter<6)
  415.                 {
  416.                         do{
  417.                                 key=get_key();
  418.                              }
  419.                     while(key==0x00);
  420.                         if((key!=backspace)&&(key!=enter))
  421.                         {
  422.                                 lcm_write_data('*');
  423.                                 password[counter]=key;
  424.                         }
  425.                         if(key==backspace)
  426.                         {
  427.                                 if(counter>0)
  428.                                 {
  429.                                         lcm_setxy(--counter,2);
  430.                                         lcm_write_data(' ');
  431.                                         password[counter]=0x00;
  432.                                         lcm_setxy(counter,2);
  433.                                         counter--;
  434.                                 }
  435.                         }
  436.                         if(key==enter)
  437.                         {
  438.                                 lcm_setxy(0,1);                               
  439.                                 return(        0);
  440.                         }               
  441.                 }
  442.                 if(counter==6)
  443.                 {
  444.                         do{
  445.                                 key=get_key();
  446.                              }
  447.                         while((key!=backspace)&&(key!=enter));
  448.                         if(key==backspace)
  449.                         {
  450.                                 lcm_setxy(--counter,2);
  451.                                 lcm_write_data(' ');
  452.                                 password[counter]=0x00;
  453.                                 lcm_setxy(counter,2);
  454.                                 counter--;
  455.                         }
  456.                         if(key==enter)
  457.                         {                                                 
  458.                                 return(1);                       
  459.                     }
  460.                 }               
  461.         }
  462. }

  463. void mima()
  464. {
  465.         uchar key;
  466.         uchar idata temp1_password[6]={0,0,0,0,0,0};
  467.         uchar idata temp3_password[6]={0,0,0,0,0,0};
  468.         uchar idata temp2_password[6]={0,0,0,0,0,0};
  469.         uchar idata temp4_password[6]={0,0,0,0,0,0};
  470.         key=step_choose();
  471.         if(key==no1)
  472.         {
  473.                 I2C_receive_string(temp1_password,6,0x00);
  474.                 lcm_write_cmd(0x01);
  475.                 lcm_write_cmd(0x06);
  476.                 lcm_setxy(2,0);
  477.                 lcm_write_string("input password");       
  478.                 if(input_password(temp2_password))
  479.                 {
  480.                         convert_code(temp2_password);
  481.                         if(compare_string(temp1_password,temp2_password))
  482.                         {
  483.                                 lcm_setxy(0,2);
  484.                                 lcm_write_string("correct!");
  485.                                 longdelay(6);
  486.                                 return;
  487.                         }
  488.                         else
  489.                         {
  490.                                 lcm_setxy(0,2);
  491.                                 lcm_write_string("wrong password");
  492.                                 longdelay(6);
  493.                                 return;
  494.                         }
  495.                 }
  496.                 else
  497.                 {
  498.                         lcm_setxy(0,2);
  499.                         lcm_write_string("error!  ");
  500.                         longdelay(6);
  501.                 }                 
  502.         }
  503.         else
  504.         {
  505.                 I2C_receive_string(temp1_password,6,0x00);
  506.                 lcm_write_cmd(0x01);
  507.                 lcm_write_cmd(0x06);
  508.                 lcm_setxy(0,0);
  509.                 lcm_write_string("input old password");
  510.             if(input_password(temp2_password))
  511.                 {
  512.                         convert_code(temp2_password);
  513.                         if(compare_string(temp1_password,temp2_password))
  514.                         {
  515.                                 lcm_setxy(0,2);
  516.                                 lcm_write_string("password correct!");
  517.                                 longdelay(6);
  518.                                 lcm_write_cmd(0x01);
  519.                                 lcm_write_cmd(0x06);
  520.                                 lcm_setxy(0,1);
  521.                                 lcm_write_string("input new password");
  522.                                 if(input_password(temp3_password))
  523.                                 {
  524.                                         lcm_write_cmd(0x01);
  525.                                         lcm_write_cmd(0x06);
  526.                                         lcm_setxy(0,1);
  527.                                         lcm_write_string("input password again");
  528.                                         if(input_password(temp4_password))
  529.                                         {
  530.                                                 if(compare_string(temp3_password,temp4_password))
  531.                                                 {
  532.                                                         convert_code(temp3_password);
  533.                                                         I2C_send_string(temp3_password,6,0x00);
  534.                                                         lcm_write_cmd(0x01);
  535.                                                         lcm_write_cmd(0x06);
  536.                                                         lcm_setxy(0,1);
  537.                                                         lcm_write_string("password has");
  538.                                                         lcm_setxy(0,2);
  539.                                                         lcm_write_string("been changed");
  540.                                                         longdelay(10);
  541.                                                         return;
  542.                                                 }
  543.                                                 else
  544.                                                 {
  545.                                                    lcm_write_cmd(0x01);
  546.                                                    lcm_write_cmd(0x06);
  547.                                                    lcm_setxy(0,1);
  548.                                                    lcm_write_string("twice input");
  549.                                                    lcm_setxy(0,2);
  550.                                                    lcm_write_string("is different");
  551.                                                    longdelay(10);
  552.                                                    return;
  553.                                                 }
  554.                                         }
  555.                                         else
  556.                                         {
  557.                                                 lcm_setxy(0,2);
  558.                                                 lcm_write_string("error!  ");
  559.                                                 longdelay(10);
  560.                                                 return;
  561.                                         }
  562.                                 }
  563.                                 else
  564.                                 {
  565.                                         lcm_setxy(0,2);
  566.                                         lcm_write_string("error!  ");
  567.                                         longdelay(10);
  568.                                         return;
  569.                                 }
  570.                         }
  571.                         else
  572.                         {
  573.                                 lcm_setxy(0,2);
  574.                                 lcm_write_string("wrong password");
  575.                                 longdelay(6);
  576.                                 return;
  577.                         }
  578.                 }
  579.                 else
  580.                 {
  581.                         lcm_setxy(0,2);
  582.                         lcm_write_string("error!  ");
  583.                         longdelay(6);
  584.                         return;
  585.                 }                 
  586.         }
  587.                           
  588.                                
  589. }

  590. void        int_service(void)        using 0 interrupt        0
  591. {
  592.         EA=0;
  593.         EX0=0;
  594.         for(int_counter_1=10;int_counter_1>0;int_counter_1--)
  595.         {
  596.                 for(int_counter_2=0xffff;int_counter_2>0;int_counter_2--)
  597.                 {
  598.                         if(i)
  599.                         {
  600.                                 EA=1;
  601.                                 EX0=1;
  602.                                 return;
  603.                         }       
  604.                 }
  605.         }
  606.         lcm_write_cmd(0x01);
  607.         lcm_write_cmd(0x06);
  608.         lcm_write_string("rest success!");
  609.         longdelay(12);
  610.         I2C_send_string(temp5_password,6,0x00);

  611.         (*(void(*)())0)();
  612. }
  613.   


  614. void main(void)
  615. {
  616.        
  617.         IT0=1;
  618.         EX0=1;
  619.         EA=1;
  620.        
  621.         lcm_init();
  622.     while(1)
  623.         {
  624.                 mima();
  625.         }
  626. }
  627.        
  628.                                
复制代码




评分

参与人数 1黑币 +5 收起 理由
niconiconi + 5 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:150513 发表于 2016-11-30 22:10 | 只看该作者
能换成C语言的吗?
回复

使用道具 举报

板凳
ID:212990 发表于 2017-6-20 16:32 | 只看该作者
可以,很强
回复

使用道具 举报

地板
ID:212117 发表于 2017-6-20 23:45 | 只看该作者
老铁,来个压缩包
回复

使用道具 举报

5#
ID:212117 发表于 2017-6-20 23:45 | 只看该作者
老铁,来个安装包
回复

使用道具 举报

6#
ID:248401 发表于 2017-11-11 17:27 | 只看该作者
支持支持学习了
回复

使用道具 举报

7#
ID:248401 发表于 2017-11-11 17:46 | 只看该作者
不知道为啥下载不了资源了,楼主你能解决一下吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表