RC522通过SPI方式读写M1卡22页_第1页
RC522通过SPI方式读写M1卡22页_第2页
RC522通过SPI方式读写M1卡22页_第3页
RC522通过SPI方式读写M1卡22页_第4页
RC522通过SPI方式读写M1卡22页_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1.准备工作战舰V3开发板,RC522模块为淘宝所买,赠送一张M1卡。SPI方式实测可以读写。2.RC522部分:rc522.h:/#include sys.h/MF522命令字/#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 0x0

2、3 /CRC计算/Mifare_One卡片命令字/#define PICC_REQIDL 0x26 /寻天线区内未进入休眠状态#define PICC_REQALL 0x52 /寻天线区内全部卡#define PICC_ANTICOLL1 0x93 /防冲撞#define PICC_ANTICOLL2 0x95 /防冲撞#define PICC_AUTHENT1A 0x60 /验证A密钥#define PICC_AUTHENT1B 0x61 /验证B密钥#define PICC_READ 0x30 /读块#define PICC_WRITE 0xA0 /写块#define PICC_DECRE

3、MENT 0xC0 /扣款#define PICC_INCREMENT 0xC1 /充值#define PICC_RESTORE 0xC2 /调块数据到缓冲区#define PICC_TRANSFER 0xB0 /保存缓冲区中数据#define PICC_HALT 0x50 /休眠/MF522 FIFO长度定义/#define DEF_FIFO_LENGTH 64 /FIFO size=64byte#define MAXRLEN 18/MF522寄存器定义/ PAGE 0#define RFU00 0x00 #define CommandReg 0x01 #define ComIEnReg 0

4、x02 #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

5、0x0F/ PAGE 1 #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 RF

6、U1D 0x1D#define RFU1E 0x1E#define SerialSpeedReg 0x1F/ PAGE 2 #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 0x2

7、A#define TPrescalerReg 0x2B#define TReloadRegH 0x2C#define TReloadRegL 0x2D#define TCounterValueRegH 0x2E#define TCounterValueRegL 0x2F/ PAGE 3 #define RFU30 0x30#define TestSel1Reg 0x31#define TestSel2Reg 0x32#define TestPinEnReg 0x33#define TestPinValueReg 0x34#define TestBusReg 0x35#define AutoTe

8、stReg 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/和MF522通讯时返回的错误代码/#define MI_OK 0#define MI_NOTAGERR (1)#define MI_ERR (2)#defineSHAQU10X01#

9、defineKUAI40X04#defineKUAI70X07#defineREGCARD0xa1#defineCONSUME0xa2#define READCARD0xa3#define ADDMONEY0xa4#define SPIReadByte()SPIWriteByte(0)u8 SPIWriteByte(u8 byte);void SPI2_Init(void);#define SET_SPI_CS GPIO_SetBits(GPIOF,GPIO_Pin_0);#define CLR_SPI_CS GPIO_ResetBits(GPIOF,GPIO_Pin_0)#define SE

10、T_RC522RST GPIO_SetBits(GPIOF,GPIO_Pin_1);#define CLR_RC522RST GPIO_ResetBits(GPIOF,GPIO_Pin_1)void InitRc522(void);void Usart1_Send_String(unsigned char *str);void ClearBitMask(u8 reg,u8 mask);void WriteRawRC(u8 Address, u8 value);void SetBitMask(u8 reg,u8 mask);char PcdComMF522(u8 Command, u8 *pIn

11、 , u8 InLenByte, u8 *pOut , u8 *pOutLenBit);void CalulateCRC(u8 *pIn ,u8 len,u8 *pOut );u8 ReadRawRC(u8 Address);void PcdAntennaOn(void);char PcdReset(void);char PcdRequest(unsigned char req_code,unsigned char *pTagType);void PcdAntennaOn(void);void PcdAntennaOff(void);char M500PcdConfigISOType(unsi

12、gned char type);char PcdAnticoll(unsigned char *pSnr);char PcdSelect(unsigned char *pSnr);char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr);char PcdWrite(unsigned char addr,unsigned char *pData);char PcdRead(unsigned char addr,unsigned char *pData)

13、;char PcdHalt(void);void Reset_RC522(void);char PcdValue(u8 dd_mode,u8 addr,u8 *pValue);/rc522.c:#include sys.h#include rc522.h#include delay.hvoid delay_ns(u32 ns) u32 i; for(i=0;i200)return 0; SPI_I2S_SendData(SPI2, TxData); /通过外设SPIx发送一个数据retry=0;while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RX

