//讀寫扇區(qū)的例子://全部扇區(qū)數(shù)據(jù)塊可單獨(dú)讀寫,也可全部扇區(qū)同時(shí)讀寫,可以復(fù)制全部扇區(qū)的內(nèi)容,也可以重新賦值全部扇區(qū)的內(nèi)容
#include <SPI.h>
#include <RFID.h>
RFID rfid(53,49); //D53--讀卡器MOSI引腳、D49--讀卡器RST引腳//2560板子
#define qh 43//功能切換讀取
#define d 44//LED燈,滅代表讀狀態(tài),亮代表寫狀態(tài)
char b=0;
//4字節(jié)卡序列號(hào),第5字節(jié)為校驗(yàn)字節(jié)
unsigned char serNum[5];
//寫卡數(shù)據(jù),45為數(shù)據(jù)塊數(shù)量,16為長(zhǎng)度
//數(shù)據(jù)塊是1-15扇區(qū)相加,每個(gè)扇區(qū)3個(gè)數(shù)據(jù)塊故3*15=45;0號(hào)扇區(qū)固定不能改
unsigned char writeDate[45][16] ={
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1','1', '1', '1', '1'},
{'1', '2', '2','2', '2', '2','2', '2', '2','2', '2', '2','2', '2', '2', '2'},
{'1', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'2', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'2', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'2', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'3', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'3', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'3', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'4', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'4', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'4', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'5', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'5', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'5', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'6', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'6', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'6', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'7', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'7', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'7', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'8', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'8', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'8', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'9', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'9', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'9', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'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},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'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},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'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},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'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},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'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},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'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},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
};
unsigned char fz[45][16];
//原扇區(qū)A密碼,16個(gè)扇區(qū),每個(gè)扇區(qū)密碼6Byte
unsigned char sectorKeyA[16][16] = {
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//0~16扇區(qū)原密碼
//{0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{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},
};
//新扇區(qū)A密碼,16個(gè)扇區(qū),每個(gè)扇區(qū)密碼6Byte,一行代表一個(gè)扇區(qū)
//改密碼只要改前面6個(gè)ff任意一個(gè), 0xff,0x07,0x80,0x69, 千萬不能動(dòng)否則可能報(bào)廢扇區(qū)
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扇區(qū)新密碼
{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, 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aa[16];
void setup()
{
Serial.begin(9600);
SPI.begin();
rfid.init();
pinMode(qh, INPUT);
digitalWrite(qh, 1);
pinMode(d, OUTPUT);
}
void loop()
{
int j;
char tmp;
unsigned char status;
unsigned char str[MAX_LEN];
unsigned char blockAddr; //選擇操作的塊地址0~63。但只能寫4的倍數(shù)+3;比如7,11,15,19---
if(digitalRead(qh)==0) //功能按鍵操作
{
b=b^1;
digitalWrite(d, b);
delay(30);
while(!(digitalRead(qh)));
delay(30);
}
//找卡
rfid.isCard();
//讀取卡序列號(hào)
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);
//復(fù)制數(shù)據(jù)卡
if(b==1)
{
for(int ii= 7;ii<64;ii=ii+4)
{
for(int i=ii-3;i<ii;i++)//循環(huán)寫入數(shù)據(jù)塊012,不循環(huán)就單獨(dú)寫i值
{
blockAddr = ii;
if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[ blockAddr/4], rfid.serNum) == MI_OK) //認(rèn)證
{
// 寫密碼
status = rfid.write(blockAddr, sectorNewKeyA[blockAddr/4]);
//寫數(shù)據(jù)
blockAddr =i; //數(shù)據(jù)塊i
j=i-i/4-3;
// status = rfid.write(blockAddr, writeDate[j]);//如果使用賦值功能則打開此行
status = rfid.write(blockAddr, fz[j]);//如果使用復(fù)制功能則打開此行,與下面的
if(status == MI_OK)
{
Serial.print("扇區(qū):");
Serial.print(i/4);
Serial.print("數(shù)據(jù)塊:");
Serial.println(i);
//Serial.println(j);
Serial.println("Write card OK!");
}
}
}
}
}
else
{
//讀卡
for(int ii= 7;ii<64;ii=ii+4)
{
for(int i=ii-3;i<ii;i++)
{
blockAddr = ii;
status = rfid.auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], rfid.serNum);
if (status == MI_OK) //認(rèn)證
{
//讀數(shù)據(jù)
blockAddr =i; //數(shù)據(jù)塊i
j=i-i/4-3;
/*********如果使用復(fù)制卡里的數(shù)據(jù)則打開,否則關(guān)閉,與上面的status = rfid.write(blockAddr, fz[j])同時(shí)使用***************/
if( rfid.read(blockAddr, str) == MI_OK)
{
for(int z=0;z<16;z++)//復(fù)制扇區(qū)數(shù)據(jù)塊
{
fz[j][z]=str[z];
}
/************************/
// Serial.println((char*)fz[i]);//打印復(fù)制的值,可省去
Serial.print("扇區(qū):");
Serial.print(blockAddr/4);
Serial.print("數(shù)據(jù)塊:");
Serial.print(i);
Serial.print(":");
Serial.print("the data is : ");
Serial.println((char *)str);
}
}
}
}
}
rfid.halt();//去掉本行連續(xù)讀寫
}
|