CPS-Lab1 / Mbed 2 deprecated Lab7

Dependencies:   MPL3115A2 mbed

Committer:
csinders
Date:
Tue Mar 06 14:28:15 2018 +0000
Revision:
3:1238aecec770
Parent:
2:bae4b43dc1ef
Child:
4:dd5899b3eb3b
Cleaning up commented out code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csinders 0:45ed48d2fec2 1 #include "mbed.h"
csinders 1:84b9a3e560fe 2 #include "MPL3115A2.h"
csinders 1:84b9a3e560fe 3
csinders 1:84b9a3e560fe 4 Serial pc(SERIAL_TX, SERIAL_RX);
csinders 1:84b9a3e560fe 5 DigitalOut myled(LED1);
csinders 1:84b9a3e560fe 6 InterruptIn event(PA_4); // GPIO pin being read for when to start Elevator Altimeter
csinders 0:45ed48d2fec2 7
csinders 1:84b9a3e560fe 8 // Selects SDA as I2C1_SDA on pin PB_7
csinders 1:84b9a3e560fe 9 // Selects SCL on I2C1_SCL on pin PB_6
csinders 1:84b9a3e560fe 10 // The I2C address of the pressure sensor is fixed at 0x60.
csinders 1:84b9a3e560fe 11 MPL3115A2 pressure_sensor(PB_7,PB_6,0x60);
csinders 1:84b9a3e560fe 12 int mpl3115_reg_print(int start, int length);
csinders 0:45ed48d2fec2 13
csinders 0:45ed48d2fec2 14 int main() {
csinders 1:84b9a3e560fe 15 pc.printf("starting program\n\r");
csinders 3:1238aecec770 16
csinders 3:1238aecec770 17
csinders 3:1238aecec770 18 /* Prints out registrys
csinders 3:1238aecec770 19 // Use for demo
csinders 3:1238aecec770 20 while(1) {
csinders 3:1238aecec770 21 mpl3115_reg_print(0,0);
csinders 3:1238aecec770 22 }
csinders 3:1238aecec770 23 */
csinders 3:1238aecec770 24
csinders 3:1238aecec770 25 //Test is getID returns the correct value and we are getting good readings
csinders 3:1238aecec770 26 pc.printf("\n\r*** MPL3115A2 Pressure/Temperature Sensor Test *** \n\r");
csinders 3:1238aecec770 27 uint8_t id;
csinders 3:1238aecec770 28 while ((id=pressure_sensor.getID())!=0xC4) {
csinders 3:1238aecec770 29 pc.printf("Status read unsuccessful: Value = 0x%02x\n\r",id);
csinders 3:1238aecec770 30 pc.printf("Check wiring to the pressure sensor\n\r",id);
csinders 3:1238aecec770 31 pc.printf("Retesting for correct ID in 1 second...\n\r");
csinders 3:1238aecec770 32 wait(1);
csinders 3:1238aecec770 33 }
csinders 3:1238aecec770 34 pc.printf("Status read successfully: Value = 0x%02x\n\r",id);
csinders 3:1238aecec770 35 pc.printf("***1hz readings from the pressure sensor***\n\r");
csinders 2:bae4b43dc1ef 36 int i = 0;
csinders 2:bae4b43dc1ef 37 int arraySize = 1000;
csinders 2:bae4b43dc1ef 38 double altitudeReading[arraySize];
csinders 3:1238aecec770 39 // Set all elements in array to -1, so easier to tell what start values were
csinders 2:bae4b43dc1ef 40 for (i = 0; i < arraySize; i++) {
csinders 2:bae4b43dc1ef 41 altitudeReading[i] = -1;
csinders 2:bae4b43dc1ef 42 }
csinders 2:bae4b43dc1ef 43 i = 0;
csinders 2:bae4b43dc1ef 44 while (1) {
csinders 3:1238aecec770 45 if(event.read()) {// if pin A3 has electricy
csinders 2:bae4b43dc1ef 46 pc.printf("reading an altitude at this point\r\n");
csinders 1:84b9a3e560fe 47 altitudeReading[i] = pressure_sensor.getAltitude();
csinders 1:84b9a3e560fe 48 i++;
csinders 1:84b9a3e560fe 49 if (i > arraySize) {
csinders 1:84b9a3e560fe 50 i = 0;
csinders 1:84b9a3e560fe 51 }
csinders 1:84b9a3e560fe 52 }
csinders 3:1238aecec770 53 if (pc.readable()) { // check if we need to print everything
csinders 1:84b9a3e560fe 54 char c = pc.getc();
csinders 1:84b9a3e560fe 55 pc.printf("we read a char, it was %c\r\n",c);
csinders 1:84b9a3e560fe 56 if (c == 'p') {
csinders 1:84b9a3e560fe 57 pc.printf("read a p\r\n");
csinders 1:84b9a3e560fe 58 int j = 0;
csinders 1:84b9a3e560fe 59 for (j = 0; j < arraySize; j++) {
csinders 2:bae4b43dc1ef 60 pc.printf("j = %d, altitude = %f\r\n", j, altitudeReading[j]);
csinders 1:84b9a3e560fe 61 }
csinders 1:84b9a3e560fe 62 }
csinders 1:84b9a3e560fe 63 }
csinders 3:1238aecec770 64 wait_ms(500); // Half a minute is spent between each measure
csinders 1:84b9a3e560fe 65 }
csinders 3:1238aecec770 66 }
csinders 1:84b9a3e560fe 67
csinders 1:84b9a3e560fe 68 int mpl3115_reg_print(int start, int length) {
csinders 1:84b9a3e560fe 69 int maxLength = 0x2E;
csinders 1:84b9a3e560fe 70 int end = maxLength;
csinders 1:84b9a3e560fe 71 uint8_t data;
csinders 1:84b9a3e560fe 72 if ((start + length) < end) {
csinders 1:84b9a3e560fe 73 end = (start + length); // so it only goes to the length
csinders 1:84b9a3e560fe 74 }
csinders 1:84b9a3e560fe 75 if (length == 0) {
csinders 1:84b9a3e560fe 76 end = maxLength; // so it prints till the end
csinders 1:84b9a3e560fe 77 }
csinders 1:84b9a3e560fe 78
csinders 1:84b9a3e560fe 79 // Check if start is within registry
csinders 1:84b9a3e560fe 80 if (start < 0 || start > end) {
csinders 1:84b9a3e560fe 81 pc.printf("Error: start value passed to mpl3115_reg_print outside of range of registry\n\r");
csinders 1:84b9a3e560fe 82 return -1;
csinders 1:84b9a3e560fe 83 }
csinders 1:84b9a3e560fe 84 // check if length is negative
csinders 1:84b9a3e560fe 85 if (length < 0) {
csinders 1:84b9a3e560fe 86 pc.printf("Error: length passed to mpl3115_reg_print is negative\n\r");
csinders 1:84b9a3e560fe 87 return -1;
csinders 1:84b9a3e560fe 88 }
csinders 1:84b9a3e560fe 89 // check if valid communication with device going
csinders 1:84b9a3e560fe 90 if ((data=pressure_sensor.getID())!=0xC4) {
csinders 1:84b9a3e560fe 91 pc.printf("Error: Unable to read from WHO_AM_I register\n\r");
csinders 1:84b9a3e560fe 92 return -1;
csinders 1:84b9a3e560fe 93 }
csinders 2:bae4b43dc1ef 94 // array of names of the registries
csinders 1:84b9a3e560fe 95 char regNames [0x2E][30] = {
csinders 1:84b9a3e560fe 96 "STATUS", "OUT_P_MSB", "OUT_P_CSB",
csinders 1:84b9a3e560fe 97 "OUT_P_LSB", "OUT_T_MSB", "OUT_T_LSB",
csinders 1:84b9a3e560fe 98 "DR_STATUS", "OUT_P_DELTA_MSB", "OUT_P_DELTA_CSB",
csinders 1:84b9a3e560fe 99 "OUT_P_DELTA_LSB", "OUT_T_DELTA_MSB", "OUT_T_DELTA_LSB",
csinders 1:84b9a3e560fe 100 "WHO_AM_I", "F_STATUS", "F_DATA",
csinders 1:84b9a3e560fe 101 "F_SETUP", "TIME_DLY", "SYSMOD",
csinders 1:84b9a3e560fe 102 "INT_SOURCE", "PT_DATA_CFG", "BAR_IN_MSB",
csinders 1:84b9a3e560fe 103 "BAR_IN_LSB", "P_TGT_MSB", "P_TGT_LSB",
csinders 1:84b9a3e560fe 104 "T_TGT", "P_WND_MSB", "P_WND_LSB",
csinders 1:84b9a3e560fe 105 "T_WND", "P_MIN_MSB", "P_MIN_CSB",
csinders 1:84b9a3e560fe 106 "P_MIN_LSB", "T_MIN_MSB", "T_MIN_LSB",
csinders 1:84b9a3e560fe 107 "P_MAX_MSB", "P_MAX_CSB", "P_MAX_LSB",
csinders 1:84b9a3e560fe 108 "T_MAX_MSB", "T_MAX_LSB", "CTRL_REG1",
csinders 1:84b9a3e560fe 109 "CTRL_REG2","CTRL_REG3","CTRL_REG4",
csinders 1:84b9a3e560fe 110 "CTRL_REG5", "OFF_P", "OFF_T", "OFF_H"
csinders 1:84b9a3e560fe 111 };
csinders 2:bae4b43dc1ef 112 // For loop to print out each registry value
csinders 1:84b9a3e560fe 113 for (int i = start; i < end; i++) {
csinders 1:84b9a3e560fe 114 pressure_sensor.readRegs(i,&data,8);
csinders 1:84b9a3e560fe 115 pc.printf("%#04x: %s=%#04x\n\r", i, regNames[i], data); // Print register
csinders 1:84b9a3e560fe 116 wait_ms(500);
csinders 1:84b9a3e560fe 117 }
csinders 1:84b9a3e560fe 118 return 0;
csinders 0:45ed48d2fec2 119 }