14、NE) = RESET) /检查指定的SPI标志位设置与否:接受缓存非空标志位retry+;if(retry200)return 0; return SPI_I2S_ReceiveData(SPI2); /返回通过SPIx最近接收的数据 void SPI2_Init(void) GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOF, ENABLE );/PORTB时钟使能 RC

15、C_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE );/SPI2时钟使能 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; /IO-PF0、PF1 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /IO口速度为50MHz GPIO_Init(GPIOF, &GPIO_InitStructure); /根据设定参数初始化PF

16、0、PF1 GPIO_ResetBits(GPIOF,GPIO_Pin_1); /PF1输出低 /GPIO_SetBits(GPIOF,GPIO_Pin_0);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /PB13/14/15复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructur

17、e);/初始化GPIOB GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); /PB13/14/15上拉SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; /设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工SPI_InitStructure.SPI_Mode = SPI_Mode_Master;/设置SPI工作模式:设置为主SPISPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;/设置SPI的

18、数据大小:SPI发送接收8位帧结构SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;/串行同步时钟的空闲状态为低电平SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;/串行同步时钟的第一个跳变沿(上升或下降)数据被采样SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;/NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;/定

19、义波特率预分频的值:波特率预分频值为256SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;/指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始SPI_InitStructure.SPI_CRCPolynomial = 7;/CRC值计算的多项式SPI_Init(SPI2, &SPI_InitStructure); /根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器 SPI_Cmd(SPI2, ENABLE); /使能SPI外设/SPI2_ReadWriteByte(0xff);/启动传输void InitRc5

20、22(void) SPI2_Init(); PcdReset(); PcdAntennaOff(); delay_ms(2); PcdAntennaOn(); M500PcdConfigISOType( A );void Reset_RC522(void) PcdReset(); PcdAntennaOff(); delay_ms(2); PcdAntennaOn(); /功 能:寻卡/参数说明: req_codeIN:寻卡方式/ 0x52 = 寻感应区内所有符合14443A标准的卡/ 0x26 = 寻未进入休眠状态的卡/ pTagTypeOUT:卡片类型代码/ 0x4400 = Mifare

21、_UltraLight/ 0x0400 = Mifare_One(S50)/ 0x0200 = Mifare_One(S70)/ 0x0800 = Mifare_Pro(X)/ 0x4403 = Mifare_DESFire/返 回: 成功返回MI_OK/char PcdRequest(u8 req_code,u8 *pTagType)char status; u8 unLen;u8 ucComMF522BufMAXRLEN; ClearBitMask(Status2Reg,0x08);WriteRawRC(BitFramingReg,0x07);SetBitMask(TxControlReg

22、,0x03); ucComMF522Buf0 = req_code;status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen);if (status = MI_OK) & (unLen = 0x10) *pTagType = ucComMF522Buf0;*(pTagType+1) = ucComMF522Buf1;else status = MI_ERR; return status;/功 能:防冲撞/参数说明: pSnrOUT:卡片序列号,4字节/返 回: 成功返回MI_OK/ char PcdAnti

23、coll(u8 *pSnr) char status; u8 i,snr_check=0; u8 unLen; u8 ucComMF522BufMAXRLEN; ClearBitMask(Status2Reg,0x08); WriteRawRC(BitFramingReg,0x00); ClearBitMask(CollReg,0x80); ucComMF522Buf0 = PICC_ANTICOLL1; ucComMF522Buf1 = 0x20; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen

24、); if (status = MI_OK) for (i=0; i4; i+) *(pSnr+i) = ucComMF522Bufi; snr_check = ucComMF522Bufi; if (snr_check != ucComMF522Bufi) status = MI_ERR; SetBitMask(CollReg,0x80); return status;/功 能:选定卡片/参数说明: pSnrIN:卡片序列号,4字节/返 回: 成功返回MI_OK/char PcdSelect(u8 *pSnr) char status; u8 i; u8 unLen; u8 ucComMF5

25、22BufMAXRLEN; ucComMF522Buf0 = PICC_ANTICOLL1; ucComMF522Buf1 = 0x70; ucComMF522Buf6 = 0; for (i=0; i4; i+) ucComMF522Bufi+2 = *(pSnr+i); ucComMF522Buf6 = *(pSnr+i); CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf7); ClearBitMask(Status2Reg,0x08); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,9,ucCom

26、MF522Buf,&unLen); if (status = MI_OK) & (unLen = 0x18) status = MI_OK; else status = MI_ERR; return status;/功 能:验证卡片密码/参数说明: auth_modeIN: 密码验证模式/ 0x60 = 验证A密钥/ 0x61 = 验证B密钥 / addrIN:块地址/ pKeyIN:密码/ pSnrIN:卡片序列号,4字节/返 回: 成功返回MI_OK/ char PcdAuthState(u8 auth_mode,u8 addr,u8 *pKey,u8 *pSnr) char status

