แก้ให้แล้ว

Dependencies:   NOKIA_5110 mbed

Fork of Lost-Found_BOX by FRA221:A

Committer:
kantapon501
Date:
Mon Dec 07 00:18:29 2015 +0000
Revision:
5:8e3bd2501b17
Parent:
4:75346bd905c0
Child:
6:6635082215c7
?????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mustwillza 0:8b94afcb61eb 1 //Test of cheap 13.56 Mhz RFID-RC522 module from eBay
mustwillza 0:8b94afcb61eb 2 //This code is based on Martin Olejar's MFRC522 library. Minimal changes
mustwillza 0:8b94afcb61eb 3 //Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too
mustwillza 0:8b94afcb61eb 4 //Connect as follows:
mustwillza 0:8b94afcb61eb 5 //RFID pins -> Nucleo header CN5 (Arduino-compatible header)
mustwillza 0:8b94afcb61eb 6 //----------------------------------------
mustwillza 0:8b94afcb61eb 7 //RFID IRQ=pin5 -> Not used. Leave open
mustwillza 0:8b94afcb61eb 8 //RFID MISO=pin4 -> Nucleo SPI_MISO=PA_6=D12
mustwillza 0:8b94afcb61eb 9 //RFID MOSI=pin3 -> Nucleo SPI_MOSI=PA_7=D11
mustwillza 0:8b94afcb61eb 10 //RFID SCK=pin2 -> Nucleo SPI_SCK =PA_5=D13
kantapon501 1:568e35232180 11 //RFID SDA=pin1 -> Nucleo SPI_CS =PB_6=D10 SlaveSelect
mustwillza 0:8b94afcb61eb 12 //RFID RST=pin7 -> Nucleo =PA_9=D8
kantapon501 5:8e3bd2501b17 13 //3.3V and Gnd to the respective pins
mustwillza 0:8b94afcb61eb 14 #include "mbed.h"
mustwillza 0:8b94afcb61eb 15 #include "MFRC522.h"
kantapon501 1:568e35232180 16 #include "NOKIA_5110.h" // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8)
kantapon501 2:5764ad3214cd 17 #define MF_RESET D9
mustwillza 4:75346bd905c0 18 #include "speaker.h"
mustwillza 0:8b94afcb61eb 19 DigitalOut LedGreen(LED1);
kantapon501 1:568e35232180 20 DigitalOut spi_address[2] = {SPI_CS,PB_4};
mustwillza 0:8b94afcb61eb 21 //Serial connection to PC for output
mustwillza 0:8b94afcb61eb 22 Serial pc(SERIAL_TX, SERIAL_RX);
mustwillza 4:75346bd905c0 23 Speakers speak(D3);
mustwillza 4:75346bd905c0 24 DigitalOut lock(PB_13);
mustwillza 0:8b94afcb61eb 25 MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
mustwillza 0:8b94afcb61eb 26
kantapon501 1:568e35232180 27 void GetID(uint8_t ID[])
kantapon501 1:568e35232180 28 {
kantapon501 1:568e35232180 29 for (uint8_t i = 0; i < RfChip.uid.size; i++)
kantapon501 1:568e35232180 30 {
kantapon501 1:568e35232180 31 ID[i] = RfChip.uid.uidByte[i];
kantapon501 1:568e35232180 32 };
kantapon501 1:568e35232180 33 }
kantapon501 5:8e3bd2501b17 34 uint8_t check(uint8_t ID[])
kantapon501 3:c927196a726f 35 {
kantapon501 5:8e3bd2501b17 36 uint8_t x=0;
kantapon501 5:8e3bd2501b17 37 for(uint8_t i = 0; i < RfChip.uid.size; i++)
kantapon501 5:8e3bd2501b17 38 {
kantapon501 5:8e3bd2501b17 39 if(ID[i] == RfChip.uid.uidByte[i]){ x = x+1;}
kantapon501 5:8e3bd2501b17 40 }
kantapon501 5:8e3bd2501b17 41 if(x == RfChip.uid.size){return 1;}
kantapon501 5:8e3bd2501b17 42 else {return 0;}
kantapon501 3:c927196a726f 43 }
mustwillza 0:8b94afcb61eb 44 int main(void) {
mustwillza 4:75346bd905c0 45 pc.baud(115200);
kantapon501 1:568e35232180 46 uint8_t RFID[10];
kantapon501 5:8e3bd2501b17 47 int checker = 0;
kantapon501 5:8e3bd2501b17 48 int checks = 0;
kantapon501 1:568e35232180 49 LcdPins myPins;
kantapon501 1:568e35232180 50 myPins.sce = PB_4; //D5 ActiveLow SlaveSelect
mustwillza 0:8b94afcb61eb 51 myPins.rst = PB_10; //D6
kantapon501 1:568e35232180 52 myPins.dc = PA_8; //D7
kantapon501 1:568e35232180 53 myPins.mosi = PA_7; //SPI_MOSI;
mustwillza 0:8b94afcb61eb 54 myPins.miso = NC;
mustwillza 0:8b94afcb61eb 55 myPins.sclk = PA_5; //SPI_SCK;
kantapon501 1:568e35232180 56 NokiaLcd myLcd( myPins );
mustwillza 0:8b94afcb61eb 57
mustwillza 0:8b94afcb61eb 58 myLcd.InitLcd(); // LCD is reset and DDRAM is cleared
kantapon501 1:568e35232180 59 myLcd.DrawString("Welcome");
mustwillza 0:8b94afcb61eb 60 myLcd.stop();
kantapon501 1:568e35232180 61
kantapon501 1:568e35232180 62 //RfChip.start();
kantapon501 1:568e35232180 63 pc.printf("starting...\n");
kantapon501 1:568e35232180 64 // Init. RC522 Chip
kantapon501 1:568e35232180 65 RfChip.PCD_Init();
kantapon501 1:568e35232180 66 pc.printf("Begin RFID!\n");
kantapon501 1:568e35232180 67 while (true) {
mustwillza 0:8b94afcb61eb 68 LedGreen = 1;
mustwillza 0:8b94afcb61eb 69 // Look for new cards
mustwillza 0:8b94afcb61eb 70 if ( ! RfChip.PICC_IsNewCardPresent())
mustwillza 0:8b94afcb61eb 71 {
mustwillza 0:8b94afcb61eb 72 wait_ms(500);
mustwillza 0:8b94afcb61eb 73 continue;
mustwillza 0:8b94afcb61eb 74 }
mustwillza 0:8b94afcb61eb 75 // Select one of the cards
mustwillza 0:8b94afcb61eb 76 if ( ! RfChip.PICC_ReadCardSerial())
mustwillza 0:8b94afcb61eb 77 {
mustwillza 0:8b94afcb61eb 78 wait_ms(500);
mustwillza 0:8b94afcb61eb 79 continue;
mustwillza 0:8b94afcb61eb 80 }
mustwillza 0:8b94afcb61eb 81 LedGreen = 0;
kantapon501 5:8e3bd2501b17 82 myLcd.InitLcd();
kantapon501 5:8e3bd2501b17 83 myLcd.DrawString("Ready");
kantapon501 5:8e3bd2501b17 84 myLcd.stop();
kantapon501 5:8e3bd2501b17 85 checker = check(RFID);
kantapon501 5:8e3bd2501b17 86 if(checks == 1)
kantapon501 1:568e35232180 87 {
kantapon501 5:8e3bd2501b17 88 if (checker == 1)
kantapon501 1:568e35232180 89 {
kantapon501 5:8e3bd2501b17 90 pc.printf("Right\n");
kantapon501 5:8e3bd2501b17 91 myLcd.InitLcd();
kantapon501 5:8e3bd2501b17 92 myLcd.DrawString("Right");
kantapon501 5:8e3bd2501b17 93 myLcd.stop();
kantapon501 5:8e3bd2501b17 94 checks = 0;
kantapon501 5:8e3bd2501b17 95 speak.speakRight();
kantapon501 5:8e3bd2501b17 96 continue;
kantapon501 1:568e35232180 97 }
kantapon501 5:8e3bd2501b17 98 else
kantapon501 5:8e3bd2501b17 99 {
kantapon501 5:8e3bd2501b17 100 pc.printf("Wrong!!\n");
kantapon501 5:8e3bd2501b17 101 myLcd.InitLcd();
kantapon501 5:8e3bd2501b17 102 myLcd.DrawString("Wrong!!");
kantapon501 5:8e3bd2501b17 103 myLcd.stop();
kantapon501 5:8e3bd2501b17 104 speak.speakWrong();
kantapon501 5:8e3bd2501b17 105 continue;
kantapon501 5:8e3bd2501b17 106 }
kantapon501 1:568e35232180 107 }
mustwillza 0:8b94afcb61eb 108 // Print Card UID
mustwillza 0:8b94afcb61eb 109 pc.printf("Card UID: ");
mustwillza 0:8b94afcb61eb 110 for (uint8_t i = 0; i < RfChip.uid.size; i++)
mustwillza 0:8b94afcb61eb 111 {
kantapon501 1:568e35232180 112 pc.printf(" %X", RfChip.uid.uidByte[i]);
mustwillza 0:8b94afcb61eb 113 }
kantapon501 5:8e3bd2501b17 114 checker = check(RFID);
mustwillza 4:75346bd905c0 115 speak.speak();
kantapon501 1:568e35232180 116 GetID(RFID);
kantapon501 3:c927196a726f 117 lock = 0;
kantapon501 1:568e35232180 118 pc.printf("\n\r");
mustwillza 0:8b94afcb61eb 119 wait_ms(1000);
kantapon501 5:8e3bd2501b17 120 checks = 1;
kantapon501 1:568e35232180 121 }
mustwillza 0:8b94afcb61eb 122
mustwillza 0:8b94afcb61eb 123 }