Pages: [1]   Go Down
Author Topic: Mifare RFID  (Read 1205 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been searching about an RFID I got from eBay, but there is not much information.

I have this code working (edited for length and Chinese to get under 9500 characters)

Code:
/*
#include <SPI.h>
#define uchar unsigned char
#define uint unsigned int
#define MAX_LEN 16

//set the pin

const int chipSelectPin = 53;
const int NRSTPD = 5;

//MF522
#define PCD_IDLE              0x00              
#define PCD_AUTHENT           0x0E              
#define PCD_RECEIVE           0x08              
#define PCD_TRANSMIT          0x04              
#define PCD_TRANSCEIVE        0x0C              
#define PCD_RESETPHASE        0x0F              
#define PCD_CALCCRC           0x03              

//Mifare_One
#define PICC_REQIDL           0x26              
#define PICC_REQALL           0x52              
#define PICC_ANTICOLL         0x93              
#define PICC_SElECTTAG        0x93              
#define PICC_AUTHENT1A        0x60              
#define PICC_AUTHENT1B        0x61            
#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              

#define MI_OK                 0
#define MI_NOTAGERR           1
#define MI_ERR                2

//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

uchar serNum[5];
uchar  writeData[16]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100};  
uchar  moneyConsume = 18 ;  
uchar  moneyAdd = 10 ;  
uchar sectorKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
                             {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//when you try it again, please change it into your new password
                             {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
                             {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},
                                {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0x19,0x33,0x07,0x15,0x34,0x14},
                                {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0x19,0x33,0x07,0x15,0x34,0x14},
                               };
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;
status = MFRC522_Request(PICC_REQIDL, str);
if (status == MI_OK)
{
Serial.println("Find out a card ");
Serial.print(str[0],BIN);
Serial.print(" , ");
Serial.print(str[1],BIN);
Serial.println(" ");
}
status = MFRC522_Anticoll(str);
memcpy(serNum, str, 5);
if (status == MI_OK)
{
Serial.println("The card's number is  : ");
Serial.print(serNum[0],BIN);
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(" ");
}
RC_size = MFRC522_SelectTag(serNum);
if (RC_size != 0)
{
Serial.print("The size of the card is  :   ");
Serial.print(RC_size,DEC);
Serial.println(" K ");
}
blockAddr = 11;
status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr/4], serNum); //认证
if (status == MI_OK)
{
status = MFRC522_Write(blockAddr, sectorNewKeyA[blockAddr/4]);
Serial.print("set the new card password, and can modify the data of the Sector ");
Serial.print(blockAddr/4,DEC);
Serial.println(" : ");
for (i=0; i<6; i++)
{
Serial.print(sectorNewKeyA[blockAddr/4][i],HEX);
Serial.print(" , ");
}
Serial.println(" ");
blockAddr = blockAddr - 3 ;
status = MFRC522_Write(blockAddr, writeData);
if(status == MI_OK)
{
Serial.println("The card has  $100 !");
}
}
blockAddr = 11;
status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], serNum); //认证
if (status == MI_OK)
{
blockAddr = blockAddr - 3 ;
status = MFRC522_Read(blockAddr, str);
if (status == MI_OK)
{
Serial.println("Read from the card ,the data is : ");
for (i=0; i<16; i++)
{
Serial.print(str[i],DEC);
Serial.print(" , ");
}
Serial.println(" ");
}
}
blockAddr = 11; //数据块7
status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], serNum); //认证
if (status == MI_OK)
{
blockAddr = blockAddr - 3 ;
status = MFRC522_Read(blockAddr, str);
if (status == MI_OK)
{
if( str[15] < moneyConsume )
{


}



However I can't see how the pins are selected?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I may have misunderstood your question, but,
its using the spi, which is directly connected to digital pins. The SPI library should take care of setting the pins as it knows which ones to use.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry that was a bit blunt, but after an hour of editing Chinese and spaces it was still too long.  smiley-mad

I think this will work but is not the best for the Arduino I've seen a couple of blogs that show the same code but nothing new...

http://youtu.be/aukfbRdGzHA

This Blog has some information http://www.grantgibson.co.uk/blog/2012/04/how-to-get-started-with-the-mifare-mf522-an-and-arduino/

His Youtube channel has the Tutorial video http://youtu.be/b9bjH1RLba4 but the code is the original Chinese for adding funds to a card not the security access?  smiley-yell

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I may have misunderstood your question, but,
its using the spi, which is directly connected to digital pins. The SPI library should take care of setting the pins as it knows which ones to use.


Cheers, but can I change those pins, if it will cause a conflict?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, you could look for a software spi library and use it on a custom pin set, but you would have to modify the rfid to use it as well.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, you could look for a software spi library and use it on a custom pin set, but you would have to modify the rfid to use it as well.

Thanks again, since I'm looking for a simple system, do you think this item would be better?..

http://www.ebay.co.uk/itm/200868439895?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1423.l2649

Code looks much more streamlined!

Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //pin2 Rx, pin3 Tx
int CMD[64]; // Here the Max command length is set as 64 bytes. For example, Command “AB 02 01” is 3 bytes int comlen =0;
int out_flag =0;
void setup()
{
Serial.begin(9600);
mySerial.listen();
Serial.println("Serial number will be displayed here if a card is detected by the module:\n"); // set the data rate for the SoftwareSerial port
mySerial.begin(9600);
delay(10);
mySerial.write(0x02); //Send the command to read RFID tag, please refer to the manual for more detail. }
void loop() // run over and over
{
while (Serial.available())
{
int a = SerialReadHexDigit(); //Serial.println(a);
RFID module
GND VCC TXD RXD

if(a>=0){ CMD[comlen] = a; comlen++;
}
delay(10);
}
for(int i=0; i<comlen; i+=2){
int c = mySerial.write( CMD[i]*16 + CMD[i+1]); //Convert Hex Characters in to Hex number, and send it to RFID. }
comlen =0;
while (mySerial.available()) {
byte C = mySerial.read();
if (C<10) Serial.print("0"); Serial.print(C ,HEX); //Display in HEX Serial.print(" ");
out_flag =1; }
if (out_flag >0) { Serial.println(); out_flag = 0;
} }
/************************************************************************************* The following function is to receive data and judge if it is HEX character. Hex characters include 1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,A,B,C,D,E,F
Any other characters sent with the command will be ignored. **************************************************************************************/
int SerialReadHexDigit()
{
byte c = (byte) Serial.read(); if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
returnc-'a'+10;
} else if (c >= 'A' && c <= 'F') {
returnc-'A'+10; }else{
return -1; // getting here is bad: it means the character was invalid }
}
Logged

Pages: [1]   Go Up
Jump to: