这是我的程序:
void main(void)
{
init(); //初始化
while (1)
{
cmd_execution(); //IC卡处理函数
}
}
void init(void)
{
RC632RST=0; //复位RC632
ET2 = 0; //禁止T2中断
T2CON = 0x04; //启动定时器
PCON = 0x80; //µçÔ´¿ØÖƼĴæÆ÷£¬Ê¹´®ÐÐͨѶ·½Ê½1£¬2£¬3µÄ²¨ÌØÂʼӱ¶
SCON = 0x70; //·½Ê½1ͨѶ¼°8λUART£¬²¢ÇÒÔÊÐí´®ÐнÓÊÜ
TMOD = 0x21; //¼ÆÊýÆ÷1Ϊ8λ×Ô¶¯ÖØ×°Ôض¨Ê±Æ÷£¬¼ÆÊýÆ÷T0Ϊ16λ£»TMOD = 0x22;
TH1 = BAUD_9600; //ÉèÖÃT1¼ÆÊý³£Êý£¬¼ÆÊý¸ß×Ö½Ú
TL1 = TH1; //ÉèÖÃT1¼ÆÊý³£Êý£¬¼ÆÊýµÍ×Ö½Ú
TR1 = TRUE; //Æô¶¯¶¨Ê±/¼ÆÊýÆ÷1¼ÆÊý£»²¨ÌØÂÊ·¢ÉúÆ÷
TH0 = 0x60; //ÉèÖÃT0¼ÆÊý³£Êý£¬¼ÆÊý¸ß×Ö½Ú
TL0 = 0x60; //ÉèÖÃT0¼ÆÊý³£Êý£¬¼ÆÊýµÍ×Ö½Ú
TR0 = 0; //½ûÖ¹T0¼ÆÊý
ET0=0; //½ûÖ¹T0ÖжÏ
ET1=0; //½ûÖ¹T1ÖжÏ
EA=1; //CPU×ÜÖжÏÔÊÐí
EX0=1; //ÍⲿÖжÏ0ÔÊÐí
IT0 = 1; //P3.2Òý½ÅϽµÑØ´¥·¢ÖжÏ
TR2=0; //Í£Ö¹¶¨Ê±Æ÷2
ES = TRUE; //´®ÐпÚÖжÏÔÊÐí
CmdValid=0; //ÃüÁîÓÐЧ±êÖ¾
delay_10ms(30); //
delay_10ms(30); //
MfConfig(); //ÅäÖÃRC632
}
void cmd_execution(void)
{
unsigned char cmd;
unsigned char status;
cmd = RevBuffer[0];
switch(cmd)
{
case 1:
// Halt the card //ÖÕÖ¹¿¨µÄ²Ù×÷
status=M500PiccHalt();
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 2:
// Request,Anticoll,Select,return CardType(2 bytes)+CardSerialNo(4 bytes)
// Ñ°¿¨£¬·À³åÍ»£¬Ñ¡Ôñ¿¨ ·µ»Ø¿¨ÀàÐÍ£¨2 bytes£©+ ¿¨ÏµÁкÅ(4 bytes)
status=M500PiccCommonRequest(RevBuffer[1],&RevBuffer[2]);
if(status!=0)
{
status=M500PiccCommonRequest(RevBuffer[1],&RevBuffer[2]);
if(status!=0)
{
RevBuffer[0]=1;
RevBuffer[1]=status;
P2 &=0xc7; //ÏÔʾûÓп¨Æ¬
break;
}
}
if(RevBuffer[2]==2)
cardtype=mifarepro; // Mifare Pro ¿¨
else if(RevBuffer[2]==4)
cardtype=mifare1; // Mifare One ¿¨
else if(RevBuffer[2]==16)
cardtype=mifarelight; // Mifare Light ¿¨
else
cardtype=unknowncard;
RevBuffer[0]=3;
RevBuffer[1]=status;
P2 &=0xc7; //ÏÔʾûÓп¨Æ¬
break;
case 3:
// ·À³åÍ» ¶Á¿¨µÄϵÁкŠMLastSelectedSnr//
status = M500PiccCascAnticoll(0,&RevBuffer[2]);
if(status!=0)
{
RevBuffer[0]=1;
RevBuffer[1]=status;
P2 &=0xc7; //ÏÔʾûÓп¨Æ¬
break;
}
memcpy(MLastSelectedSnr,&RevBuffer[2],4);
RevBuffer[0]=5;
RevBuffer[1]=status;
break;
case 4:
// Ñ¡Ôñ¿¨ Select Card
status=M500PiccCascSelect(MLastSelectedSnr,&RevBuffer[2]);
if(status!=MI_OK)
{
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
}
RevBuffer[0]=3;
RevBuffer[1]=status;
break;
case 5:
// Key loading into the MF RC500's EEPROM
// УÑ鿨ÃÜÂë(E2)
status=M500PiccAuthE2(RevBuffer[1],MLastSelectedSnr,RevBuffer[2],RevBuffer[3]);
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 6:
// Key loading into the MF RC500's EEPROM
// ÏÂÔØÃÜÂë(E2)
status=M500PcdLoadKeyE2(RevBuffer[1],RevBuffer[2],&RevBuffer[3]);
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 7:
// Authentication with direct key loading from the uC
// Ö±½ÓУÑéÃÜÂë
memcpy(&RevBuffer[9],&RevBuffer[3],6);
M500HostCodeKey(&RevBuffer[9],&RevBuffer[3]);
status=M500PiccAuthKey(RevBuffer[1],MLastSelectedSnr,&RevBuffer[3],RevBuffer[2]);
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 8:
// Read the mifare card
// ¶Á¿¨
status=M500PiccRead(RevBuffer[1],&RevBuffer[2]);
if(status==0)
{
if(cardtype==mifare1||cardtype==mifarepro)
RevBuffer[0]=17;
else if(cardtype==1)
RevBuffer[0]=9;
else
RevBuffer[0]=16;
}
else
{
RevBuffer[0]=1;
}
RevBuffer[1]=status;
break;
case 9:
// Write the mifare card
// д¿¨ ÏÂÔØÃÜÂë
if(status=M500PiccWrite(RevBuffer[1],&RevBuffer[2])==0)
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 10:
status = M500PiccValue(RevBuffer[1],RevBuffer[2],&RevBuffer[3],RevBuffer[7]);
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 12:
// ²ÎÊýÉèÖÃ
RevBuffer[0]=1;
RevBuffer[1]=0;
break;
}
}
char MfConfig(void)
{
char status=MI_OK;
M500PcdConfig();
return status;
}
char M500PcdConfig(void)
{
char idata status;
if ((status = M500PcdReset()) == MI_OK)
{
WriteIO(RegClockQControl,0x00);
WriteIO(RegClockQControl,0x40);
delay_50us(2);
ClearBitMask(RegClockQControl,0x40);
WriteIO(RegBitPhase,0xAD);
WriteIO(RegRxThreshold,0xFF);
WriteIO(RegRxControl2,0x01);
WriteIO(RegFIFOLevel,0x1A);
WriteIO(RegTimerControl,0x02);
WriteIO(RegIRqPinConfig,0x03);
M500PcdRfReset(1);
}
return status;
}
|