www.pudn.com > RC500.rar > main.LST
C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 1 C51 COMPILER V7.07, COMPILATION OF MODULE MAIN OBJECT MODULE PLACED IN main.OBJ COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE main.c BROWSE DEBUG OBJECTEXTEND stmt level source 1 /**************************************************************************** 2 * File : main.c * 3 * COPYRIGHT BY HUOYAN LTD.COMPANY * 4 * Version: V5.2 * 5 * Created: 18.11.2002 * 6 * Last Change: 8.Jan.2005 * 7 * Author: NIL * 8 * Compiler: KEIL C51 V7.10 * 9 * Description: AT89S52-Firmware for MFRC500 Demo Serial Reader * 10 * * 11 ****************************************************************************/ 12 #define __SRC 13 #include "main.h" 14 #undef __SRC 15 16 #include17 #include 18 #include 19 #include "intrins.h" 20 #include "at89c5131.h" 21 #include "Mfreg500.h" 22 #include "M500a.h" 23 24 25 #define uchar unsigned char 26 #define uint unsigned int 27 //#define DataPort P0 // 数据端口 28 29 30 //pin define mcu控制管脚定义 31 32 sbit RC500RST = P1^0; //由高变低时复位RC500 33 sbit RC500CS=P1^1; 34 sbit CARD_LED=P1^2; 35 sbit SPEAKER = P1^3; 36 37 bit bcard; 38 39 /////////////////////////////////////////////////////////////////////// 40 // 主函数 41 /////////////////////////////////////////////////////////////////////// 42 void main(void) 43 { //设置变量 44 1 uchar baud; 45 1 46 1 47 1 InitSystem(); //初始化系统 48 1 // bcard=0; 49 1 50 1 delay_10ms(5); 51 1 while (1) 52 1 { 53 2 54 2 // KeyPress(); 处理按键 55 2 delay_10ms(5); C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 2 56 2 //检查串口命令标志 57 2 if (bCmd) 58 2 { 59 3 60 3 bCmd = FALSE; //清零命令标志以便接收下一个命令 61 3 if(RevBuffer[0]==0x10) //测试蜂鸣器 62 3 { 63 4 RevBuffer[2]=RevBuffer[1]; //蜂鸣时间存到RevBuffer[2] 64 4 cmd_ok(); //发送测试结果01 00给上位机 65 4 TI=1; //准备发给上位机数据 66 4 SPEAKER=1; //开蜂鸣器和指示灯 67 4 CARD_LED=ON; 68 4 delay_10ms(RevBuffer[2]); //延时 69 4 SPEAKER=0; 70 4 CARD_LED=OFF; 71 4 72 4 } 73 3 else if(RevBuffer[0]==0x11) //串口参数设置,设置通讯波特率 74 3 { 75 4 switch(RevBuffer[1]) 76 4 { 77 5 case 0: 78 5 baud=BAUD_9600; 79 5 break; 80 5 case 1: 81 5 baud=BAUD_14400; 82 5 break; 83 5 case 2: 84 5 baud=BAUD_19200; 85 5 break; 86 5 case 3: 87 5 baud=BAUD_28800; 88 5 break; 89 5 case 4: 90 5 baud=BAUD_38400; 91 5 break; 92 5 case 5: 93 5 baud=BAUD_57600; 94 5 break; 95 5 case 6: 96 5 baud=BAUD_115200; 97 5 break; 98 5 default: 99 5 baud=BAUD_19200; 100 5 break; 101 5 } 102 4 cmd_ok(); //发送测试结果01 00给上位机 103 4 TI=1; 104 4 delay_10ms(5); //延时设置波特率 105 4 TR1 = 0; 106 4 TH1 = baud; 107 4 TL1 = TH1; 108 4 delay_10ms(2); 109 4 TR1 = TRUE; 110 4 } 111 3 112 3 else 113 3 { 114 4 uart_process(); // 进入串口处理程序 115 4 TI=1; //处理完准备发送结果给上位机 116 4 } 117 3 C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 3 118 3 119 3 120 3 } 121 2 122 2 } 123 1 } 124 125 /////////////////////////////////////////////////////////////////////// 126 // 系统初始化 127 /////////////////////////////////////////////////////////////////////// 128 void InitSystem(void) 129 { 130 1 RC500CS=0; //选中RC500 131 1 RC500RST=0; //复位 132 1 133 1 ET2 = FALSE; // T/C2关中断 134 1 T2CON = 0x04; //TR2=1,设为TIMER2,auto reload 135 1 PCON = 0x80; 136 1 SCON = 0x70; // SMOD = 1; 137 1 TMOD = 0x21; //TMOD = 0x22; 138 1 139 1 TH1 = BAUD_19200; //默认波特率 140 1 TL1 = TH1; 141 1 TR1 = TRUE; // 以T1作为波特率发生器 142 1 143 1 ET1=FALSE; 144 1 IT0 =TRUE; // Config ext0 as edge trigger for RC500 145 1 EX0 =TRUE; // Enable ext0 interrupt for RC500 146 1 EA = TRUE; // Enable all interrupts 147 1 TR2=FALSE; //Close T2 148 1 IP=0x10; // 设串口中断高优先级 149 1 ES = TRUE; //打开串口 150 1 151 1 bCmd=FALSE; //初始化为0,没有收到命令 152 1 153 1 beep(1); //开机喇叭和指示灯测试 154 1 splash(1); 155 1 MfConfig(); //配置RC500 156 1 157 1 158 1 } 159 160 /////////////////////////////////////////////////////////////////////// 161 // 串口接收和发送中断 162 //数据包格式:数据包长度L(1byte)+命令字C(1byte)+数据包D(L-1 bytes) 163 //其中数据包长度是从命令字开始算起的。 164 /////////////////////////////////////////////////////////////////////// 165 void Uart_Int(void) interrupt 4 using 1 166 { 167 1 uchar len, i; 168 1 uint j=0; 169 1 170 1 if(RI) //收到数据 171 1 { 172 2 len=SBUF; //第一个字节是数据包的长度 173 2 RI=0; //清零RI以便接收下一个 174 2 for(i=0;i 1000) C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 4 180 4 { 181 5 break; 182 5 } 183 4 } 184 3 if(j<1000) 185 3 { 186 4 RevBuffer[i]=SBUF; 187 4 RI=0; 188 4 j=0; 189 4 } 190 3 else 191 3 { 192 4 break; 193 4 } 194 3 } 195 2 if(i==len) 196 2 { 197 3 REN=0; // 清零接收位 198 3 bCmd=TRUE; //接收完毕,收到命令有效,置位1 199 3 } 200 2 } 201 1 else if(!RI && TI) //发送-RI=0,TI=1 202 1 { 203 2 TI=0; 204 2 len=RevBuffer[0]; //发送的第一个字节是包的长度 205 2 for(i=0;i irqSource = 0x00; 448 1 switch(cmd) 449 1 { 450 2 case PCD_IDLE: 451 2 irqEn = 0x00; 452 2 waitFor = 0x00; 453 2 break; 454 2 case PCD_WRITEE2: 455 2 irqEn = 0x11; 456 2 waitFor = 0x10; 457 2 break; 458 2 case PCD_READE2: 459 2 irqEn = 0x07; 460 2 waitFor = 0x04; 461 2 break; 462 2 case PCD_LOADCONFIG: 463 2 case PCD_LOADKEYE2: 464 2 case PCD_AUTHENT1: 465 2 irqEn = 0x05; 466 2 waitFor = 0x04; 467 2 break; 468 2 case PCD_CALCCRC: 469 2 irqEn = 0x11; 470 2 waitFor = 0x10; 471 2 break; 472 2 case PCD_AUTHENT2: 473 2 irqEn = 0x04; 474 2 waitFor = 0x04; 475 2 break; 476 2 case PCD_RECEIVE: 477 2 info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4); 478 2 irqEn = 0x06; 479 2 waitFor = 0x04; 480 2 break; 481 2 case PCD_LOADKEY: 482 2 irqEn = 0x05; 483 2 waitFor = 0x04; 484 2 break; 485 2 case PCD_TRANSMIT: 486 2 irqEn = 0x05; 487 2 waitFor = 0x04; 488 2 break; 489 2 case PCD_TRANSCEIVE: C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 9 490 2 info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4); 491 2 irqEn = 0x3D; 492 2 waitFor = 0x04; 493 2 break; 494 2 default: 495 2 status = MI_UNKNOWN_COMMAND; 496 2 } 497 1 if (status == MI_OK) 498 1 { 499 2 irqEn |= 0x20; 500 2 waitFor |= 0x20; 501 2 timecnt=1000; 502 2 WriteIO(RegInterruptEn,irqEn | 0x80); 503 2 WriteIO(RegCommand,cmd); 504 2 while (!(MpIsrInfo->irqSource & waitFor||!(timecnt--))); 505 2 WriteIO(RegInterruptEn,0x7F); 506 2 WriteIO(RegInterruptRq,0x7F); 507 2 SetBitMask(RegControl,0x04); 508 2 WriteIO(RegCommand,PCD_IDLE); 509 2 if (!(MpIsrInfo->irqSource & waitFor)) 510 2 { 511 3 status = MI_ACCESSTIMEOUT; 512 3 } 513 2 else 514 2 { 515 3 status = MpIsrInfo->status; 516 3 } 517 2 if (status == MI_OK) 518 2 { 519 3 if (tmpStatus = (ReadIO(RegErrorFlag) & 0x17)) 520 3 { 521 4 if (tmpStatus & 0x01) 522 4 { 523 5 info->collPos = ReadIO(RegCollPos); 524 5 status = MI_COLLERR; 525 5 } 526 4 else 527 4 { 528 5 info->collPos = 0; 529 5 if (tmpStatus & 0x02) 530 5 { 531 6 status = MI_PARITYERR; 532 6 } 533 5 } 534 4 if (tmpStatus & 0x04) 535 4 { 536 5 status = MI_FRAMINGERR; 537 5 } 538 4 if (tmpStatus & 0x10) 539 4 { 540 5 FlushFIFO(); 541 5 status = MI_OVFLERR; 542 5 } 543 4 if (tmpStatus & 0x08) 544 4 { 545 5 status = MI_CRCERR; 546 5 } 547 4 if (status == MI_OK) 548 4 status = MI_NY_IMPLEMENTED; 549 4 } 550 3 if (cmd == PCD_TRANSCEIVE) 551 3 { C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 10 552 4 lastBits = ReadIO(RegSecondaryStatus) & 0x07; 553 4 if (lastBits) 554 4 info->nBitsReceived += (info->nBytesReceived-1) * 8 + lastBits; 555 4 else 556 4 info->nBitsReceived += info->nBytesReceived * 8; 557 4 } 558 3 } 559 2 else 560 2 { 561 3 info->collPos = 0x00; 562 3 } 563 2 } 564 1 MpIsrInfo = 0; 565 1 MpIsrOut = 0; 566 1 return status; 567 1 } 568 569 /////////////////////////////////////////////////////////////////////// 570 // 置一个bit 571 /////////////////////////////////////////////////////////////////////// 572 char SetBitMask(uchar reg,uchar mask) 573 { 574 1 char idata tmp = 0x00; 575 1 576 1 tmp = ReadIO(reg); 577 1 WriteIO(reg,tmp | mask); // set bit mask 578 1 return 0x00; 579 1 } 580 581 /////////////////////////////////////////////////////////////////////// 582 // 清一个bit 583 /////////////////////////////////////////////////////////////////////// 584 char ClearBitMask(uchar reg,uchar mask) 585 { 586 1 char idata tmp = 0x00; 587 1 588 1 tmp = ReadIO(reg); 589 1 WriteIO(reg,tmp & ~mask); // clear bit mask 590 1 return 0x00; 591 1 } 592 593 /////////////////////////////////////////////////////////////////////// 594 //清除FIFO 595 /////////////////////////////////////////////////////////////////////// 596 void FlushFIFO(void) 597 { 598 1 SetBitMask(RegControl,0x01); 599 1 } 600 601 /////////////////////////////////////////////////////////////////////// 602 // Value format operations for Mifare Standard card ICs 603 // 块值操作:加.减 604 /////////////////////////////////////////////////////////////////////// 605 char M500PiccValue(uchar dd_mode, 606 uchar addr, 607 uchar *value, 608 uchar trans_addr) 609 { 610 1 char status = MI_OK; 611 1 612 1 M500PcdSetTmo(1); 613 1 ResetInfo(MInfo); C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 11 614 1 SerBuffer[0] = dd_mode; 615 1 SerBuffer[1] = addr; 616 1 MInfo.nBytesToSend = 2; 617 1 status = M500PcdCmd(PCD_TRANSCEIVE, 618 1 SerBuffer, 619 1 &MInfo); 620 1 621 1 if (status != MI_NOTAGERR) 622 1 { 623 2 if (MInfo.nBitsReceived != 4) 624 2 { 625 3 status = MI_BITCOUNTERR; 626 3 } 627 2 else 628 2 { 629 3 SerBuffer[0] &= 0x0f; 630 3 switch(SerBuffer[0]) 631 3 { 632 4 case 0x00: 633 4 status = MI_NOTAUTHERR; 634 4 break; 635 4 case 0x0a: 636 4 status = MI_OK; 637 4 break; 638 4 case 0x01: 639 4 status = MI_VALERR; 640 4 break; 641 4 default: 642 4 status = MI_CODEERR; 643 4 break; 644 4 } 645 3 } 646 2 } 647 1 648 1 if ( status == MI_OK) 649 1 { 650 2 M500PcdSetTmo(3); 651 2 ResetInfo(MInfo); 652 2 memcpy(SerBuffer,value,4); 653 2 MInfo.nBytesToSend = 4; 654 2 status = M500PcdCmd(PCD_TRANSCEIVE, 655 2 SerBuffer, 656 2 &MInfo); 657 2 658 2 if (status == MI_OK) 659 2 { 660 3 if (MInfo.nBitsReceived != 4) 661 3 { 662 4 status = MI_BITCOUNTERR; 663 4 } 664 3 else 665 3 { 666 4 SerBuffer[0] &= 0x0f; 667 4 switch(SerBuffer[0]) 668 4 { 669 5 case 0x00: 670 5 status = MI_NOTAUTHERR; 671 5 break; 672 5 case 0x01: 673 5 status = MI_VALERR; 674 5 break; 675 5 default: C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 12 676 5 status = MI_CODEERR; 677 5 break; 678 5 } 679 4 } 680 3 } 681 2 else 682 2 { 683 3 if (status == MI_NOTAGERR ) 684 3 status = MI_OK; 685 3 } 686 2 } 687 1 if (status == MI_OK) 688 1 { 689 2 ResetInfo(MInfo); 690 2 SerBuffer[0] = PICC_TRANSFER; 691 2 SerBuffer[1] = trans_addr; 692 2 MInfo.nBytesToSend = 2; 693 2 status = M500PcdCmd(PCD_TRANSCEIVE, 694 2 SerBuffer, 695 2 &MInfo); 696 2 if (status != MI_NOTAGERR) 697 2 { 698 3 if (MInfo.nBitsReceived != 4) 699 3 { 700 4 status = MI_BITCOUNTERR; 701 4 } 702 3 else 703 3 { 704 4 SerBuffer[0] &= 0x0f; 705 4 switch(SerBuffer[0]) 706 4 { 707 5 case 0x00: 708 5 status = MI_NOTAUTHERR; 709 5 break; 710 5 case 0x0a: 711 5 status = MI_OK; 712 5 break; 713 5 case 0x01: 714 5 status = MI_VALERR; 715 5 break; 716 5 default: 717 5 status = MI_CODEERR; 718 5 break; 719 5 } 720 4 } 721 3 } 722 2 } 723 1 return status; 724 1 } 725 726 /////////////////////////////////////////////////////////////////////// 727 //HALT the card 728 // 终止卡的操作 729 /////////////////////////////////////////////////////////////////////// 730 char M500PiccHalt(void) 731 { 732 1 char idata status = MI_CODEERR; 733 1 734 1 // ************* Cmd Sequence ********************************** 735 1 ResetInfo(MInfo); 736 1 SerBuffer[0] = PICC_HALT ; // Halt command code 737 1 SerBuffer[1] = 0x00; // dummy address C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 13 738 1 MInfo.nBytesToSend = 2; 739 1 status = M500PcdCmd(PCD_TRANSCEIVE, 740 1 SerBuffer, 741 1 &MInfo); 742 1 if (status) 743 1 { 744 2 // timeout error ==> no NAK received ==> OK 745 2 if (status == MI_NOTAGERR || status == MI_ACCESSTIMEOUT) 746 2 { 747 3 status = MI_OK; 748 3 } 749 2 } 750 1 //reset command register - no response from tag 751 1 WriteIO(RegCommand,PCD_IDLE); 752 1 return status; 753 1 } 754 755 /////////////////////////////////////////////////////////////////////// 756 // Reset the MF RC500 757 /////////////////////////////////////////////////////////////////////// 758 char M500PcdReset(void) 759 { 760 1 char idata status = MI_OK; 761 1 unsigned int idata timecnt=0; 762 1 763 1 RC500RST = 0; 764 1 delay_1ms(25); 765 1 RC500RST = 1; 766 1 delay_50us(200); 767 1 RC500RST = 0; 768 1 delay_50us(50); 769 1 timecnt=1000; 770 1 while ((ReadIO(RegCommand) & 0x3F) && timecnt--); 771 1 if(!timecnt) 772 1 { 773 2 status = MI_RESETERR; 774 2 } 775 1 if (status == MI_OK) 776 1 { 777 2 //WriteIO(RegPage,0x80); 778 2 if (ReadIO(RegCommand) != 0x00) 779 2 { 780 3 status = MI_INTERFACEERR; 781 3 } 782 2 } 783 1 return status; 784 1 } 785 786 /////////////////////////////////////////////////////////////////////// 787 // Configures the MF RC500 配置RC500内部寄存器函数 788 /////////////////////////////////////////////////////////////////////// 789 char M500PcdConfig(void) 790 { 791 1 char idata status; 792 1 793 1 if ((status = M500PcdReset()) == MI_OK) 794 1 { 795 2 WriteIO(RegClockQControl,0x00); 796 2 WriteIO(RegClockQControl,0x40); 797 2 delay_50us(2); 798 2 ClearBitMask(RegClockQControl,0x40); 799 2 WriteIO(RegBitPhase,0xAD); C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 14 800 2 WriteIO(RegRxThreshold,0xFF); 801 2 WriteIO(RegRxControl2,0x01); 802 2 WriteIO(RegFIFOLevel,0x1A); 803 2 WriteIO(RegTimerControl,0x02); 804 2 WriteIO(RegIRqPinConfig,0x03); 805 2 M500PcdRfReset(1); 806 2 } 807 1 return status; 808 1 } 809 810 /////////////////////////////////////////////////////////////// 811 // Key loading into the MF RC500's EEPROM 812 /////////////////////////////////////////////////////////////// 813 char M500PcdLoadKeyE2(uchar key_type, 814 uchar sector, 815 uchar *uncoded_keys) 816 { 817 1 signed char status = MI_OK; 818 1 unsigned int e2addr = 0x80 + sector * 0x18; 819 1 uchar coded_keys[12]; 820 1 821 1 if (key_type == PICC_AUTHENT1B) 822 1 { 823 2 e2addr += 12; // key B offset 824 2 } 825 1 if ((status = M500HostCodeKey(uncoded_keys,coded_keys)) == MI_OK) 826 1 { 827 2 status = PcdWriteE2(e2addr,12,coded_keys); 828 2 } 829 1 return status; 830 1 } 831 832 /////////////////////////////////////////////////////////////////////// 833 // Write the MF RC500's EEPROM 834 // 向RC500 EEPROM写数据 835 /////////////////////////////////////////////////////////////////////// 836 char PcdWriteE2(unsigned int startaddr, 837 uchar length, 838 uchar* _data) 839 { 840 1 char status = MI_OK; 841 1 ResetInfo(MInfo); 842 1 SerBuffer[0] = startaddr & 0xFF; 843 1 SerBuffer[1] = (startaddr >> 8) & 0xFF; 844 1 memcpy(SerBuffer + 2,_data,length); 845 1 846 1 MInfo.nBytesToSend = length + 2; 847 1 848 1 status = M500PcdCmd(PCD_WRITEE2, 849 1 SerBuffer, 850 1 &MInfo); 851 1 return status; 852 1 } 853 854 /////////////////////////////////////////////////////////////////////// 855 // Select Command defined in ISO14443(MIFARE) 856 /////////////////////////////////////////////////////////////////////// 857 char M500PcdMfOutSelect(uchar type) 858 { 859 1 WriteIO(RegMfOutSelect,type&0x7); 860 1 return MI_OK; 861 1 } C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 15 862 863 /////////////////////////////////////////////////////////////////////// 864 // Request Command defined in ISO14443(MIFARE) 865 // Request 866 // 寻卡 867 /////////////////////////////////////////////////////////////////////// 868 char M500PiccCommonRequest(uchar req_code,uchar *atq) 869 { 870 1 char idata status = MI_OK; 871 1 872 1 M500PcdSetTmo(3); 873 1 WriteIO(RegChannelRedundancy,0x03); 874 1 ClearBitMask(RegControl,0x08); 875 1 WriteIO(RegBitFraming,0x07); 876 1 SetBitMask(RegTxControl,0x03); 877 1 ResetInfo(MInfo); 878 1 SerBuffer[0] = req_code; 879 1 MInfo.nBytesToSend = 1; 880 1 881 1 status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo); 882 1 if (status) 883 1 { 884 2 *atq = 0; 885 2 } 886 1 else 887 1 { 888 2 if (MInfo.nBitsReceived != 16) 889 2 { 890 3 *atq = 0; 891 3 status = MI_BITCOUNTERR; 892 3 } 893 2 else 894 2 { 895 3 status = MI_OK; 896 3 memcpy(atq,SerBuffer,2); 897 3 } 898 2 } 899 1 return status; 900 1 } 901 902 /////////////////////////////////////////////////////////////////// 903 // Cascaded Anti-Collision Command defined in ISO14443(MIFARE) 904 // 防冲突 读卡的系列号 CardSnr 905 /////////////////////////////////////////////////////////////////// 906 char M500PiccCascAnticoll (uchar bcnt,uchar *snr) 907 { 908 1 char idata status = MI_OK; 909 1 char idata snr_in[4]; 910 1 char idata nbytes = 0; 911 1 char idata nbits = 0; 912 1 char idata complete = 0; 913 1 char idata i = 0; 914 1 char idata byteOffset = 0; 915 1 uchar dummyShift1; 916 1 uchar dummyShift2; 917 1 918 1 M500PcdSetTmo(106); 919 1 memcpy(snr_in,snr,4); 920 1 921 1 WriteIO(RegDecoderControl,0x28); 922 1 ClearBitMask(RegControl,0x08); 923 1 complete = 0; C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 16 924 1 while (!complete && (status == MI_OK) ) 925 1 { 926 2 ResetInfo(MInfo); 927 2 WriteIO(RegChannelRedundancy,0x03); 928 2 nbits = bcnt % 8; 929 2 if(nbits) 930 2 { 931 3 WriteIO(RegBitFraming,nbits << 4 | nbits); 932 3 nbytes = bcnt / 8 + 1; 933 3 if (nbits == 7) 934 3 { 935 4 MInfo.cmd = PICC_ANTICOLL1; 936 4 WriteIO(RegBitFraming,nbits); 937 4 } 938 3 } 939 2 else 940 2 { 941 3 nbytes = bcnt / 8; 942 3 } 943 2 SerBuffer[0] = 0x93; 944 2 SerBuffer[1] = 0x20 + ((bcnt/8) << 4) + nbits; 945 2 946 2 for (i = 0; i < nbytes; i++) 947 2 { 948 3 SerBuffer[i + 2] = snr_in[i]; 949 3 } 950 2 MInfo.nBytesToSend = 2 + nbytes; 951 2 952 2 status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo); 953 2 if (nbits == 7) 954 2 { 955 3 dummyShift1 = 0x00; 956 3 for (i = 0; i < MInfo.nBytesReceived; i++) 957 3 { 958 4 dummyShift2 = SerBuffer[i]; 959 4 SerBuffer[i] = (dummyShift1 >> (i+1)) | (SerBuffer[i] << (7-i)); 960 4 dummyShift1 = dummyShift2; 961 4 } 962 3 MInfo.nBitsReceived -= MInfo.nBytesReceived; 963 3 if ( MInfo.collPos ) MInfo.collPos += 7 - (MInfo.collPos + 6) / 9; 964 3 } 965 2 if ( status == MI_OK || status == MI_COLLERR) 966 2 { 967 3 if ( MInfo.nBitsReceived != (40 - bcnt) ) 968 3 { 969 4 status = MI_BITCOUNTERR; 970 4 } 971 3 else 972 3 { 973 4 byteOffset = 0; 974 4 if( nbits != 0 ) 975 4 { 976 5 snr_in[nbytes - 1] = snr_in[nbytes - 1] | SerBuffer[0]; 977 5 byteOffset = 1; 978 5 } 979 4 980 4 for ( i =0; i < (4 - nbytes); i++) 981 4 { 982 5 snr_in[nbytes + i] = SerBuffer[i + byteOffset]; 983 5 } 984 4 985 4 if (status != MI_COLLERR ) C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 17 986 4 { 987 5 dummyShift2 = snr_in[0] ^ snr_in[1] ^ snr_in[2] ^ snr_in[3]; 988 5 dummyShift1 = SerBuffer[MInfo.nBytesReceived - 1]; 989 5 if (dummyShift2 != dummyShift1) 990 5 { 991 6 status = MI_SERNRERR; 992 6 } 993 5 else 994 5 { 995 6 complete = 1; 996 6 } 997 5 } 998 4 else 999 4 { 1000 5 bcnt = bcnt + MInfo.collPos - nbits; 1001 5 status = MI_OK; 1002 5 } 1003 4 } 1004 3 } 1005 2 } 1006 1 if (status == MI_OK) 1007 1 { 1008 2 memcpy(snr,snr_in,4); 1009 2 } 1010 1 else 1011 1 { 1012 2 memcpy(snr,"0000",4); 1013 2 } 1014 1 ClearBitMask(RegDecoderControl,0x20); 1015 1 1016 1 return status; 1017 1 } 1018 1019 ////////////////////////////////////////////////////////////////// 1020 // Cascaded Select command defined in ISO14443(MIFARE) 1021 // 选择卡 Select Card 1022 ////////////////////////////////////////////////////////////////// 1023 char M500PiccCascSelect(uchar *snr,uchar *sak) 1024 { 1025 1 char idata status = MI_OK; 1026 1 1027 1 M500PcdSetTmo(106); 1028 1 1029 1 WriteIO(RegChannelRedundancy,0x0F); 1030 1 ClearBitMask(RegControl,0x08); 1031 1 ResetInfo(MInfo); 1032 1 SerBuffer[0] = 0x93; 1033 1 SerBuffer[1] = 0x70; 1034 1 1035 1 memcpy(SerBuffer + 2,snr,4); 1036 1 SerBuffer[6] = SerBuffer[2] ^ SerBuffer[3] ^ SerBuffer[4] ^ SerBuffer[5]; 1037 1 MInfo.nBytesToSend = 7; 1038 1 status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo); 1039 1 1040 1 *sak = 0; 1041 1 if (status == MI_OK) 1042 1 { 1043 2 if (MInfo.nBitsReceived != 8) 1044 2 { 1045 3 status = MI_BITCOUNTERR; 1046 3 } 1047 2 else C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 18 1048 2 { 1049 3 *sak = SerBuffer[0]; 1050 3 } 1051 2 } 1052 1 return status; 1053 1 } 1054 1055 /////////////////////////////////////////////////////////////////////// 1056 // Key loading into the MF RC500's EEPROM 1057 // 校验卡密码(E2) 1058 /////////////////////////////////////////////////////////////////////// 1059 char M500PiccAuthE2( uchar auth_mode,uchar *snr, 1060 uchar keynr,uchar block) 1061 1062 { 1063 1 char idata status = MI_OK; 1064 1 unsigned int e2addr = 0x80 + keynr * 0x18; 1065 1 uchar *e2addrbuf; 1066 1 1067 1 e2addrbuf = (uchar *)&e2addr; 1068 1 if (auth_mode == PICC_AUTHENT1B) 1069 1 { 1070 2 e2addr += 12; 1071 2 } 1072 1 FlushFIFO(); 1073 1 ResetInfo(MInfo); 1074 1 1075 1 memcpy(SerBuffer,e2addrbuf,2); 1076 1 SerBuffer[2] = SerBuffer[0]; 1077 1 SerBuffer[0] = SerBuffer[1]; 1078 1 SerBuffer[1] = SerBuffer[2]; 1079 1 MInfo.nBytesToSend = 2; 1080 1 if ((status=M500PcdCmd(PCD_LOADKEYE2,SerBuffer,&MInfo)) == MI_OK) 1081 1 { 1082 2 status = M500PiccAuthState(auth_mode,snr,block); 1083 2 } 1084 1 return status; 1085 1 } 1086 1087 /////////////////////////////////////////////////////////////////////// 1088 // Authentication key coding 1089 /////////////////////////////////////////////////////////////////////// 1090 char M500HostCodeKey( uchar *uncoded, uchar *coded) 1091 { 1092 1 char idata status = MI_OK; 1093 1 uchar idata cnt = 0; 1094 1 uchar idata ln = 0; 1095 1 uchar idata hn = 0; 1096 1 for (cnt = 0; cnt < 6; cnt++) 1097 1 { 1098 2 ln = uncoded[cnt] & 0x0F; 1099 2 hn = uncoded[cnt] >> 4; 1100 2 coded[cnt * 2 + 1] = (~ln << 4) | ln; 1101 2 coded[cnt * 2 ] = (~hn << 4) | hn; 1102 2 } 1103 1 return MI_OK; 1104 1 } 1105 1106 /////////////////////////////////////////////////////////////////////// 1107 //校验状态 1108 /////////////////////////////////////////////////////////////////////// 1109 char M500PiccAuthState( uchar auth_mode,uchar *snr,uchar block) C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 19 1110 { 1111 1 char idata status = MI_OK; 1112 1 uchar idata i = 0; 1113 1 1114 1 status = ReadIO(RegErrorFlag); 1115 1 if (status != MI_OK) 1116 1 { 1117 2 if (status & 0x40) 1118 2 { 1119 3 status = MI_KEYERR; 1120 3 } 1121 2 else 1122 2 { 1123 3 status = MI_AUTHERR; 1124 3 } 1125 2 } 1126 1 else 1127 1 { 1128 2 SerBuffer[0] = auth_mode; 1129 2 1130 2 SerBuffer[1] = block; 1131 2 memcpy(SerBuffer + 2,snr,4); 1132 2 ResetInfo(MInfo); 1133 2 MInfo.nBytesToSend = 6; 1134 2 if ((status = M500PcdCmd(PCD_AUTHENT1,SerBuffer,&MInfo)) == MI_OK) 1135 2 { 1136 3 if (ReadIO(RegSecondaryStatus) & 0x07) 1137 3 { 1138 4 status = MI_BITCOUNTERR; 1139 4 } 1140 3 else 1141 3 { 1142 4 ResetInfo(MInfo); 1143 4 MInfo.nBytesToSend = 0; 1144 4 if ((status = M500PcdCmd(PCD_AUTHENT2, 1145 4 SerBuffer, 1146 4 &MInfo)) == MI_OK) 1147 4 { 1148 5 if ( ReadIO(RegControl) & 0x08 ) 1149 5 { 1150 6 status = MI_OK; 1151 6 } 1152 5 else 1153 5 { 1154 6 status = MI_AUTHERR; 1155 6 } 1156 5 } 1157 4 } 1158 3 } 1159 2 } 1160 1 return status; 1161 1 } 1162 1163 //////////////////////////////////////////////////////////////// 1164 // Read the mifare card 1165 // 读卡 1166 //////////////////////////////////////////////////////////////// 1167 char M500PiccRead(uchar addr,uchar *_data) 1168 { 1169 1 char idata status = MI_OK; 1170 1 char idata tmp = 0; 1171 1 C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 20 1172 1 FlushFIFO(); 1173 1 1174 1 M500PcdSetTmo(3); 1175 1 WriteIO(RegChannelRedundancy,0x0F); 1176 1 ResetInfo(MInfo); 1177 1 SerBuffer[0] = PICC_READ; 1178 1 SerBuffer[1] = addr; 1179 1 MInfo.nBytesToSend = 2; 1180 1 status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo); 1181 1 1182 1 if (status != MI_OK) 1183 1 { 1184 2 if (status != MI_NOTAGERR ) 1185 2 { 1186 3 if (MInfo.nBitsReceived == 4) 1187 3 { 1188 4 SerBuffer[0] &= 0x0f; 1189 4 if ((SerBuffer[0] & 0x0a) == 0) 1190 4 { 1191 5 status = MI_NOTAUTHERR; 1192 5 } 1193 4 else 1194 4 { 1195 5 status = MI_CODEERR; 1196 5 } 1197 4 } 1198 3 } 1199 2 memcpy(_data,"0000000000000000",16); 1200 2 } 1201 1 else // Response Processing 1202 1 { 1203 2 if (MInfo.nBytesReceived != 16) 1204 2 { 1205 3 status = MI_BYTECOUNTERR; 1206 3 memcpy(_data,"0000000000000000",16); 1207 3 } 1208 2 else 1209 2 { 1210 3 memcpy(_data,SerBuffer,16); 1211 3 } 1212 2 } 1213 1 M500PcdSetTmo(1); 1214 1 return status; 1215 1 } 1216 1217 //////////////////////////////////////////////////////////////// 1218 // Write the mifare card 1219 // 写卡 下载密码 1220 //////////////////////////////////////////////////////////////// 1221 char M500PiccWrite( uchar addr,uchar *_data) 1222 { 1223 1 char idata status = MI_OK; 1224 1 1225 1 ResetInfo(MInfo); 1226 1 SerBuffer[0] = PICC_WRITE; 1227 1 SerBuffer[1] = addr; 1228 1 MInfo.nBytesToSend = 2; 1229 1 status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo); 1230 1 1231 1 if (status != MI_NOTAGERR) 1232 1 { 1233 2 if (MInfo.nBitsReceived != 4) C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 21 1234 2 { 1235 3 status = MI_BITCOUNTERR; 1236 3 } 1237 2 else 1238 2 { 1239 3 SerBuffer[0] &= 0x0f; 1240 3 if ((SerBuffer[0] & 0x0a) == 0) 1241 3 { 1242 4 status = MI_NOTAUTHERR; 1243 4 } 1244 3 else 1245 3 { 1246 4 if (SerBuffer[0] == 0x0a) 1247 4 { 1248 5 status = MI_OK; 1249 5 } 1250 4 else 1251 4 { 1252 5 status = MI_CODEERR; 1253 5 } 1254 4 } 1255 3 } 1256 2 } 1257 1 1258 1 if ( status == MI_OK) 1259 1 { 1260 2 M500PcdSetTmo(3); 1261 2 1262 2 ResetInfo(MInfo); 1263 2 memcpy(SerBuffer,_data,16); 1264 2 MInfo.nBytesToSend = 16; 1265 2 status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo); 1266 2 1267 2 if (status & 0x80) 1268 2 { 1269 3 status = MI_NOTAGERR; 1270 3 } 1271 2 else 1272 2 { 1273 3 if (MInfo.nBitsReceived != 4) 1274 3 { 1275 4 status = MI_BITCOUNTERR; 1276 4 } 1277 3 else 1278 3 { 1279 4 SerBuffer[0] &= 0x0f; 1280 4 if ((SerBuffer[0] & 0x0a) == 0) 1281 4 { 1282 5 status = MI_WRITEERR; 1283 5 } 1284 4 else 1285 4 { 1286 5 if (SerBuffer[0] == 0x0a) 1287 5 { 1288 6 status = MI_OK; 1289 6 } 1290 5 else 1291 5 { 1292 6 status = MI_CODEERR; 1293 6 } 1294 5 } 1295 4 } C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 22 1296 3 } 1297 2 M500PcdSetTmo(1); 1298 2 } 1299 1 return status; 1300 1 } 1301 1302 /////////////////////////////////////////////////////////////////////// 1303 // Reset Rf Card 1304 /////////////////////////////////////////////////////////////////////// 1305 char M500PcdRfReset(uchar ms) 1306 { 1307 1 char idata status = MI_OK; 1308 1 1309 1 if(ms) 1310 1 { 1311 2 ClearBitMask(RegTxControl,0x03); 1312 2 delay_1ms(2); 1313 2 SetBitMask(RegTxControl,0x03); 1314 2 } 1315 1 else 1316 1 { 1317 2 ClearBitMask(RegTxControl,0x03); 1318 2 } 1319 1 return status; 1320 1 } 1321 1322 #pragma noaregs 1323 1324 /////////////////////////////////////////////////////////////////////// 1325 // Delay 50us 1326 /////////////////////////////////////////////////////////////////////// 1327 void delay_50us(uchar _50us) 1328 { 1329 1 RCAP2LH = RCAP2_50us; 1330 1 T2LH = RCAP2_50us; 1331 1 ET2 = 0; // Disable timer2 interrupt 1332 1 T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer 1333 1 1334 1 while (_50us--) 1335 1 { 1336 2 while (!TF2); 1337 2 TF2 = FALSE; 1338 2 } 1339 1 1340 1 TR2 = FALSE; 1341 1 1342 1 } 1343 1344 /////////////////////////////////////////////////////////////////////// 1345 // Delay 1ms 1346 /////////////////////////////////////////////////////////////////////// 1347 void delay_1ms(uchar _1ms) 1348 { 1349 1 RCAP2LH = RCAP2_1ms; 1350 1 T2LH = RCAP2_1ms; 1351 1 ET2 = 0; // Disable timer2 interrupt 1352 1 T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer 1353 1 1354 1 while (_1ms--) 1355 1 { 1356 2 while (!TF2); 1357 2 TF2 = FALSE; C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 23 1358 2 } 1359 1 TR2 = FALSE; 1360 1 } 1361 1362 /////////////////////////////////////////////////////////////////////// 1363 // Delay 10ms 1364 /////////////////////////////////////////////////////////////////////// 1365 void delay_10ms(uint _10ms) 1366 { 1367 1 RCAP2LH = RCAP2_10ms; 1368 1 T2LH = RCAP2_10ms; 1369 1 ET2 = 0; // Disable timer2 interrupt 1370 1 T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer 1371 1 1372 1 while (_10ms--) 1373 1 { 1374 2 while (!TF2) 1375 2 { 1376 3 if (bCmd) 1377 3 { 1378 4 TR2 = FALSE; 1379 4 TF2 = FALSE; 1380 4 return; 1381 4 } 1382 3 } 1383 2 TF2 = FALSE; 1384 2 } 1385 1 TR2 = FALSE; 1386 1 1387 1 } 1388 1389 /////////////////////////////////////////////////////////////////////// 1390 /////////////////////////////////////////////////////////////////////// 1391 void RC500ISR (void) interrupt 0 using 1 1392 { 1393 1 static uchar idata irqBits; 1394 1 static uchar idata irqMask; 1395 1 static uchar idata nbytes; 1396 1 static uchar idata cnt; 1397 1 1398 1 IE0 = 0; 1399 1 WriteRawIO(0,0x80); 1400 1 if (MpIsrInfo && MpIsrOut) 1401 1 { 1402 2 while( ReadRawIO(RegPrimaryStatus) & 0x08) 1403 2 { 1404 3 irqMask = ReadRawIO(RegInterruptEn); 1405 3 irqBits = ReadRawIO(RegInterruptRq) & irqMask; 1406 3 MpIsrInfo->irqSource |= irqBits; 1407 3 if (irqBits & 0x01) 1408 3 { 1409 4 nbytes = 64 - ReadRawIO(RegFIFOLength); 1410 4 if ((MpIsrInfo->nBytesToSend - MpIsrInfo->nBytesSent) <= nbytes) 1411 4 { 1412 5 nbytes = MpIsrInfo->nBytesToSend - MpIsrInfo->nBytesSent; 1413 5 WriteRawIO(RegInterruptEn,0x01); 1414 5 } 1415 4 for ( cnt = 0;cnt < nbytes;cnt++) 1416 4 { 1417 5 WriteRawIO(RegFIFOData,MpIsrOut[MpIsrInfo->nBytesSent]); 1418 5 MpIsrInfo->nBytesSent++; 1419 5 } C51 COMPILER V7.07 MAIN 05/28/2008 17:54:13 PAGE 24 1420 4 WriteRawIO(RegInterruptRq,0x01); 1421 4 } 1422 3 if (irqBits & 0x10) 1423 3 { 1424 4 WriteRawIO(RegInterruptRq,0x10); 1425 4 WriteRawIO(RegInterruptEn,0x82); 1426 4 if (MpIsrInfo->cmd == PICC_ANTICOLL1) 1427 4 { 1428 5 WriteIO(RegChannelRedundancy,0x02); 1429 5 WriteRawIO(0,0x80); 1430 5 } 1431 4 } 1432 3 if (irqBits & 0x0E) 1433 3 { 1434 4 nbytes = ReadRawIO(RegFIFOLength); 1435 4 for ( cnt = 0; cnt < nbytes; cnt++) 1436 4 { 1437 5 MpIsrOut[MpIsrInfo->nBytesReceived] = ReadRawIO(RegFIFOData); 1438 5 MpIsrInfo->nBytesReceived++; 1439 5 } 1440 4 WriteRawIO(RegInterruptRq,0x0A & irqBits); 1441 4 } 1442 3 if (irqBits & 0x04) 1443 3 { 1444 4 WriteRawIO(RegInterruptEn,0x20); 1445 4 WriteRawIO(RegInterruptRq,0x20); 1446 4 irqBits &= ~0x20; 1447 4 MpIsrInfo->irqSource &= ~0x20; 1448 4 WriteRawIO(RegInterruptRq,0x04); 1449 4 } 1450 3 if (irqBits & 0x20) 1451 3 { 1452 4 WriteRawIO(RegInterruptRq,0x20); 1453 4 MpIsrInfo->status = MI_NOTAGERR; 1454 4 } 1455 3 } 1456 2 } 1457 1 } 1458 1459 1460 1461 /////////////////////////////////////////////////////////////////////// 1462 // MF RC500 Config 1463 /////////////////////////////////////////////////////////////////////// 1464 char MfConfig(void) 1465 { 1466 1 char status=MI_OK; 1467 1 1468 1 M500PcdConfig(); 1469 1 M500PcdMfOutSelect(2); 1470 1 return status; 1471 1 } 1472 ///////////////////////////////////////////////////////////////////////// 1473 //Beep(n);n为鸣叫的声音次数 1474 ///////////////////////////////////////////////////////////////////////// 1475 void beep(uchar n) 1476 { 1477 1 uchar i; 1478 1 for(i=0;i