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 Final by
main.cpp
00001 #include "mbed.h" 00002 #include "eeprom.h" 00003 #include "MFRC522.h" 00004 00005 //KL25Z Pins for MFRC522 SPI interface 00006 #define SPI_MOSI PTC6 00007 #define SPI_MISO PTC7 00008 #define SPI_SCLK PTC5 00009 #define SPI_CS PTC4 00010 // KL25Z Pin for MFRC522 reset 00011 #define MF_RESET PTC3 00012 00013 //Pinos EEPROM 00014 #define SDA PTC2 // I2C SDA pin 00015 #define SCL PTC1 // I2C SCL pin 00016 #define EEPROM_CONTROL_BYTE 0xA0 00017 #define I2C_READ 0x01 00018 00019 #define OFF 1 00020 #define ON 0 00021 00022 DigitalOut LedRed (LED_RED); 00023 DigitalOut LedGreen (LED_GREEN); 00024 00025 EEPROM i2c(SDA, SCL, 0, EEPROM::T24C16); // T24C01 should work for the 24AA02E48 eeprom 00026 MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET); 00027 00028 //I2C i2c(PTE0, PTE1); // sda, scl on KL25Z 00029 Serial pc(USBTX, USBRX); // tx, rx 00030 00031 void escreve_mem(int8_t dado, uint8_t i){ 00032 int8_t buf, elementos; 00033 00034 LedGreen = 0; 00035 i2c.read(0, elementos); wait_ms(1); 00036 buf=dado; 00037 i2c.write(5*elementos+1+i, buf); wait_ms(1);//escreve na memoria 00038 } 00039 00040 /*char read_mem(uint8_t elemento, uint8_t i){ 00041 char x; 00042 uint8_t buf, elementos; 00043 00044 i2c.read(0, elementos); wait_ms(1); 00045 if (elementos < elemento){ 00046 printf("não há numero de elementos na memória"); 00047 return=-1; 00048 }else{ 00049 i2c.read(5*(elemento-1)+1+i, buf); wait_ms(1); 00050 x=buf; 00051 return x; 00052 } 00053 }*/ 00054 00055 /*int varre_mem( uint8_t *s){ 00056 int8_t elementos=0; 00057 uint8_t i = 0,buf; 00058 uint8_t *compara; 00059 int k; 00060 00061 i2c.read(0, elementos); wait_ms(1); 00062 for (k=0;k==elementos;k++){ 00063 for (i=0; i < RfChip.uid.size; i++){ 00064 i2c.read(5*(elementos-1)+1+i, buf); wait_ms(1); 00065 compara[i]=buf; 00066 } 00067 if (compara==s) return 0; //retorna zero se encontrar id ja registrado 00068 } 00069 00070 return 1; //retorna 1 se nao encontrar id igual 00071 }*/ 00072 00073 int main(void) { 00074 int encontrou=0; 00075 int t,jafalou=0,flag_registro=1, flag_identificacao=0, senha; 00076 char idcartao[4],compara[4];//vetor que grava id do cartao 00077 char x; 00078 int8_t buf=0, elementos,elemento,buf1; 00079 uint8_t i = 0,k; 00080 00081 pc.baud(9600); 00082 00083 // Init. RC522 Chip 00084 RfChip.PCD_Init(); 00085 00086 i2c.write(0, 0); wait_ms(1);//escreve 0 elementos na memoria 00087 00088 while (true){ 00089 i2c.read(0, elementos); wait_ms(1); 00090 00091 if(elementos==0 && jafalou==0){ 00092 printf("Nenhum cartao registrado!\n\r"); 00093 printf("Favor aproximar os cartoes a serem registrados\n\r"); 00094 jafalou=1; 00095 } 00096 inicio: 00097 LedGreen = 1; 00098 // Look for new cards 00099 if ( ! RfChip.PICC_IsNewCardPresent()) 00100 { 00101 wait_ms(500); 00102 continue; 00103 } 00104 00105 // Select one of the cards 00106 if ( ! RfChip.PICC_ReadCardSerial()) 00107 { 00108 wait_ms(500); 00109 continue; 00110 } 00111 if(flag_registro==1){ 00112 //verifica se cartao ja foi registrado 00113 if (elementos != 0){ 00114 for (i=0; i < RfChip.uid.size; i++){ 00115 buf=RfChip.uid.uidByte[i]; 00116 idcartao[i]=buf; 00117 } 00118 i2c.read(0, elementos); wait_ms(1); 00119 for (k=0;k<elementos;k++){ 00120 for (i=0; i < RfChip.uid.size; i++){ 00121 i2c.read(5*(elementos-1)+1+i, buf); wait_ms(1); 00122 compara[i]=buf; 00123 } 00124 if ((compara[0] == idcartao[0]) && 00125 (compara[1] == idcartao[1]) && 00126 (compara[2] == idcartao[2]) && 00127 (compara[3] == idcartao[3])){ 00128 printf("cartao ja registrado\n\r"); 00129 goto inicio; 00130 } 00131 } 00132 } 00133 00134 //escrita na memoria 00135 printf("\n\r"); 00136 //if(flag_registro==1){ 00137 for (i=0; i < RfChip.uid.size; i++){ 00138 escreve_mem(RfChip.uid.uidByte[i], i); 00139 } 00140 elementos++; 00141 i2c.write(0, elementos); wait_ms(1); 00142 i2c.read(0, elementos); wait_ms(1); 00143 printf("\n\r"); 00144 printf("elementos: %d\n\r", elementos); 00145 //} 00146 //le o que esta na memoria 00147 printf("memoria: "); 00148 //elemento=2; 00149 //if (elemento<elementos){ 00150 // printf("nao exite esse numero de elementos na memoria"); 00151 //}else { 00152 for (i=0; i < RfChip.uid.size; i++){ 00153 i2c.read(5*(elementos-1)+1+i, buf); wait_ms(1); 00154 x=buf; 00155 printf(" %d", x); 00156 } 00157 //} 00158 00159 /*for (i=0; i < RfChip.uid.size; i++){ 00160 idcartao[i]=read_mem(elemento, i); 00161 } 00162 for (i=0;i<4;i++){ 00163 printf(" %d", idcartao[i]); 00164 }*/ 00165 printf("\n\r"); 00166 printf("\n\r"); 00167 wait_ms(200); 00168 00169 printf("Senha:"); 00170 scanf("%d", &senha); 00171 printf("senha %d\n\r", senha); 00172 buf=senha; 00173 i2c.write(5*elementos, buf); wait_ms(1); 00174 00175 printf("Deseja continuar a registrar cartoes?\n\r"); 00176 printf("1:Sim 2:Nao\n\r"); 00177 scanf("%d", &t); //le botao se 1 goto comeco e anda pos na memoria se 2 continua 00178 printf("\n\r"); 00179 if (t==1)goto inicio; 00180 if (t==2){ 00181 printf("Fim registros!\n\r"); 00182 flag_registro=0; 00183 } 00184 } 00185 else{ 00186 printf("Passe cartao para obter acesso\n\r"); 00187 for (i=0; i < RfChip.uid.size; i++){ 00188 buf=RfChip.uid.uidByte[i]; 00189 idcartao[i]=buf; 00190 } 00191 i2c.read(0, elementos); wait_ms(1); 00192 for (k=0;k<elementos;k++){ 00193 for (i=0; i < RfChip.uid.size; i++){ 00194 i2c.read(5*(elementos-1)+1+i, buf); wait_ms(1); 00195 compara[i]=buf; 00196 } 00197 if ((compara[0] == idcartao[0]) && 00198 (compara[1] == idcartao[1]) && 00199 (compara[2] == idcartao[2]) && 00200 (compara[3] == idcartao[3])){ 00201 printf("cartao encontrado!\n\r"); 00202 printf("Digite a senha\n\r"); 00203 scanf("%d", &senha); 00204 printf("senha %d\n\r", senha); 00205 buf1=senha; 00206 encontrou=1; 00207 } 00208 } 00209 if(encontrou==1){ 00210 encontrou=0; 00211 i2c.read(5*elementos, buf); wait_ms(1); 00212 if (buf1==buf){ 00213 printf("acesso permitido\n\r"); 00214 goto inicio; 00215 }else { 00216 printf("acesso negado\n\r"); 00217 goto inicio; 00218 } 00219 } 00220 } 00221 } 00222 }
Generated on Thu Jul 28 2022 05:09:07 by
1.7.2
