想做一个52+RC500的读卡器,但老是寻不到卡,求指点
#include
#include
#include
sbit data1=P3^3;
sbit data0=P3^2;
sbit beep=P1^4;
sbit grn=P1^5;
sbit red=P1^7;
sbit rst500=P3^5;
unsigned char buff[20],Card_UID[5];
void Nop(void)
{
unsigned char i;
for(i=0;i<5;i++)
{
}
}
//============================================
//=====================================================
char PcdRequest(uchar req_code)
{
char status;
idata struct TranSciveBuffer {uchar MfCommand;
uchar MfLength;
uchar MfData[1];
}MfComData;
struct TranSciveBuffer *pi;
pi=&MfComData;
PcdSetTmo(106);
WriteRC(RegChannelRedundancy,0x03);
ClearBitMask(RegControl,0x08);
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;
}
//----------------------------------------------------------
char PcdAnticoll(uchar *snr)
{
uchar i;
uchar snr_check=0;
char status=MI_OK;
idata struct TranSciveBuffer{uchar MfCommand;
uchar MfLength;
uchar MfData[5];
}MfComData;
struct TranSciveBuffer *pi;
pi=&MfComData;
PcdSetTmo(106);
WriteRC(RegDecoderControl,0x28);
ClearBitMask(RegControl,0x08);
WriteRC(RegChannelRedundancy,0x03);
MfComData.MfCommand=PCD_TRANSCEIVE;
MfComData.MfLength=2;
MfComData.MfData[0]=PICC_ANTICOLL1;
MfComData.MfData[1]=0x20;
status=PcdComTransceive(pi);
if(!status)
{
for(i=0;i<4;i++)
{
snr_check^=MfComData.MfData[i];
}
if(snr_check!=MfComData.MfData[i])
{
status=MI_SERNRERR;
}
else
{
for(i=0;i<4;i++)
{
*(snr+i)=MfComData.MfData[i];
}
}
}
ClearBitMask(RegDecoderControl,0x20);
return status;
}
//*********************************************
unsigned char Get_UID()
{
unsigned char i;
for(i=0;i<5;i++)
buff[i]=P0;
Card_UID[i]=buff[i];
return(0);
}
//========================
void delay(unsigned char i)
{
unsigned char k,j;
for(j=0;j<50;j++)
{ //wdog=~wdog;
for(k=0;k
{
;
}
}
}
//============================
void delay_50us(void)
{
unsigned char i;
for(i=0;i<14;i++)
{
}
}
//=============================
void send_wiegend_data(void)
{
unsigned char k,j,temp;
data1=0;
data0=1;
delay_50us();
data0=1;
data1=1;
delay(1);
for(k=0;k<4;k++)
{
temp=Card_UID[k];
for(j=0;j<8;j++)
{
if(temp&0x80)data1=0;
else data0=0;
delay_50us();
data1=1;
data0=1;
delay(1);
temp=temp<<1;
}
}
data1=0;
data0=1;
delay_50us();
data1=1;
data0=1;
}
//=============================
void main(void)
{
unsigned char i,k,temp,temp_buff[4];
unsigned char request,anticoll;
data0=1;
data1=1;
delay(200);
beep=0;
red=0;
rst500=1;
delay(200);
delay(200);
beep=1;
rst500=0;
delay(200);
data0=1;
data1=1;
while(1)
{
//if(Card_UID[0]!=temp_buff[0]||Card_UID[1]!=temp_buff[1]||Card_UID[2]!=temp_buff[2]||Card_UID[3]!=temp_buff[3])
request=PcdRequest();
anticoll=PcdAnticoll();
if ((request==0)&&(anticoll==0))
{
grn=0;
}
else
continue;
{ data0=1;
data1=1;
send_wiegend_data();
beep=0;
grn=0;
delay(150);
delay(100);
beep=1;
grn=1;
data0=1;
data1=1;
for(k=0;k<4;k++)temp_buff[k]=P0;
}
}
}
提问者:xtl1117
地点:-
浏览次数:3312
提问时间:02-01 17:31
我有更好的答案