first publish

Dependencies:   HMC5883L mbed

Committer:
roger_wee
Date:
Sun Jun 04 06:58:45 2017 +0000
Revision:
3:394c971eab83
Parent:
2:359f1f075c72
9-dof implementation using madgwick's filter

Who changed what in which revision?

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