microcontrolandos_ pic_ rfid mfrc522

19
01/03/2016 Mi cr ocontrol andos: PIC: RFID MFRC522 http://microcontrolandos.bl ogspot.com.br /2014/02/pi c-r fi d-mfrc522.html 1/19 Neste post estarei falando sobre o módulo le itor de cartões RFID M FRC522. Este é um módulo baseado no chip MFRC522 da NXP. Utiliza a comunicação SPI, tornandoo mais simples e fácil de usar. Ele é compatível com os seguintes cartões: MIFARE Mini, MIFARE 1K, MIFARE 4K, MIFARE Ultralight, MIFARE DESFire. Este módulo pode ser usado em uma variedade de aplicações: gestão de acesso, rastreamento de bens, rastreamento de pessoas e animais, identificação e etc. O módulo é a limentado com 3.3V. Preste muita atenção nisso. O pino NSS é o pino de seleção. No código verás como CS ( MFRC522_CS ). PIC: R FID MFRC522

Upload: antonio-victor-barbosa-lins

Post on 07-Jul-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 1/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 1/19

Neste post estarei falando sobre o módulo leitor de cartões RFID MFRC522.

Este é  um módulo baseado no chip MFRC522  da NXP. Utiliza a comunicação SPI, tornando‐o maissimples e fácil de usar.

Ele é compatível com os seguintes cartões: MIFARE Mini, MIFARE 1K, MIFARE 4K, MIFARE Ultralight,MIFARE DESFire.

Este módulo pode ser usado em uma variedade de aplicações: gestão de acesso, rastreamento de

bens, rastreamento de pessoas e animais, identificação e etc.

O módulo é alimentado com 3.3V. Preste muita atenção nisso.O pino NSS é o pino de seleção. No código verás como CS (MFRC522_CS ).

PIC: R FID MFRC522

Page 2: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 2/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 2/19

CARACTERÍSTICAS

Fonte de alimentação: 3.3V

Suporte ISO / IEC 14443A/MIFARE

Distância de funcionamento: até 40 milímetros

Interface SPI

Buffer FIFO de 64 bytesCorrente de trabalho: 13 ~ 26mA

Temperatura de trabalho: ‐25 ~ 85℃

BIBLIOTECA

01. extern sfr sbit  MFRC522_CS;

02. extern sfr sbit  MFRC522_Rst;

03. extern sfr sbit  MFRC522_CS_Direction;

04. extern sfr sbit  MFRC522_Rst_Direction;

05.

06.

07.  //MF522 Command word 

08. #define PCD_IDLE 0x00 //NO action; Cancel the curre

09. #define PCD_AUTHENT 0x0E //Authentication Key

10. #define PCD_RECEIVE 0x08 //Receive Data

11. #define PCD_TRANSMIT 0x04 //Transmit data

12. #define PCD_TRANSCEIVE 0x0C //Transmit and receive data,

13. #define PCD_RESETPHASE 0x0F //Reset

14. #define PCD_CALCCRC 0x03 //CRC Calculate

15.

16.  // Mifare_One card command word 

17. #define PICC_REQIDL 0x26 // find the antenna area does

18. #define PICC_REQALL 0x52 // find all the cards antenna

19.#define PICC_ANTICOLL 0x93 // anti‐collision

20. #define PICC_SElECTTAG 0x93 // election card 

21. #define PICC_AUTHENT1A 0x60 // authentication key A

22. #define PICC_AUTHENT1B 0x61 // authentication key B

23. #define PICC_READ 0x30 // Read Block

24. #define PICC_WRITE 0xA0 // write block

25. #define PICC_DECREMENT 0xC0 // debit

26. #define PICC_INCREMENT 0xC1 // recharge

27. #define PICC_RESTORE 0xC2 // transfer block data to the

28. #define PICC_TRANSFER 0xB0 // save the data in the buffer

29. #define PICC_HALT 0x50 // Sleep

30.

31.  //And MF522 The error code is returned when communication

Page 3: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 3/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 3/19

32. #define MI_OK 0

33. #define MI_NOTAGERR 1

34. #define MI_ERR 2

35.

36.  //‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐MFRC522 Register‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

37.  //Page 0:Command and Status

