PcdComTransceive.c
资源名称:RC500.rar [点击查看]
上传用户:hbtfgg
上传日期:2021-12-10
资源大小:16k
文件大小:3k
源码类别:
RFID编程
开发平台:
C/C++
- #include<reg52.h>
- #include<rc500.h>
- idata struct TranSciveBuffer{uchar MfCommand;
- uchar MfLength;
- uchar MfData[19];
- };
- char PcdComTransceive(struct TranSciveBuffer *pi)
- {
- bit recebyte=0;
- char status;
- uchar irqEn=0x00;
- uchar waitFor=0x00;
- uchar lastBits;
- uchar n;
- uint i;
- switch(pi->MfCommand)
- {
- case PCD_IDLE:
- irqEn = 0x00;
- waitFor = 0x00;
- break;
- case PCD_WRITEE2:
- irqEn = 0x11;
- waitFor = 0x10;
- break;
- case PCD_READE2:
- irqEn = 0x07;
- waitFor = 0x04;
- recebyte=1;
- break;
- case PCD_LOADCONFIG:
- case PCD_LOADKEYE2:
- case PCD_AUTHENT1:
- irqEn = 0x05;
- waitFor = 0x04;
- break;
- case PCD_CALCCRC:
- irqEn = 0x11;
- waitFor = 0x10;
- break;
- case PCD_AUTHENT2:
- irqEn = 0x04;
- waitFor = 0x04;
- break;
- case PCD_RECEIVE:
- irqEn = 0x06;
- waitFor = 0x04;
- recebyte=1;
- break;
- case PCD_LOADKEY:
- irqEn = 0x05;
- waitFor = 0x04;
- break;
- case PCD_TRANSMIT:
- irqEn = 0x05;
- waitFor = 0x04;
- break;
- case PCD_TRANSCEIVE:
- irqEn = 0x3D;
- waitFor = 0x04;
- recebyte=1;
- break;
- default:
- pi->MfCommand=MI_UNKNOWN_COMMAND;
- break;
- }
- if(pi->MfCommand!=MI_UNKNOWN_COMMAND)
- {
- WriteRC(RegPage,0x00);
- WriteRC(RegInterruptEn,0x7F);
- WriteRC(RegInterruptRq,0x7F);
- WriteRC(RegCommand,PCD_IDLE);
- SetBitMask(RegControl,0x01);
- WriteRC(RegInterruptEn,irqEn|0x80);
- for(i=0;i<pi->MfLength;i++)
- {
- WriteRC(RegFIFOData,pi->MfData[i]);
- }
- WriteRC(RegCommand,pi->MfCommand);
- i=0x2000;
- do
- {
- n=ReadRC(RegInterruptRq);
- i--;
- }
- while((i!=0)&&!(n&irqEn&0x20)&&!(n&waitFor));
- status=MI_COM_ERR;
- if((i!=0)&&!(n&irqEn&0x20))
- {
- if(!(ReadRC(RegErrorFlag)&0x17))
- {
- status=MI_OK;
- if(recebyte)
- {
- n=ReadRC(RegFIFOLength);
- lastBits=ReadRC(RegSecondaryStatus)&0x07;
- if(lastBits)
- {
- pi->MfLength=(n-1)*8+lastBits;
- }
- else
- {
- pi->MfLength=n*8;
- }
- if(n==0)
- {
- n=1;
- }
- for(i=0;i<n;i++)
- {
- pi->MfData[i]=ReadRC(RegFIFOData);
- }
- }
- }
- }
- else if(n&irqEn&0x20)
- {
- status=MI_NOTAGERR;
- }
- else
- {
- status=MI_COM_ERR;
- }
- WriteRC(RegInterruptEn,0x7F);
- WriteRC(RegInterruptRq,0x7F);
- }
- return status;
- }