RC522驱动:
一、硬件连接: SDA——P2.0 SCK——P2.1 MISO——P2.3 MOSI——P2.2 RST——P2.4 IRQ——悬空 二、驱动代码编写: RC522.c文件
#include <intrins.h>
#include <string.h>
#include "rc522.h"
#include "delay.h"
sfr RCAP2LH = 0xCA;
sfr T2LH = 0xCC;
sbit MF522_NSS = P2^0;
sbit MF522_SCK = P2^1;
sbit MF522_SI = P2^2;
sbit MF522_SO = P2^3;
sbit MF522_RST = P2^4;
char PcdRequest(u8 req_code,u8 *pTagType){
char status;
u32 unLen;
u8 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;
}
char PcdAnticoll(u8 *pSnr)
{
char status;
u8 i,snr_check=0;
u32 unLen;
u8 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;
}
char PcdReset(void){
MF522_RST=1;
_nop_();
MF522_RST=0;
_nop_();
MF522_RST=1;
_nop_();
WriteRawRC(CommandReg,PCD_RESETPHASE);
_nop_();
WriteRawRC(ModeReg,0x3D);
WriteRawRC(TReloadRegL,30);
WriteRawRC(TReloadRegH,0);
WriteRawRC(TModeReg,0x8D);
WriteRawRC(TPrescalerReg,0x3E);
WriteRawRC(TxAutoReg,0x40);
return MI_OK;
}
char M500PcdConfigISOType(u8 type){
if (type == 'A')
{
ClearBitMask(Status2Reg,0x08);
WriteRawRC(ModeReg,0x3D);
WriteRawRC(RxSelReg,0x86);
WriteRawRC(RFCfgReg,0x7F);
WriteRawRC(TReloadRegL,30);
WriteRawRC(TReloadRegH,0);
WriteRawRC(TModeReg,0x8D);
WriteRawRC(TPrescalerReg,0x3E);
delay_n10ms(1);
PcdAntennaOn();
}
else{ return -1; }
return MI_OK;
}
u8 ReadRawRC(u8 Address){
u8 i, ucAddr;
u8 ucResult=0;
MF522_SCK = 0;
MF522_NSS = 0;
ucAddr = ((Address<<1)&0x7E)|0x80;
for(i=8;i>0;i--)
{
MF522_SI = ((ucAddr&0x80)==0x80);
MF522_SCK = 1;
ucAddr <<= 1;
MF522_SCK = 0;
}
for(i=8;i>0;i--)
{
MF522_SCK = 1;
ucResult <<= 1;
ucResult|=(bit)MF522_SO;
MF522_SCK = 0;
}
MF522_NSS = 1;
MF522_SCK = 1;
return ucResult;
}
void WriteRawRC(u8 Address, u8 value){
u8 i, ucAddr;
MF522_SCK = 0;
MF522_NSS = 0;
ucAddr = ((Address<<1)&0x7E);
for(i=8;i>0;i--)
{
MF522_SI = ((ucAddr&0x80)==0x80);
MF522_SCK = 1;
ucAddr <<= 1;
MF522_SCK = 0;
}
for(i=8;i>0;i--)
{
MF522_SI = ((value&0x80)==0x80);
MF522_SCK = 1;
value <<= 1;
MF522_SCK = 0;
}
MF522_NSS = 1;
MF522_SCK = 1;
}
void SetBitMask(u8 reg,u8 mask)
{
char tmp = 0x0;
tmp = ReadRawRC(reg);
WriteRawRC(reg,tmp | mask);
}
void ClearBitMask(u8 reg,u8 mask)
{
char tmp = 0x0;
tmp = ReadRawRC(reg);
WriteRawRC(reg, tmp & ~mask);
}
char PcdComMF522(u8 Command,
u8 *pInData,
u8 InLenByte,
u8 *pOutData,
u32 *pOutLenBit)
{
char status = MI_ERR;
u8 irqEn = 0x00;
u8 waitFor = 0x00;
u8 lastBits;
u8 n;
u32 i;
switch (Command)
{
case PCD_AUTHENT:
irqEn = 0x12;
waitFor = 0x10;
break;
case PCD_TRANSCEIVE:
irqEn = 0x77;
waitFor = 0x30;
break;
default:
break;
}
WriteRawRC(ComIEnReg,irqEn|0x80);
ClearBitMask(ComIrqReg,0x80);
WriteRawRC(CommandReg,PCD_IDLE);
SetBitMask(FIFOLevelReg,0x80);
for (i=0; i<InLenByte; i++)
{ WriteRawRC(FIFODataReg, pInData[i]); }
WriteRawRC(CommandReg, Command);
if (Command == PCD_TRANSCEIVE)
{ SetBitMask(BitFramingReg,0x80); }
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
{ *pOutLenBit = n*8; }
if (n == 0)
{ n = 1; }
if (n > MAXRLEN)
{ n = MAXRLEN; }
for (i=0; i<n; i++)
{ pOutData[i] = ReadRawRC(FIFODataReg); }
}
}
else
{ status = MI_ERR; }
}
SetBitMask(ControlReg,0x80);
WriteRawRC(CommandReg,PCD_IDLE);
return status;
}
void PcdAntennaOn(){
u8 i;
i = ReadRawRC(TxControlReg);
if (!(i & 0x03))
{
SetBitMask(TxControlReg, 0x03);
}
}
void PcdAntennaOff()
{
ClearBitMask(TxControlReg, 0x03);
}
#if 0
void delay_n10ms(u32 _10ms)
{
#ifndef NO_TIMER2
RCAP2LH = RCAP2_10ms;
T2LH = RCAP2_10ms;
TR2 = TRUE;
while (_10ms--)
{
while (!TF2);
TF2 = FALSE;
}
TR2 = FALSE;
#else
while (_10ms--)
{
delay_50us(19);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid )
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(19);
if (CmdValid)
return;
}
#endif
}
#endif
rc522.h文件
#ifndef RC522_H__
#define RC522_H__
#include "main.h"
typedef enum
{
false,
true,
}bool;
#define OSC_FREQ (22118400L)
#define RCAP2_50us (65536L - OSC_FREQ/40417L)
#define RCAP2_1ms (65536L - OSC_FREQ/2000L)
#define RCAP2_10ms (65536L - OSC_FREQ/1200L)
#define TIME0_500us (65536L - OSC_FREQ/8000L)
#define TIME0_10ms (65536L - OSC_FREQ/200)
#define MAXRLEN 18
#define PICC_REQIDL 0x26
#define PICC_REQALL 0x52
#define PICC_ANTICOLL1 0x93
#define PICC_ANTICOLL2 0x95
#define PICC_AUTHENT1A 0x60
#define PICC_AUTHENT1B 0x61
#define PICC_READ 0x30
#define PICC_WRITE 0xA0
#define PICC_DECREMENT 0xC0
#define PICC_INCREMENT 0xC1
#define PICC_RESTORE 0xC2
#define PICC_TRANSFER 0xB0
#define PICC_HALT 0x50
#define PCD_IDLE 0x00
#define PCD_AUTHENT 0x0E
#define PCD_RECEIVE 0x08
#define PCD_TRANSMIT 0x04
#define PCD_TRANSCEIVE 0x0C
#define PCD_RESETPHASE 0x0F
#define PCD_CALCCRC 0x03
#define DEF_FIFO_LENGTH 64
#define RFU00 0x00
#define CommandReg 0x01
#define ComIEnReg 0x02
#define DivlEnReg 0x03
#define ComIrqReg 0x04
#define DivIrqReg 0x05
#define ErrorReg 0x06
#define Status1Reg 0x07
#define Status2Reg 0x08
#define FIFODataReg 0x09
#define FIFOLevelReg 0x0A
#define WaterLevelReg 0x0B
#define ControlReg 0x0C
#define BitFramingReg 0x0D
#define CollReg 0x0E
#define RFU0F 0x0F
#define RFU10 0x10
#define ModeReg 0x11
#define TxModeReg 0x12
#define RxModeReg 0x13
#define TxControlReg 0x14
#define TxAutoReg 0x15
#define TxSelReg 0x16
#define RxSelReg 0x17
#define RxThresholdReg 0x18
#define DemodReg 0x19
#define RFU1A 0x1A
#define RFU1B 0x1B
#define MifareReg 0x1C
#define RFU1D 0x1D
#define RFU1E 0x1E
#define SerialSpeedReg 0x1F
#define RFU20 0x20
#define CRCResultRegM 0x21
#define CRCResultRegL 0x22
#define RFU23 0x23
#define ModWidthReg 0x24
#define RFU25 0x25
#define RFCfgReg 0x26
#define GsNReg 0x27
#define CWGsCfgReg 0x28
#define ModGsCfgReg 0x29
#define TModeReg 0x2A
#define TPrescalerReg 0x2B
#define TReloadRegH 0x2C
#define TReloadRegL 0x2D
#define TCounterValueRegH 0x2E
#define TCounterValueRegL 0x2F
#define RFU30 0x30
#define TestSel1Reg 0x31
#define TestSel2Reg 0x32
#define TestPinEnReg 0x33
#define TestPinValueReg 0x34
#define TestBusReg 0x35
#define AutoTestReg 0x36
#define VersionReg 0x37
#define AnalogTestReg 0x38
#define TestDAC1Reg 0x39
#define TestDAC2Reg 0x3A
#define TestADCReg 0x3B
#define RFU3C 0x3C
#define RFU3D 0x3D
#define RFU3E 0x3E
#define RFU3F 0x3F
#define MI_OK 0
#define MI_NOTAGERR (-1)
#define MI_ERR (-2)
char PcdReset(void);
void PcdAntennaOn(void);
void PcdAntennaOff(void);
char PcdRequest(u8 req_code,u8 *pTagType);
char PcdAnticoll(u8 *pSnr);
char PcdComMF522(u8 Command,
u8 *pInData,
u8 InLenByte,
u8 *pOutData,
u32 *pOutLenBit);
void WriteRawRC(u8 Address,u8 value);
u8 ReadRawRC(u8 Address);
void SetBitMask(u8 reg,u8 mask);
void ClearBitMask(u8 reg,u8 mask);
char M500PcdConfigISOType(u8 type);
void delay_10ms(u32 _10ms);
char PcdSelect(u8 *pSnr);
char PcdAuthState(u8 auth_mode,u8 addr,u8 *pKey,u8 *pSnr);
char PcdRead(u8 addr,u8 *pData);
char PcdWrite(u8 addr,u8 *pData);
char PcdValue(u8 dd_mode,u8 addr,u8 *pValue);
char PcdBakValue(u8 sourceaddr, u8 goaladdr);
void CalulateCRC(u8 *pIndata,u8 len,u8 *pOutData);
#endif
IC.c文件
#include "IC.h"
#include "rc522.h"
#include "key.h"
#include "uart.h"
#include <string.h>
#include "beep.h"
#include "delay.h"
u8 UID2[5] = {0x00,0x01,0x02,0x03};
void IC_Init(void)
{
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
M500PcdConfigISOType('A');
}
bool Read_IC_Card(u8 *DesUid)
{
u8 temp[4],UID[5];
if (PcdRequest(0x52, temp) == MI_OK)
{
if (PcdAnticoll(UID) == MI_OK)
{
strcpy((char *)DesUid,(char *)UID);
return true;
}
}
return false;
}
bool Cmp_IC_Card(u8 *DesUid,u8 *SrcUid)
{
if(!strcmp((char *)DesUid,(char *)SrcUid))
{
return true;
}
return false;
}
void IC_Card_Run(void)
{
u8 UID[5];
u16 keyval = NOKEY_PRES;
bool res;
while(1)
{
res = Read_IC_Card(UID);
if(res == true)
{
Uart_Send_Data("读卡成功\r\n");
res = Cmp_IC_Card(UID2,UID);
if(res == true)
{
Uart_Send_Data("卡号正确\r\n");
Buzz_Times(1);
JDQ = 0;
Delay1s();
Delay1s();
Delay1s();
JDQ = 1;
}
else
{
Uart_Send_Data("卡号错误\r\n");
Buzz_Times(3);
}
}
keyval = key_scan();
if(keyval == KEY16_PRES)
break;
}
}
void IC_Card_Read(void)
{
u8 UID[5];
u16 keyval = NOKEY_PRES;
bool res;
while(1)
{
res = Read_IC_Card(UID);
if(res == true)
{
strcpy((char *)UID2,(char *)UID);
Buzz_Times(1);
Delay1s();
}
keyval = key_scan();
if(keyval == KEY16_PRES)
break;
}
}
IC.h
#ifndef IC_H__
#define IC_H__
#include "main.h"
void IC_Init(void);
bool Read_IC_Card(u8 *DesUid);
bool Cmp_IC_Card(u8 *DesUid,u8 *SrcUid);
void IC_Card_Run(void);
void IC_Card_Read(void);
#endif
以上就是RC522的相关驱动代码,在使用时只需要先调用IC_Init初始化函数进行初始化;IC_Card_Run和IC_Card_Read函数为测试函数,测试时需要先调用IC_Card_Read读取卡号,再调用IC_Card_Run函数。
|