![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
.
Dependencies: SHT3XA mbed LIS3DH mbed-rtos USBDevice
main.cpp@1:323175fe9443, 2018-12-16 (annotated)
- Committer:
- diogo966
- Date:
- Sun Dec 16 11:21:09 2018 +0000
- Revision:
- 1:323175fe9443
- Parent:
- 0:941291e23892
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:941291e23892 | 1 | #include "mbed.h" |
bcostm | 0:941291e23892 | 2 | #include "USBSerial.h" |
diogo966 | 1:323175fe9443 | 3 | #include "stdio.h" |
diogo966 | 1:323175fe9443 | 4 | #include "stdlib.h" |
diogo966 | 1:323175fe9443 | 5 | #include "math.h" |
diogo966 | 1:323175fe9443 | 6 | #include "SHT3XA.h" |
diogo966 | 1:323175fe9443 | 7 | #include "rtos.h" |
bcostm | 0:941291e23892 | 8 | |
diogo966 | 1:323175fe9443 | 9 | I2C i2c(PB_9,PB_8); |
diogo966 | 1:323175fe9443 | 10 | SHT3XA sht31a(PA_4); |
diogo966 | 1:323175fe9443 | 11 | int Acel_Buffer[4]; |
diogo966 | 1:323175fe9443 | 12 | float Acel_value=0; |
diogo966 | 1:323175fe9443 | 13 | int ddx = 1; |
diogo966 | 1:323175fe9443 | 14 | char ccc; |
diogo966 | 1:323175fe9443 | 15 | |
bcostm | 0:941291e23892 | 16 | USBSerial usb_serial; |
diogo966 | 1:323175fe9443 | 17 | DigitalOut led(LED3); |
diogo966 | 1:323175fe9443 | 18 | DigitalOut led2(LED4); |
diogo966 | 1:323175fe9443 | 19 | |
diogo966 | 1:323175fe9443 | 20 | Thread thread; |
diogo966 | 1:323175fe9443 | 21 | |
diogo966 | 1:323175fe9443 | 22 | //GPS_Coordenates[80]; |
diogo966 | 1:323175fe9443 | 23 | // addr |
diogo966 | 1:323175fe9443 | 24 | #define WRITE_ADDR 0x52 |
diogo966 | 1:323175fe9443 | 25 | #define READ_ADDR 0x53 |
diogo966 | 1:323175fe9443 | 26 | // LIS3DH ID |
diogo966 | 1:323175fe9443 | 27 | #define I_AM_LIS3DH 0x33 |
bcostm | 0:941291e23892 | 28 | |
diogo966 | 1:323175fe9443 | 29 | // Register's definition |
diogo966 | 1:323175fe9443 | 30 | #define LIS3DH_STATUS_REG_AUX 0x07 |
diogo966 | 1:323175fe9443 | 31 | #define LIS3DH_OUT_ADC1_L 0x08 |
diogo966 | 1:323175fe9443 | 32 | #define LIS3DH_OUT_ADC1_H 0x09 |
diogo966 | 1:323175fe9443 | 33 | #define LIS3DH_OUT_ADC2_L 0x0a |
diogo966 | 1:323175fe9443 | 34 | #define LIS3DH_OUT_ADC2_H 0x0b |
diogo966 | 1:323175fe9443 | 35 | #define LIS3DH_OUT_ADC3_L 0x0c |
diogo966 | 1:323175fe9443 | 36 | #define LIS3DH_OUT_ADC3_H 0x0d |
diogo966 | 1:323175fe9443 | 37 | #define LIS3DH_INT_COUNTER_REG 0x0e |
diogo966 | 1:323175fe9443 | 38 | #define LIS3DH_WHO_AM_I 0x0f |
diogo966 | 1:323175fe9443 | 39 | #define LIS3DH_TEMP_CFG_REG 0x1f |
diogo966 | 1:323175fe9443 | 40 | #define LIS3DH_CTRL_REG1 0x20 |
diogo966 | 1:323175fe9443 | 41 | #define LIS3DH_CTRL_REG2 0x21 |
diogo966 | 1:323175fe9443 | 42 | #define LIS3DH_CTRL_REG3 0x22 |
diogo966 | 1:323175fe9443 | 43 | #define LIS3DH_CTRL_REG4 0x23 |
diogo966 | 1:323175fe9443 | 44 | #define LIS3DH_CTRL_REG5 0x24 |
diogo966 | 1:323175fe9443 | 45 | #define LIS3DH_CTRL_REG6 0x25 |
diogo966 | 1:323175fe9443 | 46 | #define LIS3DH_REFERENCE 0x26 |
diogo966 | 1:323175fe9443 | 47 | #define LIS3DH_STATUS_REG2 0x27 |
diogo966 | 1:323175fe9443 | 48 | #define LIS3DH_OUT_X_H 0x29 |
diogo966 | 1:323175fe9443 | 49 | #define LIS3DH_OUT_Y_H 0x2b |
diogo966 | 1:323175fe9443 | 50 | #define LIS3DH_OUT_Z_H 0x2d |
diogo966 | 1:323175fe9443 | 51 | #define LIS3DH_FIFO_CTRL_REG 0x2e |
diogo966 | 1:323175fe9443 | 52 | #define LIS3DH_FIFO_SRC_REG 0x2f |
diogo966 | 1:323175fe9443 | 53 | #define LIS3DH_INT1_CFG 0x30 |
diogo966 | 1:323175fe9443 | 54 | #define LIS3DH_INT1_SOURCE 0x31 |
diogo966 | 1:323175fe9443 | 55 | #define LIS3DH_INT1_THS 0x32 |
diogo966 | 1:323175fe9443 | 56 | #define LIS3DH_INT1_DURATION 0x33 |
diogo966 | 1:323175fe9443 | 57 | #define LIS3DH_CLICK_CFG 0x38 |
diogo966 | 1:323175fe9443 | 58 | #define LIS3DH_CLICK_SRC 0x39 |
diogo966 | 1:323175fe9443 | 59 | #define LIS3DH_CLICK_THS 0x3a |
diogo966 | 1:323175fe9443 | 60 | #define LIS3DH_TIME_LIMIT 0x3b |
diogo966 | 1:323175fe9443 | 61 | #define LIS3DH_TIME_LATENCY 0x3c |
diogo966 | 1:323175fe9443 | 62 | #define LIS3DH_TIME_WINDOW 0x3d |
bcostm | 0:941291e23892 | 63 | |
diogo966 | 1:323175fe9443 | 64 | // definition for Nomalization |
diogo966 | 1:323175fe9443 | 65 | #define LIS3DH_SENSITIVITY_2G (0.001F) |
diogo966 | 1:323175fe9443 | 66 | #define LIS3DH_SENSITIVITY_4G (0.002F) |
diogo966 | 1:323175fe9443 | 67 | #define LIS3DH_SENSITIVITY_8G (0.004F) |
diogo966 | 1:323175fe9443 | 68 | #define LIS3DH_SENSITIVITY_16G (0.012F) |
diogo966 | 1:323175fe9443 | 69 | |
diogo966 | 1:323175fe9443 | 70 | |
diogo966 | 1:323175fe9443 | 71 | #define GRAVITY (9.80665F) |
bcostm | 0:941291e23892 | 72 | |
diogo966 | 1:323175fe9443 | 73 | void main_thread(){ |
diogo966 | 1:323175fe9443 | 74 | char dbf[2],addr,data[6]; |
diogo966 | 1:323175fe9443 | 75 | float dt[3],acc[2][3],a,b,c,d; |
diogo966 | 1:323175fe9443 | 76 | int xx=0; |
diogo966 | 1:323175fe9443 | 77 | addr = WRITE_ADDR; |
diogo966 | 1:323175fe9443 | 78 | dbf[0] = LIS3DH_WHO_AM_I; |
diogo966 | 1:323175fe9443 | 79 | i2c.write(addr, dbf, 1); |
diogo966 | 1:323175fe9443 | 80 | i2c.read(addr | 1, dbf, 1); |
diogo966 | 1:323175fe9443 | 81 | led2=1; |
diogo966 | 1:323175fe9443 | 82 | if(dbf[0] == I_AM_LIS3DH) { |
diogo966 | 1:323175fe9443 | 83 | |
diogo966 | 1:323175fe9443 | 84 | // |
diogo966 | 1:323175fe9443 | 85 | while(1){ |
diogo966 | 1:323175fe9443 | 86 | for(int i=0; i<12;i++) { |
diogo966 | 1:323175fe9443 | 87 | |
diogo966 | 1:323175fe9443 | 88 | dbf[0] = LIS3DH_CTRL_REG1; |
diogo966 | 1:323175fe9443 | 89 | dbf[1] = 0x27; //ORIGINAL 0x7f |
diogo966 | 1:323175fe9443 | 90 | i2c.write(addr , dbf , 2); |
diogo966 | 1:323175fe9443 | 91 | //======================================================// |
diogo966 | 1:323175fe9443 | 92 | dbf[0] = LIS3DH_OUT_X_H | 0x80; |
diogo966 | 1:323175fe9443 | 93 | //dbf[1] = 0xff; |
diogo966 | 1:323175fe9443 | 94 | i2c.write(addr, dbf, 1); |
diogo966 | 1:323175fe9443 | 95 | |
diogo966 | 1:323175fe9443 | 96 | dbf[0] = LIS3DH_OUT_Y_H | 0x80; |
diogo966 | 1:323175fe9443 | 97 | i2c.write(addr, dbf, 1); |
diogo966 | 1:323175fe9443 | 98 | |
diogo966 | 1:323175fe9443 | 99 | dbf[0] = LIS3DH_OUT_Z_H | 0x80; |
diogo966 | 1:323175fe9443 | 100 | i2c.write(addr, dbf, 1); |
diogo966 | 1:323175fe9443 | 101 | |
diogo966 | 1:323175fe9443 | 102 | i2c.read(addr | 1, data, 6); |
diogo966 | 1:323175fe9443 | 103 | dt[0] = float(short((data[1]<<8) | data[0])) *LIS3DH_SENSITIVITY_16G / 15 * GRAVITY; |
diogo966 | 1:323175fe9443 | 104 | dt[1] = float(short((data[3]<<8) | data[2])) *LIS3DH_SENSITIVITY_16G / 15 * GRAVITY; |
diogo966 | 1:323175fe9443 | 105 | dt[2] = float(short((data[5]<<8) | data[4])) *LIS3DH_SENSITIVITY_16G / 15 * GRAVITY; |
diogo966 | 1:323175fe9443 | 106 | |
diogo966 | 1:323175fe9443 | 107 | acc[xx][0] = dt[0]; |
diogo966 | 1:323175fe9443 | 108 | acc[xx][1] = dt[1]; |
diogo966 | 1:323175fe9443 | 109 | acc[xx][2] = dt[2]; |
diogo966 | 1:323175fe9443 | 110 | if (xx == 0){ |
diogo966 | 1:323175fe9443 | 111 | xx=1; |
diogo966 | 1:323175fe9443 | 112 | } |
diogo966 | 1:323175fe9443 | 113 | else{ |
diogo966 | 1:323175fe9443 | 114 | xx =0; |
diogo966 | 1:323175fe9443 | 115 | } |
diogo966 | 1:323175fe9443 | 116 | a = (acc[1][0]-acc[0][0])*(acc[1][0]-acc[0][0]); |
diogo966 | 1:323175fe9443 | 117 | b = (acc[1][1]-acc[0][1])*(acc[1][1]-acc[0][1]); |
diogo966 | 1:323175fe9443 | 118 | c = (acc[1][2]-acc[0][2])*(acc[1][2]-acc[0][2]); |
diogo966 | 1:323175fe9443 | 119 | d = sqrt(a+b+c); |
diogo966 | 1:323175fe9443 | 120 | wait(5); |
diogo966 | 1:323175fe9443 | 121 | //usb_serial.printf("x -> %f y -> %f z -> %f\r\n",dt[0],dt[1],dt[2]); |
diogo966 | 1:323175fe9443 | 122 | usb_serial.printf("Acelaration -> %f\r\n",d); |
diogo966 | 1:323175fe9443 | 123 | |
diogo966 | 1:323175fe9443 | 124 | if (d < 0.5){ |
diogo966 | 1:323175fe9443 | 125 | Acel_value++; |
diogo966 | 1:323175fe9443 | 126 | } |
diogo966 | 1:323175fe9443 | 127 | else{ |
diogo966 | 1:323175fe9443 | 128 | Acel_value = 0; |
diogo966 | 1:323175fe9443 | 129 | |
diogo966 | 1:323175fe9443 | 130 | } |
diogo966 | 1:323175fe9443 | 131 | |
diogo966 | 1:323175fe9443 | 132 | if (Acel_value == 4){ |
diogo966 | 1:323175fe9443 | 133 | while(1){ |
diogo966 | 1:323175fe9443 | 134 | led2=0; |
diogo966 | 1:323175fe9443 | 135 | wait(0.5); |
diogo966 | 1:323175fe9443 | 136 | led2 = 1; |
diogo966 | 1:323175fe9443 | 137 | } |
diogo966 | 1:323175fe9443 | 138 | } |
diogo966 | 1:323175fe9443 | 139 | |
diogo966 | 1:323175fe9443 | 140 | led = 0; |
diogo966 | 1:323175fe9443 | 141 | wait(0.5); |
diogo966 | 1:323175fe9443 | 142 | led = 1; |
diogo966 | 1:323175fe9443 | 143 | wait(5); |
diogo966 | 1:323175fe9443 | 144 | |
diogo966 | 1:323175fe9443 | 145 | usb_serial.printf("Temperature -> %f\r\n",sht31a.readTempC()); |
diogo966 | 1:323175fe9443 | 146 | if (sht31a.readTempC() > 35){ |
diogo966 | 1:323175fe9443 | 147 | while(1){ |
diogo966 | 1:323175fe9443 | 148 | led2=0; |
diogo966 | 1:323175fe9443 | 149 | wait(0.5); |
diogo966 | 1:323175fe9443 | 150 | led2 = 1; |
diogo966 | 1:323175fe9443 | 151 | } |
diogo966 | 1:323175fe9443 | 152 | } |
diogo966 | 1:323175fe9443 | 153 | |
diogo966 | 1:323175fe9443 | 154 | led = 0; |
diogo966 | 1:323175fe9443 | 155 | wait(0.5); |
diogo966 | 1:323175fe9443 | 156 | led = 1; |
bcostm | 0:941291e23892 | 157 | |
diogo966 | 1:323175fe9443 | 158 | |
diogo966 | 1:323175fe9443 | 159 | } |
diogo966 | 1:323175fe9443 | 160 | } |
diogo966 | 1:323175fe9443 | 161 | } |
diogo966 | 1:323175fe9443 | 162 | |
diogo966 | 1:323175fe9443 | 163 | } |
bcostm | 0:941291e23892 | 164 | |
diogo966 | 1:323175fe9443 | 165 | |
diogo966 | 1:323175fe9443 | 166 | |
diogo966 | 1:323175fe9443 | 167 | int main() { |
diogo966 | 1:323175fe9443 | 168 | |
diogo966 | 1:323175fe9443 | 169 | thread.start(main_thread); |
diogo966 | 1:323175fe9443 | 170 | |
bcostm | 0:941291e23892 | 171 | } |