找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 28134|回复: 132
收起左侧

51单片机的电子密码锁(功能和手机的解锁一样 12864显示 没有用AT24c02 电路很简单)

  [复制链接]
ID:63839 发表于 2014-8-31 16:05 | 显示全部楼层 |阅读模式
  1. #include<stc12.h>                //头文件我用的(stc12c5a60s2)
  2. #define uint unsigned int                                                                  //
  3. #define uchar unsigned char                                                                  //
  4. uint k,num,a,b,c,s;        //num数据显示的位置 abc用于跳出while()循环 s数据输错的次数
  5. uchar code table1[12]={"0123456789*"};                                          //
  6. uchar code table2[]={"请输入密码:"};                                          //
  7. uchar code table3[]={"密码证确!"};        //因为我的12864显示“正”乱码于是就用“证”
  8. uchar code table4[]={"密码错误!"};                                                 //
  9. uchar code table5[]={"还有"};                                                         //
  10. uchar code table6[]={"请输入当前密码:"};                                         //
  11. uchar code table7[]={"修改后的密码:"};
  12. uchar code table8[]={"次机会"};
  13. uchar code table9[]={"系统已经锁死"};                                                        //
  14. uint Cipher1[5];                        //储存你所输入的数据                                                        
  15. uint Cipher2[5]={0,1,2,3,4,};//储存密码(初始密码123)                                                   
  16.                                                                                                            
  17. sbit LCD_RS=P1^0;        // 数据/命令选择端                     
  18. sbit LCD_RW=P1^1;        // 读写选择端                           
  19. sbit LCD_EN=P1^2;        // 使能信号                                    
  20. sbit LCD_PSB=P1^6;           // 并/串选择                                   
  21. sbit beep=P3^1;                                                                                 
  22.                                                                                                          
  23. void shaomiao();                                                                         //矩阵键盘扫描
  24. void delay(uint);                                                                        // 延时函数
  25. void lcd_init();                                                                        // 12864初始化
  26. void write_cmd(uchar);                                                                //
  27. void write_dat(uchar);                                                                //
  28. void lcd_pos(uchar,char);                                                         //
  29. void show();                                                                                // 在12864上显示
  30.                                                                                                    //
  31. void main()                                                                                   //
  32. {        
  33.         s=5;                                                                                //输错密码次数
  34.         lcd_init();                                                                          //
  35.         while(1)                                                                         //
  36.         {                                                                                         //
  37.          show();                                                                         //
  38.          shaomiao();                                                                 //
  39.         }                                                                                         //
  40. }                                                                                                 //
  41.                                                                                                 //
  42. void show()                                                                                //
  43. {        uint i,m;                                                                        //
  44.         m=0;                                                                                //
  45.         lcd_pos(0,0);                                                                //
  46.         i=0;                                                                                //
  47.         while(table2[i]!='\0')                                                //
  48.          {write_dat(table2[i]);i++;}                                //
  49.         shaomiao();                                                                        //
  50.         if(num>0)                                                                        // 输入密码
  51.          {                                                                                        //
  52.                 lcd_pos(1,num-1);                                                //
  53.                 write_dat(table1[k]);                                        //
  54.                 Cipher1[num]=k;
  55.          }                                                                                   //删除上一个数据
  56.          if(b==1)
  57.          {
  58.                  lcd_pos(1,num-1);
  59.                 write_dat('\0');
  60.                 num--;
  61.                 k=Cipher1[num];
  62.                 b=0;
  63.                 while(num<1) {shaomiao();b=0;}
  64.         }
  65. //        while(num>2) {num=3;shaomiao();if(c==1) m=4;if(m==4){c=0;break;}}

  66.         while(c==1)                                 //判断密码是否正确
  67.         {
  68.                 for(i=0;i<8;i++)
  69.                  {lcd_pos(0,i);write_dat(table1[11]);lcd_pos(1,i);write_dat(table1[11]);}
  70.                    while((Cipher1[1]==Cipher2[1])&(Cipher1[2]==Cipher2[2])&(Cipher1[3]==Cipher2[3]))
  71.                 {        
  72.                         c=0;s=5;
  73.                         shaomiao();
  74.                         lcd_pos(0,0);
  75.                         i=0;
  76.                         while(table3[i]!='\0')
  77.                          {write_dat(table3[i]);i++;}
  78.                         if(c==1) {m=3;Cipher1[1]=Cipher1[2]=Cipher1[3]='\0';break;}
  79.                 }
  80.                 if(m!=3)
  81.                 {
  82.                         s--;
  83.                         while(!((Cipher1[1]==Cipher2[1])&(Cipher1[2]==Cipher2[2])&(Cipher1[3]==Cipher2[3])))
  84.                          {
  85.                                  c=0;beep=0;delay(500);
  86.                                 shaomiao();
  87.                                 lcd_pos(0,0);
  88.                                 i=0;
  89.                                 while(table5[i]!='\0')
  90.                                   {write_dat(table5[i]);i++;}
  91.                                 lcd_pos(0,2);
  92.                                 write_dat(table1[s]);
  93.                                 write_dat('\0');
  94.                                 lcd_pos(0,3);
  95.                                 i=0;
  96.                                 while(table8[i]!='\0')
  97.                                   {write_dat(table8[i]);i++;}
  98.                                 while(s<1)
  99.                                 {        lcd_pos(0,0);i=0;
  100.                                         while(table9[i]!='\0')
  101.                                           {write_dat(table9[i]);i++;}
  102.                                 }
  103.                                 if(c==1) {m=4;break;}
  104.                          }
  105.                 }
  106.                 if(m==3|m==4) {a=0;b=0;c=0;num=0;}
  107.         }
  108.         
  109.          while(a==1)                                //修改密码
  110.          {        num=0;
  111.                  for(i=0;i<8;i++)
  112.                  {lcd_pos(1,i);write_dat(table1[11]);}
  113.                 lcd_pos(0,0);
  114.                 i=0;
  115.                 while(table6[i]!='\0')
  116.                   {write_dat(table6[i]);i++;}
  117.                 shaomiao();
  118.                 while(num>0)                                        //输入当前密码正确后才可以修改
  119.                  {        shaomiao();
  120.                         lcd_pos(1,num-1);
  121.                         write_dat(table1[k]);
  122.                         Cipher1[num]=k;
  123.                         if(b==1)
  124.                          {
  125.                                  lcd_pos(1,num-1);
  126.                                 write_dat(table1[11]);
  127.                                 num--;
  128.                                 k=Cipher1[num];
  129.                                 b=0;
  130.                          }         
  131.                         while(c==1)
  132.                         {        
  133.                                 num=0;
  134.                                 shaomiao();
  135.                                 for(i=0;i<8;i++)
  136.                                  {lcd_pos(0,i);write_dat(table1[11]);lcd_pos(1,i);write_dat(table1[11]);}
  137.                                 while((Cipher1[1]==Cipher2[1])&(Cipher1[2]==Cipher2[2])&(Cipher1[3]==Cipher2[3]))
  138.                                 {         
  139.                                         c=0;s=5;
  140.                                         lcd_pos(0,0);
  141.                                         i=0;
  142.                                         while(table7[i]!='\0')
  143.                                           {write_dat(table7[i]);i++;}
  144.                                         shaomiao();
  145.                                         while(num>0)
  146.                                          {
  147.                                                 shaomiao();
  148.                                                 lcd_pos(1,num-1);
  149.                                                 write_dat(table1[10]);
  150.                                                 Cipher2[num]=k;
  151.                                                  if(b==1)
  152.                                                  {
  153.                                                  lcd_pos(1,num-1);
  154.                                                 write_dat(table1[11]);
  155.                                                 num--;
  156.                                                 b=0;
  157.                                                 }
  158.                                                 while(num<1) {shaomiao();b=0;}
  159.                                                 m=1;
  160.                                                 if(c==1) break;
  161.                                         }
  162.                                         if(m==1)
  163.                                         {        a=0;b=0;c=0;num=0;
  164.                                                 for(i=0;i<8;i++)
  165.                                                  {lcd_pos(0,i);write_dat(table1[11]);lcd_pos(1,i);write_dat(table1[11]);}
  166.                                         }
  167.                                 }
  168.                                 if(m!=1)
  169.                                 {        s--;
  170.                                         while(!((Cipher1[1]==Cipher2[1])&(Cipher1[2]==Cipher2[2])&(Cipher1[3]==Cipher2[3])))
  171.                                         {
  172.                                                 c=0;
  173.                                                 beep=0;delay(500);
  174.                                                 shaomiao();
  175.                                                 lcd_pos(0,0);
  176.                                                 i=0;
  177.                                                 while(table5[i]!='\0')
  178.                                                   {write_dat(table5[i]);i++;}
  179.                                                 lcd_pos(0,2);
  180.                                                 write_dat(table1[s]);
  181.                                                 write_dat('\0');
  182.                                                 lcd_pos(0,3);
  183.                                                 i=0;
  184.                                                 while(table8[i]!='\0')
  185.                                                   {write_dat(table8[i]);i++;}
  186.                                                 while(s<1)                                            //输错5次锁死系统
  187.                                                 {        
  188.                                                         lcd_pos(0,0);i=0;
  189.                                                         while(table9[i]!='\0')
  190.                                                           {write_dat(table9[i]);i++;}
  191.                                                 }
  192.                                                 m=2;
  193.                                                 if(c==1) break;
  194.                                         }
  195.                                 }
  196.                         if(m==1|m==2) c=0;
  197.                         }
  198.                 }
  199.          }
  200. }
  201. //写指令数据到LCD
  202. void write_cmd(char cmd)
  203. {
  204.         LCD_RS=0;
  205.         LCD_RW=0;
  206.         LCD_EN=0;
  207.         P0=cmd;         
  208.         delay(5);
  209.         LCD_EN=1;
  210.         delay(5);
  211.         LCD_EN=0;
  212. }
  213. //写显示数据到LCD
  214. void write_dat(char dat)
  215. {
  216.         LCD_RS=1;
  217.         LCD_RW=0;
  218.         LCD_EN=0;
  219.         P0=dat;
  220.         delay(5);
  221.         LCD_EN=1;
  222.         delay(5);
  223.         LCD_EN=0;        
  224. }
  225. //设定显示位置
  226. void lcd_pos(char x,char y)
  227. {
  228.         uchar pos;
  229.         if(x==0)
  230.         {x=0x80;}
  231.         else if(x==1)
  232.         {x=0x90;}
  233.         else if(x==2)
  234.         {x=0x88;}
  235.         else if(x==3)
  236.         {x=0x98;}
  237.         pos=x+y;
  238.         write_cmd(pos);    //显示的地址
  239. }
  240. //12864初始化
  241. void lcd_init()//12864初始化
  242. {
  243.         LCD_PSB=1;                                //并口模式
  244.         write_cmd(0x30);                //基本指令操作
  245.         delay(5);
  246.         write_cmd(0x0c);                //显示开,光标关
  247.         delay(5);
  248.         write_cmd(0x01);                //消除所有显示
  249.         delay(5);
  250. }

  251. void shaomiao()
  252. {
  253.         uchar t;
  254.         P3=0xdf;
  255.         t=P3;
  256.         t=t&0x1c;
  257.         if(t!=0x1c)
  258.         {
  259.                 delay(10);
  260.                 t=P3;
  261.                 t=t&0x1c;
  262.                 if(t!=0x1c)
  263.                  {
  264.                          t=P3;
  265.                         switch(t)
  266.                         {
  267.                                 case 0xdb:k=1;num++;break;
  268.                                 case 0xd7:k=2;num++;break;
  269.                                 case 0xcf:k=3;num++;break;
  270.                                 default:  break;
  271.                         }
  272.                         while(t!=0x1c)
  273.                         {
  274.                                 t=P3;
  275.                                 t=t&0x1c;
  276.                         }
  277.                  }
  278.         }
  279.         P3=0xbf;
  280.         t=P3;
  281.         t=t&0x1c;
  282.         if(t!=0x1c)
  283.         {
  284.                 delay(10);
  285.                 t=P3;
  286.                 t=t&0x1c;
  287.                 if(t!=0x1c)
  288.                  {
  289.                          t=P3;
  290.                         switch(t)
  291.                         {
  292.                                 case 0xbb:a=1;break;
  293.                                 case 0xb7:b=1;break;
  294.                                 case 0xaf:c=1;break;
  295.                                 default:  break;
  296.                         }
  297.                         while(t!=0x1c)
  298.                         {
  299.                                 t=P3;
  300.                                 t=t&0x1c;
  301.                         }
  302.                  }
  303.         }        
  304. }
  305. void delay(uint z)
  306. {
  307.         uint x,y;
  308.         for(x=z;x>0;x--)
  309.          for(y=110;y>0;y--);
  310. } [/hide]因为我的单片机的矩阵键盘只有六个键 于是上面的三个作为数据  下面三个一个是修改密码 ,清除键 ,确定键 功能和手机的解锁功能一样
  311. #include<stc12.h>  //头文件我用的(stc12c5a60s2)
  312. #define uint unsigned int          //
  313. #define uchar unsigned char          //
  314. uint k,num,a,b,c,s; //num数据显示的位置 abc用于跳出while()循环 s数据输错的次数
  315. uchar code table1[12]={"0123456789*"};       //
  316. uchar code table2[]={"请输入密码:"};       //
  317. uchar code table3[]={"密码证确!"}; //因为我的12864显示“正”乱码于是就用“证”
  318. uchar code table4[]={"密码错误!"};       //
  319. uchar code table5[]={"还有"};        //
  320. uchar code table6[]={"请输入当前密码:"};      //
  321. uchar code table7[]={"修改后的密码:"};
  322. uchar code table8[]={"次机会"};
  323. uchar code table9[]={"系统已经锁死"};       //
  324. uint Cipher1[5];   //储存你所输入的数据      
  325. uint Cipher2[5]={0,1,2,3,4,};//储存密码(初始密码123)         
  326.                
  327. sbit LCD_RS=P1^0;        // 数据/命令选择端        
  328. sbit LCD_RW=P1^1;        // 读写选择端      
  329. sbit LCD_EN=P1^2;        // 使能信号        
  330. sbit LCD_PSB=P1^6;    // 并/串选择      
  331. sbit beep=P3^1;           
  332.               
  333. void shaomiao();          //矩阵键盘扫描
  334. void delay(uint);         // 延时函数
  335. void lcd_init();         // 12864初始化
  336. void write_cmd(uchar);        //
  337. void write_dat(uchar);        //
  338. void lcd_pos(uchar,char);        //
  339. void show();          // 在12864上显示
  340.                //
  341. void main()             //
  342. {
  343. s=5;          //输错密码次数
  344. lcd_init();           //
  345. while(1)          //
  346. {            //
  347.   show();          //
  348.   shaomiao();         //
  349. }            //
  350. }
  351. //部分代码
