CPS-Lab1 / Mbed 2 deprecated Lab7

Dependencies:   MPL3115A2 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MPL3115A2.h"
00003 
00004 Serial pc(SERIAL_TX, SERIAL_RX);
00005 DigitalOut myled(LED1);
00006 InterruptIn event(PA_4); // GPIO pin being read for when to start Elevator Altimeter
00007 
00008 // Selects SDA as I2C1_SDA on pin PB_7
00009 // Selects SCL on I2C1_SCL on pin PB_6
00010 // The I2C address of the pressure sensor is fixed at 0x60. 
00011 MPL3115A2 pressure_sensor(PB_7,PB_6,0x60);
00012 int mpl3115_reg_print(int start, int length);
00013 
00014 int main() {
00015     pc.printf("starting program\n\r");
00016     
00017     
00018     //Prints out registrys 
00019     // Use for demo
00020     while(1) {
00021         mpl3115_reg_print(0,0);
00022     }
00023     
00024     
00025     //Test is getID returns the correct value and we are getting good readings
00026     pc.printf("\n\r*** MPL3115A2 Pressure/Temperature Sensor Test *** \n\r");
00027     uint8_t id;
00028     while ((id=pressure_sensor.getID())!=0xC4) {
00029         pc.printf("Status read unsuccessful: Value = 0x%02x\n\r",id);
00030         pc.printf("Check wiring to the pressure sensor\n\r",id);
00031         pc.printf("Retesting for correct ID in 1 second...\n\r");
00032         wait(1);
00033     }
00034     pc.printf("Status read successfully: Value = 0x%02x\n\r",id);
00035     pc.printf("***1hz readings from the pressure sensor***\n\r");
00036     int i = 0;
00037     int arraySize = 1000;
00038     double altitudeReading[arraySize];
00039     // Set all elements in array to -1, so easier to tell what start values were
00040     for (i = 0; i < arraySize; i++) { 
00041         altitudeReading[i] = -1;
00042     }
00043     i = 0;
00044     while (1) {
00045         if(event.read()) {// if pin A3 has electricy
00046             pc.printf("reading an altitude at this point\r\n");
00047             altitudeReading[i] = pressure_sensor.getAltitude();
00048             i++;
00049             if (i > arraySize) {
00050                 i = 0;
00051             }
00052         }
00053         if (pc.readable()) { // check if we need to print everything
00054             char c = pc.getc();
00055             pc.printf("we read a char, it was %c\r\n",c);
00056             if (c == 'p') {
00057                 pc.printf("read a p\r\n");
00058                 int j = 0;
00059                 for (j = 0; j < arraySize; j++) { 
00060                     pc.printf("j = %d, altitude = %f\r\n", j, altitudeReading[j]);                
00061                 }
00062             }
00063         }
00064         wait_ms(500); // Half a second is spent between each measure
00065     }
00066 }   
00067 
00068 int mpl3115_reg_print(int start, int length)  {
00069     int maxLength = 0x2E;
00070     int end = maxLength;
00071     uint8_t data;
00072     if ((start + length) < end) {
00073         end = (start + length); // so it only goes to the length
00074     }
00075     if (length == 0) { 
00076         end = maxLength; // so it prints till the end
00077     }
00078     
00079     // Check if start is within registry
00080     if (start < 0 || start > end) {
00081         pc.printf("Error: start value passed to mpl3115_reg_print outside of range of registry\n\r");
00082         return -1;
00083     }
00084     // check if length is negative
00085     if (length < 0) {
00086         pc.printf("Error: length passed to mpl3115_reg_print is negative\n\r");
00087         return -1;
00088     }
00089     // check if valid communication with device going
00090     if ((data=pressure_sensor.getID())!=0xC4) {
00091         pc.printf("Error: Unable to read from WHO_AM_I register\n\r");
00092         return -1;
00093     }
00094     // array of names of the registries
00095     char regNames [0x2E][30] = {
00096         "STATUS", "OUT_P_MSB", "OUT_P_CSB",
00097         "OUT_P_LSB", "OUT_T_MSB", "OUT_T_LSB",
00098         "DR_STATUS", "OUT_P_DELTA_MSB", "OUT_P_DELTA_CSB",
00099         "OUT_P_DELTA_LSB", "OUT_T_DELTA_MSB", "OUT_T_DELTA_LSB",
00100         "WHO_AM_I", "F_STATUS", "F_DATA", 
00101         "F_SETUP", "TIME_DLY", "SYSMOD", 
00102         "INT_SOURCE", "PT_DATA_CFG", "BAR_IN_MSB",
00103         "BAR_IN_LSB", "P_TGT_MSB", "P_TGT_LSB",
00104         "T_TGT", "P_WND_MSB", "P_WND_LSB",
00105         "T_WND", "P_MIN_MSB", "P_MIN_CSB",
00106         "P_MIN_LSB", "T_MIN_MSB", "T_MIN_LSB",
00107         "P_MAX_MSB", "P_MAX_CSB", "P_MAX_LSB",
00108         "T_MAX_MSB", "T_MAX_LSB", "CTRL_REG1",
00109         "CTRL_REG2","CTRL_REG3","CTRL_REG4",
00110         "CTRL_REG5", "OFF_P", "OFF_T", "OFF_H" 
00111         };
00112     // For loop to print out each registry value
00113     for (int i = start; i < end; i++) { 
00114         pressure_sensor.readRegs(i,&data,8);
00115         pc.printf("%#04x: %s=%#04x\n\r", i, regNames[i], data); // Print register
00116         wait_ms(50);
00117     } 
00118     return 0;
00119 }