本帖最后由 new_year 于 2013-10-14 21:05 编辑
MFRC531和ISO14443卡通讯
MFRC531和ISO14443卡通讯
// input: pi->MfCommand = RC531命令字
// pi->MfLength = 发送的数据长度
// pi->MfData[] = 发送数据
// output:status = 错误字
// pi->MfLength = 接收的数据BIT长度
// pi->MfData[] = 接收数据
signed char MFRC531_ISO14443_Transceive(struct TransceiveBuffer *pi)
{
unsigned char recebyte = 0;
signed char status;
unsigned char irqEn = 0x00;
unsigned char waitFor = 0x00;
unsigned char lastBits;
unsigned char n;
unsigned int 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:
waitFor = 0x04;
break;
irqEn = 0x04;
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: // this one
irqEn = 0x3D;
waitFor = 0x04;
recebyte=1;
break;
default:
pi->MfCommand = MI_UNKNOWN_COMMAND;
break;
}
if (pi->MfCommand != MI_UNKNOWN_COMMAND)
{
MFRC531_WriteReg(RegPage1,0x00);
MFRC531_WriteReg(RegInterruptEn,0x7F);
MFRC531_WriteReg(RegInterruptRq,0x7F);
MFRC531_WriteReg(RegCommand,PCD_IDLE);
MFRC531_SetBitMask(RegControl,0x01); // Clear FIFO
MFRC531_WriteReg(RegInterruptEn,irqEn|0x80);
for (i=0; i<pi->MfLength; i++)
{
MFRC531_WriteReg(RegFIFOData, pi->MfData); //将数据写入fifo
}
MFRC531_WriteReg(RegCommand, pi->MfCommand); //将命令写入command
i = 0x3500;
do
{
n = MFRC531_ReadReg(RegInterruptRq);
i--;
}while ((i!=0) && !(n&irqEn&0x20) && !(n&waitFor)); //这个while循环的条件是怎么控制的,什么意思??????
|