![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Demo program for the MPL3115A2 library.
Dependencies: FOCOMETROPRESION mbed
Fork of WIGO_MPL3115A2 by
main.cpp@8:c415332bdea7, 2014-10-02 (annotated)
- Committer:
- perezalvarezhi
- Date:
- Thu Oct 02 21:31:27 2014 +0000
- Revision:
- 8:c415332bdea7
- Parent:
- 7:97f5edf7d270
adfasdf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
clemente | 0:5d92b48edaff | 1 | #include "mbed.h" |
clemente | 0:5d92b48edaff | 2 | #include "MPL3115A2.h" |
clemente | 0:5d92b48edaff | 3 | |
clemente | 0:5d92b48edaff | 4 | #define MPL3115A2_I2C_ADDRESS (0x60<<1) |
perezalvarezhi | 8:c415332bdea7 | 5 | #define |
clemente | 0:5d92b48edaff | 6 | DigitalOut myled(LED1); |
perezalvarezhi | 8:c415332bdea7 | 7 | MPL3115A2 wigo_sensor1( PTC9, PTC8, MPL3115A2_I2C_ADDRESS); |
clemente | 0:5d92b48edaff | 8 | Serial pc(USBTX, USBRX); |
clemente | 0:5d92b48edaff | 9 | |
clemente | 0:5d92b48edaff | 10 | /* pos [0] = altimeter or pressure value */ |
clemente | 0:5d92b48edaff | 11 | /* pos [1] = temperature value */ |
clemente | 0:5d92b48edaff | 12 | float sensor_data[2]; |
clemente | 0:5d92b48edaff | 13 | |
clemente | 2:0b726b8c8ab9 | 14 | void dataready( void); // callback function for data streaming using Interrupt |
clemente | 2:0b726b8c8ab9 | 15 | void alttrigger( void); |
clemente | 2:0b726b8c8ab9 | 16 | |
clemente | 3:e2a621ea6976 | 17 | /* Helper functions to print out as float the raw data */ |
clemente | 3:e2a621ea6976 | 18 | float print_PressureValue( unsigned char *dt); |
clemente | 3:e2a621ea6976 | 19 | float print_AltimiterValue( unsigned char *dt); |
clemente | 3:e2a621ea6976 | 20 | float print_TemperatureValue( unsigned char *dt); |
clemente | 3:e2a621ea6976 | 21 | |
clemente | 0:5d92b48edaff | 22 | int main() { |
perezalvarezhi | 8:c415332bdea7 | 23 | pc.baud(9600); |
clemente | 2:0b726b8c8ab9 | 24 | pc.printf("MPL3115A2 Sensor. [%X]\r\n", wigo_sensor1.getDeviceID()); |
clemente | 7:97f5edf7d270 | 25 | // ***** Data acquisition using polling method and delta values |
clemente | 7:97f5edf7d270 | 26 | // Array for the dela values |
perezalvarezhi | 8:c415332bdea7 | 27 | //float delta[2]; |
clemente | 7:97f5edf7d270 | 28 | |
clemente | 7:97f5edf7d270 | 29 | // Set over sampling value (see MPL3115A2.h for details) |
clemente | 7:97f5edf7d270 | 30 | wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_128); |
clemente | 7:97f5edf7d270 | 31 | // Configure the sensor as Barometer. |
clemente | 7:97f5edf7d270 | 32 | wigo_sensor1.Barometric_Mode(); |
perezalvarezhi | 8:c415332bdea7 | 33 | int i =0; |
clemente | 7:97f5edf7d270 | 34 | while(1) { |
clemente | 7:97f5edf7d270 | 35 | // |
perezalvarezhi | 8:c415332bdea7 | 36 | if ( wigo_sensor1.getAllData( &sensor_data[0]) ) { |
clemente | 7:97f5edf7d270 | 37 | pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]); |
perezalvarezhi | 8:c415332bdea7 | 38 | //pc.printf("\tDelatPress: %f\tDeltaTemp: %f\r\n", delta[0], delta[1]); |
perezalvarezhi | 8:c415332bdea7 | 39 | }else{ |
perezalvarezhi | 8:c415332bdea7 | 40 | pc.printf("Not reading any more"); |
clemente | 7:97f5edf7d270 | 41 | } |
perezalvarezhi | 8:c415332bdea7 | 42 | pc.printf("Measurement# %d",i); |
perezalvarezhi | 8:c415332bdea7 | 43 | i++; |
clemente | 7:97f5edf7d270 | 44 | // |
clemente | 7:97f5edf7d270 | 45 | wait( 1.0); |
clemente | 7:97f5edf7d270 | 46 | } |
clemente | 7:97f5edf7d270 | 47 | |
clemente | 6:07f92b4716b6 | 48 | |
clemente | 6:07f92b4716b6 | 49 | |
clemente | 0:5d92b48edaff | 50 | } |
clemente | 2:0b726b8c8ab9 | 51 | |
clemente | 2:0b726b8c8ab9 | 52 | void dataready( void) |
clemente | 2:0b726b8c8ab9 | 53 | { |
clemente | 2:0b726b8c8ab9 | 54 | wigo_sensor1.getAllData( &sensor_data[0]); |
clemente | 2:0b726b8c8ab9 | 55 | pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]); |
clemente | 2:0b726b8c8ab9 | 56 | } |
clemente | 3:e2a621ea6976 | 57 | |
clemente | 3:e2a621ea6976 | 58 | float print_PressureValue( unsigned char *dt) |
clemente | 3:e2a621ea6976 | 59 | { |
clemente | 3:e2a621ea6976 | 60 | unsigned int prs; |
clemente | 3:e2a621ea6976 | 61 | float fprs; |
clemente | 3:e2a621ea6976 | 62 | |
clemente | 3:e2a621ea6976 | 63 | /* |
clemente | 3:e2a621ea6976 | 64 | * dt[0] = Bits 12-19 of 20-bit real-time Pressure sample. (b7-b0) |
clemente | 3:e2a621ea6976 | 65 | * dt[1] = Bits 4-11 of 20-bit real-time Pressure sample. (b7-b0) |
clemente | 3:e2a621ea6976 | 66 | * dt[2] = Bits 0-3 of 20-bit real-time Pressure sample (b7-b4) |
clemente | 3:e2a621ea6976 | 67 | */ |
clemente | 3:e2a621ea6976 | 68 | prs = (dt[0]<<10) | (dt[1]<<2) | (dt[2]>>6); |
clemente | 3:e2a621ea6976 | 69 | // |
clemente | 3:e2a621ea6976 | 70 | fprs = (float)prs * 1.0f; |
clemente | 3:e2a621ea6976 | 71 | |
clemente | 3:e2a621ea6976 | 72 | if ( dt[2] & 0x20) |
clemente | 3:e2a621ea6976 | 73 | fprs += 0.25f; |
clemente | 3:e2a621ea6976 | 74 | if ( dt[2] & 0x10) |
clemente | 3:e2a621ea6976 | 75 | fprs += 0.5f; |
clemente | 3:e2a621ea6976 | 76 | |
clemente | 3:e2a621ea6976 | 77 | return fprs; |
clemente | 3:e2a621ea6976 | 78 | } |
clemente | 3:e2a621ea6976 | 79 | |
clemente | 3:e2a621ea6976 | 80 | float print_AltimiterValue( unsigned char *dt) |
clemente | 3:e2a621ea6976 | 81 | { |
clemente | 3:e2a621ea6976 | 82 | unsigned short altm; |
clemente | 3:e2a621ea6976 | 83 | float faltm; |
clemente | 3:e2a621ea6976 | 84 | |
clemente | 3:e2a621ea6976 | 85 | /* |
clemente | 3:e2a621ea6976 | 86 | * dt[0] = Bits 12-19 of 20-bit real-time Altitude sample. (b7-b0) |
clemente | 3:e2a621ea6976 | 87 | * dt[1] = Bits 4-11 of 20-bit real-time Altitude sample. (b7-b0) |
clemente | 3:e2a621ea6976 | 88 | * dt[2] = Bits 0-3 of 20-bit real-time Altitude sample (b7-b4) |
clemente | 3:e2a621ea6976 | 89 | */ |
clemente | 3:e2a621ea6976 | 90 | altm = (dt[0]<<8) | dt[1]; |
clemente | 3:e2a621ea6976 | 91 | // |
clemente | 3:e2a621ea6976 | 92 | if ( dt[0] > 0x7F) { |
clemente | 3:e2a621ea6976 | 93 | altm = ~altm + 1; |
clemente | 3:e2a621ea6976 | 94 | faltm = (float)altm * -1.0f; |
clemente | 3:e2a621ea6976 | 95 | } else { |
clemente | 3:e2a621ea6976 | 96 | faltm = (float)altm * 1.0f; |
clemente | 3:e2a621ea6976 | 97 | } |
clemente | 3:e2a621ea6976 | 98 | // |
clemente | 3:e2a621ea6976 | 99 | faltm = faltm+((float)(dt[2]>>4) * 0.0625f); |
clemente | 3:e2a621ea6976 | 100 | return faltm; |
clemente | 3:e2a621ea6976 | 101 | } |
clemente | 3:e2a621ea6976 | 102 | |
clemente | 3:e2a621ea6976 | 103 | float print_TemperatureValue( unsigned char *dt) |
clemente | 3:e2a621ea6976 | 104 | { |
clemente | 3:e2a621ea6976 | 105 | unsigned short temp; |
clemente | 3:e2a621ea6976 | 106 | float ftemp; |
clemente | 3:e2a621ea6976 | 107 | |
clemente | 3:e2a621ea6976 | 108 | /* |
clemente | 3:e2a621ea6976 | 109 | * dt[0] = Bits 4-11 of 16-bit real-time temperature sample. (b7-b0) |
clemente | 3:e2a621ea6976 | 110 | * dt[1] = Bits 0-3 of 16-bit real-time temperature sample. (b7-b4) |
clemente | 3:e2a621ea6976 | 111 | */ |
clemente | 3:e2a621ea6976 | 112 | temp = dt[0]; |
clemente | 3:e2a621ea6976 | 113 | // |
clemente | 3:e2a621ea6976 | 114 | if ( dt[0] > 0x7F) { |
clemente | 3:e2a621ea6976 | 115 | temp = ~temp + 1; |
clemente | 3:e2a621ea6976 | 116 | ftemp = (float)temp * -1.0f; |
clemente | 3:e2a621ea6976 | 117 | } else { |
clemente | 3:e2a621ea6976 | 118 | ftemp = (float)temp * 1.0f; |
clemente | 3:e2a621ea6976 | 119 | } |
clemente | 3:e2a621ea6976 | 120 | // |
clemente | 3:e2a621ea6976 | 121 | ftemp = ftemp+((float)(dt[1]>>4) * 0.0625f); |
clemente | 3:e2a621ea6976 | 122 | return ftemp; |
clemente | 3:e2a621ea6976 | 123 | |
clemente | 3:e2a621ea6976 | 124 | } |
clemente | 3:e2a621ea6976 | 125 |