แก้ให้แล้ว

Dependencies:   NOKIA_5110 mbed

Fork of Lost-Found_BOX by FRA221:A

Committer:
kantapon501
Date:
Sun Dec 06 22:51:40 2015 +0000
Revision:
3:c927196a726f
Parent:
2:5764ad3214cd
Child:
4:75346bd905c0
??????????

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 0:8b94afcb61eb 19
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 0:8b94afcb61eb 24 PwmOut speaker(D3);
kantapon501 3:c927196a726f 25 DigitalOut lock(D2);
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 }
kantapon501 3:c927196a726f 35 void speak()
kantapon501 3:c927196a726f 36 {
kantapon501 3:c927196a726f 37 speaker.period_ms(1);
kantapon501 3:c927196a726f 38 speaker.write(0.1f);
kantapon501 3:c927196a726f 39 wait(0.2);
kantapon501 3:c927196a726f 40 speaker.write(0);
kantapon501 3:c927196a726f 41 }
kantapon501 3:c927196a726f 42 void speakRight()
kantapon501 3:c927196a726f 43 {
kantapon501 3:c927196a726f 44 speaker.period_ms(1);
kantapon501 3:c927196a726f 45 speaker.write(0.1f);
kantapon501 3:c927196a726f 46 wait(1);
kantapon501 3:c927196a726f 47 speaker.write(0);
kantapon501 3:c927196a726f 48 }
kantapon501 3:c927196a726f 49 void speakWrong()
kantapon501 3:c927196a726f 50 {
kantapon501 3:c927196a726f 51 speaker.period_ms(1);
kantapon501 3:c927196a726f 52 speaker.write(0.1f);
kantapon501 3:c927196a726f 53 wait(0.2);
kantapon501 3:c927196a726f 54 speaker.write(0);
kantapon501 3:c927196a726f 55 wait(0.1);
kantapon501 3:c927196a726f 56 speaker.write(0.1f);
kantapon501 3:c927196a726f 57 wait(0.2);
kantapon501 3:c927196a726f 58 speaker.write(0);
kantapon501 3:c927196a726f 59 }
kantapon501 3:c927196a726f 60 void checkRight()
kantapon501 3:c927196a726f 61 {
kantapon501 3:c927196a726f 62 }
mustwillza 0:8b94afcb61eb 63 int main(void) {
kantapon501 1:568e35232180 64 uint8_t RFID[10];
kantapon501 3:c927196a726f 65 int check=0;
kantapon501 1:568e35232180 66 LcdPins myPins;
kantapon501 1:568e35232180 67 myPins.sce = PB_4; //D5 ActiveLow SlaveSelect
mustwillza 0:8b94afcb61eb 68 myPins.rst = PB_10; //D6
kantapon501 1:568e35232180 69 myPins.dc = PA_8; //D7
kantapon501 1:568e35232180 70 myPins.mosi = PA_7; //SPI_MOSI;
mustwillza 0:8b94afcb61eb 71 myPins.miso = NC;
mustwillza 0:8b94afcb61eb 72 myPins.sclk = PA_5; //SPI_SCK;
kantapon501 1:568e35232180 73 NokiaLcd myLcd( myPins );
mustwillza 0:8b94afcb61eb 74
mustwillza 0:8b94afcb61eb 75 myLcd.InitLcd(); // LCD is reset and DDRAM is cleared
kantapon501 1:568e35232180 76 myLcd.DrawString("Welcome");
mustwillza 0:8b94afcb61eb 77 myLcd.stop();
kantapon501 1:568e35232180 78
kantapon501 1:568e35232180 79 //RfChip.start();
kantapon501 1:568e35232180 80 pc.printf("starting...\n");
kantapon501 1:568e35232180 81 // Init. RC522 Chip
kantapon501 1:568e35232180 82 RfChip.PCD_Init();
kantapon501 1:568e35232180 83 pc.printf("Begin RFID!\n");
kantapon501 1:568e35232180 84 while (true) {
mustwillza 0:8b94afcb61eb 85 LedGreen = 1;
mustwillza 0:8b94afcb61eb 86 // Look for new cards
mustwillza 0:8b94afcb61eb 87 if ( ! RfChip.PICC_IsNewCardPresent())
mustwillza 0:8b94afcb61eb 88 {
mustwillza 0:8b94afcb61eb 89 wait_ms(500);
mustwillza 0:8b94afcb61eb 90 continue;
mustwillza 0:8b94afcb61eb 91 }
mustwillza 0:8b94afcb61eb 92 // Select one of the cards
mustwillza 0:8b94afcb61eb 93 if ( ! RfChip.PICC_ReadCardSerial())
mustwillza 0:8b94afcb61eb 94 {
mustwillza 0:8b94afcb61eb 95 wait_ms(500);
mustwillza 0:8b94afcb61eb 96 continue;
mustwillza 0:8b94afcb61eb 97 }
mustwillza 0:8b94afcb61eb 98 LedGreen = 0;
kantapon501 1:568e35232180 99
kantapon501 1:568e35232180 100 if(check == 1)
kantapon501 1:568e35232180 101 {
kantapon501 1:568e35232180 102 if(RFID[1] == RfChip.uid.uidByte[1])
kantapon501 1:568e35232180 103 {
kantapon501 1:568e35232180 104 pc.printf("Right\n");
kantapon501 1:568e35232180 105 for (uint8_t i = 0; i < RfChip.uid.size; i++)
kantapon501 1:568e35232180 106 {
kantapon501 1:568e35232180 107 RfChip.uid.uidByte[i] = '0';
kantapon501 1:568e35232180 108 }
kantapon501 1:568e35232180 109 myLcd.InitLcd();
kantapon501 1:568e35232180 110 myLcd.DrawString("Right!! ");
kantapon501 1:568e35232180 111 myLcd.stop();
kantapon501 1:568e35232180 112 check = 0;
kantapon501 3:c927196a726f 113 lock = 1;
kantapon501 3:c927196a726f 114 speakRight();
kantapon501 3:c927196a726f 115 wait(2.5);
kantapon501 3:c927196a726f 116 lock = 0;
kantapon501 1:568e35232180 117 continue;
kantapon501 1:568e35232180 118 }
kantapon501 1:568e35232180 119 else
kantapon501 1:568e35232180 120 {
kantapon501 1:568e35232180 121 pc.printf("Wrong!!\n");
kantapon501 1:568e35232180 122 myLcd.InitLcd();
kantapon501 1:568e35232180 123 myLcd.DrawString("Wrong!! ");
kantapon501 1:568e35232180 124 myLcd.stop();
kantapon501 3:c927196a726f 125 speakWrong();
kantapon501 1:568e35232180 126 continue;
kantapon501 1:568e35232180 127 }
kantapon501 1:568e35232180 128 }
kantapon501 1:568e35232180 129 if(check == 0)
kantapon501 1:568e35232180 130 {
kantapon501 1:568e35232180 131 myLcd.InitLcd();
kantapon501 1:568e35232180 132 myLcd.DrawString("Ready");
kantapon501 1:568e35232180 133 myLcd.stop();
kantapon501 1:568e35232180 134 }
kantapon501 1:568e35232180 135
mustwillza 0:8b94afcb61eb 136 // Print Card UID
mustwillza 0:8b94afcb61eb 137 pc.printf("Card UID: ");
mustwillza 0:8b94afcb61eb 138 for (uint8_t i = 0; i < RfChip.uid.size; i++)
mustwillza 0:8b94afcb61eb 139 {
kantapon501 1:568e35232180 140 pc.printf(" %X", RfChip.uid.uidByte[i]);
mustwillza 0:8b94afcb61eb 141 }
kantapon501 3:c927196a726f 142 speak();
kantapon501 1:568e35232180 143 GetID(RFID);
kantapon501 3:c927196a726f 144 lock = 0;
kantapon501 1:568e35232180 145 pc.printf("\n\r");
mustwillza 0:8b94afcb61eb 146 wait_ms(1000);
kantapon501 1:568e35232180 147 check = 1;
kantapon501 1:568e35232180 148 }
mustwillza 0:8b94afcb61eb 149
mustwillza 0:8b94afcb61eb 150 }