38. #define RESERVED00 0x00

39. #define COMMANDREG 0x01

40. #define COMMIENREG 0x02

41. #define DIVLENREG 0x03

42. #define COMMIRQREG 0x04

43. #define DIVIRQREG 0x05

44. #define ERRORREG 0x06

45. #define STATUS1REG 0x07

46. #define STATUS2REG 0x08

47. #define FIFODATAREG 0x09

48. #define FIFOLEVELREG 0x0A

49. #define WATERLEVELREG 0x0B

50. #define CONTROLREG 0x0C 

51. #define BITFRAMINGREG 0x0D

52. #define COLLREG 0x0E

53. #define RESERVED01 0x0F

54.  //PAGE 1:Command

55. #define RESERVED10 0x1056. #define MODEREG 0x11

57. #define TXMODEREG 0x12

58. #define RXMODEREG 0x13

59. #define TXCONTROLREG 0x14

60. #define TXAUTOREG 0x15

61. #define TXSELREG 0x16

62. #define RXSELREG 0x17 

63. #define RXTHRESHOLDREG 0x18

64. #define DEMODREG 0x19

65. #define RESERVED11 0x1A

66. #define RESERVED12 0x1B

67. #define MIFAREREG 0x1C 

68. #define RESERVED13 0x1D

69. #define RESERVED14 0x1E

70. #define SERIALSPEEDREG 0x1F

71.  //PAGE 2:CFG

72. #define RESERVED20 0x20

73. #define CRCRESULTREGM 0x21

74. #define CRCRESULTREGL 0x22

75. #define RESERVED21 0x23

Page 4: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 4/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 4/19

76. #define MODWIDTHREG 0x24

77. #define RESERVED22 0x25

78. #define RFCFGREG 0x26

79. #define GSNREG 0x27 

80. #define CWGSPREG 0x28

81. #define MODGSPREG 0x29

82. #define TMODEREG 0x2A

83. #define TPRESCALERREG 0x2B

84. #define TRELOADREGH 0x2C 

85. #define TRELOADREGL 0x2D

86. #define TCOUNTERVALUEREGH 0x2E

87. #define TCOUNTERVALUEREGL 0x2F

88.  //PAGE 3:TEST REGISTER

89. #define RESERVED30 0x30

90. #define TESTSEL1REG 0x31

91. #define TESTSEL2REG 0x32

92. #define TESTPINENREG 0x33

93. #define TESTPINVALUEREG 0x34

94. #define TESTBUSREG 0x35

95. #define AUTOTESTREG 0x36

96. #define VERSIONREG 0x37 

97. #define ANALOGTESTREG 0x38

98. #define TESTDAC1REG 0x39

99. #define TESTDAC2REG 0x3A100. #define TESTADCREG 0x3B

101. #define RESERVED31 0x3C

102. #define RESERVED32 0x3D

103. #define RESERVED33 0x3E

104. #define RESERVED34 0x3F

105.

106.

107.

108.

109.

110.

111. static void MFRC522_Wr( char  addr, char  value )

112. {

113. MFRC522_CS = 0;

114. Soft_SPI_Write( ( addr << 1  ) & 0x7E  );

115. Soft_SPI_Write( value );

116. MFRC522_CS = 1;117. }

118.

119.

Page 5: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 5/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 5/19

120.

121.

122.

123.

124.

125. static char MFRC522_Rd( char  addr )

126. {

127. char  value;

128. MFRC522_CS = 0;

129. Soft_SPI_Write( (( addr << 1  ) & 0x7E) | 0x80  );

130. value = Soft_SPI_Read( 0x00  );

131. MFRC522_CS = 1;

132. return  value;

133. }

134.

135.136.

137. static void MFRC522_Clear_Bit( char  addr, char  mask )

138. {

139. MFRC522_Wr( addr, MFRC522_Rd( addr ) &  (~mask) );

140. }

141.

142.

143. static void MFRC522_Set_Bit( char  addr, char  mask )

144. {

145. MFRC522_Wr( addr, MFRC522_Rd( addr ) |  mask );

146. }

147.

148. void MFRC522_Reset()

149. {

150. MFRC522_Wr( COMMANDREG, PCD_RESETPHASE );

151. }

