基于单片机的IC卡门禁系统 下载本文

WriteRawRC(ComIEnReg,irqEn|0x80); ClearBitMask(ComIrqReg,0x80);

WriteRawRC(CommandReg,PCD_IDLE); SetBitMask(FIFOLevelReg,0x80);

for (i=0; i

WriteRawRC(CommandReg, Command);

if (Command == PCD_TRANSCEIVE) { SetBitMask(BitFramingReg,0x80); }

//i = 600;//根据时钟频率调整,操作M1卡最大等待时间25ms i = 2000; do {

n = ReadRawRC(ComIrqReg); i--; }

while ((i!=0) && !(n&0x01) && !(n&waitFor)); ClearBitMask(BitFramingReg,0x80);

if (i!=0) {

if(!(ReadRawRC(ErrorReg)&0x1B)) {

status = MI_OK;

if (n & irqEn & 0x01)

{ status = MI_NOTAGERR; } if (Command == PCD_TRANSCEIVE) {

n = ReadRawRC(FIFOLevelReg);

lastBits = ReadRawRC(ControlReg) & 0x07; if (lastBits) { *pOutLenBit = (n-1)*8 + lastBits; } else {

43

*pOutLenBit = n*8; }

if (n == 0) { n = 1; }

if (n > MAXRLEN) { n = MAXRLEN; }

for (i=0; i

SetBitMask(ControlReg,0x80); // stop timer now WriteRawRC(CommandReg,PCD_IDLE); return status; }

///////////////////////////////////////////////////////////////////// //功 能:寻卡

//参数说明: req_code[IN]:寻卡方式

// 0x52 = 寻感应区内所有符合14443A标准的卡 // 0x26 = 寻未进入休眠状态的卡 // pTagType[OUT]:卡片类型代码 // 0x4400 = Mifare_UltraLight // 0x0400 = Mifare_One(S50) // 0x0200 = Mifare_One(S70) // 0x0800 = Mifare_Pro(X) // 0x4403 = Mifare_DESFire //返 回: 成功返回MI_OK

/////////////////////////////////////////////////////////////////////

char PcdRequest(unsigned char req_code,unsigned char *pTagType) { char status;

44

unsigned int unLen; unsigned char ucComMF522Buf[MAXRLEN]; ClearBitMask(Status2Reg,0x08); WriteRawRC(BitFramingReg,0x07); SetBitMask(TxControlReg,0x03); ucComMF522Buf[0] = req_code; status

PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen); if ((status == MI_OK) && (unLen == 0x10)) { *pTagType = ucComMF522Buf[0]; *(pTagType+1) = ucComMF522Buf[1]; } else { status = MI_ERR; } return status; }

///////////////////////////////////////////////////////////////////// //功 能:防冲撞

//参数说明: pSnr[OUT]:卡片序列号,4字节 //返 回: 成功返回MI_OK

///////////////////////////////////////////////////////////////////// char PcdAnticoll(unsigned char *pSnr) {

char status;

unsigned char i,snr_check=0; unsigned int unLen;

unsigned char ucComMF522Buf[MAXRLEN];

ClearBitMask(Status2Reg,0x08); WriteRawRC(BitFramingReg,0x00); ClearBitMask(CollReg,0x80);

ucComMF522Buf[0] = PICC_ANTICOLL1;

45

=

ucComMF522Buf[1] = 0x20;

status

PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen);

if (status == MI_OK) { for (i=0; i<4; i++) { *(pSnr+i) = ucComMF522Buf[i]; snr_check ^= ucComMF522Buf[i]; } if (snr_check != ucComMF522Buf[i]) { status = MI_ERR; } }

SetBitMask(CollReg,0x80); return status; }

///////////////////////////////////////////////////////////////////// //功 能:选定卡片

//参数说明: pSnr[IN]:卡片序列号,4字节 //返 回: 成功返回MI_OK

///////////////////////////////////////////////////////////////////// char PcdSelect(unsigned char *pSnr) {

char status; unsigned char i; unsigned int unLen;

unsigned char ucComMF522Buf[MAXRLEN];

ucComMF522Buf[0] = PICC_ANTICOLL1; ucComMF522Buf[1] = 0x70; ucComMF522Buf[6] = 0; for (i=0; i<4; i++) { ucComMF522Buf[i+2] = *(pSnr+i); ucComMF522Buf[6] ^= *(pSnr+i); }

CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf[7]);

46

=