复制代码

电子密码锁.zip

32.09 KB, 下载次数: 417, 下载积分: 黑币 -5

大家如果感觉好就给点分

评分

参与人数 6威望 +55 黑币 +58 收起 理由
caiyuejian + 5 + 5 赞一个!
瑞雪 + 5 + 5
黑暗主宰 + 5 + 5 很给力!
daliu + 6 + 6 绝世好帖!
liuz + 24 + 27 赞一个!
hujia + 10 + 10 很给力!

查看全部评分

回复

使用道具 举报

ID:26188 发表于 2014-8-31 16:42 | 显示全部楼层
根据io接好就行吧,,问下 12864 是带字库的吗? 什么驱动芯片的12864液晶?
回复

使用道具 举报

ID:50962 发表于 2014-8-31 16:45 | 显示全部楼层
果然很给力啊   要是有图就好了
回复

使用道具 举报

ID:65608 发表于 2014-8-31 16:48 | 显示全部楼层
同求 12864液晶的型号???
回复

使用道具 举报

ID:63839 发表于 2014-8-31 19:56 | 显示全部楼层
hujia 发表于 2014-8-31 16:42
根据io接好就行吧,,问下 12864 是带字库的吗? 什么驱动芯片的12864液晶?

带字库的
回复

使用道具 举报

