แก้ให้แล้ว

Dependencies:   NOKIA_5110 mbed

Fork of Lost-Found_BOX by FRA221:A

Revision:
1:568e35232180
Parent:
0:8b94afcb61eb
Child:
2:5764ad3214cd
--- a/main.cpp	Sun Nov 15 19:38:36 2015 +0000
+++ b/main.cpp	Sun Dec 06 10:16:47 2015 +0000
@@ -1,7 +1,6 @@
 //Test of cheap 13.56 Mhz RFID-RC522 module from eBay
 //This code is based on Martin Olejar's MFRC522 library. Minimal changes
 //Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too
-
 //Connect as follows:
 //RFID pins        ->  Nucleo header CN5 (Arduino-compatible header)
 //----------------------------------------
@@ -9,48 +8,51 @@
 //RFID MISO=pin4   ->   Nucleo SPI_MISO=PA_6=D12
 //RFID MOSI=pin3   ->   Nucleo SPI_MOSI=PA_7=D11
 //RFID SCK=pin2    ->   Nucleo SPI_SCK =PA_5=D13
-//RFID SDA=pin1    ->   Nucleo SPI_CS  =PB_6=D10
+//RFID SDA=pin1    ->   Nucleo SPI_CS  =PB_6=D10 SlaveSelect
 //RFID RST=pin7    ->   Nucleo         =PA_9=D8
 //3.3V and Gnd to the respective pins                              
                               
 #include "mbed.h"
 #include "MFRC522.h"
-#include "NOKIA_5110.h"
-// Nucleo Pin for MFRC522 reset (pick another D pin if you need D8)
+#include "NOKIA_5110.h" // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8)
 #define MF_RESET    D8
 
 DigitalOut LedGreen(LED1);
-DigitalOut spi_address[2] = {SPI_CS,PA_9};
+DigitalOut spi_address[2] = {SPI_CS,PB_4};
 //Serial connection to PC for output
 Serial pc(SERIAL_TX, SERIAL_RX);
 PwmOut speaker(D3);
 MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
 
-
-    
-    
+void GetID(uint8_t ID[])
+{
+    for (uint8_t i = 0; i < RfChip.uid.size; i++)
+    {
+      ID[i] = RfChip.uid.uidByte[i];
+    };  
+}
 int main(void) {
-        
-        LcdPins myPins;   
-//    myPins.sce  = PB_6;   
-    myPins.sce  = PB_4; //D5
+    uint8_t RFID[10];
+    int check=0;   
+    LcdPins myPins;      
+    myPins.sce  = PB_4;  //D5 ActiveLow SlaveSelect
     myPins.rst  = PB_10; //D6
-    myPins.dc   = PA_8; //D7
-    myPins.mosi = PA_7; //SPI_MOSI;
+    myPins.dc   = PA_8;  //D7
+    myPins.mosi = PA_7;  //SPI_MOSI;
     myPins.miso = NC;
     myPins.sclk = PA_5; //SPI_SCK;    
-      NokiaLcd myLcd( myPins );
+    NokiaLcd myLcd( myPins );
  
     myLcd.InitLcd();                // LCD is reset and DDRAM is cleared
-    myLcd.DrawString("Hello");   
+    myLcd.DrawString("Welcome");   
     myLcd.stop();
-  //RfChip.start();
-  pc.printf("starting...\n");
-
-  // Init. RC522 Chip
-  RfChip.PCD_Init();
-  pc.printf("Begin RFID!\n");
-  while (true) {
+    
+    //RfChip.start();
+    pc.printf("starting...\n");
+    // Init. RC522 Chip
+    RfChip.PCD_Init();
+    pc.printf("Begin RFID!\n");
+    while (true) {
     LedGreen = 1;
     // Look for new cards
     if ( ! RfChip.PICC_IsNewCardPresent())
@@ -58,29 +60,56 @@
       wait_ms(500);
       continue;
     }
-
     // Select one of the cards
     if ( ! RfChip.PICC_ReadCardSerial())
     {
       wait_ms(500);
       continue;
     }
-
     LedGreen = 0;
-
+    
+    if(check == 1)
+    {
+        if(RFID[1] == RfChip.uid.uidByte[1])
+        {
+            pc.printf("Right\n");
+            for (uint8_t i = 0; i < RfChip.uid.size; i++)
+            {
+                RfChip.uid.uidByte[i] = '0';
+            }
+            myLcd.InitLcd();              
+            myLcd.DrawString("Right!!  ");   
+            myLcd.stop();
+            check = 0;
+            continue;
+        }
+        else 
+        {
+            pc.printf("Wrong!!\n");
+            myLcd.InitLcd();              
+            myLcd.DrawString("Wrong!!  ");   
+            myLcd.stop();
+            continue;
+        }
+    }
+    if(check == 0)
+    {
+        myLcd.InitLcd();              
+        myLcd.DrawString("Ready");   
+        myLcd.stop();
+    }
+    
     // Print Card UID
     pc.printf("Card UID: ");
     for (uint8_t i = 0; i < RfChip.uid.size; i++)
     {
-      pc.printf(" %X02", RfChip.uid.uidByte[i]);
+      pc.printf(" %X", RfChip.uid.uidByte[i]);
     }
-    pc.printf("\n\r");
-
-    // Print Card type
-    uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
-    pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
+    
+    GetID(RFID);
+    pc.printf("\n\r");   
     wait_ms(1000);
-    
-  }
+    check = 1;
+    }
   
 }
\ No newline at end of file