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