找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1553|回复: 1
打印 上一主题 下一主题
收起左侧

RC522读卡历程基于Arduino

[复制链接]
跳转到指定楼层
楼主
ID:379683 发表于 2019-7-1 22:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
RC522读卡历程基于Arduino
  1. /*
  2. * --------------------------------------------------------------------------------------------------------------------
  3. * Example to change UID of changeable MIFARE card.
  4. * --------------------------------------------------------------------------------------------------------------------
  5. * This is a MFRC522 library example; for further details and other examples see: [url]https://github.com/miguelbalboa/rfid[/url]
  6. *
  7. * This sample shows how to set the UID on a UID changeable MIFARE card.
  8. * NOTE: for more informations read the README.rst
  9. *
  10. * @author Tom Clement
  11. * @license Released into the public domain.
  12. *
  13. * Typical pin layout used:
  14. * -----------------------------------------------------------------------------------------
  15. *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
  16. *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
  17. * Signal      Pin          Pin           Pin       Pin        Pin              Pin
  18. * -----------------------------------------------------------------------------------------
  19. * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
  20. * SPI SS      SDA(SS)      10            53        D10        10               10
  21. * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
  22. * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
  23. * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
  24. */
  25.   /*
  26.        Name:    AccessSystem.ino
  27.        Created: 2016/7/10 12:22:07
  28.        Author:  Fing
  29.   */

  30.       #include <SoftwareSerial.h>
  31.       #include <Servo.h>
  32.       #include <MFRC522.h>
  33.       #include <SPI.h>

  34.       /*自定义RC522的两个PIN口*/
  35.       #define SS_PIN 10
  36.       #define RST_PIN 9

  37.       #define BT_RX 2   //蓝牙模块端口
  38.       #define BT_TX 1
  39.       #define SERVO_PIN 6   //舵机端口
  40.       #define BUZZ_PIN 4
  41.       #define CARDS 2   //已验证的卡片数目

  42.       MFRC522 mfrc522(SS_PIN, RST_PIN);   //新建RC522对象
  43.       SoftwareSerial BT(BT_TX, BT_RX);    //创建蓝牙软串口对象,避免冲突

  44.       Servo myservo;    //创建舵机对象
  45.       bool isAuthed = false;    //验证是否通过
  46.       const byte AuthedID[1][4] = {0x66,0xA8,0x14,0xF9};  //可以保存多个卡片UID值
  47.       char val;   //用来存储蓝牙接收数据

  48.       void setup() {
  49.         Serial.begin(9600);//串口,波特率9600
  50.         BT.begin(9600);   //蓝牙串口,波特率9600
  51.         myservo.attach(SERVO_PIN);    //连接舵机
  52.         SPI.begin();    //初始化SPI总线
  53.         mfrc522.PCD_Init();   //初始化MFRC522卡
  54.         pinMode(BUZZ_PIN, OUTPUT);    //初始化蜂鸣器
  55.         digitalWrite(BUZZ_PIN, HIGH);
  56.         myservo.write(45);
  57.         Serial.println(F("This code scan the MIFARE Classsic NUID."));
  58.         Serial.print(F("Using the following key:"));
  59.       }

  60.       // the loop function runs over and over again until power down or reset
  61.       void loop()
  62.       {
  63.         Authenticate();
  64.         if (isAuthed) {
  65.           OpenDoor();
  66.           BeepChecked();
  67.           delay(3000);
  68.           isAuthed = false;
  69.         }
  70.         CloseDoor();
  71.         isAuthed = false;
  72.         BTCheck();
  73.         delay(500);
  74.       }


  75.       //Servo开关门
  76.       void OpenDoor()
  77.       {
  78.         myservo.write(140);
  79.       }

  80.       void CloseDoor()
  81.       {
  82.         myservo.write(45);
  83.       }

  84.       //验证卡片
  85.       void Authenticate()
  86.       {
  87.         //检测是否有新卡片
  88.         if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
  89.           delay(50);
  90.           return;
  91.         }

  92.         //进行卡片验证
  93.         for (byte num = 0; num < CARDS; num++)    //卡片循环  
  94.         {      
  95.           byte i;
  96.           for (i = 0; i < mfrc522.uid.size; i++)
  97.           {           
  98.             if (mfrc522.uid.uidByte[i] != AuthedID[num][i]) break;
  99.           }
  100.           Serial.println(F("The NUID tag is:"));
  101.           Serial.print(F("In hex: "));
  102.           printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
  103.           Serial.println();
  104.           Serial.print(F("In dec: "));
  105.           printDec(mfrc522.uid.uidByte, mfrc522.uid.size);
  106.           Serial.println();
  107.           if (i == mfrc522.uid.size)
  108.           {
  109.             isAuthed = true;    //验证通过
  110.             break;
  111.           }
  112.           else if (num == CARDS - 1) {
  113.             BeepFailed();
  114.           }

  115.         }
  116.       }

  117.       //蓝牙扫描
  118.       void BTCheck() {
  119.         if (BT.available()) {
  120.           val = BT.read();
  121.           if (val == '1') {
  122.             OpenDoor();
  123.             BeepChecked();
  124.             delay(3000);
  125.             isAuthed = false;
  126.           }
  127.           val = 0;
  128.         }
  129.       }

  130.       void BeepChecked() {
  131.         digitalWrite(BUZZ_PIN, LOW);
  132.         delay(200);
  133.         digitalWrite(BUZZ_PIN, HIGH);
  134.       }

  135.       void BeepFailed() {
  136.         digitalWrite(BUZZ_PIN, LOW);
  137.         delay(100);
  138.         digitalWrite(BUZZ_PIN, HIGH);
  139.         delay(100);
  140.         digitalWrite(BUZZ_PIN, LOW);
  141.         delay(100);
  142.         digitalWrite(BUZZ_PIN, HIGH);
  143.       }
  144.       void printHex(byte *buffer, byte bufferSize) {
  145.       for (byte a = 0; a < bufferSize; a++) {
  146.             Serial.print(buffer[a] < 0x10 ? " 0" : " ");
  147.             Serial.print(buffer[a], HEX);
  148.           }
  149.         }

  150.         /**
  151.          * Helper routine to dump a byte array as dec values to Serial.
  152.          */
  153.        void printDec(byte *buffer, byte bufferSize) {
  154.        for (byte a = 0; a < bufferSize; a++) {
  155.             Serial.print(buffer[a] < 0x10 ? " 0" : " ");
  156.             Serial.print(buffer[a], DEC);
  157.           }
  158.         }
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2019-7-2 15:42 | 只看该作者
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表