⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 11t6_sc.lst

📁 读写Mifare One卡, 说明:此例程为一个块的读写操作
💻 LST
📖 第 1 页 / 共 3 页
字号:
 233   1              while(UART_RX_BIT);
 234   1              delay104us();
 235   1              while(i<8)
 236   1              {       UARTDATA>>=1;
 237   2                      if(UART_RX_BIT)
 238   2                      {
 239   3                          UARTDATA|=0x80;
 240   3                      }
 241   2                      i++;
C51 COMPILER V7.06   11T6_SC                                                               09/12/2007 12:40:46 PAGE 5   

 242   2                      delay104us();
 243   2              }
 244   1              UART_RX_BIT=1;
 245   1              return  UARTDATA;
 246   1      }
 247          
 248          /*--------------------------------------------------------------------
 249           FUNCTION: Commu_test(),通讯测试
 250           INPUT:
 251           OUTPUT:   1byte CardStatus
 252          --------------------------------------------------------------------*/
 253          /************************************************
 254          一、    通讯测试,测试单片机与模块通讯是否正常
 255          master command:3C 04 00 60 00 00  BCC 0D
 256          ************************************************/
 257          void Commu_test(void)
 258          {       uchar   i;
 259   1              uchar txbuff[8];
 260   1              uchar rxbuff[6];
 261   1              CardStatus=0;
 262   1      t111:
 263   1          txbuff[0]=0x3C;
 264   1              txbuff[1]=0x04;
 265   1              txbuff[2]=0x00;
 266   1              txbuff[3]=0x60;
 267   1              txbuff[4]=0x00;
 268   1              txbuff[5]=0x00;
 269   1              txbuff[6]=0;
 270   1              for(i=0;i<5;i++) txbuff[6]^=txbuff[i];
 271   1              txbuff[7]=0x0D;
 272   1      
 273   1      
 274   1              for(i=0;i<8;i++)
 275   1              {
 276   2                      UART_PutByte(txbuff[i]);
 277   2              }
 278   1      //;--------------------------------------------------------------------
 279   1      //; Right: 3C 01 01 BCC 0D
 280   1      //; Error: 3C 01 00 BCC 0D
 281   1      //;--------------------------------------------------------------------
 282   1      //;;;;;;
 283   1              for(i=0;i<5;i++)
 284   1              {
 285   2                      rxbuff[i]=UART_GetByte();
 286   2                       if(rxbuff[0]!=0x3C)i=0;//纠错
 287   2              }
 288   1          if(rxbuff[0]!=0x3C)goto t111;
 289   1          if(rxbuff[1]!=0x01)goto t111;
 290   1          if(rxbuff[2]!=0x01)goto t111;
 291   1          if(rxbuff[4]!=0x0d)goto t111;
 292   1      //t111:
 293   1              rxbuff[5]=0;
 294   1              for(i=0;i<3;i++)
 295   1                      rxbuff[5]^=rxbuff[i];//纠错
 296   1              if(rxbuff[5]==rxbuff[3])
 297   1                if(rxbuff[2])
 298   1                {
 299   2                      CardStatus=0x01;
 300   2                }
 301   1      
 302   1      }
 303          
