久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 11623|回復: 10
打印 上一主題 下一主題
收起左側

RC522的讀寫卡程序,可以讀寫1~16扇區的任意數據塊

  [復制鏈接]
跳轉到指定樓層
樓主
ID:173447 發表于 2018-8-8 15:58 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
//讀寫扇區的例子:
#include <SPI.h>
#include <RFID.h>

RFID rfid(53,49);    //D10--讀卡器MOSI引腳、D5--讀卡器RST引腳

//4字節卡序列號,第5字節為校驗字節
unsigned char serNum[5];
//寫卡數據,3為數據塊012,16為長度
unsigned char writeDate[3][16] ={
{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '1', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
};
//原扇區A密碼,16個扇區,每個扇區密碼6Byte
unsigned char sectorKeyA[16][16] = {
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//0~16扇區原密碼
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},


        };
//新扇區A密碼,16個扇區,每個扇區密碼6Byte,一行代表一個扇區
//改密碼只要改前面6個ff任意一個, 0xff,0x07,0x80,0x69, 千萬不能動否則可能報廢扇區
unsigned char sectorNewKeyA[16][16] = {
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//1-16扇區新密碼
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},



        };
//char aa[16];
void setup()
{
  Serial.begin(9600);
  SPI.begin();
  rfid.init();
}

void loop()
{
  //unsigned char i,tmp;//i為數據塊號0.1.2
  unsigned tmp;
  unsigned char status;
  unsigned char str[MAX_LEN];
// unsigned char RC_size;
  unsigned char blockAddr;        //選擇操作的塊地址0~63。但只能寫4的倍數+3;比如7,11,15,19---

  //找卡
  rfid.isCard();

  //讀取卡序列號
  if (rfid.readCardSerial())
  {
    Serial.print("The card's number is  : ");
    Serial.print(rfid.serNum[0],HEX);
    Serial.print(rfid.serNum[1],HEX);
    Serial.print(rfid.serNum[2],HEX);
    Serial.print(rfid.serNum[3],HEX);
    Serial.print(rfid.serNum[4],HEX);
    Serial.println(" ");
  }

  //選卡,返回卡容量(鎖定卡片,防止多次讀寫)
rfid.selectTag(rfid.serNum);



  //寫數據卡
//  for(int i=0;i<3;i++)//循環寫入數據塊012,不循環就單獨寫i值
//    {
//  blockAddr = 15;                //扇區3,控制塊3
//  if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[ blockAddr/4], rfid.serNum) == MI_OK)  //認證
//  {
////    寫密碼
//    status = rfid.write(blockAddr, sectorNewKeyA[blockAddr/4]);   
//    //寫數據
//   
//    blockAddr = blockAddr - 3+i; //數據塊i
//    tmp=(blockAddr+3-i)/4;
//    status = rfid.write(blockAddr, writeDate[i]);
//    if(status == MI_OK)
//    {
//      
//      Serial.print("扇區:");
//      Serial.print(tmp,DEC);
//      Serial.print("數據塊:");
//      Serial.println(i);
//      Serial.println("Write card OK!");
//    }
//  }
//    }




  //讀卡
  for(int i=2;i<3;i++)
    {
  blockAddr = 15;                ////扇區3,控制塊3
  status = rfid.auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], rfid.serNum);
  if (status == MI_OK)  //認證
  {
    //讀數據
    blockAddr = blockAddr - 3+i;
    if( rfid.read(blockAddr, str) == MI_OK)
    {
      Serial.print("Read from the card ,the data is : ");
      Serial.println((char *)str);
    Serial.print("扇區:");
    Serial.print(blockAddr/4,DEC);
    Serial.print("數據塊:");
    Serial.println(i);
    }
    }
  }


  rfid.halt();//去掉本行連續讀寫

}



//驗證密碼+數據后,再對arduino設定的sj進行減5操作
#include <SPI.h>
#include <RFID.h>

RFID rfid(53,49);    //D10--讀卡器MOSI引腳、D5--讀卡器RST引腳

