แก้ให้แล้ว

Dependencies:   NOKIA_5110 mbed

Fork of Lost-Found_BOX by FRA221:A

Committer:
kantapon501
Date:
Mon Dec 07 13:27:25 2015 +0000
Revision:
6:6635082215c7
Parent:
5:8e3bd2501b17
Child:
7:725f5fb7e3a3
??????

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);
kantapon501 6:6635082215c7 26 char buffers[5]={'\0'};
kantapon501 6:6635082215c7 27 char IDs[17]={'\0'};
kantapon501 1:568e35232180 28 void GetID(uint8_t ID[])
kantapon501 1:568e35232180 29 {
kantapon501 1:568e35232180 30 for (uint8_t i = 0; i < RfChip.uid.size; i++)
kantapon501 1:568e35232180 31 {
kantapon501 6:6635082215c7 32 ID[i] = RfChip.uid.uidByte[i];
kantapon501 6:6635082215c7 33 sprintf(buffers,"%X",RfChip.uid.uidByte[i]);
kantapon501 6:6635082215c7 34 strcat(IDs,buffers);
kantapon501 1:568e35232180 35 };
kantapon501 1:568e35232180 36 }
kantapon501 5:8e3bd2501b17 37 uint8_t check(uint8_t ID[])
kantapon501 3:c927196a726f 38 {
kantapon501 5:8e3bd2501b17 39 uint8_t x=0;
kantapon501 5:8e3bd2501b17 40 for(uint8_t i = 0; i < RfChip.uid.size; i++)
kantapon501 5:8e3bd2501b17 41 {
kantapon501 5:8e3bd2501b17 42 if(ID[i] == RfChip.uid.uidByte[i]){ x = x+1;}
kantapon501 5:8e3bd2501b17 43 }
kantapon501 5:8e3bd2501b17 44 if(x == RfChip.uid.size){return 1;}
kantapon501 5:8e3bd2501b17 45 else {return 0;}
kantapon501 3:c927196a726f 46 }
mustwillza 0:8b94afcb61eb 47 int main(void) {
mustwillza 4:75346bd905c0 48 pc.baud(115200);
kantapon501 1:568e35232180 49 uint8_t RFID[10];
kantapon501 5:8e3bd2501b17 50 int checker = 0;
kantapon501 5:8e3bd2501b17 51 int checks = 0;
kantapon501 1:568e35232180 52 LcdPins myPins;
kantapon501 1:568e35232180 53 myPins.sce = PB_4; //D5 ActiveLow SlaveSelect
mustwillza 0:8b94afcb61eb 54 myPins.rst = PB_10; //D6
kantapon501 1:568e35232180 55 myPins.dc = PA_8; //D7
kantapon501 1:568e35232180 56 myPins.mosi = PA_7; //SPI_MOSI;
mustwillza 0:8b94afcb61eb 57 myPins.miso = NC;
mustwillza 0:8b94afcb61eb 58 myPins.sclk = PA_5; //SPI_SCK;
kantapon501 1:568e35232180 59 NokiaLcd myLcd( myPins );
mustwillza 0:8b94afcb61eb 60
mustwillza 0:8b94afcb61eb 61 myLcd.InitLcd(); // LCD is reset and DDRAM is cleared
kantapon501 1:568e35232180 62 myLcd.DrawString("Welcome");
mustwillza 0:8b94afcb61eb 63 myLcd.stop();
kantapon501 1:568e35232180 64
kantapon501 1:568e35232180 65 //RfChip.start();
kantapon501 1:568e35232180 66 pc.printf("starting...\n");
kantapon501 1:568e35232180 67 // Init. RC522 Chip
kantapon501 1:568e35232180 68 RfChip.PCD_Init();
kantapon501 1:568e35232180 69 pc.printf("Begin RFID!\n");
kantapon501 1:568e35232180 70 while (true) {
mustwillza 0:8b94afcb61eb 71 LedGreen = 1;
mustwillza 0:8b94afcb61eb 72 // Look for new cards
mustwillza 0:8b94afcb61eb 73 if ( ! RfChip.PICC_IsNewCardPresent())
mustwillza 0:8b94afcb61eb 74 {
mustwillza 0:8b94afcb61eb 75 wait_ms(500);
mustwillza 0:8b94afcb61eb 76 continue;
mustwillza 0:8b94afcb61eb 77 }
mustwillza 0:8b94afcb61eb 78 // Select one of the cards
mustwillza 0:8b94afcb61eb 79 if ( ! RfChip.PICC_ReadCardSerial())
mustwillza 0:8b94afcb61eb 80 {
mustwillza 0:8b94afcb61eb 81 wait_ms(500);
mustwillza 0:8b94afcb61eb 82 continue;
mustwillza 0:8b94afcb61eb 83 }
mustwillza 0:8b94afcb61eb 84 LedGreen = 0;
kantapon501 5:8e3bd2501b17 85 myLcd.InitLcd();
kantapon501 5:8e3bd2501b17 86 myLcd.DrawString("Ready");
kantapon501 5:8e3bd2501b17 87 myLcd.stop();
kantapon501 5:8e3bd2501b17 88 checker = check(RFID);
kantapon501 5:8e3bd2501b17 89 if(checks == 1)
kantapon501 1:568e35232180 90 {
kantapon501 5:8e3bd2501b17 91 if (checker == 1)
kantapon501 1:568e35232180 92 {
kantapon501 5:8e3bd2501b17 93 pc.printf("Right\n");
kantapon501 5:8e3bd2501b17 94 myLcd.InitLcd();
kantapon501 5:8e3bd2501b17 95 myLcd.DrawString("Right");
kantapon501 5:8e3bd2501b17 96 myLcd.stop();
kantapon501 5:8e3bd2501b17 97 checks = 0;
kantapon501 5:8e3bd2501b17 98 speak.speakRight();
kantapon501 6:6635082215c7 99 strcpy(IDs,"");
kantapon501 5:8e3bd2501b17 100 continue;
kantapon501 1:568e35232180 101 }
kantapon501 5:8e3bd2501b17 102 else
kantapon501 5:8e3bd2501b17 103 {
kantapon501 5:8e3bd2501b17 104 pc.printf("Wrong!!\n");
kantapon501 5:8e3bd2501b17 105 myLcd.InitLcd();
kantapon501 5:8e3bd2501b17 106 myLcd.DrawString("Wrong!!");
kantapon501 5:8e3bd2501b17 107 myLcd.stop();
kantapon501 5:8e3bd2501b17 108 speak.speakWrong();
kantapon501 5:8e3bd2501b17 109 continue;
kantapon501 5:8e3bd2501b17 110 }
kantapon501 1:568e35232180 111 }
mustwillza 0:8b94afcb61eb 112 // Print Card UID
mustwillza 0:8b94afcb61eb 113 pc.printf("Card UID: ");
mustwillza 0:8b94afcb61eb 114 for (uint8_t i = 0; i < RfChip.uid.size; i++)
mustwillza 0:8b94afcb61eb 115 {
kantapon501 1:568e35232180 116 pc.printf(" %X", RfChip.uid.uidByte[i]);
mustwillza 0:8b94afcb61eb 117 }
kantapon501 5:8e3bd2501b17 118 checker = check(RFID);
mustwillza 4:75346bd905c0 119 speak.speak();
kantapon501 1:568e35232180 120 GetID(RFID);
kantapon501 3:c927196a726f 121 lock = 0;
kantapon501 6:6635082215c7 122 pc.printf("\n\r");
mustwillza 0:8b94afcb61eb 123 wait_ms(1000);
kantapon501 5:8e3bd2501b17 124 checks = 1;
kantapon501 1:568e35232180 125 }
mustwillza 0:8b94afcb61eb 126
mustwillza 0:8b94afcb61eb 127 }