Test for MS5803 and MS5837 pressure sensors (pressure and temp readings) and QEI testing for encoder.

Dependencies:   MS5803 QEI mbed

Committer:
alex93
Date:
Sun Mar 20 03:05:25 2016 +0000
Revision:
0:f8bc804eadbc
Test for MS5803 and MS5837 pressure sensors (pressure and temp readings) and QEI testing for encoder.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alex93 0:f8bc804eadbc 1 #include <stdlib.h>
alex93 0:f8bc804eadbc 2 #include "MS5837.h"
alex93 0:f8bc804eadbc 3
alex93 0:f8bc804eadbc 4
alex93 0:f8bc804eadbc 5 /*
alex93 0:f8bc804eadbc 6 * Sensor operating function according data sheet
alex93 0:f8bc804eadbc 7 */
alex93 0:f8bc804eadbc 8
alex93 0:f8bc804eadbc 9 void MS5837::MS5837Init(void)
alex93 0:f8bc804eadbc 10 {
alex93 0:f8bc804eadbc 11 MS5837Reset();
alex93 0:f8bc804eadbc 12 MS5837ReadProm();
alex93 0:f8bc804eadbc 13 return;
alex93 0:f8bc804eadbc 14 }
alex93 0:f8bc804eadbc 15
alex93 0:f8bc804eadbc 16 /* Send soft reset to the sensor */
alex93 0:f8bc804eadbc 17 void MS5837::MS5837Reset(void)
alex93 0:f8bc804eadbc 18 {
alex93 0:f8bc804eadbc 19 /* transmit out 1 byte reset command */
alex93 0:f8bc804eadbc 20 ms5837_tx_data[0] = ms5837_reset;
alex93 0:f8bc804eadbc 21 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
alex93 0:f8bc804eadbc 22 //printf("send soft reset");
alex93 0:f8bc804eadbc 23 wait_ms(20);
alex93 0:f8bc804eadbc 24 }
alex93 0:f8bc804eadbc 25
alex93 0:f8bc804eadbc 26 /* read the sensor calibration data from rom */
alex93 0:f8bc804eadbc 27 void MS5837::MS5837ReadProm(void)
alex93 0:f8bc804eadbc 28 {
alex93 0:f8bc804eadbc 29 uint8_t i,j;
alex93 0:f8bc804eadbc 30 for (i=0; i<8; i++) {
alex93 0:f8bc804eadbc 31 j = i;
alex93 0:f8bc804eadbc 32 ms5837_tx_data[0] = ms5837_PROMread + (j<<1);
alex93 0:f8bc804eadbc 33 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
alex93 0:f8bc804eadbc 34 if ( i2c.read( device_address, ms5837_rx_data, 2 ) );
alex93 0:f8bc804eadbc 35 C[i] = ms5837_rx_data[1] + (ms5837_rx_data[0]<<8);
alex93 0:f8bc804eadbc 36 }
alex93 0:f8bc804eadbc 37 }
alex93 0:f8bc804eadbc 38
alex93 0:f8bc804eadbc 39 /* Start the sensor pressure conversion */
alex93 0:f8bc804eadbc 40 void MS5837::MS5837ConvertD1(void)
alex93 0:f8bc804eadbc 41 {
alex93 0:f8bc804eadbc 42 ms5837_tx_data[0] = ms5837_convD1;
alex93 0:f8bc804eadbc 43 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
alex93 0:f8bc804eadbc 44 }
alex93 0:f8bc804eadbc 45
alex93 0:f8bc804eadbc 46 /* Start the sensor temperature conversion */
alex93 0:f8bc804eadbc 47 void MS5837:: MS5837ConvertD2(void)
alex93 0:f8bc804eadbc 48 {
alex93 0:f8bc804eadbc 49 ms5837_tx_data[0] = ms5837_convD2;
alex93 0:f8bc804eadbc 50 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
alex93 0:f8bc804eadbc 51 }
alex93 0:f8bc804eadbc 52
alex93 0:f8bc804eadbc 53 /* Read the previous started conversion results */
alex93 0:f8bc804eadbc 54 int32_t MS5837::MS5837ReadADC(void)
alex93 0:f8bc804eadbc 55 {
alex93 0:f8bc804eadbc 56 int32_t adc;
alex93 0:f8bc804eadbc 57 wait_ms(150);
alex93 0:f8bc804eadbc 58 ms5837_tx_data[0] = ms5837_ADCread;
alex93 0:f8bc804eadbc 59 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
alex93 0:f8bc804eadbc 60 if ( i2c.read( device_address, ms5837_rx_data, 3 ) );
alex93 0:f8bc804eadbc 61 adc = ms5837_rx_data[2] + (ms5837_rx_data[1]<<8) + (ms5837_rx_data[0]<<16);
alex93 0:f8bc804eadbc 62 return (adc);
alex93 0:f8bc804eadbc 63 }
alex93 0:f8bc804eadbc 64
alex93 0:f8bc804eadbc 65 /* return the results */
alex93 0:f8bc804eadbc 66 float MS5837::MS5837_Pressure (void)
alex93 0:f8bc804eadbc 67 {
alex93 0:f8bc804eadbc 68 return P_MS5837;
alex93 0:f8bc804eadbc 69 }
alex93 0:f8bc804eadbc 70 float MS5837::MS5837_Temperature (void)
alex93 0:f8bc804eadbc 71 {
alex93 0:f8bc804eadbc 72 return T_MS5837;
alex93 0:f8bc804eadbc 73 }
alex93 0:f8bc804eadbc 74
alex93 0:f8bc804eadbc 75 /* Sensor reading and calculation procedure */
alex93 0:f8bc804eadbc 76 void MS5837::Barometer_MS5837(void)
alex93 0:f8bc804eadbc 77 {
alex93 0:f8bc804eadbc 78 int32_t dT, temp;
alex93 0:f8bc804eadbc 79 int64_t OFF, SENS, press;
alex93 0:f8bc804eadbc 80
alex93 0:f8bc804eadbc 81 //no need to do this everytime!
alex93 0:f8bc804eadbc 82 //MS5837Reset(); // reset the sensor
alex93 0:f8bc804eadbc 83 //MS5837ReadProm(); // read the calibration values
alex93 0:f8bc804eadbc 84
alex93 0:f8bc804eadbc 85
alex93 0:f8bc804eadbc 86 MS5837ConvertD1(); // start pressure conversion
alex93 0:f8bc804eadbc 87 D1 = MS5837ReadADC(); // read the pressure value
alex93 0:f8bc804eadbc 88 MS5837ConvertD2(); // start temperature conversion
alex93 0:f8bc804eadbc 89 D2 = MS5837ReadADC(); // read the temperature value
alex93 0:f8bc804eadbc 90
alex93 0:f8bc804eadbc 91 /* calculation according MS5837-01BA data sheet DA5837-01BA_006 */
alex93 0:f8bc804eadbc 92 dT = D2 - (C[5]* 256);
alex93 0:f8bc804eadbc 93 OFF = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7);
alex93 0:f8bc804eadbc 94 SENS = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8);
alex93 0:f8bc804eadbc 95
alex93 0:f8bc804eadbc 96 temp = 2000 + (dT * C[6]) / (1<<23);
alex93 0:f8bc804eadbc 97 T_MS5837 = (float) temp / 100.0f; // result of temperature in deg C in this var
alex93 0:f8bc804eadbc 98 press = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<13);
alex93 0:f8bc804eadbc 99 P_MS5837 = (float) press / 10.0f; // result of pressure in mBar in this var
alex93 0:f8bc804eadbc 100 }