char PcdRequest(uchar req_code)
{
char status;
idata struct TranSciveBuffer MfComData;
struct TranSciveBuffer *pi;
pi=&MfComData;
PcdSetTmo(106);
PcdAntennaOn();
WriteRC(RegChannelRedundancy,0x03);
ClearBitMask(RegControl,0x08);//Crypto1on=0
WriteRC(RegBitFraming,0x07);
MfComData.MfCommand=PCD_TRANSCEIVE;
MfComData.MfLength=1;
MfComData.MfData[0]=req_code;
status=PcdComTransceive(pi);
if(!status)
{
if(MfComData.MfLength!=0x10)
{
status=MI_BITCOUNTERR;
}
}
return status;
}
uchar PiccCascAnticoll(uchar *snr)//這個指針變量沒有用,但少了就會報錯
{
uchar i;
uchar snr_check=0;
char status=MI_OK;
idata struct TranSciveBuffer MfComData;
struct TranSciveBuffer *pi;
pi=&MfComData;
PcdAntennaOn();
PcdSetTmo(106);//設(shè)置定時器
WriteRC(RegDecoderControl,0x28);//設(shè)置碰撞后接收的所有位都用0表示
ClearBitMask(RegControl,0x08);//Crypto1=0
WriteRC(RegChannelRedundancy,0x03);//使能奇校驗
MfComData.MfCommand=PCD_TRANSCEIVE;//發(fā)送接收命令
MfComData.MfLength=2;//發(fā)送兩個字節(jié)
MfComData.MfData[0]=PICC_ANTICOLL1;//PICC_ANTICOLL1=93H,PiCC反碰撞指令
MfComData.MfData[1]=0x20;//反碰撞還需要在發(fā)送一個20H,即想picc發(fā)送兩個字節(jié)
status=PcdComTransceive(pi);//PcdComTransceive函數(shù)將Mfdate中的數(shù)據(jù)發(fā)送給Picc
if(!status)//表示執(zhí)行命令沒出錯
{
for(i=0;i<4;i++)//執(zhí)行命令后返回的前4個字節(jié)
{
snr_check^=MfComData.MfData[i];//接收到的前四個字節(jié)按位異或存入snr_check中
}
if(snr_check!=MfComData.MfData[i])//判斷所接收序列號是否正確
{
status=MI_SERNRERR;//接收到的5個字節(jié)出錯
}
else
{
for(i=0;i<5;i++)
{
*(snr+i)=MfComData.MfData[i];
}
}
}
ClearBitMask(RegDecoderControl,0x20);
return status;
}
char PcdSelect(uchar *snr,uchar *sar)
{
uchar i;
char status;
uchar snr_check=0;
idata struct TranSciveBuffer{uchar MfCommand;
uchar MfLength;
uchar MfData[7];
}MfComData;
struct TranSciveBuffer *pi;本文引用地址:http://m.butianyuan.cn/article/201611/321654.htmpi=&MfComData;
PcdSetTmo(106);
WriteRC(RegChannelRedundancy,0x0F);
ClearBitMask(RegControl,0x08);
MfComData.MfCommand=PCD_TRANSCEIVE;
MfComData.MfLength=7;
MfComData.MfData[0]=PICC_ANTICOLL1;
MfComData.MfData[1]=0x70;
for(i=0;i<4;i++)
{
snr_check^=*(snr+i);
MfComData.MfData[i+2]=*(snr+i);
}
MfComData.MfData[6]=snr_check;//select要傳7個字節(jié)
status=PcdComTransceive(pi);
if(status==MI_OK)
{if(MfComData.MfLength!=0x8)
{
status = MI_BITCOUNTERR;
}
*sar=MfComData.MfData[0];
}
return status;
}
void init()
{
delay(500);
RC500CS=1;//這個位定義在rc500文件里
RC500RST=1;
delay(100);
RC500RST=0;
TMOD=0x20;//串口通訊的設(shè)置,采用9600波特率
TH1=0xfd;
TL1=0xfd;
TR1=1;
SCON=0x52;
PCON&=0x7f;
TI=0;
delay(10);//這個延時很重要,否則會寫不進(jìn)數(shù)據(jù)到RC500
}
void main()
{
uchar i,a[5],b[5],status,*snr,c;//傳遞數(shù)組的首地址可以,傳遞指針必須初始化,且每個指針都要知道指向的地址,所以傳遞數(shù)組比較好
snr=&c;
init();
PcdRequest(PICC_REQALL);
PiccCascAnticoll(a);
for(i=0;i<5;i++)
{
b[i]=a[i];
SBUF=b[i];
while(!TI);
TI=0;
}
status=PcdSelect(b,snr);
SBUF=status;
while(!TI);
TI=0;
SBUF=*snr;
while(!TI);
TI=0;
while(1);
}
評論