9.2-230 安装RFID射频识别传感器库_第1页
9.2-230 安装RFID射频识别传感器库_第2页
9.2-230 安装RFID射频识别传感器库_第3页
9.2-230 安装RFID射频识别传感器库_第4页
9.2-230 安装RFID射频识别传感器库_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

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

文档简介

安装RFID射频识别库

2硬件接线1安装MFRC522库RFID示例程序解析2目录PART.1安装库RC522库安装RC522库安装PART.2示例程序解析函数的理解MFRC522程序主要函数:MFRC522物件.PCD_Init():初始化MFRC522读卡器模块MFRC522物件.PICC_IsNewCardPresent():是否感应到新的卡片MFRC522物件.PICC_ReadCardSerial():读取卡片的资料MFRC522物件.PICC_GetType():取得卡片类型MFRC522物件.PICC_GetTypeName():取得卡片类型名称当读写器读到卡片的资料之后,UID的长度和内容,可从底下两个属性值取得:MFRC522物件.uid.size:包含UID的长度MFRC522物件.uid.uidByte:包含UID码的阵列读卡操作过程Mifare的关键词按感应距离分类紧耦合(close-coupled,需要紧贴感测器)接近式(proximity,10cm以內)邻近式(vicinity,50cm以內)PICC(ProximityICCard)接近式IC卡PCD(ProximityCouplingDevice)接近型耦合器非接触型IC卡12示例1-Dumplnfo#include<SPI.h>#include<MFRC522.h>constexpruint8_tRST_PIN=9;//Configurable,seetypicalpinlayoutaboveconstexpruint8_tSS_PIN=10;//Configurable,seetypicalpinlayoutaboveMFRC522mfrc522(SS_PIN,RST_PIN);//CreateMFRC522instance示例1-Dumplnfovoidsetup(){ Serial.begin(9600); //InitializeserialcommunicationswiththePC

while(!Serial); //Donothingifnoserialportisopened(addedforArduinosbasedonATMEGA32U4) SPI.begin(); //InitSPIbus mfrc522.PCD_Init(); //InitMFRC522 mfrc522.PCD_DumpVersionToSerial(); //ShowdetailsofPCD-MFRC522CardReaderdetails Serial.println(F("ScanPICCtoseeUID,SAK,type,anddatablocks..."));}14示例1-Dumplnfo voidloop(){ //Lookfornewcards if(!mfrc522.PICC_IsNewCardPresent()){ return; } //Selectoneofthecards if(!mfrc522.PICC_ReadCardSerial()){ return; } //Dumpdebuginfoaboutthecard;PICC_HaltA()isautomaticallycalled mfrc522.PICC_DumpToSerial(&(mfrc522.uid));}15示例1-Dumplnfo串口监视器观察卡内容卡内块分布17示例2-ReadNUID18示例2-ReadNUID#include<SPI.h>#include<MFRC522.h>constexpruint8_tRST_PIN=9;//Configurable,seetypicalpinlayoutaboveconstexpruint8_tSS_PIN=10;//Configurable,seetypicalpinlayoutabove

MFRC522rfid(SS_PIN,RST_PIN);//InstanceoftheclassMFRC522::MIFARE_Keykey;//InitarraythatwillstorenewNUIDbytenuidPICC[4];19示例2-ReadNUIDvoidsetup(){Serial.begin(9600);SPI.begin();//InitSPIbusrfid.PCD_Init();//InitMFRC522for(bytei=0;i<6;i++){key.keyByte[i]=0xFF;}Serial.println(F("ThiscodescantheMIFAREClasssicNUID."));Serial.print(F("Usingthefollowingkey:"));printHex(key.keyByte,MFRC522::MF_KEY_SIZE);}20示例2-ReadNUIDvoidloop(){//Lookfornewcardsif(!rfid.PICC_IsNewCardPresent())return;//VerifyiftheNUIDhasbeenreadedif(!rfid.PICC_ReadCardSerial())return;Serial.print(F("PICCtype:"));MFRC522::PICC_TypepiccType=rfid.PICC_GetType(rfid.uid.sak);Serial.println(rfid.PICC_GetTypeName(piccType));//CheckisthePICCofClassicMIFAREtypeif(piccType!=MFRC522::PICC_TYPE_MIFARE_MINI&&piccType!=MFRC522::PICC_TYPE_MIFARE_1K&&piccType!=MFRC522::PICC_TYPE_MIFARE_4K){Serial.println(F("YourtagisnotoftypeMIFAREClassic."));return;}if(rfid.uid.uidByte[0]!=nuidPICC[0]||rfid.uid.uidByte[1]!=nuidPICC[1]||rfid.uid.uidByte[2]!=nuidPICC[2]||rfid.uid.uidByte[3]!=nuidPICC[3]){Serial.println(F("Anewcardhasbeendetected."));//StoreNUIDintonuidPICCarrayfor(bytei=0;i<4;i++){nuidPICC[i]=rfid.uid.uidByte[i];}

