Demo program for the MPL3115A2 library.
Example program for MPL3115 sensor. Added code for data acquisition using Interrupt.
main.cpp
- Committer:
- clemente
- Date:
- 2013-09-24
- Revision:
- 7:97f5edf7d270
- Parent:
- 6:07f92b4716b6
File content as of revision 7:97f5edf7d270:
#include "mbed.h" #include "MPL3115A2.h" #define MPL3115A2_I2C_ADDRESS (0x60<<1) DigitalOut myled(LED1); MPL3115A2 wigo_sensor1( PTE0, PTE1, MPL3115A2_I2C_ADDRESS); Serial pc(USBTX, USBRX); /* pos [0] = altimeter or pressure value */ /* pos [1] = temperature value */ float sensor_data[2]; void dataready( void); // callback function for data streaming using Interrupt void alttrigger( void); /* Helper functions to print out as float the raw data */ float print_PressureValue( unsigned char *dt); float print_AltimiterValue( unsigned char *dt); float print_TemperatureValue( unsigned char *dt); int main() { pc.baud( 230400); pc.printf("MPL3115A2 Sensor. [%X]\r\n", wigo_sensor1.getDeviceID()); #if 0 // ***** Data acquisition using Interrupt // Configure the sensor as Barometer. wigo_sensor1.Barometric_Mode(); // Set callback function and over sampling value (see MPL3115A2.h for details) wigo_sensor1.DataReady( &dataready, OVERSAMPLE_RATIO_64); // just loop... while( 1) { wait( 1.0); pc.printf("."); } #endif #if 0 // ***** Data acquisition using polling method // Configure the sensor as Barometer. unsigned int mode=1; // Set over sampling value (see MPL3115A2.h for details) wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_64); // Configure the sensor as Barometer. wigo_sensor1.Barometric_Mode(); while(1) { // if ( wigo_sensor1.getAllData( &sensor_data[0])) { if ( mode & 0x0001) { pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]); wigo_sensor1.Altimeter_Mode(); } else { pc.printf("\tAltitude: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]); wigo_sensor1.Barometric_Mode(); } mode++; } // wait( 1.0); } #endif #if 0 // ***** Data acquisition using polling method with Max and Min values visualization // Configure the sensor as Barometer. unsigned int mode=1; // Set over sampling value (see MPL3115A2.h for details) wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_64); // Configure the sensor as Barometer. wigo_sensor1.Barometric_Mode(); while(1) { // if ( wigo_sensor1.getAllData( &sensor_data[0])) { pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]); if ( (mode % 20)==0) { sensor_data[0] = sensor_data[1] = 0.0; wigo_sensor1.getAllMaximumData( &sensor_data[0]); pc.printf("\tMaxPress: %f\tMaxTemp: %f\r\n", sensor_data[0], sensor_data[1]); sensor_data[0] = sensor_data[1] = 0.0; wigo_sensor1.getAllMinimumData( &sensor_data[0]); pc.printf("\tMinPress: %f\tMinTemp: %f\r\n", sensor_data[0], sensor_data[1]); } mode++; } // wait( 1.0); } #endif #if 1 // ***** Data acquisition using polling method and delta values // Array for the dela values float delta[2]; // Set over sampling value (see MPL3115A2.h for details) wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_128); // Configure the sensor as Barometer. wigo_sensor1.Barometric_Mode(); while(1) { // if ( wigo_sensor1.getAllData( &sensor_data[0], &delta[0])) { pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]); pc.printf("\tDelatPress: %f\tDeltaTemp: %f\r\n", delta[0], delta[1]); } // wait( 1.0); } #endif #if 0 // ***** Data acquisition in RAW mode using polling method // Create a buffer for raw data unsigned char raw_data[5]; // Configure the sensor as Barometer. unsigned int mode=1; // Set over sampling value (see MPL3115A2.h for details) wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_64); // Configure the sensor as Barometer. wigo_sensor1.Barometric_Mode(); while(1) { // if ( wigo_sensor1.getAllDataRaw( &raw_data[0])) { if ( mode & 0x0001) { pc.printf("\tPressure: %f\tTemperature: %f\r\n", print_PressureValue( &raw_data[0]), print_TemperatureValue( &raw_data[3])); wigo_sensor1.Altimeter_Mode(); } else { pc.printf("\tAltitude: %f\tTemperature: %f\r\n", print_AltimiterValue( &raw_data[0]), print_TemperatureValue( &raw_data[3])); wigo_sensor1.Barometric_Mode(); } mode++; } // wait( 1.0); } #endif } void dataready( void) { wigo_sensor1.getAllData( &sensor_data[0]); pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]); } float print_PressureValue( unsigned char *dt) { unsigned int prs; float fprs; /* * dt[0] = Bits 12-19 of 20-bit real-time Pressure sample. (b7-b0) * dt[1] = Bits 4-11 of 20-bit real-time Pressure sample. (b7-b0) * dt[2] = Bits 0-3 of 20-bit real-time Pressure sample (b7-b4) */ prs = (dt[0]<<10) | (dt[1]<<2) | (dt[2]>>6); // fprs = (float)prs * 1.0f; if ( dt[2] & 0x20) fprs += 0.25f; if ( dt[2] & 0x10) fprs += 0.5f; return fprs; } float print_AltimiterValue( unsigned char *dt) { unsigned short altm; float faltm; /* * dt[0] = Bits 12-19 of 20-bit real-time Altitude sample. (b7-b0) * dt[1] = Bits 4-11 of 20-bit real-time Altitude sample. (b7-b0) * dt[2] = Bits 0-3 of 20-bit real-time Altitude sample (b7-b4) */ altm = (dt[0]<<8) | dt[1]; // if ( dt[0] > 0x7F) { altm = ~altm + 1; faltm = (float)altm * -1.0f; } else { faltm = (float)altm * 1.0f; } // faltm = faltm+((float)(dt[2]>>4) * 0.0625f); return faltm; } float print_TemperatureValue( unsigned char *dt) { unsigned short temp; float ftemp; /* * dt[0] = Bits 4-11 of 16-bit real-time temperature sample. (b7-b0) * dt[1] = Bits 0-3 of 16-bit real-time temperature sample. (b7-b4) */ temp = dt[0]; // if ( dt[0] > 0x7F) { temp = ~temp + 1; ftemp = (float)temp * -1.0f; } else { ftemp = (float)temp * 1.0f; } // ftemp = ftemp+((float)(dt[1]>>4) * 0.0625f); return ftemp; }