27、; u8 unLen; u8 i,ucComMF522BufMAXRLEN; ucComMF522Buf0 = auth_mode; ucComMF522Buf1 = addr;/ for (i=0; i6; i+)/ ucComMF522Bufi+2 = *(pKey+i); / for (i=0; i6; i+)/ ucComMF522Bufi+8 = *(pSnr+i); memcpy(&ucComMF522Buf2, pKey, 6); memcpy(&ucComMF522Buf8, pSnr, 4); status = PcdComMF522(PCD_AUTHENT,ucComMF5

28、22Buf,12,ucComMF522Buf,&unLen); if (status != MI_OK) | (!(ReadRawRC(Status2Reg) & 0x08) status = MI_ERR; return status;/功 能:读取M1卡一块数据/参数说明: addrIN:块地址/ p OUT:读出的数据,16字节/返 回: 成功返回MI_OK/ char PcdRead(u8 addr,u8 *p ) char status; u8 unLen; u8 i,ucComMF522BufMAXRLEN; ucComMF522Buf0 = PICC_READ; ucComMF5

29、22Buf1 = addr; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf2); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); if (status = MI_OK) & (unLen = 0x90) / memcpy(p , ucComMF522Buf, 16); for (i=0; i16; i+) *(p +i) = ucComMF522Bufi; else status = MI_ERR; return status;/功 能:写数据到M1卡一

30、块/参数说明: addrIN:块地址/ p IN:写入的数据,16字节/返 回: 成功返回MI_OK/ char PcdWrite(u8 addr,u8 *p ) char status; u8 unLen; u8 i,ucComMF522BufMAXRLEN; ucComMF522Buf0 = PICC_WRITE; ucComMF522Buf1 = addr; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf2); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unL

31、en); if (status != MI_OK) | (unLen != 4) | (ucComMF522Buf0 & 0x0F) != 0x0A) status = MI_ERR; if (status = MI_OK) /memcpy(ucComMF522Buf, p , 16); for (i=0; i16; i+) ucComMF522Bufi = *(p +i); CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf16); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF52

32、2Buf,&unLen); if (status != MI_OK) | (unLen != 4) | (ucComMF522Buf0 & 0x0F) != 0x0A) status = MI_ERR; return status;/功 能:命令卡片进入休眠状态/返 回: 成功返回MI_OK/char PcdHalt(void) u8 status; u8 unLen; u8 ucComMF522BufMAXRLEN; ucComMF522Buf0 = PICC_HALT; ucComMF522Buf1 = 0; CalulateCRC(ucComMF522Buf,2,&ucComMF522B

33、uf2); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); return MI_OK;/用MF522计算CRC16函数/void CalulateCRC(u8 *pIn ,u8 len,u8 *pOut ) u8 i,n; ClearBitMask(DivIrqReg,0x04); WriteRawRC(CommandReg,PCD_IDLE); SetBitMask(FIFOLevelReg,0x80); for (i=0; ilen; i+) WriteRawRC(FIFODataReg,

34、 *(pIn +i); WriteRawRC(CommandReg, PCD_CALCCRC); i = 0xFF; do n = ReadRawRC(DivIrqReg); i-; while (i!=0) & !(n&0x04); pOut 0 = ReadRawRC(CRCResultRegL); pOut 1 = ReadRawRC(CRCResultRegM);/功 能:复位RC522/返 回: 成功返回MI_OK/char PcdReset(void)/PORTD|=(1RC522RST);SET_RC522RST; delay_ns(10);/PORTD&=(1RC522RST)

35、;CLR_RC522RST; delay_ns(10);/PORTD|=(1RC522RST);SET_RC522RST; delay_ns(10); WriteRawRC(CommandReg,PCD_RESETPHASE);WriteRawRC(CommandReg,PCD_RESETPHASE); delay_ns(10); WriteRawRC(ModeReg,0x3D); /和Mifare卡通讯,CRC初始值0x6363 WriteRawRC(TReloadRegL,30); WriteRawRC(TReloadRegH,0); WriteRawRC(TModeReg,0x8D); WriteRawRC(TPrescalerReg,0x3E);WriteRawRC(TxAutoReg,0x40);/必须要 return MI_OK;/设置RC632的工作方式 /char M500PcdConfigISOType(u8 type) if (type = A) /ISO14443_A ClearBitMask(Status2Reg,0x08); WriteRawRC(ModeRe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论