//4字節卡序列號,第5字節為校驗字節
unsigned char serNum[5];
//寫卡數據
unsigned char writeDate[16]={'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 0, 0};
unsigned char aa[16]=        {'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 0, 0};
char dy;
unsigned int sj=1000;
//原扇區A密碼,16個扇區,每個扇區密碼6Byte
unsigned char sectorKeyA[16][16] = {
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},

// {0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
// {0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
// {0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
        };
//新扇區A密碼,16個扇區,每個扇區密碼6Byte
//改密碼只要改第3行前面6個ff任意一個, 0xff,0x07,0x80,0x69, 千萬不能動負責報廢扇區
unsigned char sectorNewKeyA[16][16] = {
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//讀改密碼只要改前面6個ff任意一個
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//寫改密碼只要改前面6個ff任意一個
        };

void setup()
{
  Serial.begin(9600);
  SPI.begin();
  rfid.init();
}

void loop()
{
  unsigned char i,tmp;
  unsigned char status;
  unsigned char str[MAX_LEN];
  unsigned char RC_size;
  unsigned char blockAddr;        //選擇操作的塊地址0~63

  //找卡
  rfid.isCard();
  //讀取卡序列號
  if (rfid.readCardSerial())
  {
    Serial.print("The card's number is  : ");
    Serial.print(rfid.serNum[0],HEX);
    Serial.print(rfid.serNum[1],HEX);
    Serial.print(rfid.serNum[2],HEX);
    Serial.print(rfid.serNum[3],HEX);
    Serial.print(rfid.serNum[4],HEX);
    Serial.println(" ");
  }

  //選卡,返回卡容量(鎖定卡片,防止多次讀寫)
  rfid.selectTag(rfid.serNum);

  //寫數據卡
  blockAddr = 7;                //數據塊7//報廢扇區2,1,16
  if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[ blockAddr/4], rfid.serNum) == MI_OK)  //認證
  {
//    寫數據
    status = rfid.write(blockAddr, sectorNewKeyA[blockAddr/4]);
    Serial.print("set the new card password, and can modify the data of the Sector: ");
    Serial.println(blockAddr/4,DEC);
    //寫數據
    blockAddr = blockAddr - 3 ; //數據塊4
    status = rfid.write(blockAddr, writeDate);
    if(status == MI_OK)
    {
      Serial.println("Write card OK!");
    }
  }

  //讀卡
  blockAddr = 7;               
  status = rfid.auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], rfid.serNum);
  if (status == MI_OK)  //認證
  {
    //讀數據
    blockAddr = blockAddr - 3 ;
    if( rfid.read(blockAddr, str) == MI_OK)
    {
      Serial.print("Read from the card ,the data is : ");
      Serial.println((char *)str);
for(int a=0;a<15;a++)
{
  dy=1;
  if(aa[a]!=str[a])
  {
dy=0;
break;

  }
}
if(dy==1)
{
  sj-=5;
  Serial.println(sj);
}
    }
  }


  rfid.halt();
}
















RFID.zip

9.02 KB, 下載次數: 146, 下載積分: 黑幣 -5

評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏10 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:157346 發表于 2018-9-5 16:28 | 只看該作者
怎么那個庫函數一直添加不了
回復

使用道具 舉報

板凳
ID:257260 發表于 2019-12-13 15:37 | 只看該作者
好資料
回復

使用道具 舉報

地板
ID:893675 發表于 2021-4-5 22:28 | 只看該作者
是stc89c52能用的嗎
回復

使用道具 舉報

5#
ID:838368 發表于 2021-4-26 21:37 | 只看該作者

兄弟,我也想知道這是用什么板子控制的,你知道了能否也告訴一下我啊,非常感謝!
回復

使用道具 舉報

6#
ID:3802 發表于 2021-6-17 15:48 | 只看該作者

是stc89c52能用的嗎
回復

使用道具 舉報

7#
ID:964308 發表于 2021-9-11 09:54 來自手機 | 只看該作者
水煮涼白開 發表于 2021-4-5 22:28
是stc89c52能用的嗎

不可以,語言不一樣
回復

使用道具 舉報

8#
ID:966423 發表于 2021-9-16 09:33 | 只看該作者
RC522初始化語句太多了,有沒有辦法減小點,減少休眠喚醒時間
回復

使用道具 舉報

9#
ID:235200 發表于 2021-9-16 11:30 | 只看該作者
單片機不能直接用,加一個使用環境說明就好了
回復

使用道具 舉報

10#
ID:964308 發表于 2021-10-27 08:32 來自手機 | 只看該作者
水煮涼白開 發表于 2021-4-5 22:28
是stc89c52能用的嗎

不能直接用,雖然和c51很像,但很多地方都不一樣,比如入口函數,定時器,函數名
回復

使用道具 舉報

11#
ID:964308 發表于 2021-12-13 00:29 | 只看該作者
白色暴躁 發表于 2021-4-26 21:37
兄弟,我也想知道這是用什么板子控制的,你知道了能否也告訴一下我啊,非常感謝!

arduino的都能控制,我用的是Arduino Nano v3,自己寫了個讀卡號和計算水卡密碼的,現在差了個把數據寫進去 。不過我看他們都是用UNO
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 一级看片免费视频囗交动图 | 91原创视频在线观看 | 国产精品久久久久久久久久久久久 | 亚洲国产成人精品女人久久久野战 | 国产女人叫床高潮大片免费 | 国产精品综合久久 | 九九九久久国产免费 | 日韩精品一区二区三区视频播放 | 特黄一级| 国产日韩欧美综合 | 日本一区二区影视 | 国产精品久久久久久吹潮日韩动画 | 日韩欧美一区二区三区四区 | 三级免费av | 一本久久a久久精品亚洲 | 国产日韩中文字幕 | 99福利视频导航 | 高清视频一区二区三区 | 成人免费精品视频 | 成人一区二区三区在线观看 | 国内精品久久影院 | 精品久久久久一区二区国产 | 亚洲视频中文字幕 | 久久国产精品精品 | 亚洲精品国产第一综合99久久 | 久久精品福利视频 | www.日日操| 日韩激情免费 | ririsao久久精品一区 | 成人黄色av| 成人av电影在线观看 | 亚洲一区二区三区在线观看免费 | 综合激情av | 日韩视频精品在线 | 99riav国产一区二区三区 | 亚洲欧美综合 | 午夜精品一区 | 国产黄视频在线播放 | 黄色网址在线免费观看 | 免费久久视频 | 欧美精品在线免费观看 |