本帖最后由 xixihaha0 于 2013-11-22 10:53 编辑
寻卡和认证都过了,就卡在读写扇区上,一直返回CRC错误,对照了网上多份示例代码,驱动都没有问题,请各位调试过的指导一下,主要是
MIF_Read函数中,这时总是返回if((temp & 0x08) == 0x08) return FM1702_CRCERR;错误,单片机为 STC89LE54RD+
/****************************************************************/
/*名称: MIF_Read */
/*功能: 该函数实现读MIFARE卡块的数值*/
/*输入: buff: 缓冲区首地址*/
/* Block_Adr: 块地址*/
/*输出: FM1715_NOTAGERR: 无卡*/
/* FM1715_PARITYERR: 奇偶校验错*/
/* FM1715_CRCERR: CRC校验错*/
/* FM1715_BYTECOUNTERR: 接收字节错误*/
/* FM1715_OK: 应答正确*/
/****************************************************************/
uchar MIF_READ(uchar idata *buff, uchar Block_Adr)
{
/*~~~~~~~~~~~~~~~~~*/
uchar idata temp;
/*~~~~~~~~~~~~~~~~~*/
write_reg(0x23,0x63);
write_reg(0x12,0x3f);
write_reg(0x13,0x3f);
write_reg(0x22,0x0f);
buff[0] = RF_CMD_READ;
buff[1] = Block_Adr;
temp = Command_Send(2, buff, Transceive);
if(temp == 0)
{
return FM1715_NOTAGERR;
}
temp = read_reg(0x0A);
if((temp & 0x02) == 0x02) return FM1715_PARITYERR;
if((temp & 0x04) == 0x04) return FM1715_FRAMINGERR;
if((temp & 0x08) == 0x08) return FM1715_CRCERR;
temp = read_reg(0x04);
if(temp == 0x10) /* 8K卡读数据长度为16 */
{
Read_FIFO(buff);
return FM1715_OK;
}
else if(temp == 0x04) /* Token卡读数据长度为16 */
{
Read_FIFO(buff);
return FM1715_OK;
}
else
{
return FM1715_BYTECOUNTERR;
}
}
/****************************************************************/
/*名称: MIF_Write */
/*功能: 该函数实现写MIFARE卡块的数值*/
/*输入: buff: 缓冲区首地址*/
/* Block_Adr: 块地址*/
/*输出: FM1715_NOTAGERR: 无卡*/
/* FM1715_BYTECOUNTERR: 接收字节错误*/
/* FM1715_NOTAUTHERR: 未经权威认证*/
/* FM1715_EMPTY: 数据溢出错误*/
/* FM1715_CRCERR: CRC校验错*/
/* FM1715_PARITYERR: 奇偶校验错*/
/* FM1715_WRITEERR: 写卡块数据出错*/
/* FM1715_OK: 应答正确*/
/****************************************************************/
uchar MIF_Write(uchar idata *buff, uchar Block_Adr)
{
/*~~~~~~~~~~~~~~~~~~~~*/
uchar idata temp;
uchar idata *F_buff;
/*~~~~~~~~~~~~~~~~~~~~*/
write_reg(0x23,0x63);
write_reg(0x12,0x3f);
F_buff = buff + 0x10;
write_reg(0x22,0x07);
*F_buff = RF_CMD_WRITE;
*(F_buff + 1) = Block_Adr;
temp = Command_Send(2, F_buff, Transceive);
if(temp == FALSE)
{
return(FM1715_NOTAGERR);
}
temp = read_reg(0x04);
if(temp == 0)
{
return(FM1715_BYTECOUNTERR);
}
Read_FIFO(F_buff);
temp = *F_buff;
switch(temp)
{
case 0x00: return(FM1715_NOTAUTHERR); /* 暂时屏蔽掉写错误 */
case 0x04: return(FM1715_EMPTY);
case 0x0a: break;
case 0x01: return(FM1715_CRCERR);
case 0x05: return(FM1715_PARITYERR);
default: return(FM1715_WRITEERR);
}
temp = Command_Send(16, buff, Transceive);
if(temp == TRUE)
{
return(FM1715_OK);
}
else
{
temp = read_reg(0x0A);
if((temp & 0x02) == 0x02)
return(FM1715_PARITYERR);
else if((temp & 0x04) == 0x04)
return(FM1715_FRAMINGERR);
else if((temp & 0x08) == 0x08)
return(FM1715_CRCERR);
else
return(FM1715_WRITEERR);
}
}
|