模板:Arduino:MFRC522-LoadCell
出自 陳富國維基館
接腳方式
MFRC522接腳 ARDUINO接腳 SDA 10 SDK 13 MOSI 11 MISO 12 GND GND RST 5 3.3V 3V3
程式碼
/* * 文 件 名:RFID.pde * 創 建 者:Dr.Leong ( WWW.B2CQSHOP.COM ) * 創建日期:2011.09.19 * 修 改 者: * 修改日期: * 功能描述:Mifare1 尋卡→防沖突→選卡→讀寫 接口 */ // the sensor communicates using SPI, so include the library: #include <SPI.h> #define uchar unsigned char #define uint unsigned int //數組最大長度 #define MAX_LEN 16 ///////////////////////////////////////////////////////////////////// //set the pin ///////////////////////////////////////////////////////////////////// const int chipSelectPin = 10; const int NRSTPD = 5; //MF522命令字 #define PCD_IDLE 0x00 //NO action;取消當前命令 #define PCD_AUTHENT 0x0E //驗證密鑰 #define PCD_RECEIVE 0x08 //接收數據 #define PCD_TRANSMIT 0x04 //發送數據 #define PCD_TRANSCEIVE 0x0C //發送並接收數據 #define PCD_RESETPHASE 0x0F //復位 #define PCD_CALCCRC 0x03 //CRC計算 //Mifare_One卡片命令字 #define PICC_REQIDL 0x26 //尋天線區內未進入休眠狀態 #define PICC_REQALL 0x52 //尋天線區內全部卡 #define PICC_ANTICOLL 0x93 //防沖撞 #define PICC_SElECTTAG 0x93 //選卡 #define PICC_AUTHENT1A 0x60 //驗證A密鑰 #define PICC_AUTHENT1B 0x61 //驗證B密鑰 #define PICC_READ 0x30 //讀塊 #define PICC_WRITE 0xA0 //寫塊 #define PICC_DECREMENT 0xC0 //扣款 #define PICC_INCREMENT 0xC1 //充值 #define PICC_RESTORE 0xC2 //調塊數據到緩沖區 #define PICC_TRANSFER 0xB0 //保存緩沖區中數據 #define PICC_HALT 0x50 //休眠 //和MF522通訊時返回的錯誤代碼 #define MI_OK 0 #define MI_NOTAGERR 1 #define MI_ERR 2 //------------------MFRC522寄存器--------------- //Page 0:Command and Status #define Reserved00 0x00 #define CommandReg 0x01 #define CommIEnReg 0x02 #define DivlEnReg 0x03 #define CommIrqReg 0x04 #define DivIrqReg 0x05 #define ErrorReg 0x06 #define Status1Reg 0x07 #define Status2Reg 0x08 #define FIFODataReg 0x09 #define FIFOLevelReg 0x0A #define WaterLevelReg 0x0B #define ControlReg 0x0C #define BitFramingReg 0x0D #define CollReg 0x0E #define Reserved01 0x0F //Page 1:Command #define Reserved10 0x10 #define ModeReg 0x11 #define TxModeReg 0x12 #define RxModeReg 0x13 #define TxControlReg 0x14 #define TxAutoReg 0x15 #define TxSelReg 0x16 #define RxSelReg 0x17 #define RxThresholdReg 0x18 #define DemodReg 0x19 #define Reserved11 0x1A #define Reserved12 0x1B #define MifareReg 0x1C #define Reserved13 0x1D #define Reserved14 0x1E #define SerialSpeedReg 0x1F //Page 2:CFG #define Reserved20 0x20 #define CRCResultRegM 0x21 #define CRCResultRegL 0x22 #define Reserved21 0x23 #define ModWidthReg 0x24 #define Reserved22 0x25 #define RFCfgReg 0x26 #define GsNReg 0x27 #define CWGsPReg 0x28 #define ModGsPReg 0x29 #define TModeReg 0x2A #define TPrescalerReg 0x2B #define TReloadRegH 0x2C #define TReloadRegL 0x2D #define TCounterValueRegH 0x2E #define TCounterValueRegL 0x2F //Page 3:TestRegister #define Reserved30 0x30 #define TestSel1Reg 0x31 #define TestSel2Reg 0x32 #define TestPinEnReg 0x33 #define TestPinValueReg 0x34 #define TestBusReg 0x35 #define AutoTestReg 0x36 #define VersionReg 0x37 #define AnalogTestReg 0x38 #define TestDAC1Reg 0x39 #define TestDAC2Reg 0x3A #define TestADCReg 0x3B #define Reserved31 0x3C #define Reserved32 0x3D #define Reserved33 0x3E #define Reserved34 0x3F //----------------------------------------------- //4字節卡序列號,第5字節為校驗字節 uchar serNum[5]; uchar writeData[16]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100}; //初始化 100元錢 uchar moneyConsume = 18 ; //消費18元 uchar moneyAdd = 10 ; //充值10元 //扇區A密碼,16個扇區,每個扇區密碼6Byte uchar sectorKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, //{0x19, 0x84, 0x07, 0x15, 0x76, 0x14}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, }; uchar sectorNewKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0x19,0x84,0x07,0x15,0x76,0x14}, //you can set another ket , such as " 0x19, 0x84, 0x07, 0x15, 0x76, 0x14 " //{0x19, 0x84, 0x07, 0x15, 0x76, 0x14, 0xff,0x07,0x80,0x69, 0x19,0x84,0x07,0x15,0x76,0x14}, // but when loop, please set the sectorKeyA, the same key, so that RFID module can read the card {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0x19,0x33,0x07,0x15,0x34,0x14}, }; char inputBuffer[20]; void setup() { Serial.begin(9600); // RFID reader SOUT pin connected to Serial RX pin at 2400bps // start the SPI library: SPI.begin(); pinMode(chipSelectPin,OUTPUT); // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin digitalWrite(chipSelectPin, LOW); // Activate the RFID reader pinMode(NRSTPD,OUTPUT); // Set digital pin 10 , Not Reset and Power-down digitalWrite(NRSTPD, HIGH); MFRC522_Init(); } void loop() { uchar i,tmp; uchar status; uchar str[MAX_LEN]; uchar RC_size; uchar blockAddr; //選擇操作的塊地址0~63 String mynum = ""; while (Serial.available() > 0) { char c = Serial.read(); Serial.print(c); } //尋卡,返回卡類型 status = MFRC522_Request(PICC_REQIDL, str); if (status == MI_OK) { //Serial.println("Card detected"); //Serial.print(str[0],BIN); //Serial.print(" , "); //Serial.print(str[1],BIN); //Serial.println(" "); } //防沖撞,返回卡的序列號 4字節 status = MFRC522_Anticoll(str); memcpy(serNum, str, 5); if (status == MI_OK) { //buzzer analogWrite(3,20); delay(500); analogWrite(3,0); delay(500); Serial.println("The card's number is : "); Serial.print(serNum[0]); Serial.print(" , "); Serial.print(serNum[1],BIN); Serial.print(" , "); Serial.print(serNum[2],BIN); Serial.print(" , "); Serial.print(serNum[3],BIN); Serial.print(" , "); Serial.print(serNum[4],BIN); Serial.println(" "); // Should really check all pairs, but for now we'll just use the first // if(serNum[0] == 88) { // Serial.println("Hello Grant"); // } else if(serNum[0] == 173) { // Serial.println("Hello David"); // } delay(1000); } //Serial.println(" "); MFRC522_Halt(); //命令卡片進入休眠狀態 } /* * 函 數 名:Write_MFRC5200 * 功能描述:向MFRC522的某一寄存器寫一個字節數據 * 輸入參數:addr--寄存器地址﹔val--要寫入的值 * 返 回 值:無 */ void Write_MFRC522(uchar addr, uchar val) { digitalWrite(chipSelectPin, LOW); //地址格式:0XXXXXX0 SPI.transfer((addr<<1)&0x7E); SPI.transfer(val); digitalWrite(chipSelectPin, HIGH); } /* * 函 數 名:Read_MFRC522 * 功能描述:從MFRC522的某一寄存器讀一個字節數據 * 輸入參數:addr--寄存器地址 * 返 回 值:返回讀取到的一個字節數據 */ uchar Read_MFRC522(uchar addr) { uchar val; digitalWrite(chipSelectPin, LOW); //地址格式:1XXXXXX0 SPI.transfer(((addr<<1)&0x7E) | 0x80); val =SPI.transfer(0x00); digitalWrite(chipSelectPin, HIGH); return val; } /* * 函 數 名:SetBitMask * 功能描述:置RC522寄存器位 * 輸入參數:reg--寄存器地址;mask--置位值 * 返 回 值:無 */ void SetBitMask(uchar reg, uchar mask) { uchar tmp; tmp = Read_MFRC522(reg); Write_MFRC522(reg, tmp | mask); // set bit mask } /* * 函 數 名:ClearBitMask * 功能描述:清RC522寄存器位 * 輸入參數:reg--寄存器地址;mask--清位值 * 返 回 值:無 */ void ClearBitMask(uchar reg, uchar mask) { uchar tmp; tmp = Read_MFRC522(reg); Write_MFRC522(reg, tmp & (~mask)); // clear bit mask } /* * 函 數 名:AntennaOn * 功能描述:開啟天線,每次啟動或關閉天險發射之間應至少有1ms的間隔 * 輸入參數:無 * 返 回 值:無 */ void AntennaOn(void) { uchar temp; temp = Read_MFRC522(TxControlReg); if (!(temp & 0x03)) { SetBitMask(TxControlReg, 0x03); } } /* * 函 數 名:AntennaOff * 功能描述:關閉天線,每次啟動或關閉天險發射之間應至少有1ms的間隔 * 輸入參數:無 * 返 回 值:無 */ void AntennaOff(void) { ClearBitMask(TxControlReg, 0x03); } /* * 函 數 名:ResetMFRC522 * 功能描述:復位RC522 * 輸入參數:無 * 返 回 值:無 */ void MFRC522_Reset(void) { Write_MFRC522(CommandReg, PCD_RESETPHASE); } /* * 函 數 名:InitMFRC522 * 功能描述:初始化RC522 * 輸入參數:無 * 返 回 值:無 */ void MFRC522_Init(void) { digitalWrite(NRSTPD,HIGH); MFRC522_Reset(); //Timer: TPrescaler*TreloadVal/6.78MHz = 24ms Write_MFRC522(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler Write_MFRC522(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg Write_MFRC522(TReloadRegL, 30); Write_MFRC522(TReloadRegH, 0); Write_MFRC522(TxAutoReg, 0x40); //100%ASK Write_MFRC522(ModeReg, 0x3D); //CRC初始值0x6363 ??? //ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0 //Write_MFRC522(RxSelReg, 0x86); //RxWait = RxSelReg[5..0] //Write_MFRC522(RFCfgReg, 0x7F); //RxGain = 48dB AntennaOn(); //打開天線 } /* * 函 數 名:MFRC522_Request * 功能描述:尋卡,讀取卡類型號 * 輸入參數:reqMode--尋卡方式, * TagType--返回卡片類型 * 0x4400 = Mifare_UltraLight * 0x0400 = Mifare_One(S50) * 0x0200 = Mifare_One(S70) * 0x0800 = Mifare_Pro(X) * 0x4403 = Mifare_DESFire * 返 回 值:成功返回MI_OK */ uchar MFRC522_Request(uchar reqMode, uchar *TagType) { uchar status; uint backBits; //接收到的數據位數 Write_MFRC522(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ??? TagType[0] = reqMode; status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits); if ((status != MI_OK) || (backBits != 0x10)) { status = MI_ERR; } return status; } /* * 函 數 名:MFRC522_ToCard * 功能描述:RC522和ISO14443卡通訊 * 輸入參數:command--MF522命令字, * sendData--通過RC522發送到卡片的數據, * sendLen--發送的數據長度 * backData--接收到的卡片返回數據, * backLen--返回數據的位長度 * 返 回 值:成功返回MI_OK */ uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen) { uchar status = MI_ERR; uchar irqEn = 0x00; uchar waitIRq = 0x00; uchar lastBits; uchar n; uint i; switch (command) { case PCD_AUTHENT: //認證卡密 { irqEn = 0x12; waitIRq = 0x10; break; } case PCD_TRANSCEIVE: //發送FIFO中數據 { irqEn = 0x77; waitIRq = 0x30; break; } default: break; } Write_MFRC522(CommIEnReg, irqEn|0x80); //允許中斷請求 ClearBitMask(CommIrqReg, 0x80); //清除所有中斷請求位 SetBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO初始化 Write_MFRC522(CommandReg, PCD_IDLE); //NO action;取消當前命令 ??? //向FIFO中寫入數據 for (i=0; i<sendLen; i++) { Write_MFRC522(FIFODataReg, sendData[i]); } //執行命令 Write_MFRC522(CommandReg, command); if (command == PCD_TRANSCEIVE) { SetBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts } //等待接收數據完成 i = 2000; //i根據時鐘頻率調整,操作M1卡最大等待時間25ms ??? do { //CommIrqReg[7..0] //Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq n = Read_MFRC522(CommIrqReg); i--; } while ((i!=0) && !(n&0x01) && !(n&waitIRq)); ClearBitMask(BitFramingReg, 0x80); //StartSend=0 if (i != 0) { if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr { status = MI_OK; if (n & irqEn & 0x01) { status = MI_NOTAGERR; //?? } if (command == PCD_TRANSCEIVE) { n = Read_MFRC522(FIFOLevelReg); lastBits = Read_MFRC522(ControlReg) & 0x07; if (lastBits) { *backLen = (n-1)*8 + lastBits; } else { *backLen = n*8; } if (n == 0) { n = 1; } if (n > MAX_LEN) { n = MAX_LEN; } //讀取FIFO中接收到的數據 for (i=0; i<n; i++) { backData[i] = Read_MFRC522(FIFODataReg); } } } else { status = MI_ERR; } } //SetBitMask(ControlReg,0x80); //timer stops //Write_MFRC522(CommandReg, PCD_IDLE); return status; } /* * 函 數 名:MFRC522_Anticoll * 功能描述:防沖突檢測,讀取選中卡片的卡序列號 * 輸入參數:serNum--返回4字節卡序列號,第5字節為校驗字節 * 返 回 值:成功返回MI_OK */ uchar MFRC522_Anticoll(uchar *serNum) { uchar status; uchar i; uchar serNumCheck=0; uint unLen; //ClearBitMask(Status2Reg, 0x08); //TempSensclear //ClearBitMask(CollReg,0x80); //ValuesAfterColl Write_MFRC522(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0] serNum[0] = PICC_ANTICOLL; serNum[1] = 0x20; status = MFRC522_ToCard(PCD_TRANSCEIVE, serNum, 2, serNum, &unLen); if (status == MI_OK) { //校驗卡序列號 for (i=0; i<4; i++) { serNumCheck ^= serNum[i]; } if (serNumCheck != serNum[i]) { status = MI_ERR; } } //SetBitMask(CollReg, 0x80); //ValuesAfterColl=1 return status; } /* * 函 數 名:CalulateCRC * 功能描述:用MF522計算CRC * 輸入參數:pIndata--要讀數CRC的數據,len--數據長度,pOutData--計算的CRC結果 * 返 回 值:無 */ void CalulateCRC(uchar *pIndata, uchar len, uchar *pOutData) { uchar i, n; ClearBitMask(DivIrqReg, 0x04); //CRCIrq = 0 SetBitMask(FIFOLevelReg, 0x80); //清FIFO指針 //Write_MFRC522(CommandReg, PCD_IDLE); //向FIFO中寫入數據 for (i=0; i<len; i++) { Write_MFRC522(FIFODataReg, *(pIndata+i)); } Write_MFRC522(CommandReg, PCD_CALCCRC); //等待CRC計算完成 i = 0xFF; do { n = Read_MFRC522(DivIrqReg); i--; } while ((i!=0) && !(n&0x04)); //CRCIrq = 1 //讀取CRC計算結果 pOutData[0] = Read_MFRC522(CRCResultRegL); pOutData[1] = Read_MFRC522(CRCResultRegM); } /* * 函 數 名:MFRC522_SelectTag * 功能描述:選卡,讀取卡存儲器容量 * 輸入參數:serNum--傳入卡序列號 * 返 回 值:成功返回卡容量 */ uchar MFRC522_SelectTag(uchar *serNum) { uchar i; uchar status; uchar size; uint recvBits; uchar buffer[9]; //ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0 buffer[0] = PICC_SElECTTAG; buffer[1] = 0x70; for (i=0; i<5; i++) { buffer[i+2] = *(serNum+i); } CalulateCRC(buffer, 7, &buffer[7]); //?? status = MFRC522_ToCard(PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits); if ((status == MI_OK) && (recvBits == 0x18)) { size = buffer[0]; } else { size = 0; } return size; } /* * 函 數 名:MFRC522_Auth * 功能描述:驗證卡片密碼 * 輸入參數:authMode--密碼驗證模式 0x60 = 驗證A密鑰 0x61 = 驗證B密鑰 BlockAddr--塊地址 Sectorkey--扇區密碼 serNum--卡片序列號,4字節 * 返 回 值:成功返回MI_OK */ uchar MFRC522_Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *serNum) { uchar status; uint recvBits; uchar i; uchar buff[12]; //驗證指令+塊地址+扇區密碼+卡序列號 buff[0] = authMode; buff[1] = BlockAddr; for (i=0; i<6; i++) { buff[i+2] = *(Sectorkey+i); } for (i=0; i<4; i++) { buff[i+8] = *(serNum+i); } status = MFRC522_ToCard(PCD_AUTHENT, buff, 12, buff, &recvBits); if ((status != MI_OK) || (!(Read_MFRC522(Status2Reg) & 0x08))) { status = MI_ERR; } return status; } /* * 函 數 名:MFRC522_Read * 功能描述:讀塊數據 * 輸入參數:blockAddr--塊地址;recvData--讀出的塊數據 * 返 回 值:成功返回MI_OK */ uchar MFRC522_Read(uchar blockAddr, uchar *recvData) { uchar status; uint unLen; recvData[0] = PICC_READ; recvData[1] = blockAddr; CalulateCRC(recvData,2, &recvData[2]); status = MFRC522_ToCard(PCD_TRANSCEIVE, recvData, 4, recvData, &unLen); if ((status != MI_OK) || (unLen != 0x90)) { status = MI_ERR; } return status; } /* * 函 數 名:MFRC522_Write * 功能描述:寫塊數據 * 輸入參數:blockAddr--塊地址;writeData--向塊寫16字節數據 * 返 回 值:成功返回MI_OK */ uchar MFRC522_Write(uchar blockAddr, uchar *writeData) { uchar status; uint recvBits; uchar i; uchar buff[18]; buff[0] = PICC_WRITE; buff[1] = blockAddr; CalulateCRC(buff, 2, &buff[2]); status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff, &recvBits); if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A)) { status = MI_ERR; } if (status == MI_OK) { for (i=0; i<16; i++) //向FIFO寫16Byte數據 { buff[i] = *(writeData+i); } CalulateCRC(buff, 16, &buff[16]); status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 18, buff, &recvBits); if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A)) { status = MI_ERR; } } return status; } /* * 函 數 名:MFRC522_Halt * 功能描述:命令卡片進入休眠狀態 * 輸入參數:無 * 返 回 值:無 */ void MFRC522_Halt(void) { uchar status; uint unLen; uchar buff[4]; buff[0] = PICC_HALT; buff[1] = 0; CalulateCRC(buff, 2, &buff[2]); status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff,&unLen); }