projet rfid

Dependencies:   BSP_DISCO_F429ZI LCD_DISCO_F429ZI mbed

Committer:
Carobon
Date:
Thu Mar 15 08:50:12 2018 +0000
Revision:
0:7321514266d7
STM32RC522

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Carobon 0:7321514266d7 1 //Test of cheap 13.56 Mhz RFID-RC522 module from eBay
Carobon 0:7321514266d7 2 //This code is based on Martin Olejar's MFRC522 library. Minimal changes
Carobon 0:7321514266d7 3 //Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too
Carobon 0:7321514266d7 4
Carobon 0:7321514266d7 5 //Connect as follows:
Carobon 0:7321514266d7 6 //RFID pins -> STM32F429
Carobon 0:7321514266d7 7 //----------------------------------------
Carobon 0:7321514266d7 8 //RFID IRQ=pin5 -> Not used. Leave open
Carobon 0:7321514266d7 9 //RFID MISO=pin4 -> SPI_MISO =PA_6=P42
Carobon 0:7321514266d7 10 //RFID MOSI=pin3 -> SPI_MOSI =PA_7=P43
Carobon 0:7321514266d7 11 //RFID SCK=pin2 -> SPI_SCK =PA_5=P41
Carobon 0:7321514266d7 12 //RFID SDA=pin1 -> SPI_SDA =PA_4=P40
Carobon 0:7321514266d7 13 //RFID RST=pin7 -> =NRST=P25
Carobon 0:7321514266d7 14 //3.3V and Gnd to the respective pins
Carobon 0:7321514266d7 15
Carobon 0:7321514266d7 16 #include "mbed.h"
Carobon 0:7321514266d7 17 #include "MFRC522.h"
Carobon 0:7321514266d7 18 #include "LCD_DISCO_F429ZI.h"
Carobon 0:7321514266d7 19
Carobon 0:7321514266d7 20 LCD_DISCO_F429ZI lcd;
Carobon 0:7321514266d7 21
Carobon 0:7321514266d7 22 // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8)
Carobon 0:7321514266d7 23 //#define MF_RESET p25
Carobon 0:7321514266d7 24
Carobon 0:7321514266d7 25
Carobon 0:7321514266d7 26 DigitalOut led_green(LED1);
Carobon 0:7321514266d7 27
Carobon 0:7321514266d7 28 //Serial connection to PC for output
Carobon 0:7321514266d7 29 //Serial pc(SERIAL_TX, SERIAL_RX);//???
Carobon 0:7321514266d7 30
Carobon 0:7321514266d7 31 //MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, PA_0);
Carobon 0:7321514266d7 32
Carobon 0:7321514266d7 33 int main(void) {
Carobon 0:7321514266d7 34
Carobon 0:7321514266d7 35 lcd.Clear(LCD_COLOR_WHITE);
Carobon 0:7321514266d7 36 lcd.SetTextColor(LCD_COLOR_BLACK);
Carobon 0:7321514266d7 37 lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"starting...", CENTER_MODE);
Carobon 0:7321514266d7 38
Carobon 0:7321514266d7 39
Carobon 0:7321514266d7 40 Init. RC522 Chip
Carobon 0:7321514266d7 41 RfChip.PCD_Init();
Carobon 0:7321514266d7 42
Carobon 0:7321514266d7 43 while (true) {
Carobon 0:7321514266d7 44 led_green = 1;//ok
Carobon 0:7321514266d7 45
Carobon 0:7321514266d7 46 // Look for new cards
Carobon 0:7321514266d7 47 if ( !RfChip.PICC_IsNewCardPresent()){
Carobon 0:7321514266d7 48 wait_ms(500);
Carobon 0:7321514266d7 49 continue;
Carobon 0:7321514266d7 50 }
Carobon 0:7321514266d7 51
Carobon 0:7321514266d7 52 // Select one of the cards
Carobon 0:7321514266d7 53 if ( ! RfChip.PICC_ReadCardSerial()){
Carobon 0:7321514266d7 54 wait_ms(500);
Carobon 0:7321514266d7 55 continue;
Carobon 0:7321514266d7 56 }
Carobon 0:7321514266d7 57
Carobon 0:7321514266d7 58 led_green = 0;//ok
Carobon 0:7321514266d7 59
Carobon 0:7321514266d7 60 // Print Card UID
Carobon 0:7321514266d7 61 pc.printf("Card UID: ");
Carobon 0:7321514266d7 62 for (uint8_t i = 0; i < RfChip.uid.size; i++){
Carobon 0:7321514266d7 63 pc.printf(" %X02", RfChip.uid.uidByte[i]);
Carobon 0:7321514266d7 64 }
Carobon 0:7321514266d7 65 //pc.printf("\n\r");
Carobon 0:7321514266d7 66 //lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"\n\r", CENTER_MODE);
Carobon 0:7321514266d7 67
Carobon 0:7321514266d7 68 // Print Card type
Carobon 0:7321514266d7 69 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
Carobon 0:7321514266d7 70 pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
Carobon 0:7321514266d7 71 wait_ms(1000);
Carobon 0:7321514266d7 72 }
Carobon 0:7321514266d7 73 }