[Arduino] 驱动RC522 读取 UID例程
---------------------------------
Freaduino mega 2560
rc522 & arduino lib 下载地址: https://github.com/miguelbalboa/rfid
----------------------------------
现象:
-------------------------------------------codeStart------------------------------
1 /************************************************ 2 *@: main.h 3 *@: rc522 Module 4 *@: Hmily 5 *@: 2014.10.31 15:44:58 Version 1.0 6 ************************************************/ 7 8 //***************************************************************************************** 9 //@:Header files included 10 //***************************************************************************************** 11 #if defined(ARDUINO) && ARDUINO >= 100 12 #include "Arduino.h" 13 #else 14 #include "WProgram.h" 15 #endif 16 #include <SPI.h> 17 #include <MFRC522.h> 18 19 #ifndef __MAIN_H__ 20 #define __MAIN_H__ 21 22 // board type 23 #define BOARD_TYPE_ARDUINO_NANO 0x01 24 #define BOARD_TYPE_ARDUINO_MEGA 0x02 25 #define BOARD_TYPE_ARDUINO_MEGA_2560 BOARD_TYPE_ARDUINO_MEGA 26 27 #define BOARD_TYPE BOARD_TYPE_ARDUINO_MEGA 28 #define DEBUG_MODE 1 29 30 // type definition 31 #define uint_8 unsigned char 32 #define sint_8 signed char 33 #define uint_16 unsigned short int 34 #define sint_16 signed short int 35 36 // pin distribution 37 #if BOARD_TYPE == BOARD_TYPE_ARDUINO_MEGA 38 // *****for BOARD_TYPE_ARDUINO_MEGA_2560 & BOARD_TYPE_ARDUINO_MEGA 39 // http://arduino.cc/en/Main/ArduinoBoardMega2560 40 // rc522 spi interface 41 #define PIN_RC522_CS 53 //SPI_SS default 42 #define PIN_RC522_SCK 52 43 #define PIN_RC522_MISO 50 44 #define PIN_RC522_MOSI 51 45 #define PIN_RC522_RST 49 46 #else 47 // *****for BOARD_TYPE_ARDUINO_NANO 48 //http://arduino.cc/en/Main/ArduinoBoardNano 49 // rc522 spi interface 50 #define PIN_RC522_CS 10 //SPI_SS default 51 #define PIN_RC522_SCK 13 52 #define PIN_RC522_MISO 12 53 #define PIN_RC522_MOSI 11 54 #define PIN_RC522_RST 9 55 56 #endif // end of switch(BOARD_TYPE) 57 58 #endif // end of __MAIN_H__
1 /************************************************ 2 *@: op_rc522.h 3 *@: rc522 Module extern interface 4 *@: Hmily 5 *@: 2014.10.31 15:44:58 Version 1.0 6 ************************************************/ 7 8 //***************************************************************************************** 9 //@:Header files included 10 //***************************************************************************************** 11 #if defined(ARDUINO) && ARDUINO >= 100 12 #include "Arduino.h" 13 #else 14 #include "WProgram.h" 15 #endif 16 17 #include "main.h" 18 #include <SPI.h> 19 #include <MFRC522.h> 20 21 22 23 #ifndef __OP_RC522_H__ 24 #define __OP_RC522_H__ 25 26 27 28 29 30 extern void Process_RFID(void); 31 extern void RFID_Init(void); 32 33 #endif // end of __OP_RCC_H__
1 /********************************************** 2 *@: op_RC522.cpp 3 *@: RCC lib & ex 4 *@: Hmily 5 *@: 2014.11.5 11:35:31 6 ************************************************/ 7 //***************************************************************************************** 8 //@:Header files included 9 //***************************************************************************************** 10 #if defined(ARDUINO) && ARDUINO >= 100 11 #include "Arduino.h" 12 #else 13 #include "WProgram.h" 14 #endif 15 16 #include "main.h" 17 #include "op_rc522.h" 18 #include <SPI.h> 19 #include <MFRC522.h> 20 21 22 //***************************************************************************************** 23 //@: global varibles definition & extern varibles declration 24 //***************************************************************************************** 25 MFRC522 mfrc522(PIN_RC522_CS, PIN_RC522_RST); // Create MFRC522 instance. 26 27 //***************************************************************************************** 28 //@: local & extern functions declration 29 //***************************************************************************************** 30 void Process_RFID(void); 31 void RFID_Init(void); 32 33 34 35 //***************************************************************************************** 36 void RFID_Init(void){ 37 mfrc522.PCD_Init(); // Init MFRC522 card 38 #if DEBUG_MODE == 1 39 Serial.println("Scan PICC to see UID and type..."); 40 #endif // end of DEBUG_MODE 41 } 42 43 44 uint_8 card0_uid[] = {0x2d,0xaa,0x2c,0x76}; 45 uint_8 card1_uid[] = {0xea,0x9d,0xfc,0xb8}; 46 47 void Process_RFID(void){ 48 49 uint_8 idx = 0; 50 // look for new cards 51 if(!mfrc522.PICC_IsNewCardPresent()){ 52 //Serial.print("No card 00!\n"); 53 return ; 54 } 55 56 // Select one of the cards 57 if ( ! mfrc522.PICC_ReadCardSerial()) { 58 //Serial.print("No card 01!\n"); 59 return; 60 } 61 62 #if DEBUG_MODE == 1 63 Serial.print("Card UID :"); 64 for(idx = 0;idx < mfrc522.uid.size;idx++){ 65 Serial.print(mfrc522.uid.uidByte[idx] < 0x10? " 0" : " "); 66 Serial.print(mfrc522.uid.uidByte[idx], HEX); 67 } 68 if(mfrc522.uid.uidByte[0] == card0_uid[0]){ 69 Serial.print(" school card!"); 70 } 71 else if((mfrc522.uid.uidByte[0] == card1_uid[0])){ 72 Serial.print(" backspace card!"); 73 } 74 else{ 75 Serial.print(" other card!"); 76 } 77 Serial.println(); 78 #endif // end of DEBUG_MODE 79 80 81 82 83 }
1 /************************************************ 2 *@: RC522.ino 3 *@: 移植lib & ex 4 *@: Hmily 5 *@: 2014年11月5日 11:35:31 6 ************************************************/ 7 //***************************************************************************************** 8 //@:Header files included 9 //***************************************************************************************** 10 #if defined(ARDUINO) && ARDUINO >= 100 11 #include "Arduino.h" 12 #else 13 #include "WProgram.h" 14 #endif 15 16 #include "op_rc522.h" 17 #include <SPI.h> 18 #include <MFRC522.h> 19 20 //***************************************************************************************** 21 //@: global varibles definition & extern varibles declration 22 //***************************************************************************************** 23 24 //***************************************************************************************** 25 //@: local & extern functions declration 26 //***************************************************************************************** 27 void MCU_Init(void); 28 29 30 31 //----------------------------------------------------------------------------------------- 32 void setup(void){ 33 34 MCU_Init(); 35 RFID_Init(); 36 37 } 38 39 40 void loop(void){ 41 Process_RFID(); 42 43 44 } 45 46 47 48 49 50 void MCU_Init(void){ 51 #if DEBUG_MODE == 1 52 Serial.begin(9600); 53 Serial.flush(); 54 Serial.println("Please input ang key to go on :"); 55 while(!Serial.available()); 56 #endif // end of DEBUG_MODE 57 58 SPI.begin(); 59 }
------------------------------------------codestop--------------------------------
~不再更新,都不让我写公式,博客园太拉胯了