CPS_Lab*
/
AltTest2
Lab 7
Fork of AltTest2 by
main.cpp@3:93ba3e6cdaf3, 2018-03-02 (annotated)
- Committer:
- youngs2
- Date:
- Fri Mar 02 17:41:05 2018 +0000
- Revision:
- 3:93ba3e6cdaf3
- Parent:
- 2:126622942102
- Child:
- 4:eaaf67c5e1ca
**
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ajwhelan | 0:d7735965d4ca | 1 | #include "mbed.h" |
ajwhelan | 0:d7735965d4ca | 2 | #include "MPL3115A2.h" |
ajwhelan | 0:d7735965d4ca | 3 | #include <string.h> |
ajwhelan | 0:d7735965d4ca | 4 | #include <stdlib.h> |
ajwhelan | 0:d7735965d4ca | 5 | |
ajwhelan | 0:d7735965d4ca | 6 | Serial pc(SERIAL_TX, SERIAL_RX); |
ajwhelan | 0:d7735965d4ca | 7 | DigitalOut myled(LED1); |
ajwhelan | 0:d7735965d4ca | 8 | MPL3115A2 pressure_sensor(PB_7,PB_6,0x60); |
ajwhelan | 0:d7735965d4ca | 9 | |
ajwhelan | 0:d7735965d4ca | 10 | /* |
ajwhelan | 0:d7735965d4ca | 11 | char *int_array_to_string(uint8_t int_array[], int size_of_array) { |
ajwhelan | 0:d7735965d4ca | 12 | char returnstring[size_of_array]; |
ajwhelan | 0:d7735965d4ca | 13 | for (int temp = 0; temp < size_of_array; temp++) |
ajwhelan | 0:d7735965d4ca | 14 | returnstring[temp] = itoa(int_array[temp]); |
ajwhelan | 0:d7735965d4ca | 15 | return returnstring; |
ajwhelan | 0:d7735965d4ca | 16 | } |
ajwhelan | 0:d7735965d4ca | 17 | */ |
ajwhelan | 0:d7735965d4ca | 18 | int MPL3115A2_reg_print(int start, int length) { |
ajwhelan | 1:03565a1413e6 | 19 | |
ajwhelan | 0:d7735965d4ca | 20 | if(length == 0) |
ajwhelan | 0:d7735965d4ca | 21 | length = 45; |
ajwhelan | 0:d7735965d4ca | 22 | int i = start, end = start + length; |
ajwhelan | 0:d7735965d4ca | 23 | if(start > 0x2D || start < 0x00) |
ajwhelan | 0:d7735965d4ca | 24 | return(-1); |
ajwhelan | 0:d7735965d4ca | 25 | if(length < 0) |
ajwhelan | 0:d7735965d4ca | 26 | return(-1); |
ajwhelan | 0:d7735965d4ca | 27 | if (pressure_sensor.getID()!=0xC4) |
ajwhelan | 0:d7735965d4ca | 28 | return(-1); |
ajwhelan | 0:d7735965d4ca | 29 | if (end > 46) |
ajwhelan | 0:d7735965d4ca | 30 | return(-1); |
youngs2 | 3:93ba3e6cdaf3 | 31 | |
youngs2 | 3:93ba3e6cdaf3 | 32 | printf("Hola\n\r"); |
youngs2 | 3:93ba3e6cdaf3 | 33 | uint8_t valArray[10]; //the array of register values we'll be returning |
youngs2 | 3:93ba3e6cdaf3 | 34 | for(int j = 0; j<10; j++) |
youngs2 | 3:93ba3e6cdaf3 | 35 | valArray[j] = 7; |
youngs2 | 3:93ba3e6cdaf3 | 36 | |
youngs2 | 3:93ba3e6cdaf3 | 37 | for( i = start; i <=end; i++) { |
ajwhelan | 0:d7735965d4ca | 38 | char name[25]; |
ajwhelan | 0:d7735965d4ca | 39 | switch (i) { |
ajwhelan | 0:d7735965d4ca | 40 | case 0x00: |
ajwhelan | 0:d7735965d4ca | 41 | strcpy(name,"MPL_STATUS"); |
ajwhelan | 0:d7735965d4ca | 42 | break; |
ajwhelan | 0:d7735965d4ca | 43 | case 0x01: |
ajwhelan | 0:d7735965d4ca | 44 | strcpy(name,"MPL_OUT_P_MSB"); |
ajwhelan | 0:d7735965d4ca | 45 | break; |
ajwhelan | 0:d7735965d4ca | 46 | case 0x02: |
ajwhelan | 0:d7735965d4ca | 47 | strcpy(name, "MPL_OUT_P_CSB"); |
ajwhelan | 0:d7735965d4ca | 48 | break; |
ajwhelan | 0:d7735965d4ca | 49 | case 0x03: |
ajwhelan | 0:d7735965d4ca | 50 | strcpy(name,"MPL_OUT_P_LSB"); |
ajwhelan | 0:d7735965d4ca | 51 | break; |
ajwhelan | 0:d7735965d4ca | 52 | case 0x04: |
ajwhelan | 0:d7735965d4ca | 53 | strcpy(name,"MPL_OUT_T_MSB"); |
ajwhelan | 0:d7735965d4ca | 54 | break; |
ajwhelan | 0:d7735965d4ca | 55 | case 0x05: |
ajwhelan | 0:d7735965d4ca | 56 | strcpy(name,"MPL_OUT_T_LSB"); |
ajwhelan | 0:d7735965d4ca | 57 | break; |
ajwhelan | 0:d7735965d4ca | 58 | case 0x06: |
ajwhelan | 0:d7735965d4ca | 59 | strcpy(name,"MPL_DR_STATUS"); |
ajwhelan | 0:d7735965d4ca | 60 | break; |
ajwhelan | 0:d7735965d4ca | 61 | case 0x07: |
ajwhelan | 0:d7735965d4ca | 62 | strcpy(name,"MPL_OUT_P_DELTA_MSB"); |
ajwhelan | 0:d7735965d4ca | 63 | break; |
ajwhelan | 0:d7735965d4ca | 64 | case 0x08: |
ajwhelan | 0:d7735965d4ca | 65 | strcpy(name,"MPL_OUT_P_DELTA_CSB"); |
ajwhelan | 0:d7735965d4ca | 66 | break; |
ajwhelan | 0:d7735965d4ca | 67 | case 0x09: |
ajwhelan | 0:d7735965d4ca | 68 | strcpy(name,"MPL_OUT_P_DELTA_LSB"); |
ajwhelan | 0:d7735965d4ca | 69 | break; |
ajwhelan | 0:d7735965d4ca | 70 | case 0x0A: |
ajwhelan | 0:d7735965d4ca | 71 | strcpy(name,"MPL_OUT_T_DELTA_MSB"); |
ajwhelan | 0:d7735965d4ca | 72 | break; |
ajwhelan | 0:d7735965d4ca | 73 | case 0x0B: |
ajwhelan | 0:d7735965d4ca | 74 | strcpy(name,"MPL_OUT_T_DELTA_LSB"); |
ajwhelan | 0:d7735965d4ca | 75 | break; |
ajwhelan | 0:d7735965d4ca | 76 | case 0x0C: |
ajwhelan | 0:d7735965d4ca | 77 | strcpy(name,"MPL_WHO_AM_I"); |
ajwhelan | 0:d7735965d4ca | 78 | break; |
ajwhelan | 0:d7735965d4ca | 79 | case 0x0D: |
ajwhelan | 0:d7735965d4ca | 80 | strcpy(name,"MPL_F_STATUS"); |
ajwhelan | 0:d7735965d4ca | 81 | break; |
ajwhelan | 0:d7735965d4ca | 82 | case 0x0E: |
ajwhelan | 0:d7735965d4ca | 83 | strcpy(name,"MPL_F_DATA"); |
ajwhelan | 0:d7735965d4ca | 84 | break; |
ajwhelan | 0:d7735965d4ca | 85 | case 0x0F: |
ajwhelan | 0:d7735965d4ca | 86 | strcpy(name,"MPL_F_SETUP"); |
ajwhelan | 0:d7735965d4ca | 87 | break; |
ajwhelan | 0:d7735965d4ca | 88 | case 0x10: |
ajwhelan | 0:d7735965d4ca | 89 | strcpy(name,"MPL_TIME_DLY"); |
ajwhelan | 0:d7735965d4ca | 90 | break; |
ajwhelan | 0:d7735965d4ca | 91 | case 0x11: |
ajwhelan | 0:d7735965d4ca | 92 | strcpy(name,"MPL_SYSMOD"); |
ajwhelan | 0:d7735965d4ca | 93 | break; |
ajwhelan | 0:d7735965d4ca | 94 | case 0x12: |
ajwhelan | 0:d7735965d4ca | 95 | strcpy(name,"MPL_INT_SOURCE"); |
ajwhelan | 0:d7735965d4ca | 96 | break; |
ajwhelan | 0:d7735965d4ca | 97 | case 0x13: |
ajwhelan | 0:d7735965d4ca | 98 | strcpy(name,"MPL_PT_DATA_CFG"); |
ajwhelan | 0:d7735965d4ca | 99 | break; |
ajwhelan | 0:d7735965d4ca | 100 | case 0x14: |
ajwhelan | 0:d7735965d4ca | 101 | strcpy(name,"MPL_BAR_IN_MSB"); |
ajwhelan | 0:d7735965d4ca | 102 | break; |
ajwhelan | 0:d7735965d4ca | 103 | case 0x15: |
ajwhelan | 0:d7735965d4ca | 104 | strcpy(name,"MPL_BAR_IN_LSB"); |
ajwhelan | 0:d7735965d4ca | 105 | break; |
ajwhelan | 0:d7735965d4ca | 106 | case 0x16: |
ajwhelan | 0:d7735965d4ca | 107 | strcpy(name,"MPL_P_TGT_MSB"); |
ajwhelan | 0:d7735965d4ca | 108 | break; |
ajwhelan | 0:d7735965d4ca | 109 | case 0x17: |
ajwhelan | 0:d7735965d4ca | 110 | strcpy(name,"MPL_P_TGT_LSB"); |
ajwhelan | 0:d7735965d4ca | 111 | break; |
ajwhelan | 0:d7735965d4ca | 112 | case 0x18: |
ajwhelan | 0:d7735965d4ca | 113 | strcpy(name,"MPL_T_TGT"); |
ajwhelan | 0:d7735965d4ca | 114 | break; |
ajwhelan | 0:d7735965d4ca | 115 | case 0x19: |
ajwhelan | 0:d7735965d4ca | 116 | strcpy(name,"MPL_P_WND_MSB"); |
ajwhelan | 0:d7735965d4ca | 117 | break; |
ajwhelan | 0:d7735965d4ca | 118 | case 0x1A: |
ajwhelan | 0:d7735965d4ca | 119 | strcpy(name,"MPL_P_WND_LSB"); |
ajwhelan | 0:d7735965d4ca | 120 | break; |
ajwhelan | 0:d7735965d4ca | 121 | case 0x1B: |
ajwhelan | 0:d7735965d4ca | 122 | strcpy(name,"MPL_T_WND"); |
ajwhelan | 0:d7735965d4ca | 123 | break; |
ajwhelan | 0:d7735965d4ca | 124 | case 0x1C: |
ajwhelan | 0:d7735965d4ca | 125 | strcpy(name,"MPL_P_MIN_MSB"); |
ajwhelan | 0:d7735965d4ca | 126 | break; |
ajwhelan | 0:d7735965d4ca | 127 | case 0x1D: |
ajwhelan | 0:d7735965d4ca | 128 | strcpy(name,"MPL_P_MIN_CSB"); |
ajwhelan | 0:d7735965d4ca | 129 | break; |
ajwhelan | 0:d7735965d4ca | 130 | case 0x1E: |
ajwhelan | 0:d7735965d4ca | 131 | strcpy(name,"MPL_P_MIN_LSB"); |
ajwhelan | 0:d7735965d4ca | 132 | break; |
ajwhelan | 0:d7735965d4ca | 133 | case 0x1F: |
ajwhelan | 0:d7735965d4ca | 134 | strcpy(name,"MPL_T_MIN_MSB"); |
ajwhelan | 0:d7735965d4ca | 135 | break; |
ajwhelan | 0:d7735965d4ca | 136 | case 0x20: |
ajwhelan | 0:d7735965d4ca | 137 | strcpy(name,"MPL_T_MIN_LSB"); |
ajwhelan | 0:d7735965d4ca | 138 | break; |
ajwhelan | 0:d7735965d4ca | 139 | case 0x21: |
ajwhelan | 0:d7735965d4ca | 140 | strcpy(name,"MPL_P_MAX_MSB"); |
ajwhelan | 0:d7735965d4ca | 141 | break; |
ajwhelan | 0:d7735965d4ca | 142 | case 0x22: |
ajwhelan | 0:d7735965d4ca | 143 | strcpy(name,"MPL_P_MAX_CSB"); |
ajwhelan | 0:d7735965d4ca | 144 | break; |
ajwhelan | 0:d7735965d4ca | 145 | case 0x23: |
ajwhelan | 0:d7735965d4ca | 146 | strcpy(name,"MPL_P_MAX_LSB"); |
ajwhelan | 0:d7735965d4ca | 147 | break; |
ajwhelan | 0:d7735965d4ca | 148 | case 0x24: |
ajwhelan | 0:d7735965d4ca | 149 | strcpy(name,"MPL_T_MAX_MSB"); |
ajwhelan | 0:d7735965d4ca | 150 | break; |
ajwhelan | 0:d7735965d4ca | 151 | case 0x25: |
ajwhelan | 0:d7735965d4ca | 152 | strcpy(name,"MPL_T_MAX_LSB"); |
ajwhelan | 0:d7735965d4ca | 153 | break; |
ajwhelan | 0:d7735965d4ca | 154 | case 0x26: |
ajwhelan | 0:d7735965d4ca | 155 | strcpy(name,"MPL_CTRL_REG1"); |
ajwhelan | 0:d7735965d4ca | 156 | break; |
ajwhelan | 0:d7735965d4ca | 157 | case 0x27: |
ajwhelan | 0:d7735965d4ca | 158 | strcpy(name,"MPL_CTRL_REG2"); |
ajwhelan | 0:d7735965d4ca | 159 | break; |
ajwhelan | 0:d7735965d4ca | 160 | case 0x28: |
ajwhelan | 0:d7735965d4ca | 161 | strcpy(name,"MPL_CTRL_REG3"); |
ajwhelan | 0:d7735965d4ca | 162 | break; |
ajwhelan | 0:d7735965d4ca | 163 | case 0x29: |
ajwhelan | 0:d7735965d4ca | 164 | strcpy(name,"MPL_CTRL_REG4"); |
ajwhelan | 0:d7735965d4ca | 165 | break; |
ajwhelan | 0:d7735965d4ca | 166 | case 0x2A: |
ajwhelan | 0:d7735965d4ca | 167 | strcpy(name,"MPL_CTRL_REG5"); |
ajwhelan | 0:d7735965d4ca | 168 | break; |
ajwhelan | 0:d7735965d4ca | 169 | case 0x2B: |
ajwhelan | 0:d7735965d4ca | 170 | strcpy(name,"MPL_OFF_P"); |
ajwhelan | 0:d7735965d4ca | 171 | break; |
ajwhelan | 0:d7735965d4ca | 172 | case 0x2C: |
ajwhelan | 0:d7735965d4ca | 173 | strcpy(name,"MPL_OFF_T"); |
ajwhelan | 0:d7735965d4ca | 174 | break; |
ajwhelan | 0:d7735965d4ca | 175 | case 0x2D: |
ajwhelan | 0:d7735965d4ca | 176 | strcpy(name,"MPL_OFF_H"); |
ajwhelan | 0:d7735965d4ca | 177 | break; |
ajwhelan | 0:d7735965d4ca | 178 | default: |
ajwhelan | 0:d7735965d4ca | 179 | printf("ERR\n\r"); |
youngs2 | 3:93ba3e6cdaf3 | 180 | } |
youngs2 | 3:93ba3e6cdaf3 | 181 | pressure_sensor.readRegs(i, valArray, 1); |
youngs2 | 3:93ba3e6cdaf3 | 182 | printf("0x%02X: %s = 0x%02x\n\r", i, name, valArray[0]); |
ajwhelan | 0:d7735965d4ca | 183 | |
youngs2 | 3:93ba3e6cdaf3 | 184 | |
ajwhelan | 0:d7735965d4ca | 185 | } |
ajwhelan | 0:d7735965d4ca | 186 | return(0); |
ajwhelan | 0:d7735965d4ca | 187 | } |
ajwhelan | 0:d7735965d4ca | 188 | |
ajwhelan | 0:d7735965d4ca | 189 | |
ajwhelan | 0:d7735965d4ca | 190 | int main() { |
youngs2 | 3:93ba3e6cdaf3 | 191 | int start, length, return_val; |
youngs2 | 3:93ba3e6cdaf3 | 192 | uint8_t id; |
youngs2 | 3:93ba3e6cdaf3 | 193 | printf("Hola\n\r"); |
youngs2 | 3:93ba3e6cdaf3 | 194 | printf("Enter start: \n\r"); |
youngs2 | 3:93ba3e6cdaf3 | 195 | scanf("%d", &start); |
youngs2 | 3:93ba3e6cdaf3 | 196 | printf("Enter length: \n\r"); |
youngs2 | 3:93ba3e6cdaf3 | 197 | scanf("%d", &length); |
youngs2 | 3:93ba3e6cdaf3 | 198 | printf("Printing registers: \n\r"); |
youngs2 | 3:93ba3e6cdaf3 | 199 | return_val = MPL3115A2_reg_print(start, length); |
youngs2 | 3:93ba3e6cdaf3 | 200 | printf("%d\n\r", return_val); |
youngs2 | 3:93ba3e6cdaf3 | 201 | pc.printf("\n\r*** MPL3115A2 Pressure/Temperature Sensor Test *** \n\r"); |
youngs2 | 3:93ba3e6cdaf3 | 202 | while ((id=pressure_sensor.getID())!=0xC4) { |
youngs2 | 3:93ba3e6cdaf3 | 203 | pc.printf("Status read unsuccessful: Value = 0x%02x\n\r",id); |
youngs2 | 3:93ba3e6cdaf3 | 204 | pc.printf("Check wiring to the pressure sensor\n\r",id); |
youngs2 | 3:93ba3e6cdaf3 | 205 | pc.printf("Retesting for correct ID in 1 second...\n\r"); |
youngs2 | 3:93ba3e6cdaf3 | 206 | wait(1); |
ajwhelan | 1:03565a1413e6 | 207 | } |
youngs2 | 3:93ba3e6cdaf3 | 208 | pc.printf("Status read successfully: Value = 0x%02x\n\r",id); |
youngs2 | 3:93ba3e6cdaf3 | 209 | pc.printf("***1hz readings from the pressure sensor***\n\r"); |
youngs2 | 3:93ba3e6cdaf3 | 210 | while(1) { |
youngs2 | 3:93ba3e6cdaf3 | 211 | pc.printf("%f hPA; %f m\n\r",pressure_sensor.getPressure(),pressure_sensor.getAltitude()); |
youngs2 | 3:93ba3e6cdaf3 | 212 | myled = !myled; |
youngs2 | 3:93ba3e6cdaf3 | 213 | wait(1); |
youngs2 | 3:93ba3e6cdaf3 | 214 | } |
youngs2 | 3:93ba3e6cdaf3 | 215 | |
ajwhelan | 2:126622942102 | 216 | //no return; while(1) never exited |
ajwhelan | 0:d7735965d4ca | 217 | } |