Serial.println(F("TheNUIDtagis:"));Serial.print(F("Inhex:"));printHex(rfid.uid.uidByte,rfid.uid.size);Serial.println();Serial.print(F("Indec:"));printDec(rfid.uid.uidByte,rfid.uid.size);Serial.println();}elseSerial.println(F("Cardreadpreviously."));//HaltPICCrfid.PICC_HaltA();//StopencryptiononPCDrfid.PCD_StopCrypto1();}/***HelperroutinetodumpabytearrayashexvaluestoSerial.*/voidprintHex(byte*buffer,bytebufferSize){for(bytei=0;i<bufferSize;i++){Serial.print(buffer[i]<0x10?"0":"");Serial.print(buffer[i],HEX);}}/***HelperroutinetodumpabytearrayasdecvaluestoSerial.*/voidprintDec(byte*buffer,bytebufferSize){for(bytei=0;i<bufferSize;i++){Serial.print(buffer[i]<0x10?"0":"");Serial.print(buffer[i],DEC);}}21示例2-ReadNUID//CheckisthePICCofClassicMIFAREtypeif(piccType!=MFRC522::PICC_TYPE_MIFARE_MINI&&piccType!=MFRC522::PICC_TYPE_MIFARE_1K&&piccType!=MFRC522::PICC_TYPE_MIFARE_4K){Serial.println(F("YourtagisnotoftypeMIFAREClassic."));return;}if(rfid.uid.uidByte[0]!=nuidPICC[0]||rfid.uid.uidByte[1]!=nuidPICC[1]||rfid.uid.uidByte[2]!=nuidPICC[2]||rfid.uid.uidByte[3]!=nuidPICC[3]){Serial.println(F("Anewcardhasbeendetected."));//StoreNUIDintonuidPICCarrayfor(bytei=0;i<4;i++){nuidPICC[i]=rfid.uid.uidByte[i];}

Serial.println(F("TheNUIDtagis:"));Serial.print(F("Inhex:"));printHex(rfid.uid.uidByte,rfid.uid.size);Serial.println();Serial.print(F("Indec:"));printDec(rfid.uid.uidByte,rfid.uid.size);Serial.println();}elseSerial.println(F("Cardreadpreviously."));//HaltPICCrfid.PICC_HaltA();//StopencryptiononPCDrfid.PCD_StopCrypto1();}/***HelperroutinetodumpabytearrayashexvaluestoSerial.*/voidprintHex(byte*buffer,bytebufferSize){for(bytei=0;i<bufferSize;i++){Serial.print(buffer[i]<0x10?"0":"");Serial.print(buffer[i],HEX);}}/***HelperroutinetodumpabytearrayasdecvaluestoSerial.*/voidprintDec(byte*buffer,bytebufferSize){for(bytei=0;i<bufferSize;i++){Serial.print(buffer[i]<0x10?"0":"");Serial.print(buffer[i],DEC);}}22示例2-ReadNUID//StoreNUIDintonuidPICCarrayfor(bytei=0;i<4;i++){nuidPICC[i]=rfid.uid.uidByte[i];}

Serial.println(F("TheNUIDtagis:"));Serial.print(F("Inhex:"));printHex(rfid.uid.uidByte,rfid.uid.size);Serial.println();Serial.print(F("Indec:"));printDec(rfid.uid.uidByte,rfid.uid.size);Serial.println();}elseSerial.println(F("Cardreadpreviously."));//HaltPICCrfid.PICC_HaltA();//StopencryptiononPCDrfid.PCD_StopCrypto1();}/***HelperroutinetodumpabytearrayashexvaluestoSerial.*/voidprintHex(byte*buffer,bytebufferSize){for(bytei=0;i<bufferSize;i++){Serial.print(buffer[i]<0x10?"0":"");Serial.print(buffer[i],HEX);}}/***HelperroutinetodumpabytearrayasdecvaluestoSerial.*/voidprintDec(byte*buffer,bytebufferSize){for(bytei=0;i<bufferSize;i++){Serial.print(buffer[i]<0x10?"0":"");Serial.print(buffer[i],DEC);}}23示例3-ReadNUIDelseSerial.println(F("Cardreadpreviously."));//HaltPICCrfid.PICC_HaltA();//StopencryptiononPCDrfid.PCD_StopCrypto1();}/***HelperroutinetodumpabytearrayashexvaluestoSerial.*/voidprintHex(byte*buffer,bytebufferSize){for(bytei=0;i<bufferSize;i++){S

温馨提示

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

评论

0/150

提交评论