annotate Small_CPU/Src/pressure.c @ 186:f11f0bf6ef2d cleanup-2

cleanup: remove obsolete code, make static, etc. Some rather trivial cleanup things like putting demo code into ifdefs, making functions static where possible, and against my normal policy of hard removing unused code, commenting it out at this point in time. Somehow, I think that this commented code might be useful in the near future as a new pressure sensor is coming. And finally, fixed some typo's in comment. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Fri, 15 Mar 2019 12:39:28 +0100
parents 5f11787b4f42
children 2b9775f71e30
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 * @file pressure.c
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4 * @author heinrichs weikamp gmbh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 * @date 2014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 * @version V0.0.2
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 * @since 20-Oct-2016
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 * @brief
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 ##### How to use #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 V0.0.2 18-Oct-2016 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 * @attention
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 * <h2><center>&copy; COPYRIGHT(c) 2016 heinrichs weikamp</center></h2>
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 /* surface time
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 the last 30 minutes will be saved once per minute in a endless loop
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 at the beginning of a dive the oldest value will be used
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 #include "pressure.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 #include "i2c.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 #include "rtc.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 #define CMD_RESET 0x1E // ADC reset command
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 #define CMD_ADC_READ 0x00 // ADC read command
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 #define CMD_ADC_CONV 0x40 // ADC conversion command
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 #define CMD_ADC_D1 0x00 // ADC D1 conversion
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 #define CMD_ADC_D2 0x10 // ADC D2 conversion
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 #define CMD_ADC_256 0x00 // ADC OSR=256
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 #define CMD_ADC_512 0x02 // ADC OSR=512
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 #define CMD_ADC_1024 0x04 // ADC OSR=1024
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 #define CMD_ADC_2048 0x06 // ADC OSR=2056
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 #define CMD_ADC_4096 0x08 // ADC OSR=4096
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 #define CMD_PROM_RD 0xA0 // Prom read command
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
49 static uint16_t get_ci_by_coef_num(uint8_t coef_num);
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
50 //void pressure_calculation_new(void);
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
51 //void pressure_calculation_old(void);
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
52 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void);
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
53 static uint8_t crc4(uint16_t n_prom[]);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
55 static HAL_StatusTypeDef pressure_sensor_get_data(void);
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
56 static uint32_t get_adc(void);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 uint8_t pressureSensorInitSuccess = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
59 static uint16_t C[8] = { 1 };
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
60 static uint32_t D1 = 1;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
61 static uint32_t D2 = 1;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
62 static uint8_t n_crc;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
64 static int64_t C5_x_2p8 = 1;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
65 static int64_t C2_x_2p16 = 1;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
66 static int64_t C1_x_2p15 = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 short C2plus10000 = -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 short C3plus200 = -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 short C4minus250 = -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 short UT1 = -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 short C6plus100 = -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
76 static float ambient_temperature = 0;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
77 static float ambient_pressure_mbar = 0;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
78 static float surface_pressure_mbar = 1000;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
79 static float surface_ring_mbar[31] = { 0 };
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 uint8_t secondCounterSurfaceRing = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 float get_temperature(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 return ambient_temperature;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 float get_pressure_mbar(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 return ambient_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 float get_surface_mbar(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 return surface_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 void init_surface_ring(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 surface_ring_mbar[0] = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 for(int i=1; i<31; i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 surface_ring_mbar[i] = ambient_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 surface_pressure_mbar = ambient_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 /* the ring has one place with 0
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 * after that comes the oldest value
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 * the new pressure is written in this hole
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 * the oldest value is read and then the new hole
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 void update_surface_pressure(uint8_t call_rhythm_seconds)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 secondCounterSurfaceRing += call_rhythm_seconds;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 if(secondCounterSurfaceRing < 60)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 secondCounterSurfaceRing = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 int hole;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 for(hole=30;hole>0;hole--)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 if(surface_ring_mbar[hole] == 0) { break; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 surface_ring_mbar[hole] = ambient_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 hole++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 if(hole > 30)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 hole = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 surface_pressure_mbar = surface_ring_mbar[hole];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 surface_ring_mbar[hole] = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
135 #ifdef DEMOMODE
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 float demo_modify_temperature_helper(float bottom_mbar_diff_to_surface)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138 const float temperature_surface = 31.0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 const float temperature_bottom = 14.0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 const float temperature_difference = temperature_bottom - temperature_surface;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143 // range 0.0 - 1.0
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 float position_now = (ambient_pressure_mbar - surface_pressure_mbar) / bottom_mbar_diff_to_surface;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 if(position_now <= 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147 return temperature_surface;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 if(position_now >= 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 return temperature_bottom;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 return temperature_surface + (temperature_difference * position_now);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 uint32_t demo_modify_temperature_and_pressure(int32_t divetime_in_seconds, uint8_t subseconds, float ceiling_mbar)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159 const float descent_rate = 4000/60;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 const float ascent_rate = 1000/60;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161 const uint32_t seconds_descend = (1 * 60) + 30;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
162 const uint32_t turbo_seconds_at_bottom_start = (0 * 60) + 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163 const uint32_t seconds_descend_and_bottomtime = seconds_descend + turbo_seconds_at_bottom_start + (2 * 60) + 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164 uint32_t time_elapsed_in_seconds;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165 static float ambient_pressure_mbar_memory = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
166 static uint32_t time_last_call = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
167
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
168 if(divetime_in_seconds <= seconds_descend)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
169 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
170 ambient_pressure_mbar = (divetime_in_seconds * descent_rate) + ((float)(subseconds) * descent_rate) + surface_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
171 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
172
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
173 time_last_call = divetime_in_seconds;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
174 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
175 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
176 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
177 if(divetime_in_seconds <= seconds_descend + turbo_seconds_at_bottom_start)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
178 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
179 ambient_pressure_mbar = (seconds_descend * descent_rate) + surface_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
180 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
181 ambient_pressure_mbar_memory = ambient_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
182 time_last_call = divetime_in_seconds;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
183 return turbo_seconds_at_bottom_start;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
184 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
185 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
186 if(divetime_in_seconds <= seconds_descend_and_bottomtime)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
187 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
188 ambient_pressure_mbar = (seconds_descend * descent_rate) + surface_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
189 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
190 ambient_pressure_mbar_memory = ambient_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
191 time_last_call = divetime_in_seconds;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
192 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
193 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
194 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
195 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
196 time_elapsed_in_seconds = divetime_in_seconds - time_last_call;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
197 ambient_pressure_mbar = ambient_pressure_mbar_memory - time_elapsed_in_seconds * ascent_rate;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
198
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
199 if(ambient_pressure_mbar < surface_pressure_mbar)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
200 ambient_pressure_mbar = surface_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
201 else if(ambient_pressure_mbar < ceiling_mbar)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
202 ambient_pressure_mbar = ceiling_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
203
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
204 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
205 ambient_pressure_mbar_memory = ambient_pressure_mbar;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
206 time_last_call = divetime_in_seconds;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
207 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
208 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
209 }
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
210 #endif
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
211
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
212
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
213 /* called just once on power on */
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
214 /* TBD old DR5 code? */
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
215 void init_pressure_DRx(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
216 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
217 uint8_t resetCommand[1] = {0x1E};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
218
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
219 I2C_Master_Transmit( DEVICE_PRESSURE, resetCommand, 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
220 HAL_Delay(3);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
221
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
222 C[1] = get_ci_by_coef_num(0x02);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
223 C[2] = get_ci_by_coef_num(0x04);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
224 C[3] = get_ci_by_coef_num(0x06);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
225 C[4] = get_ci_by_coef_num(0x08);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
226 C[5] = get_ci_by_coef_num(0x0A);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
227 C[6] = get_ci_by_coef_num(0x0C);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
228
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
229 C5_x_2p8 = C[5] * 256;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
230 C2_x_2p16 = C[2] * 65536;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
231 C1_x_2p15 = C[1] * 32768;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
232 pressure_update();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
233 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
234
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
235 uint8_t is_init_pressure_done(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
236 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
237 return pressureSensorInitSuccess;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
238 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
239
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
240 uint8_t init_pressure(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
241 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
242 uint8_t buffer[1];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
243 buffer[0] = 0x1e;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
244 uint8_t retValue = 0xFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
245
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
246
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
247 retValue = I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
248 if(retValue != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
249 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
250 return (HAL_StatusTypeDef)retValue;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
251 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
252 HAL_Delay(3);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
253
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
254 for(uint8_t i=0;i<8;i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
255 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
256 C[i] = get_ci_by_coef_num(i);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
257 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
258 n_crc = crc4(C); // no evaluation at the moment hw 151026
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
259
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
260 C5_x_2p8 = C[5] * 256;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
261 C2_x_2p16 = C[2] * 65536;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
262 C1_x_2p15 = C[1] * 32768;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
263
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
264 if(I2C1_Status() == HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
265 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
266 pressureSensorInitSuccess = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
267 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
268 return pressure_update();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
269 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
270
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
271
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
272 static uint32_t get_adc(void)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
273 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
274 uint8_t buffer[1];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
275 uint8_t resivebuf[4];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
276 uint32_t answer = 0;
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
277
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
278 buffer[0] = 0x00; // Get ADC
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
279 I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
280 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
281 resivebuf[3] = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
282 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
283
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
284 return answer;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
285 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
286
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
287
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
288 static uint16_t get_ci_by_coef_num(uint8_t coef_num)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
289 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
290 uint8_t resivebuf[2];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
291
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
292 uint8_t cmd = CMD_PROM_RD+coef_num*2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
293 I2C_Master_Transmit( DEVICE_PRESSURE, &cmd, 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
294 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
295 return (256*(uint16_t)resivebuf[0]) + (uint16_t)resivebuf[1];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
296 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
297
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
298
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
299
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
300 uint8_t pressure_update(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
301 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
302 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
303
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
304 statusReturn = pressure_sensor_get_data();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
305 pressure_calculation();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
306 return (uint8_t)statusReturn;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
307 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
308
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
309
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
310 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
311 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
312 uint8_t command = CMD_ADC_CONV + cmd;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
313 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
314
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
315 statusReturnTemp = I2C_Master_Transmit( DEVICE_PRESSURE, &command, 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
316
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
317 if(statusReturn)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
318 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
319 *statusReturn = statusReturnTemp;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
320 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
321
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
322 switch (cmd & 0x0f) // wait necessary conversion time
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
323 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
324 case CMD_ADC_256 : HAL_Delay(1); break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
325 case CMD_ADC_512 : HAL_Delay(3); break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
326 case CMD_ADC_1024: HAL_Delay(4); break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
327 case CMD_ADC_2048: HAL_Delay(6); break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
328 case CMD_ADC_4096: HAL_Delay(10); break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
329 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
330 return get_adc();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
331 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
332
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
333
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
334 static HAL_StatusTypeDef pressure_sensor_get_data(void)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
335 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
336 HAL_StatusTypeDef statusReturn1 = HAL_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
337 HAL_StatusTypeDef statusReturn2 = HAL_TIMEOUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
338
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
339 D2 = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_4096, &statusReturn1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
340 D1 = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_4096, &statusReturn2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
341
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
342 if(statusReturn2 > statusReturn1) // if anything is not HAL_OK (0x00) or worse
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
343 return statusReturn2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
344 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
345 return statusReturn1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
346 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
347
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
348
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
349 void pressure_sensor_get_pressure_raw(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
350 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
351 D1 = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_4096, 0);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
352 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
353
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
354
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
355 void pressure_sensor_get_temperature_raw(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
356 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
357 D2 = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_4096, 0);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
358 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
359
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
360
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
361 void pressure_calculation(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
362 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
363 if(I2C1_Status() != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
364 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
365
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
366 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
367 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
368
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
369 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
370 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
371 uint32_t local_D1; // ADC value of the pressure conversion
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
372 uint32_t local_D2; // ADC value of the temperature conversion
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
373 int32_t local_Px10; // compensated pressure value
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
374 int32_t local_Tx100; // compensated temperature value
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
375 int64_t local_dT; // int32_t, difference between actual and measured temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
376 int64_t local_OFF; // offset at actual temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
377 int64_t local_SENS; // sensitivity at actual temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
378
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
379 int64_t T2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
380 int64_t OFF2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
381 int64_t SENS2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
382
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
383 local_D1 = D1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
384 local_D2 = D2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
385
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
386 local_dT = ((int64_t)local_D2) - ((int64_t)C[5]) * 256; //pow(2,8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
387 local_OFF = ((int64_t)C[2]) * 65536 + local_dT * ((int64_t)C[4]) / 128; // pow(2,16), pow(2,7)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
388 local_SENS = ((int64_t)C[1]) * 32768 + local_dT * ((int64_t)C[3]) / 256; // pow(2,15), pow(2,8)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
389
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
390 local_Tx100 = (int32_t)(2000 + (local_dT * ((int64_t)C[6])) / 8388608);// pow(2,23)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
391
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
392
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
393 if(local_Tx100 < 2000) // low temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
394 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
395 T2 = 3 * local_dT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
396 T2 *= local_dT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
397 T2 /= 8589934592;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
398
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
399 OFF2 = ((int64_t)local_Tx100) - 2000;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
400 OFF2 *= OFF2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
401 OFF2 *= 3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
402 OFF2 /= 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
403
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
404 SENS2 = ((int64_t)local_Tx100) - 2000;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
405 SENS2 *= SENS2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
406 SENS2 *= 5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
407 SENS2 /= 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
408
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
409 local_Tx100 -= (int32_t)T2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
410 local_OFF -= OFF2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
411 local_SENS -= SENS2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
412 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
413 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
414 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
415 T2 = 7 * local_dT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
416 T2 *= local_dT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
417 T2 /= 137438953472;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
418
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
419 OFF2 = ((int64_t)local_Tx100) - 2000;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
420 OFF2 *= OFF2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
421 OFF2 /= 16;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
422
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
423 local_Tx100 -= (int32_t)T2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
424 local_OFF -= OFF2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
425 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
426
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
427 local_Px10 = (int32_t)(
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
428 (((int64_t)((local_D1 * local_SENS) / 2097152)) - local_OFF)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
429 / 8192 );// )) / 10; // pow(2,21), pow(2,13)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
430
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
431 ambient_temperature = ((float)local_Tx100) / 100;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
432 ambient_pressure_mbar = ((float)local_Px10) / 10;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
433 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
434
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
435
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
436 /*
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
437 void pressure_calculation_new(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
438 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
439 #define POW2_8 (256)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
440 #define POW2_17 (131072)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
441 #define POW2_6 (64)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
442 #define POW2_16 (65536)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
443 #define POW2_7 (128)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
444 #define POW2_23 (8388608)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
445 #define POW2_21 (2097152)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
446 #define POW2_15 (32768)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
447 #define POW2_13 (8192)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
448 #define POW2_37 (137438953472)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
449 #define POW2_4 (16)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
450 #define POW2_33 (8589934592)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
451 #define POW2_3 (8)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
452
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
453 int32_t P; // compensated pressure value
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
454 int32_t T; // compensated temperature value
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
455 int32_t dT; // difference between actual and measured temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
456 int64_t OFF; // offset at actual temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
457 int64_t SENS;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
458
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
459 int32_t T2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
460 int64_t OFF2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
461 int64_t SENS2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
462
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
463 dT = ((int32_t)D2) - ((int32_t)C[5]) * POW2_8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
464 OFF = ((int64_t)C[2]) * POW2_16 + ((int64_t)dT) * ((int64_t)C[4]) / POW2_7;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
465 SENS = ((int64_t)C[1]) * POW2_15 + ((int64_t)dT) * ((int64_t)C[3]) / POW2_8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
466
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
467 T = 2000 + (dT * ((int32_t)C[6])) / POW2_23;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
468
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
469
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
470 if(T < 2000) // low temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
471 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
472 T2 = 3 * dT * dT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
473 T2 /= POW2_33;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
474 OFF2 = ((int64_t)T) - 2000;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
475 OFF2 *= OFF2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
476 OFF2 *= 3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
477 OFF2 /= 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
478 SENS2 = ((int64_t)T) - 2000;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
479 SENS2 *= SENS2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
480 SENS2 *= 5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
481 SENS2 /= POW2_3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
482 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
483 else // high temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
484 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
485 T2 = 7 * dT * dT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
486 T2 /= POW2_37;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
487 OFF2 = ((int64_t)T) - 2000;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
488 OFF2 *= OFF2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
489 OFF2 /= POW2_4;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
490 SENS2 = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
491 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
492
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
493 T = T - T2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
494 OFF = OFF - OFF2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
495 SENS = SENS - SENS2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
496
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
497 P = (int32_t)(((((int64_t)D1) * SENS) / POW2_21 - OFF) / POW2_13);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
498
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
499 ambient_temperature = ((float)T) / 100;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
500 ambient_pressure_mbar = ((float)P) / 10;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
501 }
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
502 */
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
503
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
504 /*
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
505 void pressure_calculation_old(void) {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
506 //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
507 double ambient_temperature_centigrad = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
508 double ambient_pressure_decimbar = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
509
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
510 // static for debug
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
511 static int64_t dt = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
512 static int64_t temp = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
513 static int64_t ms_off = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
514 static int64_t sens = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
515 //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
516 static int64_t ms_off2 = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
517 static int64_t sens2 = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
518 static int64_t t2 = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
519
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
520 if((D2 == 0) || (D1 == 0))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
521 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
522 //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
523
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
524 // dT = D2 - C[5] * POW2_8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
525 // T = 2000 + (dT * C[6]) / POW2_23;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
526 dt = (int64_t)D2 - C5_x_2p8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
527 //temp ; // in 10 milliGrad Celcius
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
528 ambient_temperature_centigrad = 2000 + dt * C[6] / 8388608;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
529
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
530
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
531 if(ambient_temperature_centigrad < 2000) // low temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
532 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
533 t2 = 3 * dt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
534 t2 *= dt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
535 t2 /= 8589934592;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
536 ms_off2 = ambient_temperature_centigrad - 2000;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
537 ms_off2 *= ms_off2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
538 sens2 = ms_off2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
539 ms_off2 *= 3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
540 ms_off2 /= 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
541 sens2 *= 5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
542 sens2 /= 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
543 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
544 else // high temperature
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
545 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
546 t2 = 7 * dt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
547 t2 *= dt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
548 t2 /= 137438953472;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
549 ms_off2 = ambient_temperature_centigrad - 2000;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
550 ms_off2 *= ms_off2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
551 ms_off2 /= 16;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
552 sens2 = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
553 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
554
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
555
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
556 //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
557
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
558 // pressure
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
559 // OFF = C[2] * POW2_16 + dT * C[4] / POW2_7;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
560 // SENS = C[1] * POW2_15 + dT * C[3] / POW2_8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
561 ms_off = C[4] * dt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
562 ms_off /= 128;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
563 ms_off += C2_x_2p16;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
564 //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
565 sens = C[3] * dt;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
566 sens /= 256;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
567 sens += C1_x_2p15;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
568
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
569 // 2nd order correction
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
570 ambient_temperature_centigrad -= t2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
571 ms_off -= ms_off2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
572 sens -= sens2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
573
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
574 ambient_temperature = ambient_temperature_centigrad / 100;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
575 // P = (D1 * SENS / POW2_21 - OFF) / POW2_13;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
576 temp = D1 * sens;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
577 temp /= 2097152;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
578 temp -= ms_off;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
579 temp /= 8192;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
580 ambient_pressure_decimbar = temp; // to float/double
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
581 ambient_pressure_mbar = ambient_pressure_decimbar / 10;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
582 }
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
583 */
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
584
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
585
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
586 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
587 * short and int are both 16bit according to AVR/GCC google results
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
588 */
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
589 static uint8_t crc4(uint16_t n_prom[])
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
590 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
591 uint16_t cnt; // simple counter
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
592 uint16_t n_rem; // crc reminder
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
593 uint16_t crc_read; // original value of the crc
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
594 uint8_t n_bit;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
595 n_rem = 0x00;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
596 crc_read=n_prom[7]; //save read CRC
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
597 n_prom[7]=(0xFF00 & (n_prom[7])); //CRC byte is replaced by 0
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
598 for (cnt = 0; cnt < 16; cnt++) // operation is performed on bytes
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
599 { // choose LSB or MSB
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
600 if (cnt%2==1) n_rem ^= (uint16_t) ((n_prom[cnt>>1]) & 0x00FF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
601 else n_rem ^= (uint16_t) (n_prom[cnt>>1]>>8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
602 for (n_bit = 8; n_bit > 0; n_bit--)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
603 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
604 if (n_rem & (0x8000))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
605 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
606 n_rem = (n_rem << 1) ^ 0x3000;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
607 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
608 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
609 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
610 n_rem = (n_rem << 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
611 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
612 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
613 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
614 n_rem= (0x000F & (n_rem >> 12)); // // final 4-bit reminder is CRC code
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
615 n_prom[7]=crc_read; // restore the crc_read to its original place
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
616 return (n_rem ^ 0x00);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
617 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
618 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
619 void test_calculation(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
620 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
621 C1 = 29112;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
622 C2 = 26814;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
623 C3 = 19125;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
624 C4 = 17865;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
625 C5 = 32057;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
626 C6 = 31305;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
627
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
628 C2_x_2p16 = C2 * 65536;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
629 C1_x_2p15 = C1 * 32768;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
630
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
631 D1 = 4944364;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
632 D2 = 8198974;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
633 pressure_calculation() ;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
634 };
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
635 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
636