แก้ให้แล้ว

Dependencies:   NOKIA_5110 mbed

Fork of Lost-Found_BOX by FRA221:A

Committer:
mustwillza
Date:
Sun Dec 06 23:22:21 2015 +0000
Revision:
4:75346bd905c0
Parent:
3:c927196a726f
Child:
5:8e3bd2501b17
Add : Speakers

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
mustwillza 0:8b94afcb61eb 13 //3.3V and Gnd to the respective pins
mustwillza 0:8b94afcb61eb 14
mustwillza 0:8b94afcb61eb 15 #include "mbed.h"
mustwillza 0:8b94afcb61eb 16 #include "MFRC522.h"
kantapon501 1:568e35232180 17 #include "NOKIA_5110.h" // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8)
kantapon501 2:5764ad3214cd 18 #define MF_RESET D9
mustwillza 4:75346bd905c0 19 #include "speaker.h"
mustwillza 0:8b94afcb61eb 20 DigitalOut LedGreen(LED1);
kantapon501 1:568e35232180 21 DigitalOut spi_address[2] = {SPI_CS,PB_4};
mustwillza 0:8b94afcb61eb 22 //Serial connection to PC for output
mustwillza 0:8b94afcb61eb 23 Serial pc(SERIAL_TX, SERIAL_RX);
mustwillza 4:75346bd905c0 24 Speakers speak(D3);
mustwillza 4:75346bd905c0 25 DigitalOut lock(PB_13);
mustwillza 0:8b94afcb61eb 26 MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
mustwillza 0:8b94afcb61eb 27
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 1:568e35232180 32 ID[i] = RfChip.uid.uidByte[i];
kantapon501 1:568e35232180 33 };
kantapon501 1:568e35232180 34 }
mustwillza 4:75346bd905c0 35
kantapon501 3:c927196a726f 36 void checkRight()
kantapon501 3:c927196a726f 37 {
kantapon501 3:c927196a726f 38 }
mustwillza 0:8b94afcb61eb 39 int main(void) {
mustwillza 4:75346bd905c0 40 pc.baud(115200);
kantapon501 1:568e35232180 41 uint8_t RFID[10];
kantapon501 3:c927196a726f 42 int check=0;
kantapon501 1:568e35232180 43 LcdPins myPins;
kantapon501 1:568e35232180 44 myPins.sce = PB_4; //D5 ActiveLow SlaveSelect
mustwillza 0:8b94afcb61eb 45 myPins.rst = PB_10; //D6
kantapon501 1:568e35232180 46 myPins.dc = PA_8; //D7
kantapon501 1:568e35232180 47 myPins.mosi = PA_7; //SPI_MOSI;
mustwillza 0:8b94afcb61eb 48 myPins.miso = NC;
mustwillza 0:8b94afcb61eb 49 myPins.sclk = PA_5; //SPI_SCK;
kantapon501 1:568e35232180 50 NokiaLcd myLcd( myPins );
mustwillza 0:8b94afcb61eb 51
mustwillza 0:8b94afcb61eb 52 myLcd.InitLcd(); // LCD is reset and DDRAM is cleared
kantapon501 1:568e35232180 53 myLcd.DrawString("Welcome");
mustwillza 0:8b94afcb61eb 54 myLcd.stop();
kantapon501 1:568e35232180 55
kantapon501 1:568e35232180 56 //RfChip.start();
kantapon501 1:568e35232180 57 pc.printf("starting...\n");
kantapon501 1:568e35232180 58 // Init. RC522 Chip
kantapon501 1:568e35232180 59 RfChip.PCD_Init();
kantapon501 1:568e35232180 60 pc.printf("Begin RFID!\n");
kantapon501 1:568e35232180 61 while (true) {
mustwillza 0:8b94afcb61eb 62 LedGreen = 1;
mustwillza 0:8b94afcb61eb 63 // Look for new cards
mustwillza 0:8b94afcb61eb 64 if ( ! RfChip.PICC_IsNewCardPresent())
mustwillza 0:8b94afcb61eb 65 {
mustwillza 0:8b94afcb61eb 66 wait_ms(500);
mustwillza 0:8b94afcb61eb 67 continue;
mustwillza 0:8b94afcb61eb 68 }
mustwillza 0:8b94afcb61eb 69 // Select one of the cards
mustwillza 0:8b94afcb61eb 70 if ( ! RfChip.PICC_ReadCardSerial())
mustwillza 0:8b94afcb61eb 71 {
mustwillza 0:8b94afcb61eb 72 wait_ms(500);
mustwillza 0:8b94afcb61eb 73 continue;
mustwillza 0:8b94afcb61eb 74 }
mustwillza 0:8b94afcb61eb 75 LedGreen = 0;
kantapon501 1:568e35232180 76
kantapon501 1:568e35232180 77 if(check == 1)
kantapon501 1:568e35232180 78 {
kantapon501 1:568e35232180 79 if(RFID[1] == RfChip.uid.uidByte[1])
kantapon501 1:568e35232180 80 {
kantapon501 1:568e35232180 81 pc.printf("Right\n");
kantapon501 1:568e35232180 82 for (uint8_t i = 0; i < RfChip.uid.size; i++)
kantapon501 1:568e35232180 83 {
kantapon501 1:568e35232180 84 RfChip.uid.uidByte[i] = '0';
kantapon501 1:568e35232180 85 }
kantapon501 1:568e35232180 86 myLcd.InitLcd();
kantapon501 1:568e35232180 87 myLcd.DrawString("Right!! ");
kantapon501 1:568e35232180 88 myLcd.stop();
kantapon501 1:568e35232180 89 check = 0;
kantapon501 3:c927196a726f 90 lock = 1;
mustwillza 4:75346bd905c0 91 speak.speakRight();
kantapon501 3:c927196a726f 92 wait(2.5);
kantapon501 3:c927196a726f 93 lock = 0;
kantapon501 1:568e35232180 94 continue;
kantapon501 1:568e35232180 95 }
kantapon501 1:568e35232180 96 else
kantapon501 1:568e35232180 97 {
kantapon501 1:568e35232180 98 pc.printf("Wrong!!\n");
kantapon501 1:568e35232180 99 myLcd.InitLcd();
kantapon501 1:568e35232180 100 myLcd.DrawString("Wrong!! ");
kantapon501 1:568e35232180 101 myLcd.stop();
mustwillza 4:75346bd905c0 102 speak.speakWrong();
kantapon501 1:568e35232180 103 continue;
kantapon501 1:568e35232180 104 }
kantapon501 1:568e35232180 105 }
kantapon501 1:568e35232180 106 if(check == 0)
kantapon501 1:568e35232180 107 {
kantapon501 1:568e35232180 108 myLcd.InitLcd();
kantapon501 1:568e35232180 109 myLcd.DrawString("Ready");
kantapon501 1:568e35232180 110 myLcd.stop();
kantapon501 1:568e35232180 111 }
kantapon501 1:568e35232180 112
mustwillza 0:8b94afcb61eb 113 // Print Card UID
mustwillza 0:8b94afcb61eb 114 pc.printf("Card UID: ");
mustwillza 0:8b94afcb61eb 115 for (uint8_t i = 0; i < RfChip.uid.size; i++)
mustwillza 0:8b94afcb61eb 116 {
kantapon501 1:568e35232180 117 pc.printf(" %X", RfChip.uid.uidByte[i]);
mustwillza 0:8b94afcb61eb 118 }
mustwillza 4:75346bd905c0 119 speak.speak();
kantapon501 1:568e35232180 120 GetID(RFID);
kantapon501 3:c927196a726f 121 lock = 0;
kantapon501 1:568e35232180 122 pc.printf("\n\r");
mustwillza 0:8b94afcb61eb 123 wait_ms(1000);
kantapon501 1:568e35232180 124 check = 1;
kantapon501 1:568e35232180 125 }
mustwillza 0:8b94afcb61eb 126
mustwillza 0:8b94afcb61eb 127 }