收藏 分享(赏)

FM17522-源代码.pdf

上传人:weiwoduzun 文档编号:5704215 上传时间:2019-03-13 格式:PDF 页数:24 大小:140.46KB
下载 相关 举报
FM17522-源代码.pdf_第1页
第1页 / 共24页
FM17522-源代码.pdf_第2页
第2页 / 共24页
FM17522-源代码.pdf_第3页
第3页 / 共24页
FM17522-源代码.pdf_第4页
第4页 / 共24页
FM17522-源代码.pdf_第5页
第5页 / 共24页
点击查看更多>>
资源描述

1、FM17522IC卡读卡器源码此文档一个包含3个文件,分别为fm17522_reg.h、fm17522.h、fm17522.cfm17522_reg.h为FM17522寄存器地址宏定义fm17522.h为FM17522源码头文件fm17522.c为FM17522源码源文件/*FM17522源码头文件*/#ifndef_FM17522_H#define_FM17522_H#include“stm32f2xx_hal.h“#include“spi_board.h“#include“fm17522_reg.h“/*FM17522通讯时返回的错误代码*/#defineFM17522_OK(0)#def

2、ineFM17522_NOTAGERR(-1)#defineFM17522_ERR(-2)/*MCUsystemclockfrequency*/#definecpu_freqSystemCoreClock/*delayservice*/#definefm17522_delay(ms)HAL_Delay(ms)/*FM17522IC-Card硬件接口定义,包括CS片选引脚的写电平、硬件SPI读写数据*/*FM17522CS-Pin写电平*/#definefm17522_cs_write_high()/spi2_cs_write_level(GPIO_PIN_SET)#definefm17522_

3、cs_write_low()/spi2_cs_write_level(GPIO_PIN_RESET)/*FM17522RESET-Pin写电平*/#definefm17522_reset_write_high()/HAL_GPIO_WritePin(IC_PWDN_GPIO_PORT,IC_PWDN_GPIO_PIN,GPIO_PIN_SET)#definefm17522_reset_write_low()/HAL_GPIO_WritePin(IC_PWDN_GPIO_PORT,IC_PWDN_GPIO_PIN,GPIO_PIN_RESET)/*FM17522IRQ-Pin读电平*/#defi

4、nefm17522_irq_read()/HAL_GPIO_ReadPin(IC_IRQ_GPIO_PORT,IC_IRQ_GPIO_PIN)/*briefFM17522写数据.*paramdata:写入的数据指针*paramsize:数据指针长度*retvalNone*/staticvoidfm17522_write_data(uint8_t*data,uint16_tsize)/*用户根据自己的接口修改对应的函数*/spi2_write_data(data,size);/*briefFM17522读数据.*paramdata:读入的数据指针*paramsize:数据指针长度*retvalN

5、one*/staticvoidfm17522_read_data(uint8_t*data,uint16_tsize)/*用户根据自己的接口修改对应的函数*/spi2_read_data(data,size);#endif/*_FM17522_H*/*FM17522寄存器地址宏定义*/#ifndef_FM17522_REG_H#define_FM17522_REG_H/*FM17522命令字*/#definePCD_IDLE0x00/取消当前命令#definePCD_AUTHENT0x0E/验证密钥#definePCD_RECEIVE0x08/接收数据#definePCD_TRANSMIT0x

6、04/发送数据#definePCD_TRANSCEIVE0x0C/发送并接收数据#definePCD_RESETPHASE0x0F/复位#definePCD_CALCCRC0x03/CRC计算/*Mifare_One卡片命令字*/#definePICC_REQIDL0x26/寻天线区内未进入休眠状态#definePICC_REQALL0x52/寻天线区内全部卡#definePICC_ANTICOLL10x93/防冲撞#definePICC_ANTICOLL20x95/防冲撞#definePICC_AUTHENT1A0x60/验证A密钥#definePICC_AUTHENT1B0x61/验证B密

7、钥#definePICC_READ0x30/读块#definePICC_WRITE0xA0/写块#definePICC_DECREMENT0xC0/扣款#definePICC_INCREMENT0xC1/充值#definePICC_RESTORE0xC2/调块数据到缓冲区#definePICC_TRANSFER0xB0/保存缓冲区中数据#definePICC_HALT0x50/休眠/*FM17522FIFO长度定义*/#defineDEF_FIFO_LENGTH64/FIFOsize=64byte/*FM17522寄存器地址定义*/*PAGE0*/#defineRFU000x00#define

