已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/*-.H文件-*/#ifndef _RC522_H_#define _RC522_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 0x03 /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_DECREMENT 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 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/ 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 RFU1D 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 0x2A#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 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/和MF522通讯时返回的错误代码/#define MI_OK 0#define MI_NOTAGERR (-1)#define MI_ERR (-2)#defineSHAQU10X01#defineKUAI40X04#defineKUAI70X07#defineREGCARD0xa1#defineCONSUME0xa2#define READCARD0xa3#define ADDMONEY0xa4/void delay_ns(uint16 ns);void delay10us(void);void delay1ms(void);uint8 SPIReadByte(void);void SPIWriteByte(uint8 SPIData);char PcdRequest(uint8 req_code,uint8 *pTagType);char PcdAnticoll(uint8 *pSnr);char PcdSelect(unsigned char *pSnr);char PcdAuthState(uint8 auth_mode,uint8 addr,uint8 *pKey,uint8 *pSnr);char PcdRead(uint8 addr,uint8 *pData);char PcdWrite(uint8 addr,uint8 *pData);char PcdHalt(void);void CalulateCRC(uint8 *pIndata,uint8 len,uint8 *pOutData);char PcdReset(void);char M500PcdConfigISOType(uint8 type);uint8 ReadRawRC(uint8 Address);void WriteRawRC(uint8 Address, uint8 value);void SetBitMask(uint8 reg,uint8 mask);void ClearBitMask(uint8 reg,uint8 mask);char PcdComMF522(uint8 Command,uint8 *pInData,uint8 InLenByte,uint8 *pOutData,uint16 *pOutLenBit);void PcdAntennaOn(void);void PcdAntennaOff(void);void InitRc522(void);void delay1ms500us(void);sbit SPI_CS = P13;sbit SPI_CK = P14;sbit SPI_MOSI = P15;sbit SPI_MISO = P16;sbit SPI_RST = P17;#define SET_SPI_CS SPI_CS=1#define CLR_SPI_CS SPI_CS=0#define SET_SPI_CK SPI_CK=1#define CLR_SPI_CK SPI_CK=0#define SET_SPI_MOSI SPI_MOSI=1#define CLR_SPI_MOSI SPI_MOSI=0#define SET_SPI_MISO SPI_MISO#define SET_RC522RST SPI_RST=1#define CLR_RC522RST SPI_RST=0#endif/*-.c文件-*/#include rc522.h/*void delay_ns(uint16 ns) uint16 xdata i; for(i=0;i0;b-) for(a=2;a0;a-);void delay1ms(void) /误差 0us unsigned char a,b,c; for(c=1;c0;c-) for(b=142;b0;b-) for(a=2;a0;a-);void delay1ms500us(void) /误差 0us uint8 xdata a,b; for(b=115;b0;b-) for(a=5;a0;a-);uint8 SPIReadByte(void) uint8 xdata SPICount; / Counter used to clock out the data uint8 xdata SPIData; SPIData = 0; for (SPICount = 0; SPICount 8; SPICount+) / Prepare to clock in the data to be read SPIData =1; / Rotate the data CLR_SPI_CK; nop();nop(); / Raise the clock to clock the data out of the MAX7456 if(SET_SPI_MISO) SPIData|=0x01; SET_SPI_CK; nop();nop(); / Drop the clock ready for the next bit / and loop back return (SPIData); / Finally return the read data void SPIWriteByte(uint8 SPIData) uint8 xdata SPICount; / Counter used to clock out the data for (SPICount = 0; SPICount 8; SPICount+) if (SPIData & 0x80) SET_SPI_MOSI; else CLR_SPI_MOSI; nop();nop(); CLR_SPI_CK;nop();nop(); SET_SPI_CK;nop();nop(); SPIData = 1; /功 能:寻卡/参数说明: req_codeIN:寻卡方式/ 0x52 = 寻感应区内所有符合14443A标准的卡/ 0x26 = 寻未进入休眠状态的卡/ pTagTypeOUT:卡片类型代码/ 0x4400 = Mifare_UltraLight/ 0x0400 = Mifare_One(S50)/ 0x0200 = Mifare_One(S70)/ 0x0800 = Mifare_Pro(X)/ 0x4403 = Mifare_DESFire/返 回: 成功返回MI_OK/char PcdRequest(uint8 req_code,uint8 *pTagType)char xdata status; uint16 xdata unLen;uint8 xdata ucComMF522BufMAXRLEN; ClearBitMask(Status2Reg,0x08);WriteRawRC(BitFramingReg,0x07);SetBitMask(TxControlReg,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 PcdAnticoll(uint8 *pSnr) char xdata status; uint8 xdata i,snr_check=0; uint16 xdata unLen; uint8 xdata ucComMF522BufMAXRLEN; ClearBitMask(Status2Reg,0x08); WriteRawRC(BitFramingReg,0x00); ClearBitMask(CollReg,0x80); ucComMF522Buf0 = PICC_ANTICOLL1; ucComMF522Buf1 = 0x20; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen); 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(uint8 *pSnr) char xdata status; uint8 xdata i; uint16 xdata unLen; uint8 xdata ucComMF522BufMAXRLEN; 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,ucComMF522Buf,&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(uint8 auth_mode,uint8 addr,uint8 *pKey,uint8 *pSnr) char xdata status; uint16 xdata unLen; uint8 xdata 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,ucComMF522Buf,12,ucComMF522Buf,&unLen); if (status != MI_OK) | (!(ReadRawRC(Status2Reg) & 0x08) status = MI_ERR; return status;/功 能:读取M1卡一块数据/参数说明: addrIN:块地址/ pDataOUT:读出的数据,16字节/返 回: 成功返回MI_OK/ char PcdRead(uint8 addr,uint8 *pData) char xdata status; uint16 xdata unLen; uint8 xdata i,ucComMF522BufMAXRLEN; ucComMF522Buf0 = PICC_READ; ucComMF522Buf1 = addr; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf2); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); if (status = MI_OK) & (unLen = 0x90) / memcpy(pData, ucComMF522Buf, 16); for (i=0; i16; i+) *(pData+i) = ucComMF522Bufi; else status = MI_ERR; return status;/功 能:写数据到M1卡一块/参数说明: addrIN:块地址/ pDataIN:写入的数据,16字节/返 回: 成功返回MI_OK/ char PcdWrite(uint8 addr,uint8 *pData) char xdata status; uint16 xdata unLen; uint8 xdata i,ucComMF522BufMAXRLEN; ucComMF522Buf0 = PICC_WRITE; ucComMF522Buf1 = addr; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf2); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); if (status != MI_OK) | (unLen != 4) | (ucComMF522Buf0 & 0x0F) != 0x0A) status = MI_ERR; if (status = MI_OK) /memcpy(ucComMF522Buf, pData, 16); for (i=0; i16; i+) ucComMF522Bufi = *(pData+i); CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf16); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen); if (status != MI_OK) | (unLen != 4) | (ucComMF522Buf0 & 0x0F) != 0x0A) status = MI_ERR; return status;/功 能:命令卡片进入休眠状态/返 回: 成功返回MI_OK/char PcdHalt(void) char xdata status; uint16 xdata unLen; uint8 xdata ucComMF522BufMAXRLEN; ucComMF522Buf0 = PICC_HALT; ucComMF522Buf1 = 0; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf2); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); return MI_OK;/用MF522计算CRC16函数/void CalulateCRC(uint8 *pIndata,uint8 len,uint8 *pOutData) uint8 xdata i,n; ClearBitMask(DivIrqReg,0x04); WriteRawRC(CommandReg,PCD_IDLE); SetBitMask(FIFOLevelReg,0x80); for (i=0; ilen; i+) WriteRawRC(FIFODataReg, *(pIndata+i); WriteRawRC(CommandReg, PCD_CALCCRC); i = 0xFF; do n = ReadRawRC(DivIrqReg); i-; while (i!=0) & !(n&0x04); pOutData0 = ReadRawRC(CRCResultRegL); pOutData1 = ReadRawRC(CRCResultRegM);/功 能:复位RC522/返 回: 成功返回MI_OK/char PcdReset(void)/PORTD|=(1RC522RST);SET_RC522RST; delay10us();/PORTD&=(1RC522RST);CLR_RC522RST; delay10us();/PORTD|=(1RC522RST);SET_RC522RST; delay10us(); WriteRawRC(CommandReg,PCD_RESETPHASE); delay10us(); 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(uint8 type) if (type = A) /ISO14443_A ClearBitMask(Status2Reg,0x08); WriteRawRC(ModeReg,0x3D);/3F WriteRawRC(RxSelReg,0x86);/84 WriteRawRC(RFCfgReg,0x7F); /4F WriteRawRC(TReloadRegL,30);/tmoLength);/ TReloadVal = h6a =tmoLength(dec) WriteRawRC(TReloadRegH,0); WriteRawRC(TModeReg,0x8D); WriteRawRC(TPrescalerReg,0x3E); delay1ms(); PcdAntennaOn(); else return -1; return MI_OK;/功 能:读RC632寄存器/参数说明:AddressIN:寄存器地址/返 回:读出的值/uint8 ReadRawRC(uint8 Address) uint8 xdata ucAddr; uint8 xdata ucResult=0;CLR_SPI_CS; ucAddr = (Address1)&0x7E)|0x80;SPIWriteByte(ucAddr);ucResult=SPIReadByte();SET_SPI_CS; return ucResult;/功 能:写RC632寄存器/参数说明:AddressIN:寄存器地址/ valueIN:写入的值/void WriteRawRC(uint8 Address, uint8 value) uint8 xdata ucAddr;CLR_SPI_CS; ucAddr = (Address1)&0x7E);SPIWriteByte(ucAddr);SPIWriteByte(value);SET_SPI_CS;/功 能:置RC522寄存器位/参数说明:regIN:寄存器地址/ maskIN:置位值/void SetBitMa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 杭州汽车租赁管理制度
- 冷库地膜施工方案
- 甲醇的营销方案
- Unit6课时7ReadingforWriting(教学评一体教学设计)-仁爱科普版2024八年级上册英语
- Unit3Foodandethics单词表英汉互译背诵版-高中英语沪教版选择性
- 语文中考议论文观点的提炼专题复习教案
- 中班有趣的弹性教案
- 冀教版数学一下《以内数的读写法》教案
- 五年级信息技术上册第课聆听美妙乐曲教案浙江摄影版
- 九年级数学下册二次函数全章教案人教新课标版
- T-ZZB 3679-2024 汽车用热塑性弹性体(TPE)脚垫
- 2024年河北省普通高中学业水平选择性考试物理试题含答案
- DB65-T 4874-2024 公路除冰雪作业技术规范
- 《眩晕诊断与治疗》课件
- 从“小众运动”到“全民热潮”解码中国网球人群与市场机遇
- 2024-2025学年苏科版七年级数学上学期期末必刷压轴80题(15个考点专练)含答案
- 《智能网联汽车用摄像头硬件性能要求及试验方法》
- 电影音乐欣赏知到智慧树章节测试课后答案2024年秋华南农业大学
- 专题03 基本不等式(期末压轴专项训练20题)(解析版)-25学年高一数学上学期期末考点大串讲(人教A版必修一)
- 《工业废水臭氧催化氧化深度处理技术规程》(T-SDEPI 030-2022)
- 2024-2025学年湖北省武汉市武昌区武珞路中学八年级(上)期中数学试卷
评论
0/150
提交评论