152.153.

154.

155. void MFRC522_AntennaOn()

156. {

157. MFRC522_Set_Bit( TXCONTROLREG, 0x03  );

158. }

159.

160.

161.

162. void MFRC522_AntennaOff()

163. {

164. MFRC522_Clear_Bit( TXCONTROLREG, 0x03  );

Page 6: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 6/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 6/19

165. }

166.

167.

168.

169. void MFRC522_Init()

170. {

171. MFRC522_CS_Direction = 0;

172. MFRC522_Rst_Direction = 0;

173. MFRC522_CS = 1;

174. MFRC522_Rst = 1;

175.

176. MFRC522_Reset();

177.

178. MFRC522_Wr( TMODEREG, 0x8D  );  //Tauto=1; f(Timer) = 6.78MHz/TPreS

179. MFRC522_Wr( TPRESCALERREG, 0x3E  ); //TModeReg[3..0] + TPrescalerReg

180. MFRC522_Wr( TRELOADREGL, 30  );181. MFRC522_Wr( TRELOADREGH, 0  );

182.

183. MFRC522_Wr( TXAUTOREG, 0x40  );  //100%ASK 

184. MFRC522_Wr( MODEREG, 0x3D  );  // CRC valor inicial de 0x6363

185.

186.  //MFRC522_Clear_Bit( STATUS2REG, 0x08 );//MFCrypto1On=0

187.  //MFRC522_Wr( RXSELREG, 0x86 ); //RxWait = RxSelReg[5..0]

188.  //MFRC522_Wr( RFCFGREG, 0x7F ); //RxGain = 48dB

189.

190. MFRC522_AntennaOn();

191. }

192.

193.

194.

