RC522通过SPI方式读写M1卡

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1.准备工作战舰V3开发板,RC522模块为淘宝所买,赠送一张M1卡。SPI方式实测可以读写。2.RC522部分:rc522.h://///////////////////////////////////////////////////////////////////#includesys.h//MF522命令字/////////////////////////////////////////////////////////////////////#definePCD_IDLE0x00//取消当前命令#definePCD_AUTHENT0x0E//验证密钥#definePCD_RECEIVE0x08//接收数据#definePCD_TRANSMIT0x04//发送数据#definePCD_TRANSCEIVE0x0C//发送并接收数据#definePCD_RESETPHASE0x0F//复位#definePCD_CALCCRC0x03//CRC计算///////////////////////////////////////////////////////////////////////Mifare_One卡片命令字/////////////////////////////////////////////////////////////////////#definePICC_REQIDL0x26//寻天线区内未进入休眠状态#definePICC_REQALL0x52//寻天线区内全部卡#definePICC_ANTICOLL10x93//防冲撞#definePICC_ANTICOLL20x95//防冲撞#definePICC_AUTHENT1A0x60//验证A密钥#definePICC_AUTHENT1B0x61//验证B密钥#definePICC_READ0x30//读块#definePICC_WRITE0xA0//写块#definePICC_DECREMENT0xC0//扣款#definePICC_INCREMENT0xC1//充值#definePICC_RESTORE0xC2//调块数据到缓冲区#definePICC_TRANSFER0xB0//保存缓冲区中数据#definePICC_HALT0x50//休眠///////////////////////////////////////////////////////////////////////MF522FIFO长度定义/////////////////////////////////////////////////////////////////////#defineDEF_FIFO_LENGTH64//FIFOsize=64byte#defineMAXRLEN18///////////////////////////////////////////////////////////////////////MF522寄存器定义///////////////////////////////////////////////////////////////////////PAGE0#defineRFU000x00#defineCommandReg0x01#defineComIEnReg0x02#defineDivlEnReg0x03#defineComIrqReg0x04#defineDivIrqReg0x05#defineErrorReg0x06#defineStatus1Reg0x07#defineStatus2Reg0x08#defineFIFODataReg0x09#defineFIFOLevelReg0x0A#defineWaterLevelReg0x0B#defineControlReg0x0C#defineBitFramingReg0x0D#defineCollReg0x0E#defineRFU0F0x0F//PAGE1#defineRFU100x10#defineModeReg0x11#defineTxModeReg0x12#defineRxModeReg0x13#defineTxControlReg0x14#defineTxAutoReg0x15#defineTxSelReg0x16#defineRxSelReg0x17#defineRxThresholdReg0x18#defineDemodReg0x19#defineRFU1A0x1A#defineRFU1B0x1B#defineMifareReg0x1C#defineRFU1D0x1D#defineRFU1E0x1E#defineSerialSpeedReg0x1F//PAGE2#defineRFU200x20#defineCRCResultRegM0x21#defineCRCResultRegL0x22#defineRFU230x23#defineModWidthReg0x24#defineRFU250x25#defineRFCfgReg0x26#defineGsNReg0x27#defineCWGsCfgReg0x28#defineModGsCfgReg0x29#defineTModeReg0x2A#defineTPrescalerReg0x2B#defineTReloadRegH0x2C#defineTReloadRegL0x2D#defineTCounterValueRegH0x2E#defineTCounterValueRegL0x2F//PAGE3#defineRFU300x30#defineTestSel1Reg0x31#defineTestSel2Reg0x32#defineTestPinEnReg0x33#defineTestPinValueReg0x34#defineTestBusReg0x35#defineAutoTestReg0x36#defineVersionReg0x37#defineAnalogTestReg0x38#defineTestDAC1Reg0x39#defineTestDAC2Reg0x3A#defineTestADCReg0x3B#defineRFU3C0x3C#defineRFU3D0x3D#defineRFU3E0x3E#defineRFU3F0x3F///////////////////////////////////////////////////////////////////////和MF522通讯时返回的错误代码/////////////////////////////////////////////////////////////////////#defineMI_OK0#defineMI_NOTAGERR(1)#defineMI_ERR(2)#defineSHAQU10X01#defineKUAI40X04#defineKUAI70X07#defineREGCARD0xa1#defineCONSUME0xa2#defineREADCARD0xa3#defineADDMONEY0xa4#defineSPIReadByte()SPIWriteByte(0)u8SPIWriteByte(u8byte);voidSPI2_Init(void);#defineSET_SPI_CSGPIO_SetBits(GPIOF,GPIO_Pin_0);#defineCLR_SPI_CSGPIO_ResetBits(GPIOF,GPIO_Pin_0)#defineSET_RC522RSTGPIO_SetBits(GPIOF,GPIO_Pin_1);#defineCLR_RC522RSTGPIO_ResetBits(GPIOF,GPIO_Pin_1)voidInitRc522(void);voidUsart1_Send_String(unsignedchar*str);voidClearBitMask(u8reg,u8mask);voidWriteRawRC(u8Address,u8value);voidSetBitMask(u8reg,u8mask);charPcdComMF522(u8Command,u8*pIn,u8InLenByte,u8*pOut,u8*pOutLenBit);voidCalulateCRC(u8*pIn,u8len,u8*pOut);u8ReadRawRC(u8Address);voidPcdAntennaOn(void);charPcdReset(void);charPcdRequest(unsignedcharreq_code,unsignedchar*pTagType);voidPcdAntennaOn(void);voidPcdAntennaOff(void);charM500PcdConfigISOType(unsignedchartype);charPcdAnticoll(unsignedchar*pSnr);charPcdSelect(unsignedchar*pSnr);charPcdAuthState(unsignedcharauth_mode,unsignedcharaddr,unsignedchar*pKey,unsignedchar*pSnr);charPcdWrite(unsignedcharaddr,unsignedchar*pData);charPcdRead(unsignedcharaddr,unsignedchar*pData);charPcdHalt(void);voidReset_RC522(void);charPcdValue(u8dd_mode,u8addr,u8*pValue);//////////////////////////////////////////////////////////////////////////////////////////////////////rc522.c:#includesys.h#includerc522.h#includedelay.hvoiddelay_ns(u32ns){u32i;for(i=0;ins;i++){__nop();__nop();__nop();}}u8SPIWriteByte(u8Byte){while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);SPI_I2S_SendData(SPI2,Byte);while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);returnSPI_I2S_ReceiveData(SPI2);}//SPIx读写一个字节//TxData:要写入的字节//返回值:读取到的字节u8SPI2_ReadWriteByte(u8TxData){u8retry=0;while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET)//检查指定的SPI标志位设置与否:发送缓存空标志位{retry++;if(retry200)return0;}SPI_I2S_SendData(SPI2,TxData);//通过外设SPIx发送一个数据retry=0;while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET)//检查指定的SPI标志位设置与否:接受缓存非空标志位{retry++;if(retry200)return0;}returnSPI_I2S_ReceiveData(SPI2);//返回通过SPIx最近接收的数据}voidSPI2_Init(void){GPIO_InitTypeDefGPIO_InitStructure;SPI_InitTypeDefSPI_I

1 / 22
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功