Elevator Altimeter

Dependencies:   Lab7 mbed

Dependents:   Lab7

Committer:
kmhatre
Date:
Sat Mar 10 01:40:42 2018 +0000
Revision:
0:a67807d31892
Elevator Altimeter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kmhatre 0:a67807d31892 1 /*
kmhatre 0:a67807d31892 2 * ENGR E 210 - CYBER PHYSICAL SYSTEMS (DIGITAL SYSTEMS)
kmhatre 0:a67807d31892 3 * LAB 7 - ELEVATOR ALTIMETER
kmhatre 0:a67807d31892 4 * CODE BY KRISH HEMANT MHATRE AND ETHAN ZHANG
kmhatre 0:a67807d31892 5 */
kmhatre 0:a67807d31892 6
kmhatre 0:a67807d31892 7 #include "mbed.h"
kmhatre 0:a67807d31892 8 #include "MPL3115A2.h"
kmhatre 0:a67807d31892 9
kmhatre 0:a67807d31892 10 Serial pc(SERIAL_TX, SERIAL_RX);
kmhatre 0:a67807d31892 11 DigitalOut myled(LED1);
kmhatre 0:a67807d31892 12
kmhatre 0:a67807d31892 13 // Selects SDA as I2C1_SDA on pin PB_7
kmhatre 0:a67807d31892 14 // Selects SCL on I2C1_SCL on pin PB_6
kmhatre 0:a67807d31892 15 // The I2C address of the pressure sensor is fixed at 0x60.
kmhatre 0:a67807d31892 16 MPL3115A2 pressure_sensor(PB_7,PB_6,0x60);
kmhatre 0:a67807d31892 17 int mpl3115_reg_print(int start, int length);
kmhatre 0:a67807d31892 18
kmhatre 0:a67807d31892 19 int main() {
kmhatre 0:a67807d31892 20 uint8_t id;
kmhatre 0:a67807d31892 21 pc.printf("\n\r*** MPL3115A2 Pressure/Temperature Sensor Test *** \n\r");
kmhatre 0:a67807d31892 22 while ((id=pressure_sensor.getID())!=0xC4) {
kmhatre 0:a67807d31892 23 pc.printf("Status read unsuccessful: Value = 0x%02x\n\r",id);
kmhatre 0:a67807d31892 24 pc.printf("Check wiring to the pressure sensor\n\r",id);
kmhatre 0:a67807d31892 25 pc.printf("Retesting for correct ID in 1 second...\n\r");
kmhatre 0:a67807d31892 26 wait(1);
kmhatre 0:a67807d31892 27 }
kmhatre 0:a67807d31892 28 pc.printf("Status read successfully: Value = 0x%02x\n\r",id);
kmhatre 0:a67807d31892 29 pc.printf("***1hz readings from the pressure sensor***\n\r");
kmhatre 0:a67807d31892 30 while(1)
kmhatre 0:a67807d31892 31 {
kmhatre 0:a67807d31892 32 float distanceA = pressure_sensor.getAltitude();
kmhatre 0:a67807d31892 33 wait(1);
kmhatre 0:a67807d31892 34 myled = 1;
kmhatre 0:a67807d31892 35 float distanceB = pressure_sensor.getAltitude();
kmhatre 0:a67807d31892 36 float velocity;
kmhatre 0:a67807d31892 37 if(distanceA>distanceB)
kmhatre 0:a67807d31892 38 {
kmhatre 0:a67807d31892 39 velocity = distanceA-distanceB;
kmhatre 0:a67807d31892 40 }
kmhatre 0:a67807d31892 41 else
kmhatre 0:a67807d31892 42 {
kmhatre 0:a67807d31892 43 velocity = distanceB-distanceA;
kmhatre 0:a67807d31892 44 }
kmhatre 0:a67807d31892 45 pc.printf("Velocity = %f m/s\n\r", velocity);
kmhatre 0:a67807d31892 46 pc.printf("TEMPERATURE = %2.2f C PRESSURE = %2.2f hPa\n\r", pressure_sensor.getTemperature(), pressure_sensor.getPressure());
kmhatre 0:a67807d31892 47 myled = 0;
kmhatre 0:a67807d31892 48 wait(1);
kmhatre 0:a67807d31892 49 }
kmhatre 0:a67807d31892 50
kmhatre 0:a67807d31892 51
kmhatre 0:a67807d31892 52 //int I = mpl3115_reg_print(0, 50);
kmhatre 0:a67807d31892 53 }
kmhatre 0:a67807d31892 54
kmhatre 0:a67807d31892 55
kmhatre 0:a67807d31892 56 int mpl3115_reg_print(int start, int length) {
kmhatre 0:a67807d31892 57 int return_value = 0;
kmhatre 0:a67807d31892 58 char *text = " ";
kmhatre 0:a67807d31892 59 uint8_t value;
kmhatre 0:a67807d31892 60 int address;
kmhatre 0:a67807d31892 61 if(start < 0x00 || start > 0x2E)
kmhatre 0:a67807d31892 62 {
kmhatre 0:a67807d31892 63 return_value = -1;
kmhatre 0:a67807d31892 64 exit(0);
kmhatre 0:a67807d31892 65 }
kmhatre 0:a67807d31892 66 else
kmhatre 0:a67807d31892 67 {
kmhatre 0:a67807d31892 68 int i = start;
kmhatre 0:a67807d31892 69 for(i = start; i < length; i++)
kmhatre 0:a67807d31892 70 {
kmhatre 0:a67807d31892 71 address = i;
kmhatre 0:a67807d31892 72 if(i == 0)
kmhatre 0:a67807d31892 73 {
kmhatre 0:a67807d31892 74 text = "STATUS";
kmhatre 0:a67807d31892 75 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 76 }
kmhatre 0:a67807d31892 77 else if(i == 1)
kmhatre 0:a67807d31892 78 {
kmhatre 0:a67807d31892 79 text = "OUT_P_MSB";
kmhatre 0:a67807d31892 80 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 81 }
kmhatre 0:a67807d31892 82 else if(i == 2)
kmhatre 0:a67807d31892 83 {
kmhatre 0:a67807d31892 84 text = "OUT_P_CSB";
kmhatre 0:a67807d31892 85 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 86 }
kmhatre 0:a67807d31892 87 else if(i == 3)
kmhatre 0:a67807d31892 88 {
kmhatre 0:a67807d31892 89 text = "OUT_P_LSB";
kmhatre 0:a67807d31892 90 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 91 }
kmhatre 0:a67807d31892 92 else if(i == 4)
kmhatre 0:a67807d31892 93 {
kmhatre 0:a67807d31892 94 text = "OUT_T_MSB";
kmhatre 0:a67807d31892 95 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 96 }
kmhatre 0:a67807d31892 97 else if(i == 5)
kmhatre 0:a67807d31892 98 {
kmhatre 0:a67807d31892 99 text = "OUT_T_LSB";
kmhatre 0:a67807d31892 100 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 101 }
kmhatre 0:a67807d31892 102 else if(i == 6)
kmhatre 0:a67807d31892 103 {
kmhatre 0:a67807d31892 104 text = "DR_STATUS";
kmhatre 0:a67807d31892 105 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 106 }
kmhatre 0:a67807d31892 107 else if(i == 7)
kmhatre 0:a67807d31892 108 {
kmhatre 0:a67807d31892 109 text = "OUT_P_DELTA_MSB";
kmhatre 0:a67807d31892 110 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 111 }
kmhatre 0:a67807d31892 112 else if(i == 8)
kmhatre 0:a67807d31892 113 {
kmhatre 0:a67807d31892 114 text = "OUT_P_DELTA_CSB";
kmhatre 0:a67807d31892 115 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 116 }
kmhatre 0:a67807d31892 117 else if(i == 9)
kmhatre 0:a67807d31892 118 {
kmhatre 0:a67807d31892 119 text = "OUT_P_DELTA_LSB";
kmhatre 0:a67807d31892 120 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 121 }
kmhatre 0:a67807d31892 122 else if(i == 10)
kmhatre 0:a67807d31892 123 {
kmhatre 0:a67807d31892 124 text = "OUT_T_DLETA_MSB";
kmhatre 0:a67807d31892 125 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 126 }
kmhatre 0:a67807d31892 127 else if(i == 11)
kmhatre 0:a67807d31892 128 {
kmhatre 0:a67807d31892 129 text = "OUT_T_DELTA_LSB";
kmhatre 0:a67807d31892 130 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 131 }
kmhatre 0:a67807d31892 132 else if(i == 12)
kmhatre 0:a67807d31892 133 {
kmhatre 0:a67807d31892 134 text = "WHO_AM_I";
kmhatre 0:a67807d31892 135 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 136 }
kmhatre 0:a67807d31892 137 else if(i == 13)
kmhatre 0:a67807d31892 138 {
kmhatre 0:a67807d31892 139 text = "F_STATUS";
kmhatre 0:a67807d31892 140 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 141 }
kmhatre 0:a67807d31892 142 else if(i == 14)
kmhatre 0:a67807d31892 143 {
kmhatre 0:a67807d31892 144 text = "F_DATA";
kmhatre 0:a67807d31892 145 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 146 }
kmhatre 0:a67807d31892 147 else if(i == 15)
kmhatre 0:a67807d31892 148 {
kmhatre 0:a67807d31892 149 text = "F_SETUP";
kmhatre 0:a67807d31892 150 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 151 }
kmhatre 0:a67807d31892 152 else if(i == 16)
kmhatre 0:a67807d31892 153 {
kmhatre 0:a67807d31892 154 text = "TIME_DLY";
kmhatre 0:a67807d31892 155 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 156 }
kmhatre 0:a67807d31892 157 else if(i == 17)
kmhatre 0:a67807d31892 158 {
kmhatre 0:a67807d31892 159 text = "SYSMOD";
kmhatre 0:a67807d31892 160 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 161 }
kmhatre 0:a67807d31892 162 else if(i == 18)
kmhatre 0:a67807d31892 163 {
kmhatre 0:a67807d31892 164 text = "INT_SOURCE";
kmhatre 0:a67807d31892 165 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 166 }
kmhatre 0:a67807d31892 167 else if(i == 19)
kmhatre 0:a67807d31892 168 {
kmhatre 0:a67807d31892 169 text = "PT_DATA_CFG";
kmhatre 0:a67807d31892 170 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 171 }
kmhatre 0:a67807d31892 172 else if(i == 20)
kmhatre 0:a67807d31892 173 {
kmhatre 0:a67807d31892 174 text = "BAR_IN_MSB";
kmhatre 0:a67807d31892 175 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 176 }
kmhatre 0:a67807d31892 177 else if(i == 21)
kmhatre 0:a67807d31892 178 {
kmhatre 0:a67807d31892 179 text = "BAR_IN_LSB";
kmhatre 0:a67807d31892 180 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 181 }
kmhatre 0:a67807d31892 182 else if(i == 22)
kmhatre 0:a67807d31892 183 {
kmhatre 0:a67807d31892 184 text = "P_TGT_MSB";
kmhatre 0:a67807d31892 185 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 186 }
kmhatre 0:a67807d31892 187 else if(i == 23)
kmhatre 0:a67807d31892 188 {
kmhatre 0:a67807d31892 189 text = "P_TGT_LSB";
kmhatre 0:a67807d31892 190 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 191 }
kmhatre 0:a67807d31892 192 else if(i == 24)
kmhatre 0:a67807d31892 193 {
kmhatre 0:a67807d31892 194 text = "T_TGT";
kmhatre 0:a67807d31892 195 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 196 }
kmhatre 0:a67807d31892 197 else if(i == 25)
kmhatre 0:a67807d31892 198 {
kmhatre 0:a67807d31892 199 text = "P_WND_MSB";
kmhatre 0:a67807d31892 200 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 201 }
kmhatre 0:a67807d31892 202 else if(i == 26)
kmhatre 0:a67807d31892 203 {
kmhatre 0:a67807d31892 204 text = "P_WND_LSB";
kmhatre 0:a67807d31892 205 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 206 }
kmhatre 0:a67807d31892 207 else if(i == 27)
kmhatre 0:a67807d31892 208 {
kmhatre 0:a67807d31892 209 text = "T_WND";
kmhatre 0:a67807d31892 210 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 211 }
kmhatre 0:a67807d31892 212 else if(i == 28)
kmhatre 0:a67807d31892 213 {
kmhatre 0:a67807d31892 214 text = "P_MIN_MSB";
kmhatre 0:a67807d31892 215 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 216 }
kmhatre 0:a67807d31892 217 else if(i == 29)
kmhatre 0:a67807d31892 218 {
kmhatre 0:a67807d31892 219 text = "P_MIN_CSB";
kmhatre 0:a67807d31892 220 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 221 }
kmhatre 0:a67807d31892 222 else if(i == 30)
kmhatre 0:a67807d31892 223 {
kmhatre 0:a67807d31892 224 text = "P_MIN_MSB";
kmhatre 0:a67807d31892 225 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 226 }
kmhatre 0:a67807d31892 227 else if(i == 31)
kmhatre 0:a67807d31892 228 {
kmhatre 0:a67807d31892 229 text = "T_MIN_MSB";
kmhatre 0:a67807d31892 230 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 231 }
kmhatre 0:a67807d31892 232 else if(i == 32)
kmhatre 0:a67807d31892 233 {
kmhatre 0:a67807d31892 234 text = "T_MIN_LSB";
kmhatre 0:a67807d31892 235 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 236 }
kmhatre 0:a67807d31892 237 else if(i == 33)
kmhatre 0:a67807d31892 238 {
kmhatre 0:a67807d31892 239 text = "P_MAX_MSB";
kmhatre 0:a67807d31892 240 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 241 }
kmhatre 0:a67807d31892 242 else if(i == 34)
kmhatre 0:a67807d31892 243 {
kmhatre 0:a67807d31892 244 text = "P_MAX_CSB";
kmhatre 0:a67807d31892 245 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 246 }
kmhatre 0:a67807d31892 247 else if(i == 35)
kmhatre 0:a67807d31892 248 {
kmhatre 0:a67807d31892 249 text = "P_MAX_MSB";
kmhatre 0:a67807d31892 250 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 251 }
kmhatre 0:a67807d31892 252 else if(i == 36)
kmhatre 0:a67807d31892 253 {
kmhatre 0:a67807d31892 254 text = "T_MAX_MSB";
kmhatre 0:a67807d31892 255 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 256 }
kmhatre 0:a67807d31892 257 else if(i == 37)
kmhatre 0:a67807d31892 258 {
kmhatre 0:a67807d31892 259 text = "T_MAX_LSB";
kmhatre 0:a67807d31892 260 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 261 }
kmhatre 0:a67807d31892 262 else if(i == 38)
kmhatre 0:a67807d31892 263 {
kmhatre 0:a67807d31892 264 text = "CTRL_REG1";
kmhatre 0:a67807d31892 265 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 266 }
kmhatre 0:a67807d31892 267 else if(i == 39)
kmhatre 0:a67807d31892 268 {
kmhatre 0:a67807d31892 269 text = "CTRL_REG2";
kmhatre 0:a67807d31892 270 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 271 }
kmhatre 0:a67807d31892 272 else if(i == 40)
kmhatre 0:a67807d31892 273 {
kmhatre 0:a67807d31892 274 text = "CTRL_REG3";
kmhatre 0:a67807d31892 275 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 276 }
kmhatre 0:a67807d31892 277 else if(i == 41)
kmhatre 0:a67807d31892 278 {
kmhatre 0:a67807d31892 279 text = "CTRL_REG4";
kmhatre 0:a67807d31892 280 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 281 }
kmhatre 0:a67807d31892 282 else if(i == 42)
kmhatre 0:a67807d31892 283 {
kmhatre 0:a67807d31892 284 text = "CTRL_REG5";
kmhatre 0:a67807d31892 285 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 286 }
kmhatre 0:a67807d31892 287 else if(i == 43)
kmhatre 0:a67807d31892 288 {
kmhatre 0:a67807d31892 289 text = "OFF_P";
kmhatre 0:a67807d31892 290 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 291 }
kmhatre 0:a67807d31892 292 else if(i == 44)
kmhatre 0:a67807d31892 293 {
kmhatre 0:a67807d31892 294 text = "OFF_T";
kmhatre 0:a67807d31892 295 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 296 }
kmhatre 0:a67807d31892 297 else if(i == 45)
kmhatre 0:a67807d31892 298 {
kmhatre 0:a67807d31892 299 text = "OFF_H";
kmhatre 0:a67807d31892 300 pressure_sensor.readRegs(address, &value, 8);
kmhatre 0:a67807d31892 301 }
kmhatre 0:a67807d31892 302 else
kmhatre 0:a67807d31892 303 {
kmhatre 0:a67807d31892 304 text = "NULL";
kmhatre 0:a67807d31892 305 value = NULL;
kmhatre 0:a67807d31892 306
kmhatre 0:a67807d31892 307 }
kmhatre 0:a67807d31892 308
kmhatre 0:a67807d31892 309 if(i <= 46 && text != "NULL")
kmhatre 0:a67807d31892 310 {
kmhatre 0:a67807d31892 311 printf("0x%2.2hhX: %s=0x%2.2hhX \n\r", address, text, value);
kmhatre 0:a67807d31892 312 }
kmhatre 0:a67807d31892 313 }
kmhatre 0:a67807d31892 314 }
kmhatre 0:a67807d31892 315 return return_value;
kmhatre 0:a67807d31892 316 }
kmhatre 0:a67807d31892 317