195. char MFRC522_ToCard( char  command, char *sendData, char  sendLen, char *bac

196. {

197. char  _status = MI_ERR;198. char  irqEn = 0x00;

199. char  waitIRq = 0x00;

200. char  lastBits;

201. char  n;

202. unsigned  i;

203.

204. switch  (command)

205. {

206. case  PCD_AUTHENT:  //Certification cards close

207. {

208. irqEn = 0x12;

Page 7: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 7/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 7/19

209. waitIRq = 0x10;

210. break;

211. }

212. case  PCD_TRANSCEIVE:  //Transmit FIFO data

213. {

214. irqEn = 0x77;

215. waitIRq = 0x30;

216. break;

217. }

218. default:

219. break;

220. }

221.

222. MFRC522_Wr( COMMIENREG, irqEn | 0x80  );  //Interrupt request

223. MFRC522_Clear_Bit( COMMIRQREG, 0x80  );  //Clear all interrupt request bit

224. MFRC522_Set_Bit( FIFOLEVELREG, 0x80  );  //FlushBuffer=1, FIFO Initializa

225. MFRC522_Wr( COMMANDREG, PCD_IDLE );  //NO action; Cancel the current

226.

227.

228.

229.  //Writing data to the FIFO 

230. for  ( i=0; i <  sendLen; i++  )

231. {

232. MFRC522_Wr( FIFODATAREG, sendData[i]  );233. }

234.

235.  //Execute the command 

236. MFRC522_Wr( COMMANDREG, command );

237.

238. if  (command == PCD_TRANSCEIVE )

239. {

240. MFRC522_Set_Bit( BITFRAMINGREG, 0x80  ); //StartSend=1,transmission of d

241. }

242.

243.  //Waiting to receive data to complete

244.  //i according to the clock frequency adjustment, the operator M1 card maximu

245. i = 0xFFFF;

246. do

247. {

248.  //CommIrqReg[7..0]

249.  //Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq250. n = MFRC522_Rd( COMMIRQREG );

251. i‐‐;

252. }

Page 8: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 8/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 8/19

253. while  ( i && !(n & 0x01) && !( n &  waitIRq ) );

254.

255. MFRC522_Clear_Bit( BITFRAMINGREG, 0x80  );  //StartSend=0

256.

257. if  (i != 0)

258. {

259. if( !( MFRC522_Rd( ERRORREG ) & 0x1B  ) ) //BufferOvfl Collerr CRCErr Pr

260. {

261. _status = MI_OK;

262. if  ( n &  irqEn & 0x01  )

263. {

264. _status = MI_NOTAGERR;  //??

265. }

266.

267. if  ( command == PCD_TRANSCEIVE )

268. {

269. n = MFRC522_Rd( FIFOLEVELREG );

270. lastBits = MFRC522_Rd( CONTROLREG ) & 0x07;

271. if  (lastBits)

272. {

273. *backLen = (n‐1) * 8 +  lastBits;

274. }

275. else

276. {277. *backLen = n * 8;

278. }

279.

280. if  (n == 0)

281. {

282. n = 1;

283. }

284.

285. if  (n > 16)

286. {

287. n = 16;

288. }

289.

290.  //Reading the received data in FIFO 

291. for  (i=0; i <  n; i++)

292. {

293. backData[i]  = MFRC522_Rd( FIFODATAREG );294. }

295.

296. backData[i]  = 0;

Page 9: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 9/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 9/19

297. }

298. }

299. else

300. {

301. _status = MI_ERR;

302. }

303.

304. }

305.

306.  //MFRC522_Set_Bit( CONTROLREG, 0x80 );

307.  //MFRC522_Wr( COMMANDREG, PCD_IDLE );

308.

309. return  _status;

310. }

311.

312.313.

314. char MFRC522_Request( char  reqMode, char *TagType )

315. {

316. char  _status;

317. unsigned  backBits;  //The received data bits

318.

319. MFRC522_Wr( BITFRAMINGREG, 0x07  ); //TxLastBists = BitFramingReg[2..0] ?

320.

321. TagType[0]  = reqMode;

322. _status = MFRC522_ToCard( PCD_TRANSCEIVE, TagType, 1, TagType, &backBi

323.

324. if  ( (_status != MI_OK) ||  (backBits != 0x10) )

325. {

326. _status = MI_ERR;

327. }

328.

329. return  _status;330. }

331.

332.

333.

334. void MFRC522_CRC( char *dataIn, char  length, char *dataOut )

335. {

336. char  i, n;

337. MFRC522_Clear_Bit( DIVIRQREG, 0x04  );

338. MFRC522_Set_Bit( FIFOLEVELREG, 0x80  );

339.

340.  //Escreve dados no FIFO

341. for  ( i = 0; i <  length; i++  )

Page 10: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 10/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 10/19

342. { 

343. MFRC522_Wr( FIFODATAREG, *dataIn++  );

344. }

345.

346. MFRC522_Wr( COMMANDREG, PCD_CALCCRC );

347.

348. i = 0xFF;

349.  //Espera a finalização do Calculo do CRC 

350. do 

351. {

352. n = MFRC522_Rd( DIVIRQREG );

353. i‐‐;

354. }

355. while( i && !(n & 0x04) );  //CRCIrq = 1

356.

357. dataOut[0]  = MFRC522_Rd( CRCRESULTREGL );358. dataOut[1]  = MFRC522_Rd( CRCRESULTREGM );

359. }

360.

361.

362.

363. char MFRC522_SelectTag( char *serNum )

364. {

365. char  i;

366. char  _status;

367. char  size;

368. unsigned  recvBits;

369. char  buffer[9];

370.

371.  //MFRC522_Clear_Bit( STATUS2REG, 0x08 ); //MFCrypto1On=0

372.

373. buffer[0]  = PICC_SElECTTAG;

374. buffer[1]  = 0x70;375.

376. for  ( i=2; i < 7; i++  )

377. {

378. buffer[i]  = *serNum++;

379. }

380.

381. MFRC522_CRC( buffer, 7, &buffer[7]  );

382.383. _status = MFRC522_ToCard( PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits

384.

385. if  ( (_status == MI_OK) &&  (recvBits == 0x18) )

Page 11: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 11/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 11/19

386. {

387. size = buffer[0];

388. }

389. else

390. {

391. size = 0;

392.}

393.

394. return  size;

395. }

396.

397.

398.

399.  //hibernation

400. void MFRC522_Halt()

401. {

402. unsigned  unLen;

403. char  buff[4];

404.

405. buff[0]  = PICC_HALT;

406. buff[1]  = 0;

407. MFRC522_CRC( buff, 2, &buff[2]  );

408.

409. MFRC522_Clear_Bit( STATUS2REG, 0x80  );

410. MFRC522_ToCard( PCD_TRANSCEIVE, buff, 4, buff, &unLen );

411. MFRC522_Clear_Bit( STATUS2REG, 0x08  );

412. }

413.

414.

415.

416. char MFRC522_Auth( char  authMode, char  BlockAddr, char *Sectorkey, char *s

417. {

418. char  _status;419. unsigned  recvBits;

420. char  i;

421. char  buff[12];

422.

423.  //Verify the command block address + sector + password + card serial number

424. buff[0]  = authMode;

425. buff[1]  = BlockAddr;

426.427. for  ( i = 2; i < 8; i++  )

428. {

429. buff[i]  = Sectorkey[i‐2];

Page 12: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 12/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 12/19

430. }

431.

432. for  ( i = 8; i < 12; i++  )

433. {

434. buff[i]  = serNum[i‐8];

435. }

436.

437. _status = MFRC522_ToCard( PCD_AUTHENT, buff, 12, buff, &recvBits );

438.

439. if  ( ( _status != MI_OK ) || !( MFRC522_Rd( STATUS2REG ) & 0x08  ) )

440. {

441. _status = MI_ERR;

442. }

443.

444. return  _status;

445. }

446.

447.

448.

449. char MFRC522_Write( char  blockAddr, char *writeData )

450. {

451. char  _status;

452. unsigned  recvBits;

453. char  i;

454. char  buff[18];

455.

456. buff[0]  = PICC_WRITE;

457. buff[1]  = blockAddr;

458.

459. MFRC522_CRC( buff, 2, &buff[2]  );

460. _status = MFRC522_ToCard( PCD_TRANSCEIVE, buff, 4, buff, &recvBits );

461.

462. if  ( (_status != MI_OK) ||  (recvBits != 4) ||  ( (buff[0] & 0x0F) != 0x

463. {

464. _status = MI_ERR;

465. }

466.

467. if  (_status == MI_OK)

468. {

469. for  ( i = 0; i < 16; i++  )  //Data to the FIFO write 16B

470. {

471. buff[i]  = writeData[i];

472. }

473.

474. MFRC522_CRC( buff, 16, &buff[16]  );

Page 13: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 13/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 13/19

475. _status = MFRC522_ToCard( PCD_TRANSCEIVE, buff, 18, buff, &recvBits

476.

477. if  ( (_status != MI_OK) ||  (recvBits != 4) ||  ( (buff[0] & 0x0F) !=

478. {

479. _status = MI_ERR;

480. }

481. }

482.

483. return  _status;

484. }

485.

486.

487.

488. char MFRC522_Read( char  blockAddr, char *recvData )

489. {

490. char  _status;491. unsigned  unLen;

492.

493. recvData[0]  = PICC_READ;

494. recvData[1]  = blockAddr;

495.

496. MFRC522_CRC( recvData, 2, &recvData[2]  );

497.

498. _status = MFRC522_ToCard( PCD_TRANSCEIVE, recvData, 4, recvData, &unLe

499.

500. if  ( (_status != MI_OK) ||  (unLen != 0x90) )

501. {

502. _status = MI_ERR;

503. }

504.

505. return  _status;

506. }

507.508.

509.

510. char MFRC522_AntiColl( char *serNum )

511. {

512. char  _status;

513. char  i;

514. char  serNumCheck = 0;

515. unsigned  unLen;

516.

517. MFRC522_Wr( BITFRAMINGREG, 0x00  );  //TxLastBists = BitFra

518.

519. serNum[0]  = PICC_ANTICOLL;

Page 14: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 14/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 14/19

520. serNum[1]  = 0x20;

521. MFRC522_Clear_Bit( STATUS2REG, 0x08  );

522. _status = MFRC522_ToCard( PCD_TRANSCEIVE, serNum, 2, serNum, &unLen );

523.

524. if  (_status == MI_OK)

525. {

526. for  ( i=0; i < 4; i++  )

527. {

528. serNumCheck ^= serNum[i];

529. }

530.

531. if  ( serNumCheck != serNum[4]  )

532. {

533. _status = MI_ERR;

534. }

535. }

536.

537. return  _status;

538. }

539.

540.

541.  //0x0044 = Mifare_UltraLight

542.  //0x0004 = Mifare_One (S50)

543.  //0x0002 = Mifare_One (S70)

544.  //0x0008 = Mifare_Pro (X)

545.  //0x0344 = Mifare_DESFire

546. char MFRC522_isCard( char *TagType )

547. {

548. if  (MFRC522_Request( PICC_REQIDL, TagType ) == MI_OK)

549. return 1;

550. else

551. return 0;

552. }

553.

554.

555.

556. char MFRC522_ReadCardSerial( char *str )

557. {

558. char  _status;

559. _status = MFRC522_AntiColl( str );

560. str[5]  = 0;561. if  (_status == MI_OK)

562. return 1;

563. else

Page 15: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 15/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 15/19

Principais Funções:

MFRC522_Init() ‐ Inicializa o módulo.

MFRC522_Reset() ‐ Reseta o módulo MFRC522.

MFRC522_Halt() ‐ Desativa o módulo. Coloca em estado de hibernação.

MFRC522_SelectTag( char *serNum ) ‐ Seleciona o cartão, para posteriormente ser autenticado.

 serNum ‐ Número de série. 5 bytes.

MFRC522_Auth( char authMode, char BlockAddr , char *Sectorkey , char *serNum ) ‐ Realiza aautenticação do um determinado bloco para ativar a leitura e escrita deste bloco.

authMode: tipo de autenticação, A ( PICC_AUTHENT1A ) ou B ( PICC_AUTHENT1B ). BlockAddr ‐ Número do bloco. É o quarto bloco do setor.SectorKey ‐ Senha de autenticação. 6 bytes. Geralmente é definido com 0xFFFFFFFFFFFF.

 serNum ‐ Número de série do cartão. 5 bytes.

MFRC522_Write( char blockAddr , char *writeData ) ‐ Escreve 16 bytes num determinado bloco, logoapós ser autenticado.

blockAddr ‐ Número do bloco. Exceto o quarto bloco do setor, que é usado para autenticar.writeData ‐ 16 bytes a ser escrito.

MFRC522_Read( char blockAddr , char *recvData ) ‐ Faz a leitura de 16 bytes num determinado bloco,logo após ser autenticado.blockAddr ‐ Número do bloco.recvData ‐ Retorna os 16 bytes lidos.

MFRC522_isCard( char *TagType ) ‐ Verifica se há algum cartão.TagType ‐ Retorna o tipo de cartão.

MFRC522_ReadCardSerial(  char *str ) ‐ Faz a leitura do número de série do cartão. str ‐ Retorna o número de série. 5 bytes ( 4 bytes + checksum ).

ORGANIZAÇÃO DA MEMÓRIACom o módulo MFRC522 é possível ler e escrever nos cartões. Cada cartão possui um tamanho eorganização de memória diferentes. No caso do cartão MIFARE 1K: possui 16 setores, cada setorpossui 4 blocos e cada bloco contém 16 bytes, totalizando 1024 bytes ou 1K.

564. return 0;

565. }

Page 16: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 16/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 16/19

O bloco 3 de cada setor é usado para fazer a autenticação.O bloco 0 do setor 0 é somente leitura.

EXEMPLO

MikroC PRO PIC

Page 17: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 17/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 17/19

01.  //Habilitar as seguintes bibliotecas:

02.  // ‐ Software_SPI 

03.  // ‐ Conversions e C_String

04.  // ‐ Lcd 

05.

06. #include <Built_in.h>07.

08.  //copie e cole o código da biblioteca aqui!!!

09.

10.  //O Teste foi feito com cartões MIFARE 1K 

11.

12. sbit  MFRC522_CS at  RD4_Bit;

13. sbit  MFRC522_Rst at  RD3_Bit;

14. sbit  SoftSPI_SDO at  RD1_Bit;

15. sbit  SoftSPI_CLK at  RD0_Bit;

16. sbit  SoftSPI_SDI at  RD2_Bit;

17. sbit  MFRC522_CS_Direction at  TRISD4_Bit;

18. sbit  MFRC522_Rst_Direction at  TRISD3_Bit;

19. sbit  SoftSPI_SDO_Direction at  TRISD1_Bit;

20. sbit  SoftSPI_CLK_Direction at  TRISD0_Bit;

21. sbit  SoftSPI_SDI_Direction at  TRISD2_Bit;

22.

23.  // LCD module connections24. sbit  LCD_RS at  RB4_bit;

25. sbit  LCD_EN at  RB5_bit;

26. sbit  LCD_D4 at  RB0_bit;

27. sbit  LCD_D5 at  RB1_bit;

28. sbit  LCD_D6 at  RB2_bit;

29. sbit  LCD_D7 at  RB3_bit;

30.

31. sbit  LCD_RS_Direction at  TRISB4_bit;

32. sbit  LCD_EN_Direction at  TRISB5_bit;

33. sbit  LCD_D4_Direction at  TRISB0_bit;

34. sbit  LCD_D5_Direction at  TRISB1_bit;

35. sbit  LCD_D6_Direction at  TRISB2_bit;

36. sbit  LCD_D7_Direction at  TRISB3_bit;

37.  // End LCD module connections

38.

39. char  key[6]  = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

40. char  writeData[]  = "Microcontrolandos";41.

42. void main()

43. {

Page 18: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 18/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

http://microcontrolandos.blogspot.com.br/2014/02/pic-rfid-mfrc522.html 18/19

44. char  msg[12];

45. char  UID[6];

46. unsigned  TagType;

47. char  size;

48. char  i;

49.

50.  //Desativa as Portas Analogicas

51. ADCON1  = 0x0F;

52.  //Inicializa Soft SPI 

53. Soft_SPI_Init();

54.  //Inicializa display

55. Lcd_Init();

56. Lcd_Cmd(_LCD_CLEAR);

57. Lcd_Cmd(_LCD_CURSOR_OFF);

58.

59.  //inicializa o modulo RFID

60. MFRC522_Init();

61.

62. while(1)

63. {

64.  //Verifica se há algum cartão

65. if( MFRC522_isCard( &TagType ) )

66. {

67. Lcd_Cmd(_LCD_CLEAR);68.  //Exibe o tipo do cartão no display

69. ByteToHex( Lo(TagType), msg );

70. ByteToHex( Hi(TagType), msg+2  );

71. Lcd_Out( 1, 1, "TAG TYPE: "  );

72. Lcd_Out_CP( msg );

73.

74.  //Faz a leitura do numero de serie

75. if( MFRC522_ReadCardSerial( &UID ) )

76. {

77.  //Posiciona o cursor

78. Lcd_Out( 1, 1, ""  );

79.

80. for( i=0; i < 5; i++)

81. {

82. ByteToHex( UID[i], msg );

83. Lcd_Out_CP( msg );

84. }85. size = MFRC522_SelectTag( &UID );

86. }

87.

Page 19: Microcontrolandos_ PIC_ RFID MFRC522

8/19/2019 Microcontrolandos_ PIC_ RFID MFRC522

http://slidepdf.com/reader/full/microcontrolandos-pic-rfid-mfrc522 19/19

01/03/2016 Microcontrolandos: PIC: RFID MFRC522

88.  //Posiciona o cursor

89. Lcd_Out( 2, 1, ""  );

90.

91.  //Tenta realizar a autenticação A do setor 1( blocos: 4 ‐ 7 )

92.  //bloco de autenticação é o 7 

93. if( MFRC522_Auth( PICC_AUTHENT1A, 7, &key, &UID ) == 0  )

94. {

95.  //Escreve algo no bloco 4

96. MFRC522_Write( 4, &writeData );

97. }

98. else if( MFRC522_Auth( PICC_AUTHENT1B, 7, &key, &UID ) == 0  )

99. {

100.  //Escreve algo no bloco 4

101. MFRC522_Write( 4, &writeData );

102. }

103. else

104. {

105. Lcd_Out_CP( "Erro"  );

106. continue;

107. }

108.

109.  //Faz a leitura do bloco 4

110. if( MFRC522_Read( 4, &writeData ) == 0  )

111. {112. Lcd_Out( 2, 1, &writeData );

113. }

114.

115.  //Estado de hibernação

116. MFRC522_Halt();

117. }

118. }

119. }