📄 main.lst
字号:
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 V6.23a MAIN 08/14/2006 14:40:24 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 V6.23a MAIN 08/14/2006 14:40:24 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 V6.23a MAIN 08/14/2006 14:40:24 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 V6.23a MAIN 08/14/2006 14:40:24 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -