![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
menjo de DAC CON RFID Y TFT PANTALLA
Dependencies: mbed TFT_fonts SPI_TFT_ILI9341 MFRC522 MCP4725 LCD_fonts MCP4725
main.cpp@0:25f23d7e0fa7, 2021-02-17 (annotated)
- Committer:
- jeison
- Date:
- Wed Feb 17 00:01:00 2021 +0000
- Revision:
- 0:25f23d7e0fa7
TFT_RFID_dac; ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jeison | 0:25f23d7e0fa7 | 1 | #include "mbed.h" |
jeison | 0:25f23d7e0fa7 | 2 | //DAC |
jeison | 0:25f23d7e0fa7 | 3 | #include "MCP4725.h" |
jeison | 0:25f23d7e0fa7 | 4 | //RFID |
jeison | 0:25f23d7e0fa7 | 5 | #include "MFRC522.h" |
jeison | 0:25f23d7e0fa7 | 6 | //TFT |
jeison | 0:25f23d7e0fa7 | 7 | #include "string" |
jeison | 0:25f23d7e0fa7 | 8 | #include "Arial12x12.h" |
jeison | 0:25f23d7e0fa7 | 9 | #include "Arial24x23.h" |
jeison | 0:25f23d7e0fa7 | 10 | #include "Arial_9.h" |
jeison | 0:25f23d7e0fa7 | 11 | #include "Small_6.h" |
jeison | 0:25f23d7e0fa7 | 12 | #include "Arial28x28.h" |
jeison | 0:25f23d7e0fa7 | 13 | #include "font_big.h" |
jeison | 0:25f23d7e0fa7 | 14 | #include "stdio.h" |
jeison | 0:25f23d7e0fa7 | 15 | #include "SPI_TFT_ILI9341.h" |
jeison | 0:25f23d7e0fa7 | 16 | #define M_PI 3.14159265358979323846 |
jeison | 0:25f23d7e0fa7 | 17 | //KL25Z Pins for MFRC522 SPI interface |
jeison | 0:25f23d7e0fa7 | 18 | #define SPI_MOSI PTC6 |
jeison | 0:25f23d7e0fa7 | 19 | #define SPI_MISO PTC7 |
jeison | 0:25f23d7e0fa7 | 20 | #define SPI_SCLK PTC5 |
jeison | 0:25f23d7e0fa7 | 21 | #define SPI_CS PTC4 |
jeison | 0:25f23d7e0fa7 | 22 | // KL25Z Pin for MFRC522 reset |
jeison | 0:25f23d7e0fa7 | 23 | #define MF_RESET PTC3 |
jeison | 0:25f23d7e0fa7 | 24 | // KL25Z Pins for Debug UART port |
jeison | 0:25f23d7e0fa7 | 25 | #define UART_RX PTA1 |
jeison | 0:25f23d7e0fa7 | 26 | #define UART_TX PTA2 |
jeison | 0:25f23d7e0fa7 | 27 | |
jeison | 0:25f23d7e0fa7 | 28 | DigitalOut LedRed (LED_RED); |
jeison | 0:25f23d7e0fa7 | 29 | DigitalOut LedGreen (LED_GREEN); |
jeison | 0:25f23d7e0fa7 | 30 | //Create a TFT contructor |
jeison | 0:25f23d7e0fa7 | 31 | SPI_TFT_ILI9341 TFT(PTD2,PTD3,PTD1,PTE31,PTA20,PTA17,"TFT");//mosi,miso,sclk,cs,reset,dc el reset va conectado a 3.3v |
jeison | 0:25f23d7e0fa7 | 32 | //Create a MRFID contructor |
jeison | 0:25f23d7e0fa7 | 33 | Serial DebugUART(UART_TX, UART_RX); |
jeison | 0:25f23d7e0fa7 | 34 | MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET); |
jeison | 0:25f23d7e0fa7 | 35 | //Create an MCP4725 object at the default address (ADDRESS_0) |
jeison | 0:25f23d7e0fa7 | 36 | MCP4725 dac(PTC11, PTC10); //SDA,SCL PTC1 |
jeison | 0:25f23d7e0fa7 | 37 | Serial pc(USBTX, USBRX); |
jeison | 0:25f23d7e0fa7 | 38 | InterruptIn ISW1(D10); |
jeison | 0:25f23d7e0fa7 | 39 | int x=0; |
jeison | 0:25f23d7e0fa7 | 40 | int count = 0; |
jeison | 0:25f23d7e0fa7 | 41 | int h ; |
jeison | 0:25f23d7e0fa7 | 42 | uint8_t LecturaUID[4]; |
jeison | 0:25f23d7e0fa7 | 43 | uint8_t Usuario1[4] ={0xC3,0x43,0xC9,0x2}; //tarjeta |
jeison | 0:25f23d7e0fa7 | 44 | uint8_t Usuario2[4] ={0x29,0x36,0xB8,0xC1}; //llavero |
jeison | 0:25f23d7e0fa7 | 45 | |
jeison | 0:25f23d7e0fa7 | 46 | bool comparaUID(uint8_t lectura[], uint8_t usuario[]){ |
jeison | 0:25f23d7e0fa7 | 47 | for(int8_t i = 0; i < RfChip.uid.size; i++){ |
jeison | 0:25f23d7e0fa7 | 48 | if (lectura[i]!= usuario[i]){ |
jeison | 0:25f23d7e0fa7 | 49 | return (false); |
jeison | 0:25f23d7e0fa7 | 50 | } |
jeison | 0:25f23d7e0fa7 | 51 | else { return (true); |
jeison | 0:25f23d7e0fa7 | 52 | } |
jeison | 0:25f23d7e0fa7 | 53 | } |
jeison | 0:25f23d7e0fa7 | 54 | } |
jeison | 0:25f23d7e0fa7 | 55 | |
jeison | 0:25f23d7e0fa7 | 56 | void interfaZ(){ |
jeison | 0:25f23d7e0fa7 | 57 | TFT.set_font((unsigned char*) Arial12x12); |
jeison | 0:25f23d7e0fa7 | 58 | TFT.locate(10,130); |
jeison | 0:25f23d7e0fa7 | 59 | TFT.printf("Usuario: "); |
jeison | 0:25f23d7e0fa7 | 60 | TFT.locate(10,170); |
jeison | 0:25f23d7e0fa7 | 61 | TFT.printf("Registro"); |
jeison | 0:25f23d7e0fa7 | 62 | TFT.locate(10,185); |
jeison | 0:25f23d7e0fa7 | 63 | TFT.printf("UID: "); |
jeison | 0:25f23d7e0fa7 | 64 | TFT.locate(10,210); |
jeison | 0:25f23d7e0fa7 | 65 | TFT.printf("TYPE: "); |
jeison | 0:25f23d7e0fa7 | 66 | } |
jeison | 0:25f23d7e0fa7 | 67 | void subsw1(){ // define la funcion de casos |
jeison | 0:25f23d7e0fa7 | 68 | TFT.cls(); |
jeison | 0:25f23d7e0fa7 | 69 | x=x++; |
jeison | 0:25f23d7e0fa7 | 70 | if(x==4){ |
jeison | 0:25f23d7e0fa7 | 71 | x=0; |
jeison | 0:25f23d7e0fa7 | 72 | } |
jeison | 0:25f23d7e0fa7 | 73 | } |
jeison | 0:25f23d7e0fa7 | 74 | void sineWave(){ |
jeison | 0:25f23d7e0fa7 | 75 | TFT.set_orientation(4); //orientación de la pantalla |
jeison | 0:25f23d7e0fa7 | 76 | //interfaZ(); |
jeison | 0:25f23d7e0fa7 | 77 | //Generate a sine wave on the DAC |
jeison | 0:25f23d7e0fa7 | 78 | count++; |
jeison | 0:25f23d7e0fa7 | 79 | if(count == 1){ |
jeison | 0:25f23d7e0fa7 | 80 | for (float i =0.0f ; i <= (M_PI)*2;i+=0.1f){ |
jeison | 0:25f23d7e0fa7 | 81 | dac.write((sin(i)/2)+0.5f); |
jeison | 0:25f23d7e0fa7 | 82 | float a =(sin(i)/2)+0.5; |
jeison | 0:25f23d7e0fa7 | 83 | float a1 =a*3.3; |
jeison | 0:25f23d7e0fa7 | 84 | float a2 =a*50; |
jeison | 0:25f23d7e0fa7 | 85 | TFT.pixel((i*10)+80,50+(int)a2 ,Red); |
jeison | 0:25f23d7e0fa7 | 86 | printf ("%3f\n",a1); |
jeison | 0:25f23d7e0fa7 | 87 | } |
jeison | 0:25f23d7e0fa7 | 88 | } |
jeison | 0:25f23d7e0fa7 | 89 | else{ |
jeison | 0:25f23d7e0fa7 | 90 | for (float i =0.0f ; i <= (M_PI)*2;i+=0.1f){ |
jeison | 0:25f23d7e0fa7 | 91 | dac.write((sin(i)/2)+0.5f); |
jeison | 0:25f23d7e0fa7 | 92 | float a =(sin(i)/2)+0.5; |
jeison | 0:25f23d7e0fa7 | 93 | float a1 =a*3.3; |
jeison | 0:25f23d7e0fa7 | 94 | float a2 =a*50; |
jeison | 0:25f23d7e0fa7 | 95 | printf ("%3f\n",a1); |
jeison | 0:25f23d7e0fa7 | 96 | } |
jeison | 0:25f23d7e0fa7 | 97 | } |
jeison | 0:25f23d7e0fa7 | 98 | } |
jeison | 0:25f23d7e0fa7 | 99 | void SawtoohtWave(){ |
jeison | 0:25f23d7e0fa7 | 100 | // Generate a sawthoo wave on the DAC |
jeison | 0:25f23d7e0fa7 | 101 | for(float i =0.0f ; i <= 1.0f;i+=0.01f){ |
jeison | 0:25f23d7e0fa7 | 102 | dac.write(i); |
jeison | 0:25f23d7e0fa7 | 103 | float a1 =i*3.3; |
jeison | 0:25f23d7e0fa7 | 104 | // TFT.pixel(i,100 + (int)a1 ,Red); |
jeison | 0:25f23d7e0fa7 | 105 | printf ("%3f \n",a1); |
jeison | 0:25f23d7e0fa7 | 106 | } |
jeison | 0:25f23d7e0fa7 | 107 | } |
jeison | 0:25f23d7e0fa7 | 108 | void InvertedSawtoohtWave(){ |
jeison | 0:25f23d7e0fa7 | 109 | //inverted sawthoo wave |
jeison | 0:25f23d7e0fa7 | 110 | for(float i =1.0f ; i >= 0.0f;i-=0.01f){ |
jeison | 0:25f23d7e0fa7 | 111 | dac.write(i); |
jeison | 0:25f23d7e0fa7 | 112 | float a1 =i*3.3; |
jeison | 0:25f23d7e0fa7 | 113 | // TFT.pixel(i,100 + (int)a1 ,Red); |
jeison | 0:25f23d7e0fa7 | 114 | printf ("%3f\n",a1); |
jeison | 0:25f23d7e0fa7 | 115 | } |
jeison | 0:25f23d7e0fa7 | 116 | } |
jeison | 0:25f23d7e0fa7 | 117 | void TriangularWave(){ |
jeison | 0:25f23d7e0fa7 | 118 | //Generate triangular wave |
jeison | 0:25f23d7e0fa7 | 119 | for(float i =0.0f ; i <= 1.0f;i+=0.01f){ |
jeison | 0:25f23d7e0fa7 | 120 | dac.write(i); |
jeison | 0:25f23d7e0fa7 | 121 | float a1 =i*3.3; |
jeison | 0:25f23d7e0fa7 | 122 | // printf ("Voltage; %3f \n",a1); |
jeison | 0:25f23d7e0fa7 | 123 | } |
jeison | 0:25f23d7e0fa7 | 124 | for(float i =1.0f ; i >= 0.0f;i-=0.01f){ |
jeison | 0:25f23d7e0fa7 | 125 | dac.write(i); |
jeison | 0:25f23d7e0fa7 | 126 | float a1 =i*3.3; |
jeison | 0:25f23d7e0fa7 | 127 | // printf ("Voltage; %3f \n",a1); |
jeison | 0:25f23d7e0fa7 | 128 | } |
jeison | 0:25f23d7e0fa7 | 129 | } |
jeison | 0:25f23d7e0fa7 | 130 | void signalChangue(){ |
jeison | 0:25f23d7e0fa7 | 131 | switch (x) { |
jeison | 0:25f23d7e0fa7 | 132 | case 0: |
jeison | 0:25f23d7e0fa7 | 133 | sineWave(); |
jeison | 0:25f23d7e0fa7 | 134 | break; |
jeison | 0:25f23d7e0fa7 | 135 | case 1: |
jeison | 0:25f23d7e0fa7 | 136 | SawtoohtWave(); |
jeison | 0:25f23d7e0fa7 | 137 | break; |
jeison | 0:25f23d7e0fa7 | 138 | case 2: |
jeison | 0:25f23d7e0fa7 | 139 | InvertedSawtoohtWave(); |
jeison | 0:25f23d7e0fa7 | 140 | break; |
jeison | 0:25f23d7e0fa7 | 141 | case 3: |
jeison | 0:25f23d7e0fa7 | 142 | TriangularWave(); |
jeison | 0:25f23d7e0fa7 | 143 | break; |
jeison | 0:25f23d7e0fa7 | 144 | } |
jeison | 0:25f23d7e0fa7 | 145 | } |
jeison | 0:25f23d7e0fa7 | 146 | |
jeison | 0:25f23d7e0fa7 | 147 | int main(){ |
jeison | 0:25f23d7e0fa7 | 148 | |
jeison | 0:25f23d7e0fa7 | 149 | ISW1.mode(PullUp); // Configure port to PullUp or PullDown |
jeison | 0:25f23d7e0fa7 | 150 | ISW1.fall(&subsw1); //rise ;subida fall:bajada |
jeison | 0:25f23d7e0fa7 | 151 | // Set debug UART speed |
jeison | 0:25f23d7e0fa7 | 152 | DebugUART.baud(115200); |
jeison | 0:25f23d7e0fa7 | 153 | pc.baud(9500); |
jeison | 0:25f23d7e0fa7 | 154 | //limpia la pantalla. |
jeison | 0:25f23d7e0fa7 | 155 | TFT.cls(); |
jeison | 0:25f23d7e0fa7 | 156 | TFT.set_orientation(4); //orientación de la pantalla |
jeison | 0:25f23d7e0fa7 | 157 | interfaZ(); |
jeison | 0:25f23d7e0fa7 | 158 | // Init. RC522 Chip |
jeison | 0:25f23d7e0fa7 | 159 | RfChip.PCD_Init(); |
jeison | 0:25f23d7e0fa7 | 160 | //Try to open the MCP4725 |
jeison | 0:25f23d7e0fa7 | 161 | if (dac.open()) { |
jeison | 0:25f23d7e0fa7 | 162 | // printf("Device detected!\n"); |
jeison | 0:25f23d7e0fa7 | 163 | //Wake up the DAC |
jeison | 0:25f23d7e0fa7 | 164 | //NOTE: This might wake up other I2C devices as well! |
jeison | 0:25f23d7e0fa7 | 165 | dac.wakeup(); |
jeison | 0:25f23d7e0fa7 | 166 | while (1) { |
jeison | 0:25f23d7e0fa7 | 167 | signalChangue(); |
jeison | 0:25f23d7e0fa7 | 168 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
jeison | 0:25f23d7e0fa7 | 169 | LedRed = 1; |
jeison | 0:25f23d7e0fa7 | 170 | LedGreen = 1; |
jeison | 0:25f23d7e0fa7 | 171 | |
jeison | 0:25f23d7e0fa7 | 172 | // Look for new cards |
jeison | 0:25f23d7e0fa7 | 173 | if ( ! RfChip.PICC_IsNewCardPresent()){ |
jeison | 0:25f23d7e0fa7 | 174 | // wait_ms(500); |
jeison | 0:25f23d7e0fa7 | 175 | continue; |
jeison | 0:25f23d7e0fa7 | 176 | } |
jeison | 0:25f23d7e0fa7 | 177 | |
jeison | 0:25f23d7e0fa7 | 178 | LedRed = 0; |
jeison | 0:25f23d7e0fa7 | 179 | |
jeison | 0:25f23d7e0fa7 | 180 | // Select one of the cards |
jeison | 0:25f23d7e0fa7 | 181 | if ( ! RfChip.PICC_ReadCardSerial()) |
jeison | 0:25f23d7e0fa7 | 182 | { |
jeison | 0:25f23d7e0fa7 | 183 | // wait_ms(500); |
jeison | 0:25f23d7e0fa7 | 184 | continue; |
jeison | 0:25f23d7e0fa7 | 185 | } |
jeison | 0:25f23d7e0fa7 | 186 | |
jeison | 0:25f23d7e0fa7 | 187 | LedRed = 1; |
jeison | 0:25f23d7e0fa7 | 188 | LedGreen = 0; |
jeison | 0:25f23d7e0fa7 | 189 | |
jeison | 0:25f23d7e0fa7 | 190 | // Print Card UID |
jeison | 0:25f23d7e0fa7 | 191 | pc.printf("Card UID: "); |
jeison | 0:25f23d7e0fa7 | 192 | h = RfChip.uid.size; |
jeison | 0:25f23d7e0fa7 | 193 | for (uint8_t i = 0; i < RfChip.uid.size; i++) //uint8_t unsigned int |
jeison | 0:25f23d7e0fa7 | 194 | { |
jeison | 0:25f23d7e0fa7 | 195 | LecturaUID[i] = RfChip.uid.uidByte[i]; |
jeison | 0:25f23d7e0fa7 | 196 | DebugUART.printf(" %X01", RfChip.uid.uidByte[i]); |
jeison | 0:25f23d7e0fa7 | 197 | } |
jeison | 0:25f23d7e0fa7 | 198 | |
jeison | 0:25f23d7e0fa7 | 199 | DebugUART.printf("\n\r tamano: %d ",h); |
jeison | 0:25f23d7e0fa7 | 200 | pc.printf("\n\r"); |
jeison | 0:25f23d7e0fa7 | 201 | |
jeison | 0:25f23d7e0fa7 | 202 | |
jeison | 0:25f23d7e0fa7 | 203 | if (comparaUID(LecturaUID, Usuario1)){ |
jeison | 0:25f23d7e0fa7 | 204 | pc.printf("Bienvenido Usuario1"); |
jeison | 0:25f23d7e0fa7 | 205 | TFT.set_font((unsigned char*) Arial12x12); |
jeison | 0:25f23d7e0fa7 | 206 | TFT.locate(170,130); |
jeison | 0:25f23d7e0fa7 | 207 | TFT.printf("Usuario1"); |
jeison | 0:25f23d7e0fa7 | 208 | } |
jeison | 0:25f23d7e0fa7 | 209 | else if (comparaUID(LecturaUID, Usuario2)){ |
jeison | 0:25f23d7e0fa7 | 210 | pc.printf("Bienvenido Usuario2"); |
jeison | 0:25f23d7e0fa7 | 211 | TFT.locate(170,130); |
jeison | 0:25f23d7e0fa7 | 212 | TFT.printf("Usuario2"); |
jeison | 0:25f23d7e0fa7 | 213 | } |
jeison | 0:25f23d7e0fa7 | 214 | else { |
jeison | 0:25f23d7e0fa7 | 215 | pc.printf("User Unknown"); |
jeison | 0:25f23d7e0fa7 | 216 | TFT.locate(170,130); |
jeison | 0:25f23d7e0fa7 | 217 | TFT.printf("User Unknown"); |
jeison | 0:25f23d7e0fa7 | 218 | } |
jeison | 0:25f23d7e0fa7 | 219 | |
jeison | 0:25f23d7e0fa7 | 220 | pc.printf("\n\r"); |
jeison | 0:25f23d7e0fa7 | 221 | // Print Card type |
jeison | 0:25f23d7e0fa7 | 222 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
jeison | 0:25f23d7e0fa7 | 223 | pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); |
jeison | 0:25f23d7e0fa7 | 224 | // wait_ms(250); |
jeison | 0:25f23d7e0fa7 | 225 | } |
jeison | 0:25f23d7e0fa7 | 226 | } else { |
jeison | 0:25f23d7e0fa7 | 227 | error("Device not detected!\n"); |
jeison | 0:25f23d7e0fa7 | 228 | } |
jeison | 0:25f23d7e0fa7 | 229 | } |