Digiti II
/
MPU_RFID_MAX_LAB
ptoo elq ue lo lea
main.cpp@4:fcfb02e6a11f, 2020-11-03 (annotated)
- Committer:
- michaelrodriguezg
- Date:
- Tue Nov 03 22:38:39 2020 +0000
- Revision:
- 4:fcfb02e6a11f
- Parent:
- 3:06223ec42c88
- Child:
- 5:8d56c59a45a8
HOLA;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
afroid | 3:06223ec42c88 | 1 | char whoAmI ; |
afroid | 3:06223ec42c88 | 2 | #define MPU6050 0x68<<1 // Device address when ADO = 0 |
afroid | 3:06223ec42c88 | 3 | #define RA_WHO_AM_I 0x75 |
afroid | 3:06223ec42c88 | 4 | #define RA_CONFIG 0x1A |
afroid | 3:06223ec42c88 | 5 | #define RA_USER_CTRL 0x6A |
afroid | 3:06223ec42c88 | 6 | #define RA_PWR_ADDR 0x6B |
afroid | 3:06223ec42c88 | 7 | #define RA_SMPLRT_DIV 0x19 |
afroid | 3:06223ec42c88 | 8 | #define RA_GYRO_CONFIG 0x1B |
afroid | 3:06223ec42c88 | 9 | #define RA_ACCEL_CONFIG 0x1C |
afroid | 3:06223ec42c88 | 10 | #define RA_INT_PIN_CFG 0x37 |
afroid | 3:06223ec42c88 | 11 | #define RA_ACCEL_XOUT_H 0x3B |
afroid | 3:06223ec42c88 | 12 | #define RA_ACCEL_XOUT_L 0x3C |
afroid | 3:06223ec42c88 | 13 | #define RA_ACCEL_YOUT_H 0x3D |
afroid | 3:06223ec42c88 | 14 | #define RA_ACCEL_YOUT_L 0x3E |
afroid | 3:06223ec42c88 | 15 | #define RA_ACCEL_ZOUT_H 0x3F |
afroid | 3:06223ec42c88 | 16 | #define RA_ACCEL_ZOUT_L 0x40 |
afroid | 3:06223ec42c88 | 17 | #define RA_TEMP_OUT_H 0x41 |
afroid | 3:06223ec42c88 | 18 | #define RA_TEMP_OUT_L 0x42 |
afroid | 3:06223ec42c88 | 19 | #define RA_GYRO_XOUT_H 0x43 |
afroid | 3:06223ec42c88 | 20 | #define RA_GYRO_XOUT_L 0x44 |
afroid | 3:06223ec42c88 | 21 | #define RA_GYRO_YOUT_H 0x45 |
afroid | 3:06223ec42c88 | 22 | #define RA_GYRO_YOUT_L 0x46 |
afroid | 3:06223ec42c88 | 23 | #define RA_GYRO_ZOUT_H 0x47 |
afroid | 3:06223ec42c88 | 24 | #define RA_GYRO_ZOUT_L 0x48 |
afroid | 3:06223ec42c88 | 25 | #define AF_SEL 0x00 |
afroid | 3:06223ec42c88 | 26 | #define ACC_HPF 0x00 |
afroid | 3:06223ec42c88 | 27 | #define FS_SEL 0x00 |
afroid | 3:06223ec42c88 | 28 | #define RA_PWR_MGMT_1 0x00 |
afroid | 3:06223ec42c88 | 29 | #define USER_CTRL 0x00 |
afroid | 3:06223ec42c88 | 30 | #define ACC_SELF_TEST_X 0x00 |
afroid | 3:06223ec42c88 | 31 | #define ACC_SELF_TEST_Y 0x00 |
afroid | 3:06223ec42c88 | 32 | #define ACC_SELF_TEST_Z 0x00 |
afroid | 3:06223ec42c88 | 33 | #define G_SELF_TEST_X 0x00 |
afroid | 3:06223ec42c88 | 34 | #define G_SELF_TEST_Y 0x00 |
afroid | 3:06223ec42c88 | 35 | #define G_SELF_TEST_Z 0x00 |
afroid | 3:06223ec42c88 | 36 | #define PIN_CFG 2 |
afroid | 3:06223ec42c88 | 37 | #define CONFIG 4 |
afroid | 3:06223ec42c88 | 38 | #define SMPLRT_DIV 19 |
afroid | 3:06223ec42c88 | 39 | |
kirchnet | 2:a0c7513fb634 | 40 | |
kirchnet | 2:a0c7513fb634 | 41 | #include "mbed.h" |
kirchnet | 2:a0c7513fb634 | 42 | #include "MFRC522.h" |
michaelrodriguezg | 4:fcfb02e6a11f | 43 | #include "main.h" |
michaelrodriguezg | 4:fcfb02e6a11f | 44 | #include "max7219.h" |
kirchnet | 2:a0c7513fb634 | 45 | // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8) |
kirchnet | 2:a0c7513fb634 | 46 | #define MF_RESET D8 |
michaelrodriguezg | 4:fcfb02e6a11f | 47 | Max7219 pantalla(D11, D12, D13, D10);//MOSI-MISO-CLK-SSEL |
michaelrodriguezg | 4:fcfb02e6a11f | 48 | void Print_pantalla_tabla(unsigned char *pValor); |
afroid | 3:06223ec42c88 | 49 | DigitalOut led(D4); |
afroid | 3:06223ec42c88 | 50 | DigitalOut ledG(D3); |
afroid | 3:06223ec42c88 | 51 | DigitalOut led3(D5); |
afroid | 3:06223ec42c88 | 52 | DigitalIn boton(PC_13); // 0 - presionado |
afroid | 3:06223ec42c88 | 53 | int aux = 0 ; |
afroid | 3:06223ec42c88 | 54 | int aux2 = 0; |
afroid | 3:06223ec42c88 | 55 | int signal = 0; |
michaelrodriguezg | 4:fcfb02e6a11f | 56 | unsigned char personal[8]= {0x0,0x0,0,0,0,0,0,0}; |
michaelrodriguezg | 4:fcfb02e6a11f | 57 | unsigned char filas[8]= {1,3,7,0x0f,0x1f,0x3f,0x7f,0xff}; |
kirchnet | 2:a0c7513fb634 | 58 | //Serial connection to PC for output |
kirchnet | 2:a0c7513fb634 | 59 | Serial pc(SERIAL_TX, SERIAL_RX); |
afroid | 3:06223ec42c88 | 60 | I2C i2c(D14, D15); |
kirchnet | 2:a0c7513fb634 | 61 | MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); |
afroid | 3:06223ec42c88 | 62 | void giroscope(); |
afroid | 3:06223ec42c88 | 63 | void rfid(); |
afroid | 3:06223ec42c88 | 64 | void InitMPU(); |
afroid | 3:06223ec42c88 | 65 | //int ReadData(int adress_L, int adress_H); |
afroid | 3:06223ec42c88 | 66 | char reg_value[2]; |
afroid | 3:06223ec42c88 | 67 | void WriteData(uint8_t address, uint8_t subAddress, uint8_t data); |
afroid | 3:06223ec42c88 | 68 | int ReadData(int adress_L, int adress_H); |
afroid | 3:06223ec42c88 | 69 | void giroscope(){ |
afroid | 3:06223ec42c88 | 70 | while(1) { |
afroid | 3:06223ec42c88 | 71 | pc.printf(" "); |
afroid | 3:06223ec42c88 | 72 | /* |
afroid | 3:06223ec42c88 | 73 | pc.printf("*"); |
afroid | 3:06223ec42c88 | 74 | pc.printf("%i\t",ReadData(RA_ACCEL_XOUT_L ,RA_ACCEL_XOUT_H)); |
afroid | 3:06223ec42c88 | 75 | pc.printf("%i\t",ReadData(RA_ACCEL_YOUT_L ,RA_ACCEL_YOUT_H)); |
afroid | 3:06223ec42c88 | 76 | pc.printf("%i\t",ReadData(RA_ACCEL_ZOUT_L ,RA_ACCEL_ZOUT_H)); |
afroid | 3:06223ec42c88 | 77 | pc.printf("\n\r");*/ |
afroid | 3:06223ec42c88 | 78 | // pc.printf("%i\t",ReadData(RA_GYRO_XOUT_L ,RA_GYRO_XOUT_H)); |
afroid | 3:06223ec42c88 | 79 | pc.printf("%i\t",ReadData(RA_GYRO_YOUT_L ,RA_GYRO_YOUT_H)); |
afroid | 3:06223ec42c88 | 80 | //pc.printf("%i\t",ReadData(RA_GYRO_ZOUT_L ,RA_GYRO_ZOUT_H)); |
afroid | 3:06223ec42c88 | 81 | pc.printf("\n\r"); |
michaelrodriguezg | 4:fcfb02e6a11f | 82 | //wait_us(2e6); |
afroid | 3:06223ec42c88 | 83 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 84 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 85 | void Print_pantalla_tabla(unsigned char *pValor){ |
michaelrodriguezg | 4:fcfb02e6a11f | 86 | for (int i =0;i<8;i++) |
michaelrodriguezg | 4:fcfb02e6a11f | 87 | pantalla.write_digit(1,i+1,pValor[i]); |
michaelrodriguezg | 4:fcfb02e6a11f | 88 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 89 | void matrix(){ |
michaelrodriguezg | 4:fcfb02e6a11f | 90 | max7219_configuration_t cfg = { |
michaelrodriguezg | 4:fcfb02e6a11f | 91 | .device_number = 1, |
michaelrodriguezg | 4:fcfb02e6a11f | 92 | .decode_mode = 0, |
michaelrodriguezg | 4:fcfb02e6a11f | 93 | .intensity = Max7219::MAX7219_INTENSITY_5, |
michaelrodriguezg | 4:fcfb02e6a11f | 94 | .scan_limit = Max7219::MAX7219_SCAN_8 |
michaelrodriguezg | 4:fcfb02e6a11f | 95 | }; |
michaelrodriguezg | 4:fcfb02e6a11f | 96 | pantalla.init_device(cfg); |
michaelrodriguezg | 4:fcfb02e6a11f | 97 | pantalla.enable_device(1); |
michaelrodriguezg | 4:fcfb02e6a11f | 98 | pantalla.set_display_test(); |
michaelrodriguezg | 4:fcfb02e6a11f | 99 | wait_ms(1000); |
michaelrodriguezg | 4:fcfb02e6a11f | 100 | pantalla.clear_display_test(); |
michaelrodriguezg | 4:fcfb02e6a11f | 101 | while(ledG==1){ |
michaelrodriguezg | 4:fcfb02e6a11f | 102 | for(int i=0;i<3;i++){ |
michaelrodriguezg | 4:fcfb02e6a11f | 103 | i=i; |
michaelrodriguezg | 4:fcfb02e6a11f | 104 | Print_pantalla_tabla(led2); |
michaelrodriguezg | 4:fcfb02e6a11f | 105 | wait_ms(200); |
michaelrodriguezg | 4:fcfb02e6a11f | 106 | Print_pantalla_tabla(led1); |
michaelrodriguezg | 4:fcfb02e6a11f | 107 | wait_ms(200); |
michaelrodriguezg | 4:fcfb02e6a11f | 108 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 109 | ledG=0; |
michaelrodriguezg | 4:fcfb02e6a11f | 110 | Print_pantalla_tabla(personal); |
michaelrodriguezg | 4:fcfb02e6a11f | 111 | wait_ms(100); |
michaelrodriguezg | 4:fcfb02e6a11f | 112 | } |
kirchnet | 2:a0c7513fb634 | 113 | } |
afroid | 3:06223ec42c88 | 114 | void rfid(){ |
afroid | 3:06223ec42c88 | 115 | while(true){ |
afroid | 3:06223ec42c88 | 116 | if (boton == 0){ |
afroid | 3:06223ec42c88 | 117 | led = !led; |
afroid | 3:06223ec42c88 | 118 | pc.printf("boton presionado\n\r"); |
afroid | 3:06223ec42c88 | 119 | pc.printf(" %X02",aux); |
afroid | 3:06223ec42c88 | 120 | } |
afroid | 3:06223ec42c88 | 121 | led=0; |
afroid | 3:06223ec42c88 | 122 | // Look for new cards |
michaelrodriguezg | 4:fcfb02e6a11f | 123 | if ( ! RfChip.PICC_IsNewCardPresent()){ |
afroid | 3:06223ec42c88 | 124 | wait_ms(200); |
afroid | 3:06223ec42c88 | 125 | //pc.printf("1 "); |
afroid | 3:06223ec42c88 | 126 | pc.printf("%i\t\n\r",ReadData(RA_GYRO_YOUT_L ,RA_GYRO_YOUT_H)); |
michaelrodriguezg | 4:fcfb02e6a11f | 127 | |
afroid | 3:06223ec42c88 | 128 | continue; |
afroid | 3:06223ec42c88 | 129 | } |
afroid | 3:06223ec42c88 | 130 | // Select one of the cards |
afroid | 3:06223ec42c88 | 131 | if ( ! RfChip.PICC_ReadCardSerial()) |
afroid | 3:06223ec42c88 | 132 | { |
afroid | 3:06223ec42c88 | 133 | wait_ms(200); |
afroid | 3:06223ec42c88 | 134 | continue; |
afroid | 3:06223ec42c88 | 135 | } |
afroid | 3:06223ec42c88 | 136 | led=1; |
afroid | 3:06223ec42c88 | 137 | // Print Card UID |
afroid | 3:06223ec42c88 | 138 | // pc.printf("Card UID: "); |
michaelrodriguezg | 4:fcfb02e6a11f | 139 | for (uint8_t i = 0; i < RfChip.uid.size; i++){ |
afroid | 3:06223ec42c88 | 140 | /* pc.printf(" %X02", RfChip.uid.uidByte[i]); |
afroid | 3:06223ec42c88 | 141 | pc.printf("-A1-\n\r");*/ |
michaelrodriguezg | 4:fcfb02e6a11f | 142 | if (aux == 0) { |
afroid | 3:06223ec42c88 | 143 | aux = RfChip.uid.uidByte[i]; //registra la primera tarjeta que entre |
afroid | 3:06223ec42c88 | 144 | led3 = 1; |
michaelrodriguezg | 4:fcfb02e6a11f | 145 | aux2 = 2; |
afroid | 3:06223ec42c88 | 146 | }else if (aux2 ==2){ |
afroid | 3:06223ec42c88 | 147 | aux2 = RfChip.uid.uidByte[i]; //registrar tarjeta |
michaelrodriguezg | 4:fcfb02e6a11f | 148 | ledG = 1; |
afroid | 3:06223ec42c88 | 149 | led3 = 0; |
afroid | 3:06223ec42c88 | 150 | signal = 1; |
michaelrodriguezg | 4:fcfb02e6a11f | 151 | }else if (RfChip.uid.uidByte[i] == aux){ |
afroid | 3:06223ec42c88 | 152 | ledG = 1; |
afroid | 3:06223ec42c88 | 153 | led3 = 0; |
afroid | 3:06223ec42c88 | 154 | signal = 1; |
afroid | 3:06223ec42c88 | 155 | pc.printf("15000\n\r"); |
afroid | 3:06223ec42c88 | 156 | /*------*/ |
michaelrodriguezg | 4:fcfb02e6a11f | 157 | wait_us(5e6); |
michaelrodriguezg | 4:fcfb02e6a11f | 158 | matrix(); |
afroid | 3:06223ec42c88 | 159 | ledG = 0; |
afroid | 3:06223ec42c88 | 160 | signal = 0; |
afroid | 3:06223ec42c88 | 161 | pc.printf("-15000\n\r"); |
michaelrodriguezg | 4:fcfb02e6a11f | 162 | led3 = 1; |
afroid | 3:06223ec42c88 | 163 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 164 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 165 | pc.printf("\n\r"); |
kirchnet | 2:a0c7513fb634 | 166 | } |
afroid | 3:06223ec42c88 | 167 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 168 | /*void leer (){ |
afroid | 3:06223ec42c88 | 169 | if (boton == 1){ |
afroid | 3:06223ec42c88 | 170 | led = !led; |
afroid | 3:06223ec42c88 | 171 | pc.printf("boton presionado\n\r"); |
afroid | 3:06223ec42c88 | 172 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 173 | }*/ |
afroid | 3:06223ec42c88 | 174 | int ReadData(int adress_L, int adress_H){ |
michaelrodriguezg | 4:fcfb02e6a11f | 175 | char data_L; |
afroid | 3:06223ec42c88 | 176 | char data_H; |
afroid | 3:06223ec42c88 | 177 | char adress_data_L[1] ; |
afroid | 3:06223ec42c88 | 178 | char adress_data_H[1] ; |
afroid | 3:06223ec42c88 | 179 | adress_data_L[0]= adress_L; |
afroid | 3:06223ec42c88 | 180 | adress_data_H[0]= adress_H; |
afroid | 3:06223ec42c88 | 181 | i2c.write(MPU6050, adress_data_L, 1,1); |
afroid | 3:06223ec42c88 | 182 | i2c.read(MPU6050,&data_L, 1,0); |
afroid | 3:06223ec42c88 | 183 | i2c.write(MPU6050, adress_data_H, 1,1); |
afroid | 3:06223ec42c88 | 184 | i2c.read(MPU6050,&data_H, 1,0); |
afroid | 3:06223ec42c88 | 185 | return (int16_t)((data_H<<8) | data_L); |
afroid | 3:06223ec42c88 | 186 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 187 | void WriteData(uint8_t address, uint8_t subAddress, uint8_t data){ |
afroid | 3:06223ec42c88 | 188 | char data_write[2]; |
afroid | 3:06223ec42c88 | 189 | data_write[0] = subAddress; |
afroid | 3:06223ec42c88 | 190 | data_write[1] = data; |
afroid | 3:06223ec42c88 | 191 | i2c.write(address, data_write, 2, false); |
afroid | 3:06223ec42c88 | 192 | } |
afroid | 3:06223ec42c88 | 193 | void InitMPU(){ |
afroid | 3:06223ec42c88 | 194 | WriteData(MPU6050,RA_PWR_ADDR,RA_PWR_MGMT_1); |
afroid | 3:06223ec42c88 | 195 | wait(0.1); |
afroid | 3:06223ec42c88 | 196 | WriteData(MPU6050,RA_USER_CTRL,USER_CTRL); |
afroid | 3:06223ec42c88 | 197 | WriteData(MPU6050, RA_INT_PIN_CFG,PIN_CFG); |
afroid | 3:06223ec42c88 | 198 | WriteData(MPU6050,RA_SMPLRT_DIV,SMPLRT_DIV); |
afroid | 3:06223ec42c88 | 199 | int GConfig = G_SELF_TEST_X | G_SELF_TEST_Y | G_SELF_TEST_Z | FS_SEL; |
afroid | 3:06223ec42c88 | 200 | int AConfig = ACC_SELF_TEST_X | ACC_SELF_TEST_Y | ACC_SELF_TEST_Z | AF_SEL | ACC_HPF; |
afroid | 3:06223ec42c88 | 201 | WriteData(MPU6050,RA_GYRO_CONFIG,GConfig); |
afroid | 3:06223ec42c88 | 202 | WriteData(MPU6050,RA_ACCEL_CONFIG,AConfig); |
afroid | 3:06223ec42c88 | 203 | } |
michaelrodriguezg | 4:fcfb02e6a11f | 204 | int main(void) { //MENSAJE INICIAL |
afroid | 3:06223ec42c88 | 205 | pc.baud(9600); |
afroid | 3:06223ec42c88 | 206 | i2c.frequency(400000); |
michaelrodriguezg | 4:fcfb02e6a11f | 207 | InitMPU(); |
michaelrodriguezg | 4:fcfb02e6a11f | 208 | pc.printf(".............STARTING.............\n\r"); |
afroid | 3:06223ec42c88 | 209 | // Init. RC522 Chip |
michaelrodriguezg | 4:fcfb02e6a11f | 210 | RfChip.PCD_Init(); |
afroid | 3:06223ec42c88 | 211 | |
afroid | 3:06223ec42c88 | 212 | while (true) { |
michaelrodriguezg | 4:fcfb02e6a11f | 213 | pc.printf(".............BUSCANDO.............\n\r"); |
afroid | 3:06223ec42c88 | 214 | rfid(); |
kirchnet | 2:a0c7513fb634 | 215 | } |
kirchnet | 2:a0c7513fb634 | 216 | } |