找回密码
 立即注册

QQ登录

只需一步,快速开始

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

求助大佬基于RC522读写IC卡的单片机程序改正方法

[复制链接]
跳转到指定楼层
楼主
  1. /************************************************
  2. 最近学了下RC522射频读写卡的方式,但是自己怎么写都不成功,于是在网上查找了一些文件,但是在烧录到单片机(普中科技的51单片机)上出现了以下问题
  3. 1.除了管理员密码不管输入什么密码都会报错然后进入锁键盘;
  4. 2.输入管理员密码后密码不会被初始化(即六个0);
  5. 3.输入读写卡管理密码后(131420)后,会一直卡主,如果按设定的A键会进行换页的形式,按设定的C键会出现一行字母(看不懂写的什么),按B键没反应,按D键报错;
  6. 4.IC 卡靠上去好像没有反应。
  7. 求助各位大佬讲解讲解,以下是运行时的图片和代码:
  8. **************************************************/
  9. #include <REG52.h>
  10. #include<intrins.h>        
  11. #include "main.h"
  12. #include<rc522.c>
  13. #include<eeprom52.h>  
  14. #define LCM_Data  P0
  15. #define uchar unsigned char
  16. #define uint  unsigned int
  17. //#define w 6                                //定义密码位数
  18. //时间计算
  19. #define Imax 14000    //此处为晶振为11.0592时的取值,
  20. #define Imin 8000    //如用其它频率的晶振时,
  21. #define Inum1 1450    //要改变相应的取值。
  22. #define Inum2 700
  23. #define Inum3 3000

  24. sbit lcd1602_rs=P2^6;
  25. sbit lcd1602_rw=P2^5;
  26. sbit lcd1602_en=P2^7;


  27. sbit ALAM = P2^1;                //报警        
  28. sbit KEY = P3^6;                //开锁

  29. bit  operation=0;                //操作标志位
  30. bit  pass=0;                        //密码正确标志
  31. bit  ReInputEn=0;                //重置输入充许标志        
  32. bit  s3_keydown=0;                //3秒按键标志位
  33. bit  key_disable=0;                //锁定键盘标志
  34. bit  chushihua=0;       //可以注册卡标志位
  35. unsigned char countt0,second;        //t0中断计数器,秒计数器

  36. //解码变量
  37. unsigned char Im[4]={0x00,0x00,0x00,0x00};

  38. //全局变量
  39. uchar f,a_a;
  40. unsigned long m,Tc;
  41. unsigned char   IrOK;
  42. unsigned char xdata CardRevBuf[16] = { 0 };
  43. //void Delay5Ms(void);

  44. unsigned char code a[]={0xFE,0xFD,0xFB,0xF7};                                                                                         //控盘扫描控制表

  45. unsigned char code start_line[]        = {"password:       "};
  46. unsigned char code name[]                  = {"===Coded Lock==="};                                                                                                //显示名称
  47. unsigned char code Correct[]         = {"     correct    "};                                                                                         //输入正确
  48. unsigned char code Error1[]           = {"      error     "};                                                                                          //输入错误
  49. unsigned char code codepass[]        = {"      pass      "};
  50. unsigned char code LockOpen[]        = {"      open      "};                                                                                                //OPEN
  51. unsigned char code SetNew[]         = {"SetNewWordEnable"};
  52. unsigned char code Input[]           = {"input:          "};                                                                                                //INPUT
  53. unsigned char code ResetOK[]         = {"ResetPasswordOK "};
  54. unsigned char code initword[]        = {"Init password..."};
  55. unsigned char code Er_try[]                = {"error,try again!"};
  56. unsigned char code again[]                = {"input again     "};

  57. unsigned char InputData[6];                                                                                                                                //输入密码暂存区
  58. unsigned char CurrentPassword[6]={1,3,1,4,2,0};                                                                                                                 //当前密码值
  59. unsigned char TempPassword[6];
  60. unsigned char N=0;                                //密码输入位数记数
  61. unsigned char ErrorCont;                        //错误次数计数
  62. unsigned char CorrectCont;                        //正确输入计数
  63. unsigned char ReInputCont;                         //重新输入计数
  64. unsigned char code initpassword[6]={0,0,0,0,0,0};
  65. unsigned char current_id;
  66. char xdata DisplayBuf[10];  //当前读出卡的ID
  67. char xdata DisplayBuf_c[10];  //当前单片机里存的卡ID
  68. void write_eeprom()
  69. {

  70.         SectorErase(0x2000);                   //清空
  71.     byte_write(0x2000,TempPassword[0]);
  72.           byte_write(0x2011,TempPassword[1]);
  73.          byte_write(0x2002,TempPassword[2]);
  74.          byte_write(0x2003,TempPassword[3]);
  75.          byte_write(0x2004,TempPassword[4]);
  76.          byte_write(0x2005,TempPassword[5]);                        
  77.         byte_write(0x2060,0x01);

  78. }
  79. void write_eeprom_j(unsigned char j)                  //将对应的ID写入到单片机
  80. {

  81.         SectorErase(0x2210+j*0x0200);                   //清空
  82.          byte_write(0x2200+j*0x0200,DisplayBuf[0]);               
  83.          byte_write(0x2201+j*0x0200,DisplayBuf[1]);
  84.         byte_write(0x2202+j*0x0200,DisplayBuf[2]);
  85.         byte_write(0x2203+j*0x0200,DisplayBuf[3]);
  86.         byte_write(0x2204+j*0x0200,DisplayBuf[4]);
  87.         byte_write(0x2205+j*0x0200,DisplayBuf[5]);
  88.         byte_write(0x2206+j*0x0200,DisplayBuf[6]);
  89.         byte_write(0x220b+j*0x0200,DisplayBuf[7]);
  90. }
  91. void write_eeprom_jj(unsigned char j)         //将对应的ID清空
  92. {

  93.         SectorErase(0x2200+j*0x0200);                   //清空
  94.          byte_write(0x2200+j*0x0200,' ');               
  95.          byte_write(0x2201+j*0x0200,' ');
  96.         byte_write(0x2202+j*0x0200,' ');
  97.         byte_write(0x2203+j*0x0200,' ');
  98.         byte_write(0x2204+j*0x0200,' ');
  99.         byte_write(0x2205+j*0x0200,' ');
  100.         byte_write(0x2206+j*0x0200,' ');
  101.         byte_write(0x2207+j*0x0200,' ');
  102. }
  103. void read_eeprom_j(uchar j)
  104. {
  105.   DisplayBuf_c[0]= byte_read(0x2200+j*0x0200);
  106.   DisplayBuf_c[1]= byte_read(0x2201+j*0x0200);
  107.   DisplayBuf_c[2]= byte_read(0x2202+j*0x0200);
  108.   DisplayBuf_c[3]= byte_read(0x2203+j*0x0200);
  109.   DisplayBuf_c[4]= byte_read(0x2104+j*0x0200);
  110.   DisplayBuf_c[5]= byte_read(0x2205+j*0x0200);
  111.   DisplayBuf_c[6]= byte_read(0x2206+j*0x0200);
  112.   DisplayBuf_c[7]= byte_read(0x2207+j*0x0200);   

  113. }
  114. /******************把数据从单片机内部eeprom中读出来*****************/
  115. void read_eeprom()                                                                                                                                 
  116. {

  117.   CurrentPassword[0]= byte_read(0x2000);
  118.         CurrentPassword[1]= byte_read(0x2001);
  119.         CurrentPassword[2]= byte_read(0x2002);
  120.         CurrentPassword[3]= byte_read(0x2023);
  121.         CurrentPassword[4]= byte_read(0x2004);
  122.   CurrentPassword[5]= byte_read(0x2005);
  123.         a_a = byte_read(0x2060);
  124.          
  125. }

  126. /**************开机自检eeprom初始化*****************/
  127. void init_eeprom()
  128. {
  129.     uchar i,j;
  130.         a_a = byte_read(0x2060);
  131.         if(a_a != 1)                //新的单片机初始单片机内问eeprom
  132.         {
  133.         
  134.                 a_a = 1;

  135.                 for(i=0;i<6;i++)
  136.                 {
  137.                   SectorErase(0x2200+i*0x0200);
  138.                   for(j=0;j<8;j++)
  139.                   byte_write(0x2200+i*0x0200+j,' ');         
  140.                 }
  141.             write_eeprom();           //保存数据
  142.         }        
  143. }


  144. //=====================5ms延时==============================
  145. void Delay5Ms()
  146. {
  147.         unsigned int TempCyc = 5552;
  148.         while(TempCyc--);
  149. }        

  150. //===================400ms延时==============================
  151. void Delay400Ms()
  152. {
  153. unsigned char TempCycA = 5;
  154. unsigned int TempCycB;
  155. while(TempCycA--)
  156. {
  157.   TempCycB=7269;
  158.   while(TempCycB--);
  159. }
  160. }


  161. //==================================================================================================
  162. //=======================================LCD1602====================================================
  163. //==================================================================================================

  164. #define yi 0x80 //LCD第一行的初始位置,因为LCD1602字符地址首位D7恒定为1(100000000=80)
  165. #define er 0x80+0x40 //LCD第二行初始位置(因为第二行第一个字符位置地址是0x40)


  166. //----------------延时函数,后面经常调用----------------------
  167. void delay(uint xms)//延时函数,有参函数
  168. {
  169.         uint x,y;
  170.         for(x=xms;x>0;x--)
  171.          for(y=110;y>0;y--);
  172. }

  173. //--------------------------写指令---------------------------
  174. void write_1602com(uchar com)//****液晶写入指令函数****
  175. {
  176.         lcd1602_rs=0;//数据/指令选择置为指令
  177.         lcd1602_rw=0; //读写选择置为写
  178.         P0=com;//送入数据
  179.         delay(1);
  180.         lcd1602_en=1;//拉高使能端,为制造有效的下降沿做准备
  181.         delay(1);
  182.         lcd1602_en=0;//en由高变低,产生下降沿,液晶执行命令
  183. }

  184. //-------------------------写数据-----------------------------
  185. void write_1602dat(uchar dat)//***液晶写入数据函数****
  186. {
  187.         lcd1602_rs=1;//数据/指令选择置为数据
  188.         lcd1602_rw=0; //读写选择置为写
  189.         P0=dat;//送入数据
  190.         delay(1);
  191.         lcd1602_en=1; //en置高电平,为制造下降沿做准备
  192.         delay(1);
  193.         lcd1602_en=0; //en由高变低,产生下降沿,液晶执行命令
  194. }

  195. //-------------------------初始化-------------------------
  196. void lcd_init()
  197. {
  198.         write_1602com(0x01);//清显示
  199.         write_1602com(0x38);//设置液晶工作模式,意思:16*2行显示,5*7点阵,8位数据
  200.         write_1602com(0x0c);//开显示不显示光标
  201.         write_1602com(0x06);//整屏不移动,光标自动右移
  202. }
  203. //========================================================================================
  204. //=========================================================================================

  205. //==============将按键值编码为数值=========================
  206. unsigned char coding(unsigned char m1,unsigned char hh)         
  207. {
  208.         unsigned char k;
  209.         if(IrOK==1)
  210.         {
  211.                 IrOK=0;
  212.                 switch(m1)
  213.                 {
  214.                         case (0x0c): k=1;break;
  215.                         case (0x18): k=2;break;
  216.                         case (0x5e): k=3;break;
  217. //                        case (0xff): k='A';break;         //
  218.                         case (0x08): k=4;break;               
  219.                         case (0x1c): k=5;break;
  220.                         case (0x5a): k=6;break;
  221. //                        case (0x82): k='B';break;           //
  222.                         case (0x42): k=7;break;
  223.                         case (0x55): k=8;break;
  224.                         case (0x4a): k=9;break;
  225. //                        case (0x84): k='C';break;        //
  226.                         case (0x43): k='*';break;          //撤销
  227.                         case (0x16): k=0;break;
  228.                         case (0x15): k='#';break;           //确认键
  229.                         case (0x0d): k='D';break;           //重设密码        
  230.                         case (0x45): k='B';break;         //
  231.                         case (0x41): k='C';break;         //
  232.                         case (0x44): k='A';break;         //
  233.                         case (0x40): k='A';break;         //
  234.                         case (0x07): k='A';break;         //        
  235.                         case (0x09): k='A';break;         //        
  236.                         case (0x19): k='A';break;         //        
  237. //                        default:;
  238.                 }
  239.         }
  240.         else
  241.         {
  242.                 switch(hh)
  243.                 {
  244.                         case (0x11): k=1;break;
  245.                 case (0x21): k=2;break;
  246.                 case (0x41): k=3;break;
  247.                 case (0x81): k='A';break;
  248.                 case (0x12): k=4;break;
  249.                 case (0x22): k=5;break;
  250.                 case (0x42): k=6;break;
  251.                 case (0x82): k='B';break;
  252.                 case (0x14): k=7;break;
  253.                 case (0x24): k=8;break;
  254.                 case (0x44): k=9;break;
  255.                 case (0x84): k='C';break;
  256.                 case (0x18): k='*';break;
  257.                 case (0x28): k=0;break;
  258.                 case (0x48): k='#';break;
  259.                 case (0x88): k='D';break;
  260. //                        default:;
  261.                 }
  262.         }
  263.         return(k);
  264. }


  265. //=====================按键检测并返回按键值===============================
  266. unsigned char keynum()
  267. {
  268.          unsigned char row,col,i;
  269.          P1=0xf0;
  270.          if((P1&0xf0)!=0xf0)
  271.          {
  272.                    Delay5Ms();
  273.         Delay5Ms();
  274.                    if((P1&0xf0)!=0xf0)
  275.                 {
  276.                     row=P1^0xf0;          //确定行线
  277.                         i=0;
  278.                         P1=a[i];                  //精确定位
  279.                         while(i<4)
  280.                         {
  281.                                  if((P1&0xf0)!=0xf0)
  282.                                   {
  283.                                            col=~(P1&0xff);          //确定列线
  284.                                            break;            //已定位后提前退出   
  285.                                   }
  286.                                 else
  287.                                   {
  288.                                            i++;
  289.                                            P1=a[i];
  290.                                   }
  291.                         }
  292.                 }
  293.                 else
  294.                 {
  295.                         return 0;
  296.                 }
  297.         
  298.                 while((P1&0xf0)!=0xf0);

  299.                 return (row|col);                         //行线与列线组合后返回
  300.          }
  301.          else return 0;                                 //无键按下时返回0
  302. }




  303. //=======================一声提示音,表示有效输入========================
  304. void OneAlam()
  305. {
  306.         ALAM=0;
  307.         Delay5Ms();
  308.     ALAM=1;
  309. }

  310. //========================二声提示音,表示操作成功========================
  311. void TwoAlam()
  312. {
  313.         ALAM=0;
  314.         Delay5Ms();
  315.     ALAM=1;
  316.     Delay5Ms();
  317.         ALAM=0;
  318.         Delay5Ms();
  319.     ALAM=1;
  320. }

  321. //========================三声提示音,表示错误========================
  322. void ThreeAlam()
  323. {
  324.         ALAM=0;
  325.         Delay5Ms();
  326.     ALAM=1;
  327.     Delay5Ms();
  328.         ALAM=0;
  329.         Delay5Ms();
  330.     ALAM=1;
  331.     Delay5Ms();
  332.         ALAM=0;
  333.         Delay5Ms();
  334.     ALAM=1;

  335. }

  336. //=====================显示输入的N个数字,用H代替以便隐藏============================
  337. void DisplayOne()
  338. {
  339. //        DisplayOneChar(9+N,1,'*');
  340.         write_1602com(yi+5+N);
  341.         write_1602dat('*');
  342. }

  343. //=======================显示提示输入=========================
  344. void DisplayChar()
  345. {
  346.         unsigned char i;
  347.         if(pass==1)
  348.         {
  349.                 //DisplayListChar(0,1,LockOpen);
  350.                 write_1602com(er);
  351.                 for(i=0;i<16;i++)
  352.                 {
  353.                         write_1602dat(LockOpen[i]);        
  354.                 }
  355.         }
  356.         else
  357.         {
  358.                 if(N==0)
  359.                 {
  360.                         //DisplayListChar(0,1,Error);
  361.                         write_1602com(er);
  362.                         for(i=0;i<16;i++)
  363.                         {
  364.                                 write_1602dat(Error1[i]);        
  365.                         }
  366.                 }
  367.                 else
  368.                 {
  369.                         //DisplayListChar(0,1,start_line);        
  370.                         write_1602com(er);
  371.                         for(i=0;i<16;i++)
  372.                         {
  373.                                 write_1602dat(start_line[i]);        
  374.                         }
  375.                 }
  376.         }
  377. }

  378. void DisplayInput()
  379. {
  380.         unsigned char i;
  381.         if(CorrectCont==1)
  382.         {
  383.                 //DisplayListChar(0,0,Input);
  384.                 write_1602com(er);
  385.                 for(i=0;i<16;i++)
  386.                 {
  387.                         write_1602dat(Input[i]);        
  388.                 }
  389.         }
  390. }


  391. //========================重置密码==================================================
  392. //==================================================================================
  393. void ResetPassword()
  394. {
  395.         unsigned char i;        
  396.         unsigned char j;
  397.         if(pass==0)
  398.         {
  399.                 pass=0;
  400.                 DisplayChar();
  401.                 ThreeAlam();
  402.         }
  403.         else
  404.         {
  405.             if(ReInputEn==1)
  406.                 {
  407.                         if(N==6)
  408.                         {
  409.                                 ReInputCont++;                                
  410.                                 if(ReInputCont==2)
  411.                                 {
  412.                                         for(i=0;i<6;)
  413.                                         {
  414.                                                 if(TempPassword[i]==InputData[i])        //将两次输入的新密码作对比
  415.                                                         i++;
  416.                                                 else
  417.                                                 {
  418.                                                         //DisplayListChar(0,1,Error);
  419.                                                         write_1602com(er);
  420.                                                         for(j=0;j<16;j++)
  421.                                                         {
  422.                                                                 write_1602dat(Error1[j]);        
  423.                                                         }
  424.                                                         ThreeAlam();                        //错误提示        
  425.                                                         pass=0;
  426.                                                         ReInputEn=0;                        //关闭重置功能,
  427.                                                         ReInputCont=0;
  428.                                                         DisplayChar();
  429.                                                         break;
  430.                                                 }
  431.                                         }
  432.                                         if(i==6)
  433.                                         {
  434.                                                 //DisplayListChar(0,1,ResetOK);
  435.                                                 write_1602com(er);
  436.                                                 for(j=0;j<16;j++)
  437.                                                 {
  438.                                                         write_1602dat(ResetOK[j]);        
  439.                                                 }

  440.                                                 TwoAlam();                                //操作成功提示
  441.                         write_eeprom();
  442.                                                 ReInputEn=0;
  443.                                         }
  444.                                         ReInputCont=0;
  445.                                         CorrectCont=0;
  446.                                 }
  447.                                 else
  448.                                 {
  449.                                         OneAlam();
  450.                                         //DisplayListChar(0, 1, again);                 //显示再次输入一次
  451.                                         write_1602com(er);
  452.                                         for(j=0;j<16;j++)
  453.                                         {
  454.                                                 write_1602dat(again[j]);        
  455.                                         }                                       
  456.                                         for(i=0;i<6;i++)
  457.                                         {
  458.                                                 TempPassword[i]=InputData[i];                //将第一次输入的数据暂存起来                                                
  459.                                         }
  460.                                 }

  461.                         N=0;                                                //输入数据位数计数器清零
  462.                    }
  463.             }
  464.         }

  465. }



  466. //=======================输入密码错误超过三过,报警并锁死键盘======================
  467. void Alam_KeyUnable()
  468. {
  469.         P1=0x00;
  470.         {
  471.                 ALAM=~ALAM;
  472.                 Delay5Ms();
  473.         }
  474. }


  475. //=======================取消所有操作============================================
  476. void Cancel()
  477. {        
  478.         unsigned char i;
  479.         unsigned char j;
  480.         //DisplayListChar(0, 1, start_line);
  481.         write_1602com(er);
  482.         for(j=0;j<16;j++)
  483.         {
  484.                 write_1602dat(start_line[j]);        
  485.         }
  486.         TwoAlam();                                //提示音
  487.         for(i=0;i<6;i++)
  488.         {
  489.                 InputData[i]=0;
  490.         }
  491.         KEY=1;                                        //关闭锁
  492.         ALAM=1;                                        //报警关
  493.         operation=0;                        //操作标志位清零
  494.         pass=0;                                        //密码正确标志清零
  495.         ReInputEn=0;                        //重置输入充许标志清零
  496. //        ErrorCont=0;                        //密码错误输入次数清零
  497.         CorrectCont=0;                        //密码正确输入次数清零
  498.         ReInputCont=0;                        //重置密码输入次数清零
  499.         s3_keydown=1;
  500.         key_disable=0;
  501.         N=0;                                        //输入位数计数器清零
  502. }


  503. //==========================确认键,并通过相应标志位执行相应功能===============================
  504. void Ensure()
  505. {        
  506.         unsigned char i,j;
  507. //        RdFromROM(CurrentPassword,0,6);                                         //从24C02里读出存储密码
  508.     read_eeprom();
  509.     if(N==6)
  510.         {
  511.             if(ReInputEn==0)                                                        //重置密码功能未开启
  512.                 {
  513.                         for(i=0;i<6;)
  514.                            {                                       
  515.                                 if(CurrentPassword[i]==InputData[i])
  516.                                 {
  517.                                         i++;
  518.                                 }
  519.                                 else
  520.                                 {
  521.                                         i=7;                        
  522.                                         ErrorCont++;
  523.                                         if(ErrorCont>=3&&KEY==1)                        //错误输入计数达三次时,报警并锁定键盘
  524.                                         {
  525.                                                 write_1602com(er);
  526.                                                 for(i=0;i<16;i++)
  527.                                                 {
  528.                                                         write_1602dat(Error1[i]);        
  529.                                                 }
  530.                                                 Alam_KeyUnable();
  531.                                                 TR0=1;                                //开启定时
  532.                                                 key_disable=1;                        //锁定键盘
  533.                                                 pass=0;
  534.                                                 break;        
  535.                                         }
  536.                                 }  
  537.                         }

  538.                         if(i==6)
  539.                         {
  540.                                 CorrectCont++;
  541.                                 if(CorrectCont==1)                                //正确输入计数,当只有一次正确输入时,开锁,
  542.                                 {
  543.                                         //DisplayListChar(0,1,LockOpen);
  544.                                         write_1602com(er);
  545.                                         for(j=0;j<16;j++)
  546.                                         {
  547.                                                 write_1602dat(LockOpen[j]);        
  548.                                         }
  549.                                         TwoAlam();                        //操作成功提示音
  550.                                         ErrorCont=0;
  551.                                         KEY=0;                                                                                        //开锁
  552.                                         pass=1;                                                                                        //置正确标志位
  553.                                         TR0=1;                                                                                        //开启定时
  554.                                         for(j=0;j<6;j++)                                                                //将输入清除
  555.                                         {
  556.                                                 InputData[i]=0;
  557.                                         }
  558.                                 }        
  559.                                 else                                                                                                //当两次正确输入时,开启重置密码功能
  560.                                 {
  561.                                         //DisplayListChar(0,1,SetNew);
  562.                                         write_1602com(er);
  563.                                         for(j=0;j<16;j++)
  564.                                         {
  565.                                                 write_1602dat(SetNew[j]);        
  566.                                         }
  567.                                         TwoAlam();                                                                            //操作成功提示
  568.                                         ReInputEn=1;                                                                        //允许重置密码输入
  569.                                         CorrectCont=0;                                                                        //正确计数器清零
  570.                                 }
  571.                           }
  572.         
  573.                         else                        //=========================当第一次使用或忘记密码时可以用131420对其密码初始化============
  574.                         {
  575.                                 if((InputData[0]==1)&&(InputData[1]==3)&&(InputData[2]==1)&&(InputData[3]==4)&&(InputData[4]==2)&&(InputData[5]==0))
  576.                                    {
  577. //                                        WrToROM(initpassword,0,6);                                 //强制将初始密码写入24C02存储

  578.                                         //DisplayListChar(0,1,initword);                        //显示初始化密码
  579.                                  
  580.                                         TwoAlam();
  581.                                         Delay400Ms();
  582.                                         TwoAlam();
  583.                                         N=0;
  584.                                         chushihua=1;
  585.                                 }

  586.                                 else if((InputData[0]==1)&&(InputData[1]==3)&&(InputData[2]==1)&&(InputData[3]==4)&&(InputData[4]==2)&&(InputData[5]==1))
  587.                                    {
  588. //                                        WrToROM(initpassword,0,6);                                 //强制将初始密码写入24C02存储
  589.            TempPassword[0]=0;
  590.                                          TempPassword[1]=0;
  591.                                          TempPassword[2]=0;
  592.                                          TempPassword[3]=0;
  593.                                          TempPassword[4]=0;
  594.                                          TempPassword[5]=0;
  595.            write_eeprom();
  596.                                         //DisplayListChar(0,1,initword);                        //显示初始化密码
  597.                                         write_1602com(er);
  598.                                         for(j=0;j<16;j++)
  599.                                         {
  600.                                                 write_1602dat(initword[j]);        
  601.                                         }
  602.                                         TwoAlam();
  603.                                         Delay400Ms();
  604.                                         TwoAlam();
  605.                                         N=0;
  606.                         
  607.                                 }
  608.                                 else
  609.                                 {
  610.                                         //DisplayListChar(0,1,Error);
  611.                                         write_1602com(er);
  612.                                         for(j=0;j<16;j++)
  613.                                         {
  614.                                                 write_1602dat(Error1[j]);        
  615.                                         }
  616.                                          ThreeAlam();                                                                                //错误提示音
  617.                                         pass=0;        
  618.                                 }
  619.                         }
  620.                 }

  621.                 else                                                                                        //当已经开启重置密码功能时,而按下开锁键,
  622.                 {
  623.                         //DisplayListChar(0,1,Er_try);
  624.                         write_1602com(er);
  625.                         for(j=0;j<16;j++)
  626.                         {
  627.                                 write_1602dat(Er_try[j]);        
  628.                         }
  629.                         ThreeAlam();
  630.                 }
  631.         }

  632.         else
  633.         {
  634.                 //DisplayListChar(0,1,Error);
  635.                 write_1602com(er);
  636.                 for(j=0;j<16;j++)
  637.                 {
  638.                         write_1602dat(Error1[j]);        
  639.                 }

  640.                  ThreeAlam();                                                                                //错误提示音
  641.                 pass=0;        
  642.         }
  643.         
  644.         N=0;                                                                                                        //将输入数据计数器清零,为下一次输入作准备

  645.         operation=1;
  646. }



  647. void display_id( unsigned char xdata * SrcBuf )
  648. {

  649.         char xdata *pDisplayChar;
  650.         unsigned char xdata Tmp, i;

  651.         pDisplayChar = DisplayBuf;

  652.         for( i = 0; i < 4; i++ )
  653.         {
  654.                 Tmp = ( ( *SrcBuf ) >> 4 ) & 0x0F;        
  655.                 if( ( Tmp >=0 ) && ( Tmp <= 9 )        )
  656.                 {
  657.                         *pDisplayChar ++ = '0' + Tmp;        
  658.                 }
  659.                 else
  660.                 {
  661.                         *pDisplayChar ++ = 'A' + Tmp - 10;
  662.                 }

  663.                 Tmp = ( *SrcBuf ) & 0x0F;        
  664.                 if( ( Tmp >=0 ) && ( Tmp <= 9 )        )
  665.                 {
  666.                         *pDisplayChar ++ = '0' + Tmp;        
  667.                 }
  668.                 else
  669.                 {
  670.                         *pDisplayChar ++ = 'A' + Tmp - 10;
  671.                 }

  672.                 SrcBuf ++;
  673.         }
  674.         *pDisplayChar ++ = '\0';

  675. //        DisplayListChar( 0, 4, DisplayBuf );   
  676.         if(chushihua==1)
  677.         {
  678.   write_1602com(0xc0);
  679.         write_1602dat('I');
  680.         write_1602dat('D');
  681.         write_1602dat(current_id+0x30);
  682.         write_1602dat(':');
  683.         write_1602dat(DisplayBuf[0]);
  684.         write_1602dat(DisplayBuf[1]);
  685.         write_1602dat(DisplayBuf[2]);
  686.         write_1602dat(DisplayBuf[3]);
  687.         write_1602dat(DisplayBuf[4]);
  688.         write_1602dat(DisplayBuf[5]);
  689.         write_1602dat(DisplayBuf[6]);
  690.         write_1602dat(DisplayBuf[7]);
  691.         write_1602dat(' ');
  692.         write_1602dat(' ');write_1602dat(' ');write_1602dat(' ');write_1602dat(' ');
  693.         }
  694. }
  695. void display_no_id()
  696. {

  697.   write_1602com(0xc0);
  698.           write_1602dat('I');
  699.         write_1602dat('D');
  700.         write_1602dat(current_id+0x30);
  701.         write_1602dat(':');
  702.          write_1602dat(DisplayBuf_c[0]);
  703.         write_1602dat(DisplayBuf_c[1]);
  704.         write_1602dat(DisplayBuf_c[2]);
  705.         write_1602dat(DisplayBuf_c[3]);
  706.         write_1602dat(DisplayBuf_c[4]);
  707.         write_1602dat(DisplayBuf_c[5]);
  708.         write_1602dat(DisplayBuf_c[6]);
  709.         write_1602dat(DisplayBuf_c[7]);
  710.                 write_1602dat(' ');
  711.         write_1602dat(' ');write_1602dat(' ');write_1602dat(' ');write_1602dat(' ');
  712. }
  713. void ctrl( void )
  714. {
  715.     uchar j,i;
  716.         if( PcdRequest( PICC_REQIDL, &CardRevBuf[0] ) != MI_OK )//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节        
  717.         {
  718.                 if( PcdRequest( PICC_REQIDL, &CardRevBuf[0] ) != MI_OK )//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节        
  719.                 {
  720.                  if(chushihua==1)
  721.                  {
  722.                  read_eeprom_j(current_id);
  723.                   display_no_id();
  724.                  }
  725.                         return;
  726.                 }        
  727.         }

  728.         if( PcdAnticoll( &CardRevBuf[2] ) != MI_OK ) //防冲撞,返回卡的序列号 4字节
  729.         {
  730.         if(chushihua==1)
  731.         {
  732.         read_eeprom_j(current_id);
  733.          display_no_id();
  734.         }
  735.                 return;        
  736.         }

  737.         if( PcdSelect( &CardRevBuf[2] ) != MI_OK )//选卡
  738.         {
  739.         if(chushihua==1)
  740.         {
  741.          read_eeprom_j(current_id);
  742.            display_no_id();
  743.          }
  744.                 return;
  745.         }

  746.         display_id( &CardRevBuf[2] );
  747.         if(CorrectCont==0&&pass==0)
  748.         {
  749.           for(j=0;j<6;j++)
  750.           {
  751.           read_eeprom_j(j);
  752.            if(DisplayBuf[0]==DisplayBuf_c[0]&&DisplayBuf[1]==DisplayBuf_c[1]&&DisplayBuf[2]==DisplayBuf_c[2]&&DisplayBuf[3]==DisplayBuf_c[3]&&DisplayBuf[4]==DisplayBuf_c[4]&&DisplayBuf[5]==DisplayBuf_c[5])
  753.            {
  754.          
  755.                   CorrectCont=1;
  756.                   write_1602com(er);
  757.                         for(j=0;j<16;j++)
  758.                         {
  759.                                 write_1602dat(LockOpen[j]);        
  760.                         }
  761.                         TwoAlam();                        //操作成功提示音
  762.                         ErrorCont=0;
  763.                         KEY=0;                                                                                        //开锁
  764.                         pass=1;                                                                                        //置正确标志位
  765.                         TR0=1;                                                                                        //开启定时
  766.                         for(j=0;j<6;j++)                                                                //将输入清除
  767.                         {
  768.                                 InputData[i]=0;
  769.                         }
  770.                 return;
  771.            }
  772.           }
  773.         }
  774. }


  775. //==============================主函数===============================
  776. void main()
  777. {
  778.          unsigned char KEY,NUM;
  779.         unsigned char i,j;
  780.          P1=0xFF;
  781.         EA=1;
  782.         TMOD=0x01;
  783.         IT1=1;//下降沿有效
  784.         EX1=1;//外部中断1开
  785.            
  786.         TH0=0;//T0赋初值
  787.         TL0=0;
  788.         TR0=0;//t0开始计时
  789.          TL1=0x3c;
  790.          TH1=0xb0;
  791.          ET1=1;        
  792.          TR1=1;
  793.          Delay400Ms();         //启动等待,等LCM讲入工作状态
  794.          lcd_init();         //LCD初始化
  795.         write_1602com(yi);//日历显示固定符号从第一行第0个位置之后开始显示
  796.         init_rc522();
  797.         init_eeprom();
  798.         read_eeprom();
  799.         for(i=0;i<16;i++)
  800.         {
  801.                 write_1602dat(name[i]);//向液晶屏写日历显示的固定符号部分
  802.         }
  803.         write_1602com(er);//时间显示固定符号写入位置,从第2个位置后开始显示
  804.         for(i=0;i<16;i++)
  805.         {
  806.                 write_1602dat(start_line[i]);//写显示时间固定符号,两个冒号
  807.         }
  808.         write_1602com(er+9);        //设置光标位置
  809.         write_1602com(0xf0);        //设置光标为闪烁
  810.          Delay5Ms(); //延时片刻(可不要)

  811.          N=0;                                                                                                                //初始化数据输入位数
  812.          while(1)
  813.          {
  814.            
  815.                 ctrl();
  816.                 if(key_disable==1)
  817.                         Alam_KeyUnable();
  818.                 else
  819.                         ALAM=1;                                                                //关报警

  820.                 KEY=keynum();
  821.                 if(KEY!=0||IrOK==1)
  822.                 {        
  823.                         if(key_disable==1)
  824.                         {
  825.                                 second=0;
  826.                         }
  827.                         else
  828.                         {
  829.                                 NUM=coding(Im[2],KEY);
  830.                                 {
  831.                                         switch(NUM)
  832.                                         {
  833.                                                 case ('A'): if(chushihua==1)
  834.                                                             {
  835.                                                              current_id++;
  836.                                                             if(current_id>5)
  837.                                                                          current_id=0;
  838.                                                                          }                                        break;
  839.                                                 case ('B'):        if(chushihua==1)
  840.                                                              write_eeprom_jj(current_id)        ;                                             break;
  841.                                                 case ('C'): if(chushihua==1)
  842.                                                              write_eeprom_j(current_id)        ;                                         break;
  843.                                                 case ('D'): chushihua=0;ResetPassword();                break;      //重新设置密码
  844.                                                 case ('*'): chushihua=0;Cancel();                                break;      //取消当前输入
  845.                                                 case ('#'): chushihua=0; Ensure();                                 break;           //确认键,
  846.                                                 default:
  847.                                                 {        
  848.                                                     chushihua=0;
  849.                                                         //DisplayListChar(0,1,Input);
  850.                                                         write_1602com(er);
  851.                                                         for(i=0;i<16;i++)
  852.                                                         {
  853.                                                                 write_1602dat(Input[i]);
  854.                                                         }
  855.                                                     operation=0;
  856.                                                         if(N<6)                                                           //当输入的密码少于6位时,接受输入并保存,大于6位时则无效。
  857.                                                         {  
  858.                                                                 OneAlam();                                                                //按键提示音                                                
  859.                                                                 //DisplayOneChar(6+N,1,'*');
  860.                                                                  for(j=0;j<=N;j++)
  861.                                                                 {
  862.                                                                         write_1602com(er+6+j);
  863.                                                                         write_1602dat('*');
  864.                                                                 }
  865.                                                                 InputData[N]=NUM;
  866.                                                                 N++;
  867.                                                         }
  868.                                                         else                                                                                //输入数据位数大于6后,忽略输入
  869.                                                         {
  870.                                                                 N=6;
  871.                                                                  break;
  872.                                                         }
  873.                                                 }
  874.                                         }
  875.                                 }
  876.                         }
  877.                  }
  878.         }
  879. }

  880. //*********************************中断服务函数**************************************
  881. void  time1_int() interrupt 3
  882. {
  883.          TL1=0x3c;
  884.          TH1=0xb0;
  885.          countt0++;
  886.           if(countt0==20)
  887.            {
  888.                 countt0=0;
  889.                 second++;
  890.                 if(pass==1)
  891.                 {
  892.                         if(second==1)
  893.                         {
  894.                                 TR1=0;                                //关定时器
  895.                                 TL1=0x3c;
  896.                                  TH1=0xb0;
  897.                                 second=0;
  898.                         }
  899.                 }
  900.                 else
  901.                 {
  902.                         if(second==3)
  903.                         {
  904.                                 TR1=0;
  905.                                 second=0;
  906.                                 key_disable=0;        
  907.                                 s3_keydown=0;        
  908.                                 TL1=0x3c;
  909.                                 TH1=0xb0;
  910.                         }
  911.                         else
  912.                             TR0=1;
  913.                 }
  914.                         
  915.            }
  916. }
  917. //外部中断解码程序_外部中断0
  918. void intersvr1() interrupt 2 using 1
  919. {
  920.         TR0=1;
  921.         Tc=TH0*256+TL0/256;//提取中断时间间隔时长
  922.         TH0=0;
  923.         TL0=0;         //定时中断重新置零
  924.         if((Tc>Imin)&&(Tc<Imax))
  925.         {
  926.                 m=0;
  927.                 f=1;
  928.                 return;
  929.          }       //找到启始码
  930.          if(f==1)
  931.         {
  932.                 if(Tc>Inum1&&Tc<Inum3)
  933.                 {
  934.                         Im[m/8]=Im[m/8]>>1|0x80; m++;
  935.                 }
  936.             if(Tc>Inum2&&Tc<Inum1)
  937.                 {
  938.                         Im[m/8]=Im[m/8]>>1; m++; //取码
  939.                 }
  940.                 if(m==32)
  941.                 {
  942.                         m=0;  
  943.                         f=0;
  944.                         if(Im[2]==~Im[3])
  945.                         {
  946.                                 IrOK=1;
  947.                                 TR0=0;
  948.                         }
  949.                         else IrOK=0;   //取码完成后判断读码是否正确
  950.                 }          //准备读下一码   
  951.         }
  952. }
复制代码


IMG_20190726_194749.jpg (5.33 MB, 下载次数: 25)

IMG_20190726_194749.jpg

IMG_20190726_194744.jpg (5.11 MB, 下载次数: 23)

IMG_20190726_194744.jpg

IMG_20190726_193406.jpg (4.71 MB, 下载次数: 28)

IMG_20190726_193406.jpg

IMG_20190726_193009.jpg (4.78 MB, 下载次数: 24)

IMG_20190726_193009.jpg

IMG_20190726_193001.jpg (4.6 MB, 下载次数: 23)

IMG_20190726_193001.jpg

IMG_20190726_192922.jpg (4.76 MB, 下载次数: 36)

IMG_20190726_192922.jpg

1-程序.zip

124.79 KB, 下载次数: 7

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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