แก้ให้แล้ว
Dependencies: NOKIA_5110 mbed
Fork of Lost-Found_BOX by
main.cpp@2:5764ad3214cd, 2015-12-06 (annotated)
- Committer:
- kantapon501
- Date:
- Sun Dec 06 10:23:43 2015 +0000
- Revision:
- 2:5764ad3214cd
- Parent:
- 1:568e35232180
- Child:
- 3:c927196a726f
?????????????
Who changed what in which revision?
User | Revision | Line number | New 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); |
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 | } |
mustwillza | 0:8b94afcb61eb | 34 | int main(void) { |
kantapon501 | 1:568e35232180 | 35 | uint8_t RFID[10]; |
kantapon501 | 1:568e35232180 | 36 | int check=0; |
kantapon501 | 1:568e35232180 | 37 | LcdPins myPins; |
kantapon501 | 1:568e35232180 | 38 | myPins.sce = PB_4; //D5 ActiveLow SlaveSelect |
mustwillza | 0:8b94afcb61eb | 39 | myPins.rst = PB_10; //D6 |
kantapon501 | 1:568e35232180 | 40 | myPins.dc = PA_8; //D7 |
kantapon501 | 1:568e35232180 | 41 | myPins.mosi = PA_7; //SPI_MOSI; |
mustwillza | 0:8b94afcb61eb | 42 | myPins.miso = NC; |
mustwillza | 0:8b94afcb61eb | 43 | myPins.sclk = PA_5; //SPI_SCK; |
kantapon501 | 1:568e35232180 | 44 | NokiaLcd myLcd( myPins ); |
mustwillza | 0:8b94afcb61eb | 45 | |
mustwillza | 0:8b94afcb61eb | 46 | myLcd.InitLcd(); // LCD is reset and DDRAM is cleared |
kantapon501 | 1:568e35232180 | 47 | myLcd.DrawString("Welcome"); |
mustwillza | 0:8b94afcb61eb | 48 | myLcd.stop(); |
kantapon501 | 1:568e35232180 | 49 | |
kantapon501 | 1:568e35232180 | 50 | //RfChip.start(); |
kantapon501 | 1:568e35232180 | 51 | pc.printf("starting...\n"); |
kantapon501 | 1:568e35232180 | 52 | // Init. RC522 Chip |
kantapon501 | 1:568e35232180 | 53 | RfChip.PCD_Init(); |
kantapon501 | 1:568e35232180 | 54 | pc.printf("Begin RFID!\n"); |
kantapon501 | 1:568e35232180 | 55 | while (true) { |
mustwillza | 0:8b94afcb61eb | 56 | LedGreen = 1; |
mustwillza | 0:8b94afcb61eb | 57 | // Look for new cards |
mustwillza | 0:8b94afcb61eb | 58 | if ( ! RfChip.PICC_IsNewCardPresent()) |
mustwillza | 0:8b94afcb61eb | 59 | { |
mustwillza | 0:8b94afcb61eb | 60 | wait_ms(500); |
mustwillza | 0:8b94afcb61eb | 61 | continue; |
mustwillza | 0:8b94afcb61eb | 62 | } |
mustwillza | 0:8b94afcb61eb | 63 | // Select one of the cards |
mustwillza | 0:8b94afcb61eb | 64 | if ( ! RfChip.PICC_ReadCardSerial()) |
mustwillza | 0:8b94afcb61eb | 65 | { |
mustwillza | 0:8b94afcb61eb | 66 | wait_ms(500); |
mustwillza | 0:8b94afcb61eb | 67 | continue; |
mustwillza | 0:8b94afcb61eb | 68 | } |
mustwillza | 0:8b94afcb61eb | 69 | LedGreen = 0; |
kantapon501 | 1:568e35232180 | 70 | |
kantapon501 | 1:568e35232180 | 71 | if(check == 1) |
kantapon501 | 1:568e35232180 | 72 | { |
kantapon501 | 1:568e35232180 | 73 | if(RFID[1] == RfChip.uid.uidByte[1]) |
kantapon501 | 1:568e35232180 | 74 | { |
kantapon501 | 1:568e35232180 | 75 | pc.printf("Right\n"); |
kantapon501 | 1:568e35232180 | 76 | for (uint8_t i = 0; i < RfChip.uid.size; i++) |
kantapon501 | 1:568e35232180 | 77 | { |
kantapon501 | 1:568e35232180 | 78 | RfChip.uid.uidByte[i] = '0'; |
kantapon501 | 1:568e35232180 | 79 | } |
kantapon501 | 1:568e35232180 | 80 | myLcd.InitLcd(); |
kantapon501 | 1:568e35232180 | 81 | myLcd.DrawString("Right!! "); |
kantapon501 | 1:568e35232180 | 82 | myLcd.stop(); |
kantapon501 | 1:568e35232180 | 83 | check = 0; |
kantapon501 | 1:568e35232180 | 84 | continue; |
kantapon501 | 1:568e35232180 | 85 | } |
kantapon501 | 1:568e35232180 | 86 | else |
kantapon501 | 1:568e35232180 | 87 | { |
kantapon501 | 1:568e35232180 | 88 | pc.printf("Wrong!!\n"); |
kantapon501 | 1:568e35232180 | 89 | myLcd.InitLcd(); |
kantapon501 | 1:568e35232180 | 90 | myLcd.DrawString("Wrong!! "); |
kantapon501 | 1:568e35232180 | 91 | myLcd.stop(); |
kantapon501 | 1:568e35232180 | 92 | continue; |
kantapon501 | 1:568e35232180 | 93 | } |
kantapon501 | 1:568e35232180 | 94 | } |
kantapon501 | 1:568e35232180 | 95 | if(check == 0) |
kantapon501 | 1:568e35232180 | 96 | { |
kantapon501 | 1:568e35232180 | 97 | myLcd.InitLcd(); |
kantapon501 | 1:568e35232180 | 98 | myLcd.DrawString("Ready"); |
kantapon501 | 1:568e35232180 | 99 | myLcd.stop(); |
kantapon501 | 1:568e35232180 | 100 | } |
kantapon501 | 1:568e35232180 | 101 | |
mustwillza | 0:8b94afcb61eb | 102 | // Print Card UID |
mustwillza | 0:8b94afcb61eb | 103 | pc.printf("Card UID: "); |
mustwillza | 0:8b94afcb61eb | 104 | for (uint8_t i = 0; i < RfChip.uid.size; i++) |
mustwillza | 0:8b94afcb61eb | 105 | { |
kantapon501 | 1:568e35232180 | 106 | pc.printf(" %X", RfChip.uid.uidByte[i]); |
mustwillza | 0:8b94afcb61eb | 107 | } |
kantapon501 | 1:568e35232180 | 108 | |
kantapon501 | 1:568e35232180 | 109 | GetID(RFID); |
kantapon501 | 1:568e35232180 | 110 | pc.printf("\n\r"); |
mustwillza | 0:8b94afcb61eb | 111 | wait_ms(1000); |
kantapon501 | 1:568e35232180 | 112 | check = 1; |
kantapon501 | 1:568e35232180 | 113 | } |
mustwillza | 0:8b94afcb61eb | 114 | |
mustwillza | 0:8b94afcb61eb | 115 | } |