基于51,人體紅外感應(yīng)和RC522的門禁系統(tǒng)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
總結(jié)一下最近學(xué)的東西,這兩天學(xué)的東西,rfid門卡系統(tǒng)終于弄出來(lái)來(lái)了,這個(gè)程序算現(xiàn)在寫過(guò)的比較滿意的程序,大家可以參考參考
主函數(shù):
rc522.c
#include
#include
#include "mian.h"
#include "rc522.h"
#define MAXRLEN 18
/////////////////////////////////////////////////////////////////////
//功 能:尋卡
//參數(shù)說(shuō)明: req_code[IN]:尋卡方式
// 0x52 = 尋感應(yīng)區(qū)內(nèi)所有符合14443A標(biāo)準(zhǔn)的卡
// 0x26 = 尋未進(jìn)入休眠狀態(tài)的卡
// 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;
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;
}
/////////////////////////////////////////////////////////////////////
//功 能:防沖撞
//參數(shù)說(shuō)明: pSnr[OUT]:卡片序列號(hào),4字節(jié)
//返 回: 成功返回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;
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;
}
/////////////////////////////////////////////////////////////////////
//功 能:選定卡片
//參數(shù)說(shuō)明: pSnr[IN]:卡片序列號(hào),4字節(jié)
//返 回: 成功返回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]);
ClearBitMask(Status2Reg,0x08);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,9,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x18))
{ status = MI_OK; }
else
{ status = MI_ERR; }
return status;
}
*/
/////////////////////////////////////////////////////////////////////
//功 能:驗(yàn)證卡片密碼
//參數(shù)說(shuō)明: auth_mode[IN]: 密碼驗(yàn)證模式
// 0x60 = 驗(yàn)證A密鑰
// 0x61 = 驗(yàn)證B密鑰
// addr[IN]:塊地址
// pKey[IN]:密碼
// pSnr[IN]:卡片序列號(hào),4字節(jié)
//返 回: 成功返回MI_OK
/////////////////////////////////////////////////////////////////////
/*char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = auth_mode;
ucComMF522Buf[1] = addr;
for (i=0; i<6; i++)
{ ucComMF522Buf[i+2] = *(pKey+i); }
for (i=0; i<6; i++)
{ ucComMF522Buf[i+8] = *(pSnr+i); }
// memcpy(&ucComMF522Buf[2], pKey, 6);
// memcpy(&ucComMF522Buf[8], pSnr, 4);
status = PcdComMF522(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (!(ReadRawRC(Status2Reg) & 0x08)))
{ status = MI_ERR; }
return status;
}
*/
/////////////////////////////////////////////////////////////////////
//功 能:讀取M1卡一塊數(shù)據(jù)
//參數(shù)說(shuō)明: addr[IN]:塊地址
// pData[OUT]:讀出的數(shù)據(jù),16字節(jié)
//返 回: 成功返回MI_OK
/////////////////////////////////////////////////////////////////////
/*char PcdRead(unsigned char addr,unsigned char *pData)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_READ;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x90))
// { memcpy(pData, ucComMF522Buf, 16); }
{
for (i=0; i<16; i++)
{ *(pData+i) = ucComMF522Buf[i]; }
}
else
{ status = MI_ERR; }
return status;
}
*/
/////////////////////////////////////////////////////////////////////
//功 能:寫數(shù)據(jù)到M1卡一塊
//參數(shù)說(shuō)明: addr[IN]:塊地址
// pData[IN]:寫入的數(shù)據(jù),16字節(jié)
//返 回: 成功返回MI_OK
/////////////////////////////////////////////////////////////////////
/*char PcdWrite(unsigned char addr,unsigned char *pData)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_WRITE;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
if (status == MI_OK)
{
//memcpy(ucComMF522Buf, pData, 16);
for (i=0; i<16; i++)
{ ucComMF522Buf[i] = *(pData+i); }
CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf[16]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
}
return status;
}
*/
/////////////////////////////////////////////////////////////////////
//功 能:扣款和充值
//參數(shù)說(shuō)明: dd_mode[IN]:命令字
// 0xC0 = 扣款
// 0xC1 = 充值
// addr[IN]:錢包地址
// pValue[IN]:4字節(jié)增(減)值,低位在前
//返 回: 成功返回MI_OK
/////////////////////////////////////////////////////////////////////
// char PcdValue(unsigned char dd_mode,unsigned char addr,unsigned char *pValue)
// {
// char