8、CommandReg0x01#defineComIEnReg0x02#defineDivlEnReg0x03#defineComIrqReg0x04#defineDivIrqReg0x05#defineErrorReg0x06#defineStatus1Reg0x07#defineStatus2Reg0x08#defineFIFODataReg0x09#defineFIFOLevelReg0x0A#defineWaterLevelReg0x0B#defineControlReg0x0C#defineBitFramingReg0x0D#defineCollReg0x0E#defineRFU0F0

9、x0F/*PAGE1*/#defineRFU100x10#defineModeReg0x11#defineTxModeReg0x12#defineRxModeReg0x13#defineTxControlReg0x14#defineTxAutoReg0x15#defineTxSelReg0x16#defineRxSelReg0x17#defineRxThresholdReg0x18#defineDemodReg0x19#defineRFU1A0x1A#defineRFU1B0x1B#defineMifareReg0x1C#defineRFU1D0x1D#defineRFU1E0x1E#defi

10、neSerialSpeedReg0x1F/*PAGE2*/#defineRFU200x20#defineCRCResultRegM0x21#defineCRCResultRegL0x22#defineRFU230x23#defineModWidthReg0x24#defineRFU250x25#defineRFCfgReg0x26#defineGsNReg0x27#defineCWGsCfgReg0x28#defineModGsCfgReg0x29#defineTModeReg0x2A#defineTPrescalerReg0x2B#defineTReloadRegH0x2C#defineTR

11、eloadRegL0x2D#defineTCounterValueRegH0x2E#defineTCounterValueRegL0x2F/*PAGE3*/#defineRFU300x30#defineTestSel1Reg0x31#defineTestSel2Reg0x32#defineTestPinEnReg0x33#defineTestPinValueReg0x34#defineTestBusReg0x35#defineAutoTestReg0x36#defineVersionReg0x37#defineAnalogTestReg0x38#defineTestDAC1Reg0x39#de

12、fineTestDAC2Reg0x3A#defineTestADCReg0x3B#defineRFU3C0x3C#defineRFU3D0x3D#defineRFU3E0x3E#defineRFU3F0x3F#endif/*_FM17522_REG_H*/*FM17522源码源文件*/#include“fm17522.h“#defineMAXRLEN18/*函数申明*/intfm_pcd_reset(void);voidfm_pcd_antenna_on(void);voidfm_pcd_antenna_off(void);intfm_pcd_request_card(uint8_treq_c

13、ode,uint8_t*card_type);intfm_pcd_anti_collision(uint8_t*sn);intfm_pcd_select_card(uint8_t*sn);intfm_pcd_auth_key(uint8_tauth_mode,uint8_taddr,uint8_t*key,uint8_t*sn);intfm_pcd_read_card(uint8_taddr,uint8_t*data);intfm_pcd_write_card(uint8_taddr,uint8_t*data);intfm_pcd_halt(void);intfm_pcd_value_pay(

14、uint8_tdd_mode,uint8_taddr,uint8_t*value);intfm_pcd_value_backup(uint8_tsourceaddr,uint8_tgoaladdr);intfm_pcd_communicate(uint8_tcommand,uint8_t*data_in,uint8_tlength,uint8_t*data_out,uint16_t*length_bit);voidfm_crc_calculate(uint8_t*data_in,uint8_tlength,uint8_t*data_out);voidfm17522_clear_mask_bit

15、(uint8_treg,uint8_tmask);voidfm17522_set_mask_bit(uint8_treg,uint8_tmask);voidfm17522_write_register(uint8_taddress,uint8_tvalue);uint8_tfm17522_read_register(uint8_taddress);/*brief寻卡.*paramreq_code:寻卡方式*0x52=寻感应区内所有符合14443A标准的卡*0x26=寻未进入休眠状态的卡*paramcard_type:卡片类型代码*0x4400=Mifare_UltraLight*0x0400=

16、Mifare_One(S50)*0x0200=Mifare_One(S70)*0x0800=Mifare_Pro(X)*0x4403=Mifare_DESFire*retval成功返回状态FM17522_OK*/intfm_pcd_request_card(uint8_treq_code,uint8_t*card_type)intstatus;uint16_tunLen;uint8_tucComMF522BufMAXRLEN;fm17522_clear_mask_bit(Status2Reg,0x08);fm17522_write_register(BitFramingReg,0x07);fm

