mini glider FSG
Fork of MS5837 by
MS5837.cpp@1:a8eea2a2263f, 2018-08-13 (annotated)
- Committer:
- CodyMarquardt
- Date:
- Mon Aug 13 18:48:45 2018 +0000
- Revision:
- 1:a8eea2a2263f
- Parent:
- 0:5f6034409fd0
copy mini glider programs to FSG group
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
potless | 0:5f6034409fd0 | 1 | #include <stdlib.h> |
potless | 0:5f6034409fd0 | 2 | #include "MS5837.h" |
potless | 0:5f6034409fd0 | 3 | |
potless | 0:5f6034409fd0 | 4 | |
potless | 0:5f6034409fd0 | 5 | /* |
potless | 0:5f6034409fd0 | 6 | * Sensor operating function according data sheet |
potless | 0:5f6034409fd0 | 7 | */ |
potless | 0:5f6034409fd0 | 8 | |
potless | 0:5f6034409fd0 | 9 | void MS5837::MS5837Init(void) |
potless | 0:5f6034409fd0 | 10 | { |
potless | 0:5f6034409fd0 | 11 | MS5837Reset(); |
potless | 0:5f6034409fd0 | 12 | MS5837ReadProm(); |
potless | 0:5f6034409fd0 | 13 | return; |
potless | 0:5f6034409fd0 | 14 | } |
potless | 0:5f6034409fd0 | 15 | |
potless | 0:5f6034409fd0 | 16 | /* Send soft reset to the sensor */ |
potless | 0:5f6034409fd0 | 17 | void MS5837::MS5837Reset(void) |
potless | 0:5f6034409fd0 | 18 | { |
potless | 0:5f6034409fd0 | 19 | /* transmit out 1 byte reset command */ |
potless | 0:5f6034409fd0 | 20 | ms5837_tx_data[0] = ms5837_reset; |
potless | 0:5f6034409fd0 | 21 | if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); |
CodyMarquardt | 1:a8eea2a2263f | 22 | //printf("send soft reset\n\r"); |
potless | 0:5f6034409fd0 | 23 | wait_ms(20); |
potless | 0:5f6034409fd0 | 24 | } |
potless | 0:5f6034409fd0 | 25 | |
potless | 0:5f6034409fd0 | 26 | /* read the sensor calibration data from rom */ |
potless | 0:5f6034409fd0 | 27 | void MS5837::MS5837ReadProm(void) |
potless | 0:5f6034409fd0 | 28 | { |
potless | 0:5f6034409fd0 | 29 | uint8_t i,j; |
potless | 0:5f6034409fd0 | 30 | for (i=0; i<8; i++) { |
potless | 0:5f6034409fd0 | 31 | j = i; |
potless | 0:5f6034409fd0 | 32 | ms5837_tx_data[0] = ms5837_PROMread + (j<<1); |
potless | 0:5f6034409fd0 | 33 | if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); |
potless | 0:5f6034409fd0 | 34 | if ( i2c.read( device_address, ms5837_rx_data, 2 ) ); |
potless | 0:5f6034409fd0 | 35 | C[i] = ms5837_rx_data[1] + (ms5837_rx_data[0]<<8); |
potless | 0:5f6034409fd0 | 36 | } |
potless | 0:5f6034409fd0 | 37 | } |
potless | 0:5f6034409fd0 | 38 | |
potless | 0:5f6034409fd0 | 39 | /* Start the sensor pressure conversion */ |
potless | 0:5f6034409fd0 | 40 | void MS5837::MS5837ConvertD1(void) |
potless | 0:5f6034409fd0 | 41 | { |
potless | 0:5f6034409fd0 | 42 | ms5837_tx_data[0] = ms5837_convD1; |
potless | 0:5f6034409fd0 | 43 | if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); |
potless | 0:5f6034409fd0 | 44 | } |
potless | 0:5f6034409fd0 | 45 | |
potless | 0:5f6034409fd0 | 46 | /* Start the sensor temperature conversion */ |
potless | 0:5f6034409fd0 | 47 | void MS5837:: MS5837ConvertD2(void) |
potless | 0:5f6034409fd0 | 48 | { |
potless | 0:5f6034409fd0 | 49 | ms5837_tx_data[0] = ms5837_convD2; |
potless | 0:5f6034409fd0 | 50 | if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); |
potless | 0:5f6034409fd0 | 51 | } |
potless | 0:5f6034409fd0 | 52 | |
potless | 0:5f6034409fd0 | 53 | /* Read the previous started conversion results */ |
potless | 0:5f6034409fd0 | 54 | int32_t MS5837::MS5837ReadADC(void) |
potless | 0:5f6034409fd0 | 55 | { |
potless | 0:5f6034409fd0 | 56 | int32_t adc; |
potless | 0:5f6034409fd0 | 57 | wait_ms(150); |
potless | 0:5f6034409fd0 | 58 | ms5837_tx_data[0] = ms5837_ADCread; |
potless | 0:5f6034409fd0 | 59 | if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); |
potless | 0:5f6034409fd0 | 60 | if ( i2c.read( device_address, ms5837_rx_data, 3 ) ); |
potless | 0:5f6034409fd0 | 61 | adc = ms5837_rx_data[2] + (ms5837_rx_data[1]<<8) + (ms5837_rx_data[0]<<16); |
potless | 0:5f6034409fd0 | 62 | //printf("ADC value: %x\n", adc); |
potless | 0:5f6034409fd0 | 63 | return (adc); |
potless | 0:5f6034409fd0 | 64 | } |
potless | 0:5f6034409fd0 | 65 | |
potless | 0:5f6034409fd0 | 66 | /* return the results */ |
potless | 0:5f6034409fd0 | 67 | float MS5837::MS5837_Pressure (void) |
potless | 0:5f6034409fd0 | 68 | { |
potless | 0:5f6034409fd0 | 69 | return P_MS5837; |
potless | 0:5f6034409fd0 | 70 | } |
potless | 0:5f6034409fd0 | 71 | float MS5837::MS5837_Temperature (void) |
potless | 0:5f6034409fd0 | 72 | { |
potless | 0:5f6034409fd0 | 73 | return T_MS5837; |
potless | 0:5f6034409fd0 | 74 | } |
potless | 0:5f6034409fd0 | 75 | |
potless | 0:5f6034409fd0 | 76 | /* Sensor reading and calculation procedure */ |
potless | 0:5f6034409fd0 | 77 | void MS5837::Barometer_MS5837(void) |
potless | 0:5f6034409fd0 | 78 | { |
potless | 0:5f6034409fd0 | 79 | int32_t dT, temp; |
potless | 0:5f6034409fd0 | 80 | int64_t OFF, SENS, press; |
potless | 0:5f6034409fd0 | 81 | |
potless | 0:5f6034409fd0 | 82 | //no need to do this everytime! |
potless | 0:5f6034409fd0 | 83 | |
potless | 0:5f6034409fd0 | 84 | |
potless | 0:5f6034409fd0 | 85 | MS5837ConvertD1(); // start pressure conversion |
potless | 0:5f6034409fd0 | 86 | D1 = MS5837ReadADC(); // read the pressure value |
potless | 0:5f6034409fd0 | 87 | MS5837ConvertD2(); // start temperature conversion |
potless | 0:5f6034409fd0 | 88 | D2 = MS5837ReadADC(); // read the temperature value |
potless | 0:5f6034409fd0 | 89 | //printf("D1 = %d\n", D1); |
potless | 0:5f6034409fd0 | 90 | /* calculation according MS5837-01BA data sheet DA5837-01BA_006 */ |
potless | 0:5f6034409fd0 | 91 | dT = D2 - (C[5]* 256); |
potless | 0:5f6034409fd0 | 92 | OFF = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7); |
potless | 0:5f6034409fd0 | 93 | SENS = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8); |
potless | 0:5f6034409fd0 | 94 | |
potless | 0:5f6034409fd0 | 95 | temp = 2000 + (dT * C[6]) / (1<<23); |
potless | 0:5f6034409fd0 | 96 | T_MS5837 = (float) temp / 100.0f; // result of temperature in deg C in this var |
potless | 0:5f6034409fd0 | 97 | press = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<13); |
potless | 0:5f6034409fd0 | 98 | P_MS5837 = (float) press / 10.0f; // result of pressure in mBar in this var |
potless | 0:5f6034409fd0 | 99 | |
potless | 0:5f6034409fd0 | 100 | if (P_MS5837 < 900 || P_MS5837 > 3000) { |
potless | 0:5f6034409fd0 | 101 | MS5837Reset(); // reset the sensor |
potless | 0:5f6034409fd0 | 102 | MS5837ReadProm(); // read the calibration values |
potless | 0:5f6034409fd0 | 103 | } |
potless | 0:5f6034409fd0 | 104 | } |
potless | 0:5f6034409fd0 | 105 | |
CodyMarquardt | 1:a8eea2a2263f | 106 | float MS5837::get_depth_initial(void){ |
CodyMarquardt | 1:a8eea2a2263f | 107 | depth_iter = 0; |
CodyMarquardt | 1:a8eea2a2263f | 108 | Depth_0 = 0; |
CodyMarquardt | 1:a8eea2a2263f | 109 | for(int i = 0; i < 5; i++){ |
CodyMarquardt | 1:a8eea2a2263f | 110 | Barometer_MS5837(); |
CodyMarquardt | 1:a8eea2a2263f | 111 | depth_iter = ((P_MS5837)*1.019716); |
CodyMarquardt | 1:a8eea2a2263f | 112 | Depth_0 = depth_iter + Depth_0; |
CodyMarquardt | 1:a8eea2a2263f | 113 | } |
CodyMarquardt | 1:a8eea2a2263f | 114 | Depth_0 = Depth_0/5; |
CodyMarquardt | 1:a8eea2a2263f | 115 | if(Depth_0 < 1000 || Depth_0 > 1100){ |
CodyMarquardt | 1:a8eea2a2263f | 116 | MS5837Init(); |
CodyMarquardt | 1:a8eea2a2263f | 117 | get_depth_initial(); |
CodyMarquardt | 1:a8eea2a2263f | 118 | } |
CodyMarquardt | 1:a8eea2a2263f | 119 | return Depth_0; |
CodyMarquardt | 1:a8eea2a2263f | 120 | } |
CodyMarquardt | 1:a8eea2a2263f | 121 | |
CodyMarquardt | 1:a8eea2a2263f | 122 | float MS5837::get_depth(void) { |
CodyMarquardt | 1:a8eea2a2263f | 123 | depth_iter = 0; |
CodyMarquardt | 1:a8eea2a2263f | 124 | depth = 0; |
CodyMarquardt | 1:a8eea2a2263f | 125 | for(int i = 0; i <= 2; i++){ |
CodyMarquardt | 1:a8eea2a2263f | 126 | Barometer_MS5837(); |
CodyMarquardt | 1:a8eea2a2263f | 127 | depth_iter = ((P_MS5837)*1.019716)-Depth_0; |
CodyMarquardt | 1:a8eea2a2263f | 128 | depth = depth_iter + depth; |
CodyMarquardt | 1:a8eea2a2263f | 129 | } |
CodyMarquardt | 1:a8eea2a2263f | 130 | depth = depth/3; |
CodyMarquardt | 1:a8eea2a2263f | 131 | return depth; |
potless | 0:5f6034409fd0 | 132 | } |