C51 COMPILER V7.06   11T6_SC                                                               09/12/2007 12:40:46 PAGE 6   

 304          
 305          /*--------------------------------------------------------------------
 306           FUNCTION: Search_Card,寻卡,有卡则读出卡号
 307           INPUT:    uchar *CardNo,uchar SearMode
 308           OUTPUT:   4byte CardNo and  1byte CardStatus
 309          --------------------------------------------------------------------*/
 310          /***********************************************************
 311          二、    寻卡
 312          command:3C 04 01 70 00(寻卡模式00(IDLE)或01(ALL)) 00 BCC 0D
 313          ***********************************************************/
 314          
 315          void Search_Card(uchar *CardNo_,uchar SMode_)
 316          {       uchar i;
 317   1              uchar   txbuff[8];
 318   1              uchar   rxbuff[10];
 319   1      //t112:
 320   1          CardStatus=0;    //卡状态给初值 0
 321   1              txbuff[0]=0x3C;
 322   1              txbuff[1]=0x04;
 323   1              txbuff[2]=0x01;
 324   1              txbuff[3]=0x70;
 325   1              txbuff[4]=SMode_;       //search mode
 326   1              txbuff[5]=0x00;
 327   1              txbuff[6]=0x00;
 328   1              for(i=0;i<6;i++) txbuff[6]^=txbuff[i];
 329   1              txbuff[7]=0x0D;
 330   1      
 331   1          for(i=0;i<8;i++)
 332   1              {
 333   2                      UART_PutByte(txbuff[i]);
 334   2              }
 335   1      
 336   1      //;--------------------------------------------------------------------
 337   1      //; Right: 3C 05 四字节卡号 00 BCC 0D
 338   1      //; Error: 3C 01 FF(错误返回代码) BCC 0D
 339   1      //;--------------------------------------------------------------------
 340   1      //;;;;;;
 341   1              for(i=0;i<9;i++)
 342   1              {
 343   2                      rxbuff[i]=UART_GetByte();
 344   2                      if(rxbuff[0]!=0x3C)i=0;//纠错
 345   2                      if(rxbuff[i]==0x0D)break;
 346   2              }
 347   1          if(rxbuff[0]!=0x3C)goto t112;
 348   1          if(rxbuff[1]!=0x05)goto t112;
 349   1          if(rxbuff[6]!=0x00)goto t112;
 350   1          if(rxbuff[8]!=0x0d)goto t112;
 351   1      t112:        rxbuff[9]=0;
 352   1              for(i=0;i<7;i++)rxbuff[9]^=rxbuff[i];
 353   1              if(rxbuff[9]==rxbuff[7])
 354   1              if(rxbuff[1]==0x05)
 355   1              {
 356   2                      CardStatus=0x01;
 357   2                      for(i=0;i<4;i++)*CardNo_++=rxbuff[2+i];
 358   2      
 359   2              }
 360   1      
 361   1      }
 362          
 363          
 364          
 365          /*--------------------------------------------------------------------
C51 COMPILER V7.06   11T6_SC                                                               09/12/2007 12:40:46 PAGE 7   

 366           FUNCTION: Psd_Verify,密码校验
 367           INPUT:    uchar *BlockKey,uchar BlockKeyAorB,uchar BlockNo,uchar *CardNo
 368           OUTPUT:   1byte CardStatus
 369          --------------------------------------------------------------------*/
 370          /**************************************************
 371          三、    密码校验
 372          command:3C 0E 01 6C 6字节密码 00(A密码或B密码01) 00(块号) 四字节卡号 BCC 0D
 373          ***************************************************/
 374          void    Psd_Verify(uchar *BlockKey_,uchar BlockKeyAorB_,uchar BlockNo_,uchar *CardNo_)
 375          {       uchar i;
 376   1              uchar   txbuff[18];
 377   1              uchar   rxbuff[7];
 378   1      //t113:
 379   1          CardStatus=0;
 380   1              txbuff[0]=0x3C;
 381   1              txbuff[1]=0x0E;
 382   1              txbuff[2]=0x01;
 383   1              txbuff[3]=0x6C;
 384   1              for(i=0;i<6;i++)txbuff[4+i]=*BlockKey_++;
 385   1              txbuff[10]=BlockKeyAorB_;
 386   1              txbuff[11]=BlockNo_;
 387   1              for(i=0;i<4;i++)txbuff[12+i]=*CardNo_++;
 388   1              txbuff[16]=0;
 389   1              for(i=0;i<16;i++)txbuff[16]^=txbuff[i];
 390   1              txbuff[17]=0x0D;
 391   1      
 392   1          for(i=0;i<18;i++)
 393   1              {
 394   2                      UART_PutByte(txbuff[i]);
 395   2              }
 396   1      
 397   1      //;--------------------------------------------------------------------
 398   1      //; Right: 3C 02 00 00 BCC 0D
 399   1      //; Error: 3C 01 FF(错误代码) BCC 0D
 400   1      //;--------------------------------------------------------------------
 401   1              for(i=0;i<6;i++)
 402   1              {
 403   2                      rxbuff[i]=UART_GetByte();
 404   2                      if(rxbuff[0]!=0x3C)i=0;
 405   2                      if(rxbuff[i]==0x0D)break;
 406   2              }
 407   1          if(rxbuff[0]!=0x3C)goto t113;
 408   1          if(rxbuff[1]!=0x02)goto t113;
 409   1          if(rxbuff[2]!=0x00)goto t113;
 410   1          if(rxbuff[3]!=0x00)goto t113;
 411   1          if(rxbuff[5]!=0x0d)goto t113;
 412   1      t113:        rxbuff[6]=0;
 413   1              for(i=0;i<4;i++)rxbuff[6]^=rxbuff[i];
 414   1              if(rxbuff[2]==0)
 415   1                 if(rxbuff[4]==rxbuff[6])
 416   1                      CardStatus=0x01;
 417   1      
 418   1      }
 419          
 420          /*--------------------------------------------------------------------
 421           FUNCTION: Read_Card,读指定块数据
 422           INPUT:    uchar BlockNo,uchar *CardDataBuf
 423           OUTPUT:   16byte CardData,1byte CardStatus
 424          --------------------------------------------------------------------*/
 425          /**************************************************
 426          四、    读指定块数据
 427          ;command:3C 04 01 66 00(绝对块号) 00 BCC 0D
C51 COMPILER V7.06   11T6_SC                                                               09/12/2007 12:40:46 PAGE 8   

 428          **************************************************/
 429          void Read_Card(uchar BlockNo_,uchar *CardDataBuf_)
 430          {       uchar i;
 431   1              uchar txbuff[8];
 432   1              uchar rxbuff[22];
 433   1      //t114:
 434   1          CardStatus=0;
 435   1              txbuff[0]=0x3C;
 436   1              txbuff[1]=0x04;
 437   1              txbuff[2]=0x01;
 438   1              txbuff[3]=0x66;
 439   1              txbuff[4]=BlockNo_;
 440   1              txbuff[5]=0x00;
 441   1              txbuff[6]=0;
 442   1              for(i=0;i<6;i++)txbuff[6]^=txbuff[i];
 443   1              txbuff[7]=0x0D;
 444   1      
 445   1          for(i=0;i<8;i++)
 446   1              {
 447   2                      UART_PutByte(txbuff[i]);
 448   2              }
 449   1      //;--------------------------------------------------------------------
 450   1      //; Right: 3C 12 00 DATA0~DATA15 BCC 0D
 451   1      //; Error: 3C 01 FF(错误返回代码) BCC 0D
 452   1      //;--------------------------------------------------------------------
 453   1      //;;;;;;
 454   1              for(i=0;i<21;i++)
 455   1              {
 456   2                      rxbuff[i]=UART_GetByte();
 457   2                      if(rxbuff[0]!=0x3C)i=0;
 458   2                      if(rxbuff[i]==0x0D)break;
 459   2              }
 460   1          if(rxbuff[0]!=0x3C)goto t114;
 461   1          if(rxbuff[1]!=0x12)goto t114;
 462   1          if(rxbuff[2]!=0x00)goto t114;
 463   1      t114:   rxbuff[21]=0;
 464   1              for(i=0;i<19;i++)
 465   1                      rxbuff[21]^=rxbuff[i];
 466   1              if(rxbuff[19]==rxbuff[21])
 467   1                if(rxbuff[1]==0x12)   //成功取卡中的值
 468   1                {     CardStatus=0x01;
 469   2                      for(i=0;i<16;i++)
 470   2                              *CardDataBuf_++=rxbuff[3+i];    //取卡数据
 471   2                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -