Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@3:157e0f68b775, 2019-01-22 (annotated)
- Committer:
- mariangelamone
- Date:
- Tue Jan 22 22:22:11 2019 +0000
- Revision:
- 3:157e0f68b775
- Parent:
- 2:a0c7513fb634
chiave elettronica con RFID
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mariangelamone | 3:157e0f68b775 | 1 | |
kirchnet | 2:a0c7513fb634 | 2 | //Test of cheap 13.56 Mhz RFID-RC522 module from eBay |
kirchnet | 2:a0c7513fb634 | 3 | //This code is based on Martin Olejar's MFRC522 library. Minimal changes |
kirchnet | 2:a0c7513fb634 | 4 | //Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too |
kirchnet | 2:a0c7513fb634 | 5 | |
kirchnet | 2:a0c7513fb634 | 6 | //Connect as follows: |
kirchnet | 2:a0c7513fb634 | 7 | //RFID pins -> Nucleo header CN5 (Arduino-compatible header) |
kirchnet | 2:a0c7513fb634 | 8 | //---------------------------------------- |
kirchnet | 2:a0c7513fb634 | 9 | //RFID IRQ=pin5 -> Not used. Leave open |
kirchnet | 2:a0c7513fb634 | 10 | //RFID MISO=pin4 -> Nucleo SPI_MISO=PA_6=D12 |
kirchnet | 2:a0c7513fb634 | 11 | //RFID MOSI=pin3 -> Nucleo SPI_MOSI=PA_7=D11 |
kirchnet | 2:a0c7513fb634 | 12 | //RFID SCK=pin2 -> Nucleo SPI_SCK =PA_5=D13 |
kirchnet | 2:a0c7513fb634 | 13 | //RFID SDA=pin1 -> Nucleo SPI_CS =PB_6=D10 |
kirchnet | 2:a0c7513fb634 | 14 | //RFID RST=pin7 -> Nucleo =PA_9=D8 |
kirchnet | 2:a0c7513fb634 | 15 | //3.3V and Gnd to the respective pins |
kirchnet | 2:a0c7513fb634 | 16 | |
kirchnet | 2:a0c7513fb634 | 17 | #include "mbed.h" |
kirchnet | 2:a0c7513fb634 | 18 | #include "MFRC522.h" |
mariangelamone | 3:157e0f68b775 | 19 | #include "Servo.h" |
kirchnet | 2:a0c7513fb634 | 20 | // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8) |
kirchnet | 2:a0c7513fb634 | 21 | #define MF_RESET D8 |
mariangelamone | 3:157e0f68b775 | 22 | #define PWMA PC_8 |
kirchnet | 2:a0c7513fb634 | 23 | //Serial connection to PC for output |
kirchnet | 2:a0c7513fb634 | 24 | Serial pc(SERIAL_TX, SERIAL_RX); |
mariangelamone | 3:157e0f68b775 | 25 | DigitalOut green(D7); |
mariangelamone | 3:157e0f68b775 | 26 | DigitalOut red(D6); |
kirchnet | 2:a0c7513fb634 | 27 | MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); |
mariangelamone | 3:157e0f68b775 | 28 | uint8_t card1=0x20; |
mariangelamone | 3:157e0f68b775 | 29 | uint8_t card2=0x82; |
mariangelamone | 3:157e0f68b775 | 30 | uint8_t card3=0xBF; |
mariangelamone | 3:157e0f68b775 | 31 | uint8_t card4=0x4F; |
mariangelamone | 3:157e0f68b775 | 32 | PwmOut mypwm(PWMA); |
mariangelamone | 3:157e0f68b775 | 33 | int main(void) {red=0; |
mariangelamone | 3:157e0f68b775 | 34 | volatile float speed_value = 0.5; |
kirchnet | 2:a0c7513fb634 | 35 | |
mariangelamone | 3:157e0f68b775 | 36 | Servo myservo(D2); |
kirchnet | 2:a0c7513fb634 | 37 | pc.printf("starting...\n"); |
kirchnet | 2:a0c7513fb634 | 38 | // Init. RC522 Chip |
kirchnet | 2:a0c7513fb634 | 39 | RfChip.PCD_Init(); |
kirchnet | 2:a0c7513fb634 | 40 | while (true) { |
mariangelamone | 3:157e0f68b775 | 41 | red=0; |
kirchnet | 2:a0c7513fb634 | 42 | // Look for new cards |
kirchnet | 2:a0c7513fb634 | 43 | if ( ! RfChip.PICC_IsNewCardPresent()) |
kirchnet | 2:a0c7513fb634 | 44 | { |
kirchnet | 2:a0c7513fb634 | 45 | wait_ms(500); |
kirchnet | 2:a0c7513fb634 | 46 | continue; |
kirchnet | 2:a0c7513fb634 | 47 | } |
kirchnet | 2:a0c7513fb634 | 48 | |
kirchnet | 2:a0c7513fb634 | 49 | // Select one of the cards |
kirchnet | 2:a0c7513fb634 | 50 | if ( ! RfChip.PICC_ReadCardSerial()) |
kirchnet | 2:a0c7513fb634 | 51 | { |
kirchnet | 2:a0c7513fb634 | 52 | wait_ms(500); |
kirchnet | 2:a0c7513fb634 | 53 | continue; |
kirchnet | 2:a0c7513fb634 | 54 | } |
kirchnet | 2:a0c7513fb634 | 55 | // Print Card UID |
kirchnet | 2:a0c7513fb634 | 56 | pc.printf("Card UID: "); |
kirchnet | 2:a0c7513fb634 | 57 | for (uint8_t i = 0; i < RfChip.uid.size; i++) |
mariangelamone | 3:157e0f68b775 | 58 | {pc.printf("%d%",i); |
mariangelamone | 3:157e0f68b775 | 59 | pc.printf(" %X02", RfChip.uid.uidByte[i]); |
kirchnet | 2:a0c7513fb634 | 60 | } |
kirchnet | 2:a0c7513fb634 | 61 | pc.printf("\n\r"); |
mariangelamone | 3:157e0f68b775 | 62 | if((RfChip.uid.uidByte[0]==card1)&&(RfChip.uid.uidByte[1]==card2)&&(RfChip.uid.uidByte[2]==card3)&&(RfChip.uid.uidByte[3]==card4)) |
mariangelamone | 3:157e0f68b775 | 63 | {pc.printf("\nhello"); |
mariangelamone | 3:157e0f68b775 | 64 | mypwm.pulsewidth(90); |
mariangelamone | 3:157e0f68b775 | 65 | green=1; |
mariangelamone | 3:157e0f68b775 | 66 | myservo=90; |
mariangelamone | 3:157e0f68b775 | 67 | wait_ms(3000); |
mariangelamone | 3:157e0f68b775 | 68 | myservo=-90; |
mariangelamone | 3:157e0f68b775 | 69 | green=0; |
mariangelamone | 3:157e0f68b775 | 70 | red=0; |
mariangelamone | 3:157e0f68b775 | 71 | }else {mypwm.pulsewidth(-90); |
mariangelamone | 3:157e0f68b775 | 72 | green=0; |
mariangelamone | 3:157e0f68b775 | 73 | red=1;} |
kirchnet | 2:a0c7513fb634 | 74 | // Print Card type |
kirchnet | 2:a0c7513fb634 | 75 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
kirchnet | 2:a0c7513fb634 | 76 | pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); |
kirchnet | 2:a0c7513fb634 | 77 | wait_ms(1000); |
kirchnet | 2:a0c7513fb634 | 78 | } |
kirchnet | 2:a0c7513fb634 | 79 | } |