ID:63839 发表于 2014-8-31 19:56 | 显示全部楼层
daliu 发表于 2014-8-31 16:48
同求 12864液晶的型号???

QC pass
回复

使用道具 举报

ID:63839 发表于 2014-8-31 19:57 | 显示全部楼层
liuz 发表于 2014-8-31 16:45
果然很给力啊   要是有图就好了

根据I/O就知道大概原理图了  就用到矩阵键盘和12864
回复

使用道具 举报

ID:64888 发表于 2014-9-1 13:03 | 显示全部楼层
赞一个
回复

使用道具 举报

ID:65680 发表于 2014-9-1 21:16 | 显示全部楼层
看起来很牛的样子,新人学习
回复

使用道具 举报

ID:51024 发表于 2014-9-2 01:26 | 显示全部楼层
牛人大作啊 值得仔细研读.
回复

使用道具 举报

ID:63427 发表于 2014-9-14 21:34 | 显示全部楼层
学习学习
回复

使用道具 举报

ID:66462 发表于 2014-9-21 20:18 | 显示全部楼层
学习了
回复

使用道具 举报

ID:66811 发表于 2014-9-29 18:07 | 显示全部楼层
有没有成品?
回复

使用道具 举报

ID:63839 发表于 2014-9-30 14:28 | 显示全部楼层

