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.
Fork of test_rc522 by
main.cpp
00001 00002 00003 #include "mbed.h" 00004 #include "MFRC522.h" 00005 #include <string> 00006 00007 using namespace std; 00008 // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8) 00009 DigitalOut LedGreen(LED1); 00010 00011 //Serial connection to PC for output 00012 Serial pc(SERIAL_TX, SERIAL_RX); 00013 00014 MFRC522 mfrc522 (PA_7, PA_6, PA_5, PD_14, PD_15); 00015 00016 void getHexa(char checkSum, char *str) 00017 { 00018 char strtemp[3]; 00019 memset(strtemp, 0x00, sizeof(strtemp)); 00020 sprintf(strtemp, "%02X", checkSum); 00021 memcpy(str, strtemp, 2); 00022 return; 00023 } 00024 00025 int main(void) 00026 { 00027 pc.printf("starting...\n"); 00028 mfrc522.PCD_Init(); 00029 MFRC522::MIFARE_Key key; 00030 00031 bool New_Card=false,Read_Card=false; 00032 uint8_t rettype; 00033 char buffer[34]="exultsoft"; 00034 uint8_t block; 00035 uint8_t status; 00036 uint8_t len; 00037 uint8_t data[34]={0}; 00038 00039 for (uint8_t i = 0; i < 6; i++) key.keyByte[i] = 0xFF; 00040 LedGreen = 1; 00041 // Look for new cards 00042 if ( mfrc522.PICC_IsNewCardPresent()) { 00043 wait_ms(50); 00044 New_Card=true; 00045 //continue; 00046 } 00047 // Select one of the cards 00048 if ( mfrc522.PICC_ReadCardSerial()) { 00049 // pc.printf("Rien"); 00050 wait_ms(50); 00051 Read_Card=true; 00052 } 00053 LedGreen = 0; 00054 if(Read_Card==true && New_Card==true) { 00055 char CARD_UID[9]= {'\0'}; 00056 char str1[3],str2[3],str3[3],str4[3]; 00057 getHexa(mfrc522.uid.uidByte[0], str1); 00058 getHexa(mfrc522.uid.uidByte[1], str2); 00059 getHexa(mfrc522.uid.uidByte[2], str3); 00060 getHexa(mfrc522.uid.uidByte[3], str4); 00061 00062 CARD_UID[0]=str1[0]; 00063 CARD_UID[1]=str1[1]; 00064 CARD_UID[2]=str2[0]; 00065 CARD_UID[3]=str2[1]; 00066 CARD_UID[4]=str3[0]; 00067 CARD_UID[5]=str3[1]; 00068 CARD_UID[6]=str4[0]; 00069 CARD_UID[7]=str4[1]; 00070 CARD_UID[8]='\0'; 00071 printf("CARD_UID New : %s\r\n",CARD_UID); 00072 Read_Card=false; 00073 New_Card=false; 00074 } 00075 00076 00077 00078 printf(" PICC type: "); // Dump PICC type 00079 rettype = mfrc522.PICC_GetType(mfrc522.uid.sak); 00080 printf("%s\r\n",mfrc522.PICC_GetTypeName(rettype)); 00081 00082 00083 for (int index=0; index<34;index++) 00084 { 00085 data[index]=(uint8_t)buffer[index]; 00086 } 00087 00088 block = 9; 00089 printf("Authenticating using key A..."); 00090 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); 00091 if (status != MFRC522::STATUS_OK) { 00092 printf("PCD_Authenticate() failed:\r\n "); 00093 printf("%s\r\n",mfrc522.GetStatusCodeName(status)); 00094 return; 00095 } else printf("PCD_Authenticate() success\r\n "); 00096 00097 00098 // Write block 00099 status = mfrc522.MIFARE_Write(block, data, 16); 00100 if (status != MFRC522::STATUS_OK) { 00101 printf("MIFARE_Write() failed: "); 00102 printf("%s",mfrc522.GetStatusCodeName(status)); 00103 return; 00104 } 00105 else printf("MIFARE_Write() success\r\n "); 00106 00107 00108 00109 uint8_t buffer1[18]; 00110 00111 block = 9; 00112 len = 18; 00113 00114 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file 00115 if (status != MFRC522::STATUS_OK) { 00116 printf("Authentication failed: "); 00117 printf("%s\r\n",mfrc522.GetStatusCodeName(status)); 00118 return; 00119 } 00120 00121 status = mfrc522.MIFARE_Read(block, buffer1, &len); 00122 if (status != MFRC522::STATUS_OK) { 00123 printf("Reading failed: "); 00124 printf("%s",mfrc522.GetStatusCodeName(status)); 00125 return; 00126 } 00127 00128 //PRINT FIRST NAME 00129 for (uint8_t i = 0; i < 16; i++) 00130 { 00131 if (buffer1[i] != 32) 00132 { 00133 printf("%c",buffer1[i]); 00134 } 00135 } 00136 printf("\r\n"); 00137 00138 }
Generated on Sat Jul 16 2022 05:10:30 by
1.7.2
