找回密码
 立即注册

QQ登录

只需一步,快速开始

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

SPI总线12864电子密码锁程序设计

[复制链接]
跳转到指定楼层
楼主
基于单片机设计


单片机源程序如下:
  1. #include<reg51.h>
  2. #include<intrins.h>
  3. #include<stdlib.h>
  4. #include<zimo.H>
  5. #include <ctype.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include<ds1820.H>
  10. #include<ds1302.H>
  11. #include<at2402.H>
  12. #define uchar unsigned char
  13. #define uint unsigned int
  14. #define lcd_data P1
  15. uchar a1[6];
  16. uchar t;
  17. uchar h;
  18. uchar k;
  19. uchar f=0;
  20. uchar b1[6];
  21. uchar a1[6]={9,6,3,1,0,2};
  22. void delay(uint x)
  23. {
  24.   uint i,j;
  25.   for(i=x;i>0;i--)
  26.       for(j=110;j>0;j--);
  27. }

  28. void write_cmd(uchar cmd)
  29. {
  30.    lcd_rs=0;
  31.    lcd_rw=0;
  32.    lcd_en=0;
  33.    P1=cmd;
  34.    delay(0);
  35.    lcd_en=1;
  36.    delay(0);
  37.    lcd_en=0;
  38. }
  39. void write_dat(uchar dat)
  40. {
  41.   lcd_rs=1;
  42.    lcd_rw=0;
  43.    lcd_en=0;
  44.    P1=dat;
  45.    delay(0);
  46.    lcd_en=1;
  47.    delay(0);
  48.    lcd_en=0;
  49. }

  50. void hz_disp16(unsigned char pag,unsigned char col, unsigned char code *hzk)
  51. {
  52.   unsigned char j=0,i=0;
  53.   for(j=0;j<2;j++)
  54.    {
  55.     write_cmd(0xb8+pag+j);
  56.     write_cmd(0x40+col);
  57.     for(i=0;i<16;i++)
  58.          write_dat(hzk[16*j+i]);
  59.    }
  60. }
  61. void hz_disp161(unsigned char pag,unsigned char col, unsigned char code *hzk)
  62. {
  63.   unsigned char j=0,i=0;
  64.   for(j=0;j<2;j++)
  65.    {
  66.     write_cmd(0xb8+pag+j);
  67.     write_cmd(0x40+col);
  68.     for(i=0;i<8;i++)
  69.          write_dat(hzk[8*j+i]);
  70.    }
  71. }

  72. void StartMsg(void)
  73. {
  74.   CS2=1;CS1=0;
  75.   hz_disp16(0,32,huan);        
  76.   delay(10);
  77.   hz_disp16(0,48,ying);     
  78.   delay(10);
  79.   CS2=0;CS1=1;
  80.   hz_disp16(0,0,shi);
  81.   delay(10);
  82.   hz_disp16(0,16,yong);
  83.   delay(10);
  84.   CS2=1;CS1=0;
  85.   hz_disp16(2,16,xin1);
  86.   delay(10);
  87.   hz_disp16(2,32,dian);
  88.   delay(10);
  89.   hz_disp16(2,48,zi);
  90.   delay(10);
  91.   CS2=0;CS1=1;
  92.   hz_disp16(2,0,mi);
  93.   delay(10);
  94.   hz_disp16(2,16,ma);
  95.   delay(10);
  96.   hz_disp16(2,32,suo);
  97.   delay(10);
  98.   CS2=1;CS1=0;
  99.   hz_disp16(4,16,zhi);
  100.   delay(10);
  101.   hz_disp16(4,32,zuo);
  102.   delay(10);
  103.   hz_disp16(4,48,maohao);
  104.   delay(10);
  105.   CS2=0;CS1=1;
  106.   hz_disp16(4,0,liu);
  107.   delay(10);
  108.   hz_disp16(4,16,li);
  109.   delay(10);
  110.   hz_disp16(4,32,guo);
  111.   delay(10);
  112.   CS2=1;CS1=0;

  113.   hz_disp16(6,0,wu);
  114.   delay(10);
  115.   hz_disp16(6,16,li1);
  116.   delay(10);
  117.   hz_disp16(6,32,yuan);
  118.   hz_disp161(6,48,gg);
  119.   delay(10);
  120.   hz_disp161(6,56,hh);
  121.   delay(10);
  122.   CS2=0;CS1=1;
  123.   hz_disp16(6,0,tong);
  124.   delay(10);
  125.   hz_disp16(6,16,xin);
  126.   delay(10);
  127.   hz_disp16(6,32,gong);
  128.   delay(10);
  129.   hz_disp16(6,48,cheng);
  130.   delay(10);
  131. }



  132. void Disp_Img(unsigned char code *img)
  133. {
  134.   unsigned char j=0,k=0;
  135.   for(k=0;k<8;k++)
  136.    {
  137.     CS2=1;
  138.         CS1=0;
  139.         delay(10);
  140.     write_cmd(0xb8+k);
  141.     write_cmd(0x40+0);
  142.     for(j=0;j<64;j++)
  143.         write_dat(img[k*128+j]);
  144.     CS2=0;
  145.         CS1=1;
  146.     delay(10);
  147.     write_cmd(0xb8+k);
  148.     write_cmd(0x40+0);
  149.     for(j=64;j<128;j++)
  150.          write_dat(img[k*128+j]);
  151.    }                  
  152. }
  153. void Mima(void)
  154. {
  155.   CS2=1;CS1=0;
  156.   hz_disp16(6,0,mi);
  157.   hz_disp16(6,16,ma);
  158.   hz_disp16(6,32,weixiao);
  159.   hz_disp16(6,48,maohao);
  160. }
  161. void Wrong(void)
  162. {
  163.   CS2=1;CS1=0;
  164.   hz_disp16(0,32,dui);
  165.   hz_disp16(2,32,bu);
  166.   hz_disp16(4,32,qi1);
  167.   hz_disp16(6,32,douhao);
  168.   hz_disp16(0,48,mi);
  169.   hz_disp16(2,48,ma);
  170.   hz_disp16(4,48,cuo);
  171.   hz_disp16(6,48,wu1);
  172.   CS2=0;CS1=1;
  173.   hz_disp16(0,0,qing);
  174.   hz_disp16(2,0,chong);
  175.   hz_disp16(4,0,xin1);
  176.   hz_disp16(6,0,shu);
  177.   hz_disp16(0,16,ru);
  178.   hz_disp16(2,16,douhao);
  179.   hz_disp16(4,16,xie);
  180.   hz_disp16(6,16,xie);
  181. }

  182. void Xinmima(void)
  183. {
  184.   CS2=1;CS1=0;
  185.   hz_disp16(4,16,qing);
  186.   hz_disp16(4,32,shu);
  187.   hz_disp16(4,48,ru);
  188.   CS2=0;CS1=1;
  189.   hz_disp16(4,0,xin1);
  190.   hz_disp16(4,16,mi);
  191.   hz_disp16(4,32,ma);
  192. }

  193. void xiugai(void)
  194. {
  195.   CS2=1;CS1=0;
  196.   hz_disp16(3,16,mi1);
  197.   hz_disp16(3,32,ma1);
  198.   hz_disp16(3,48,xiu);
  199.   CS2=0;CS1=1;
  200.   hz_disp16(3,0,gai);
  201.   hz_disp16(3,16,cheng1);
  202.   hz_disp16(3,32,gong1);
  203. }
  204. matrixkey()
  205. {
  206.   void Wrong(void);
  207.   void Xinmima(void);
  208.   uchar m;
  209.   uchar k=0;
  210.   uchar temp,key;
  211.   void Disp_Img(unsigned char code *img);
  212.   Mima();
  213.   P2=0xfe;
  214.   temp=P2;
  215.   temp=temp&0xf0;
  216.   if(temp!=0xf0)
  217.     {
  218.       delay(10);
  219.       temp=P2;
  220.       temp=temp&0xf0;
  221.       if(temp!=0xf0)
  222.        {
  223.          temp=P2;
  224.          switch(temp)
  225.           {  
  226.             case 0xee:
  227.                    key=7;
  228.                                    b1[t]=key;
  229.                                    if(h==1){a1[t]=key;}
  230.                                    t++;
  231.                    break;  
  232.             case 0xde:
  233.                    key=8;
  234.                                    b1[t]=key;
  235.                                    if(h==1){a1[t]=key;}
  236.                                    t++;
  237.                    break;   
  238.             case 0xbe:
  239.                    key=9;
  240.                                    b1[t]=key;
  241.                                    if(h==1){a1[t]=key;}
  242.                                    t++;
  243.                    break;   
  244.                     case 0x7e:
  245.                                    init();
  246.                    for(k=0;k<6;k++)
  247.                    {       
  248.                         write_add(k,a1[k]);               
  249.                     delayms(20);
  250.                    }
  251.                                break;

  252.           }
  253.          while(temp!=0xf0)
  254.            {
  255.             temp=P2;
  256.             temp=temp&0xf0;
  257.            }
  258.                  if(t==1)
  259.                  {
  260.                   CS2=0;CS1=1;
  261.                    hz_disp161(6,0,shuzi[10]);
  262.                    delay(5);
  263.                if(h==1) {hz_disp161(6,0,shuzi[key]);delay(5);}
  264.                   }
  265.                   if(t==2)
  266.                   {
  267.                   CS2=0;CS1=1;
  268.                   hz_disp161(6,8,shuzi[10]);
  269.                   delay(5);
  270.                    if(h==1) {hz_disp161(6,8,shuzi[key]);delay(5);}
  271.                   }
  272.                   if(t==3)
  273.                   {
  274.                   CS2=0;CS1=1;
  275.                   hz_disp161(6,16,shuzi[10]);
  276.                   delay(5);       
  277.                    if(h==1) {hz_disp161(6,16,shuzi[key]);delay(5);}
  278.                   }
  279.                   if(t==4)
  280.                   {
  281.                   CS2=0;CS1=1;
  282.                   hz_disp161(6,24,shuzi[10]);
  283.                   delay(5);
  284.                    if(h==1) {hz_disp161(6,24,shuzi[key]);delay(5);}
  285.                   }
  286.                   if(t==5)
  287.                   {
  288.                   CS2=0;CS1=1;
  289.                   hz_disp161(6,32,shuzi[10]);
  290.                   delay(5);
  291.                    if(h==1) {hz_disp161(6,32,shuzi[key]);delay(5);}
  292.                   }
  293.                   if(t==6)
  294.                   {
  295.                   CS2=0;CS1=1;
  296.                   hz_disp161(6,40,shuzi[10]);
  297.                   delay(5);
  298.                    if(h==1) {hz_disp161(6,40,shuzi[key]);delay(5);}
  299.                   }                 
  300.        }
  301.     }
  302.   P2=0xfd;
  303.   temp=P2;
  304.   temp=temp&0xf0;
  305.   if(temp!=0xf0)
  306.     {
  307.       delay(10);
  308.       temp=P2;
  309.       temp=temp&0xf0;
  310.       if(temp!=0xf0)
  311.        {
  312.          temp=P2;
  313.          switch(temp)
  314.           {  
  315.             case 0xed:
  316.                    key=4;
  317.                                    b1[t]=key;
  318.                                    if(h==1){a1[t]=key;}
  319.                                    t++;
  320.                    break;  
  321.             case 0xdd:
  322.                    key=5;
  323.                                    b1[t]=key;
  324.                                    if(h==1){a1[t]=key;}
  325.                                    t++;
  326.                    break;   
  327.             case 0xbd:
  328.                    key=6;
  329.                                    b1[t]=key;
  330.                                    if(h==1){a1[t]=key;}
  331.                                    t++;
  332.                    break;   
  333.           }
  334.          while(temp!=0xf0)
  335.            {
  336.             temp=P2;
  337.             temp=temp&0xf0;
  338.            }
  339.                     if(t==1)
  340.                  {
  341.                    CS2=0;CS1=1;
  342.                    hz_disp161(6,0,shuzi[10]);
  343.                    delay(5);
  344.                     if(h==1) {hz_disp161(6,0,shuzi[key]);delay(5);}
  345.                   }
  346.                   if(t==2)
  347.                   {
  348.                     CS2=0;CS1=1;
  349.                     hz_disp161(6,8,shuzi[10]);
  350.                     delay(5);
  351.                         if(h==1) {hz_disp161(6,8,shuzi[key]);delay(5);}       
  352.                   }
  353.                   if(t==3)
  354.                   {
  355.                     CS2=0;CS1=1;
  356.                     hz_disp161(6,16,shuzi[10]);
  357.                     delay(5);
  358.                         if(h==1) {hz_disp161(6,16,shuzi[key]);delay(5);}
  359.                   }
  360.                   if(t==4)
  361.                   {
  362.                     CS2=0;CS1=1;
  363.                     hz_disp161(6,24,shuzi[10]);
  364.                     delay(5);
  365.                         if(h==1) {hz_disp161(6,24,shuzi[key]);delay(5);}
  366.                   }
  367.                   if(t==5)
  368.                   {
  369.                     CS2=0;CS1=1;
  370.                     hz_disp161(6,32,shuzi[10]);
  371.                     delay(5);
  372.                          if(h==1) {hz_disp161(6,32,shuzi[key]);delay(5);}
  373.                   }
  374.                   if(t==6)
  375.                   {
  376.                     CS2=0;CS1=1;
  377.                     hz_disp161(6,40,shuzi[10]);
  378.                     delay(5);
  379.                          if(h==1) {hz_disp161(6,40,shuzi[key]);delay(5);}
  380.                   }       
  381.        }
  382.     }
  383.   P2=0xfb;
  384.   temp=P2;
  385.   temp=temp&0xf0;
  386.   if(temp!=0xf0)
  387.     {
  388.       delay(10);
  389.       temp=P2;
  390.       temp=temp&0xf0;
  391.       if(temp!=0xf0)
  392.        {
  393.          temp=P2;
  394.          switch(temp)
  395.           {  
  396.             case 0xeb:
  397.                    key=1;
  398.                                    b1[t]=key;
  399.                                    if(h==1){a1[t]=key;}
  400.                                    t++;
  401.                    break;  
  402.             case 0xdb:
  403.                    key=2;
  404.                                    b1[t]=key;
  405.                                    if(h==1){a1[t]=key;}
  406.                                    t++;
  407.                    break;   
  408.                         case 0xbb:
  409.                               key=3;
  410.                                   b1[t]=key;
  411.                                   if(h==1){a1[t]=key;}
  412.                                   t++;
  413.                                   break;
  414.                     
  415.           }
  416.          while(temp!=0xf0)
  417.            {
  418.             temp=P2;
  419.             temp=temp&0xf0;
  420.            }
  421.                   if(t==1)
  422.                  {
  423.                    CS2=0;CS1=1;
  424.                    hz_disp161(6,0,shuzi[10]);
  425.                    delay(5);
  426.                     if(h==1) {hz_disp161(6,0,shuzi[key]);delay(5);}
  427.                   }
  428.                   if(t==2)
  429.                   {
  430.                     CS2=0;CS1=1;
  431.                     hz_disp161(6,8,shuzi[10]);
  432.                     delay(5);
  433.                         if(h==1) {hz_disp161(6,8,shuzi[key]);delay(5);}
  434.                   }
  435.                   if(t==3)
  436.                   {
  437.                     CS2=0;CS1=1;
  438.                     hz_disp161(6,16,shuzi[10]);
  439.                     delay(5);
  440.                          if(h==1) {hz_disp161(6,16,shuzi[key]);delay(5);}
  441.                   }
  442.                   if(t==4)
  443.                   {
  444.                     CS2=0;CS1=1;
  445.                     hz_disp161(6,24,shuzi[10]);
  446.                     delay(5);
  447.                          if(h==1) {hz_disp161(6,24,shuzi[key]);delay(5);}
  448.                   }
  449.                   if(t==5)
  450.                   {
  451.                     CS2=0;CS1=1;
  452.                     hz_disp161(6,32,shuzi[10]);
  453.                     delay(5);
  454.                          if(h==1) {hz_disp161(6,0,shuzi[key]);delay(5);}
  455.                   }
  456.                   if(t==6)
  457.                   {
  458.                     CS2=0;CS1=1;
  459.                     hz_disp161(6,40,shuzi[10]);
  460.                     delay(5);
  461.                         if(h==1) {hz_disp161(6,40,shuzi[key]);delay(5);}
  462.                   }       
  463.        }
  464.     }
  465.        
  466.   P2=0xf7;
  467.   temp=P2;
  468.   temp=temp&0xf0;
  469.   if(temp!=0xf0)
  470.     {
  471.       delay(10);
  472.       temp=P2;
  473.       temp=temp&0xf0;
  474.       if(temp!=0xf0)
  475.        {
  476.          temp=P2;
  477.          switch(temp)
  478.           {  
  479.             case 0xe7:   //改密码/
  480.                       init();
  481.                       for(k=0;k<6;k++)
  482.                       {
  483.                         a1[k]=read_add(k);
  484.                        }
  485.                                  if(a1[0]==b1[0]) {        f++;}
  486.                      if(a1[1]==b1[1])  {f++;}
  487.                      if(a1[2]==b1[2]) {f++;}
  488.                      if(a1[3]==b1[3]) {f++;}
  489.                                  if(a1[4]==b1[4])  {f++;}
  490.                                  if(a1[5]==b1[5])  {f++;}
  491.                      if(f==6)
  492.                                  {
  493.                                   f=0;
  494.                                   Clr_Scr();
  495.                                   h=1;
  496.                                   t=0;
  497.                                   ClockMsg();
  498.                                   Xinmima();
  499.                                  }
  500.                                   break;
  501.             case 0xd7:
  502.                    key=0;
  503.                                    b1[t]=key;
  504.                                    if(h==1){a1[t]=key;}
  505.                                    t++;
  506.                    break;   
  507.                         case 0xb7:
  508.                                if(t==1)
  509.                           {
  510.                                     t--;
  511.                             CS2=0;CS1=1;
  512.                             hz_disp161(6,0,kongbai);
  513.                             delay(5);
  514.                           }
  515.                             if(t==2)
  516.                           {
  517.                                      t--;
  518.                              CS2=0;CS1=1;
  519.                              hz_disp161(6,8,kongbai);
  520.                              delay(5);
  521.                           }
  522.                             if(t==3)
  523.                           {
  524.                                      t--;
  525.                              CS2=0;CS1=1;
  526.                              hz_disp161(6,16,kongbai);
  527.                              delay(5);
  528.                            }
  529.                            if(t==4)
  530.                           {
  531.                                     t--;
  532.                             CS2=0;CS1=1;
  533.                             hz_disp161(6,24,kongbai);
  534.                             delay(5);
  535.                           }
  536.                            if(t==5)
  537.                           {
  538.                                     t--;
  539.                             CS2=0;CS1=1;
  540.                             hz_disp161(6,32,kongbai);
  541.                             delay(5);
  542.                           }
  543.                            if(t==6)
  544.                           {
  545.                                     t--;
  546.                             CS2=0;CS1=1;
  547.                             hz_disp161(6,40,kongbai);
  548.                             delay(5);
  549.                                    }
  550.                                   break;
  551.                     case 0x77:          //确认//
  552.                              if(h==1)
  553.                                  {
  554.                             init();         
  555.                     for(k=0;k<6;k++)
  556.                     {       
  557.                               write_add(k,b1[k]);               
  558.                               delayms(20);
  559.                      }
  560.                                         Clr_Scr();
  561.                                         xiugai();
  562.                                         delay(1000);
  563.                                         Clr_Scr();
  564.                                         f=0;t=0;
  565.                                         h=0;
  566.                                   }
  567.                                   else
  568.                                   {
  569.                                init();
  570.                        for(k=0;k<6;k++)
  571.                        {
  572.                         a1[k]=read_add(k);
  573.                         }
  574.                                if(a1[0]==b1[0]) {f++;}
  575.                        if(a1[1]==b1[1])  {f++;}
  576.                        if(a1[2]==b1[2]) {f++;}
  577.                        if(a1[3]==b1[3]) {f++;}
  578.                                    if(a1[4]==b1[4])  {f++;}
  579.                                    if(a1[5]==b1[5])  {f++;}
  580.                        if(f==6)
  581.                        { Clr_Scr();
  582.                              Disp_Img(img2);
  583.                      delay(100);
  584.                                          f=0;
  585.                              while(1);
  586.                             }
  587.                                    else
  588.                                    {
  589.                                     Clr_Scr();
  590.                                         delay(50);
  591.                                         Wrong();
  592.                                         delay(500);
  593.                                         Clr_Scr();
  594.                                         Clr_Scr();
  595.                                         f=0;
  596.                                         m++;
  597.                                         if(m==3)
  598.                                         {
  599.                                
  600.                                           while(1)
  601.                                           {       
  602.                                             speak=0;
  603.                                                 delay(1);
  604.                                                 speak=1;
  605.                                           }
  606.                                          }
  607.                                     }
  608.                                   }
  609.                                   break;
  610.           }
  611.          while(temp!=0xf0)
  612.            {
  613.             temp=P2;
  614.             temp=temp&0xf0;
  615.            }
  616.                   if(t==1)
  617.                  {
  618.                    CS2=0;CS1=1;
  619.                    hz_disp161(6,0,shuzi[10]);
  620.                    delay(5);
  621.                   
  622.                   }
  623.                   if(t==2)
  624.                   {
  625.                     CS2=0;CS1=1;
  626.                     hz_disp161(6,8,shuzi[10]);
  627.                     delay(5);
  628.                        
  629.                   }
  630.                   if(t==3)
  631.                   {
  632.                     CS2=0;CS1=1;
  633.                     hz_disp161(6,16,shuzi[10]);
  634.                     delay(5);
  635.                
  636.                   }
  637.                   if(t==4)
  638.                   {
  639.                     CS2=0;CS1=1;
  640.                     hz_disp161(6,24,shuzi[10]);
  641.                     delay(5);
  642.                
  643.                   }
  644.                   if(t==5)
  645.                   {
  646.                     CS2=0;CS1=1;
  647.                     hz_disp161(6,32,shuzi[10]);
  648.                     delay(5);
  649.                
  650.                   }
  651.                   if(t==6)
  652.                   {
  653.                     CS2=0;CS1=1;
  654.                     hz_disp161(6,40,shuzi[10]);
  655.                     delay(5);
  656.                        
  657.                   }       
  658.        }
  659.     }
  660.          
  661. }

  662. void main()
  663. {
  664.    lcd_init();
  665.    
  666.    Clr_Scr();
  667.    ClockMsg();
  668.    ds1302();
  669.    ds18b20();
  670.    ds18b20();
  671.    ds18b20();
  672.    Mima();
  673.    while(1)
  674.    {

  675.     Refresh();
  676.         matrixkey();
  677.        
  678.    }
  679. }
复制代码

所有资料51hei提供下载:
SPI总线之电子密码锁.rar (222.48 KB, 下载次数: 19)

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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