拆了!
回复

使用道具 举报

ID:60246 发表于 2014-10-6 10:02 | 显示全部楼层
谢谢分享
回复

使用道具 举报

ID:49065 发表于 2014-10-8 15:40 | 显示全部楼层
点个赞。
回复

使用道具 举报

ID:67111 发表于 2014-10-8 21:37 | 显示全部楼层
看哈有成果没有
回复

使用道具 举报

ID:55219 发表于 2014-10-11 14:17 | 显示全部楼层
不错不错~~~~~good!
回复

使用道具 举报

ID:65318 发表于 2014-10-11 22:42 | 显示全部楼层
新人学习
回复

使用道具 举报

ID:61475 发表于 2014-10-11 22:58 | 显示全部楼层
来看看,学习了。
回复

使用道具 举报

ID:51348 发表于 2014-10-16 01:52 | 显示全部楼层
很好  学习  谢谢
回复

使用道具 举报

ID:65403 发表于 2014-10-19 09:25 | 显示全部楼层
要是有图就好了
回复

使用道具 举报

ID:58625 发表于 2014-10-19 13:38 | 显示全部楼层
学习下
回复

使用道具 举报

ID:58624 发表于 2014-10-20 10:13 | 显示全部楼层
很好很强大
回复

使用道具 举报

ID:65965 发表于 2014-10-21 11:47 | 显示全部楼层
好好 好 好  好 好 好 好 好
回复

使用道具 举报

ID:50751 发表于 2014-11-3 09:30 | 显示全部楼层
赞一个
回复

使用道具 举报

ID:68432 发表于 2014-11-6 12:45 | 显示全部楼层
我是新加入的,想下点东西但是M币不够,想问下怎样可以获得M币啊?求指教
回复

使用道具 举报

ID:68589 发表于 2014-11-10 16:22 | 显示全部楼层
非常感谢楼主!
回复

使用道具 举报

ID:68625 发表于 2014-11-12 23:03 | 显示全部楼层
好东西,谢谢分享
回复

使用道具 举报

ID:69124 发表于 2014-11-20 19:59 | 显示全部楼层
看看               
回复

使用道具 举报

ID:61200 发表于 2014-11-29 09:01 | 显示全部楼层
看看                             
回复

使用道具 举报

ID:69747 发表于 2014-12-1 12:10 | 显示全部楼层
PCBPCBPCBPCB
回复

使用道具 举报

ID:69783 发表于 2014-12-1 19:32 | 显示全部楼层
谢谢分享       收下了
回复

使用道具 举报

ID:69781 发表于 2014-12-1 22:29 | 显示全部楼层
楼主掉啊
回复

使用道具 举报

ID:69706 发表于 2014-12-8 14:09 | 显示全部楼层
进来逛逛!!!!!!!!!!
回复

使用道具 举报

ID:70267 发表于 2014-12-18 17:30 | 显示全部楼层
学习一下
回复

使用道具 举报

ID:70737 发表于 2014-12-19 16:19 | 显示全部楼层
不错开始学习
回复

使用道具 举报

ID:71889 发表于 2015-1-8 23:59 | 显示全部楼层
赞一个
回复

使用道具 举报

ID:71891 发表于 2015-1-9 01:00 来自手机 | 显示全部楼层
不用at24c02,修改密码后怎么掉电存储
回复

使用道具 举报

ID:63171 发表于 2015-1-9 21:57 | 显示全部楼层

很好  学习  谢谢
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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