17、17522_set_mask_bit(TxControlReg,0x03);ucComMF522Buf0=req_code;status=fm_pcd_communicate(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,if(status=FM17522_OK)*(card_type+1)=ucComMF522Buf1;elsestatus=FM17522_ERR;returnstatus;/*brief防冲撞.*paramsn:卡片序列号,4个字节*retval成功返回状态FM17522_OK*/intfm_pcd_anti_collision(

18、uint8_t*sn)intstatus;uint8_ti;uint8_tsnr_check;uint16_tunLen;uint8_tucComMF522BufMAXRLEN;fm17522_clear_mask_bit(Status2Reg,0x08);fm17522_write_register(BitFramingReg,0x00);fm17522_clear_mask_bit(CollReg,0x80);ucComMF522Buf0=PICC_ANTICOLL1;ucComMF522Buf1=0x20;status=fm_pcd_communicate(PCD_TRANSCEIVE,

19、ucComMF522Buf,2,ucComMF522Buf,if(status=FM17522_OK)for(i=0;iMAXRLEN)n=MAXRLEN;for(i=0;in;i+)data_outi=fm17522_read_register(FIFODataReg);elsestatus=FM17522_ERR;/*stoptimernow*/fm17522_set_mask_bit(ControlReg,0x80);fm17522_write_register(CommandReg,PCD_IDLE);returnstatus;/*brief开启天线.*param:*retvalNon

20、e*NOTE:每次启动或关闭天险发射之间应至少有1ms的间隔*/voidfm_pcd_antenna_on(void)uint8_ttemp;temp=fm17522_read_register(TxControlReg);if(temp/*brief关闭天线.*paramNone*retvalNone*NOTE:每次启动或关闭天险发射之间应至少有1ms的间隔*/voidfm_pcd_antenna_off(void)fm17522_clear_mask_bit(TxControlReg,0x03);/*brief复位FM17522IC.*paramNone*retvalFM17522状态*/

21、intfm_pcd_reset(void)/*FM17522硬件复位*/fm17522_reset_write_low();fm17522_delay(100);fm17522_reset_write_high();fm17522_delay(100);fm17522_write_register(CommandReg,PCD_RESETPHASE);/*MIFARE卡通讯,CRC初始值:0x6363*/fm17522_write_register(ModeReg,0x3D);fm17522_write_register(TReloadRegL,30);fm17522_write_regist

22、er(TReloadRegH,0);fm17522_write_register(TModeReg,0x8D);fm17522_write_register(TPrescalerReg,0x3E);fm17522_write_register(TxAutoReg,0x40);returnFM17522_OK;/*brief置FM17522寄存器位.*paramreg:寄存器地址*parammask:清位值*retvalNone*/voidfm17522_set_mask_bit(uint8_treg,uint8_tmask)chartmp;tmp=fm17522_read_register(r

23、eg);/*setbitmask*/fm17522_write_register(reg,tmp|mask);/*brief清FM17522寄存器位.*paramreg:寄存器地址*parammask:清位值*retvalNone*/voidfm17522_clear_mask_bit(uint8_treg,uint8_tmask)chartemp;temp=fm17522_read_register(reg);/*clearbitmask*/fm17522_write_register(reg,temp/*FM17522IC-Card数据读写*/*briefFM17522写寄存器.*para

24、maddress:寄存器地址*paramvalue:寄存器值*retvalNone*/voidfm17522_write_register(uint8_taddress,uint8_tvalue)address=(address1)/*FM17522片选拉低*/fm17522_cs_write_low();/*FM17522写寄存器地址*/fm17522_write_data(uint8_t*)/*FM17522写寄存器数据*/fm17522_write_data(uint8_t*)/*FM17522片选拉高*/fm17522_cs_write_high();/*briefFM17522读寄存器.*paramaddress:寄存器地址*retvalvalue:寄存器值*/uint8_tfm17522_read_register(uint8_taddress)uint8_tvalue;address=(address1)/*FM17522片选拉低*/fm17522_cs_write_low();/*FM17522写寄存器地址*/fm17522_write_data(uint8_t*)/*FM17522读寄存器数据*/fm17522_read_data(uint8_t*)/*FM17522片选拉高*/fm17522_cs_write_high();returnvalue;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报