All mbed code for control over dive planes, pump motor, valve motor, BCUs, UART interface, etc.

Dependencies:   mbed ESC mbed MODDMA

Committer:
juansal12
Date:
Tue Jan 14 19:17:05 2020 +0000
Revision:
0:c3a329a5b05d
Sofi7 mbed code;

Who changed what in which revision?

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