LibDriver SHT2X
Loading...
Searching...
No Matches
driver_sht2x_read_test.c
Go to the documentation of this file.
1
36
38
39static sht2x_handle_t gs_handle;
40
49uint8_t sht2x_read_test(uint32_t times)
50{
51 uint8_t res;
52 uint32_t i;
53 sht2x_info_t info;
54
55 /* link functions */
67
68 /* sht2x info */
69 res = sht2x_info(&info);
70 if (res != 0)
71 {
72 sht2x_interface_debug_print("sht2x: get info failed.\n");
73
74 return 1;
75 }
76 else
77 {
78 /* print chip information */
79 sht2x_interface_debug_print("sht2x: chip is %s.\n", info.chip_name);
80 sht2x_interface_debug_print("sht2x: manufacturer is %s.\n", info.manufacturer_name);
81 sht2x_interface_debug_print("sht2x: interface is %s.\n", info.interface);
82 sht2x_interface_debug_print("sht2x: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
83 sht2x_interface_debug_print("sht2x: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
84 sht2x_interface_debug_print("sht2x: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
85 sht2x_interface_debug_print("sht2x: max current is %0.2fmA.\n", info.max_current_ma);
86 sht2x_interface_debug_print("sht2x: max temperature is %0.1fC.\n", info.temperature_max);
87 sht2x_interface_debug_print("sht2x: min temperature is %0.1fC.\n", info.temperature_min);
88 }
89
90 /* start read test */
91 sht2x_interface_debug_print("sht2x: start read test.\n");
92
93 /* sht2x init */
94 res = sht2x_init(&gs_handle);
95 if (res != 0)
96 {
97 sht2x_interface_debug_print("sht2x: init failed.\n");
98
99 return 1;
100 }
101
102 /* disable heater */
103 res = sht2x_set_heater(&gs_handle, SHT2X_BOOL_FALSE);
104 if (res != 0)
105 {
106 sht2x_interface_debug_print("sht2x: set heater failed.\n");
107 (void)sht2x_deinit(&gs_handle);
108
109 return 1;
110 }
111
112 /* disable otp reload */
114 if (res != 0)
115 {
116 sht2x_interface_debug_print("sht2x: set disable otp reload failed.\n");
117 (void)sht2x_deinit(&gs_handle);
118
119 return 1;
120 }
121
122 /* set hold master mode */
123 res = sht2x_set_mode(&gs_handle, SHT2X_MODE_HOLD_MASTER);
124 if (res != 0)
125 {
126 sht2x_interface_debug_print("sht2x: set mode failed.\n");
127 (void)sht2x_deinit(&gs_handle);
128
129 return 1;
130 }
131
132 /* output */
133 sht2x_interface_debug_print("sht2x: set hold master mode.\n");
134
135 /* set resolution rh 12bit, t 14bit */
137 if (res != 0)
138 {
139 sht2x_interface_debug_print("sht2x: set resolution failed.\n");
140 (void)sht2x_deinit(&gs_handle);
141
142 return 1;
143 }
144
145 /* output */
146 sht2x_interface_debug_print("sht2x: set resolution rh 12bit, t 14bit.\n");
147
148 for (i = 0; i < times; i++)
149 {
150 uint16_t temperature_raw;
151 float temperature_s;
152 uint16_t humidity_raw;
153 float humidity_s;
154
155 res = sht2x_read(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature_s, (uint16_t *)&humidity_raw, (float *)&humidity_s);
156 if (res != 0)
157 {
158 sht2x_interface_debug_print("sht2x: read failed.\n");
159 (void)sht2x_deinit(&gs_handle);
160
161 return 1;
162 }
163 sht2x_interface_debug_print("sht2x: temperature is %0.2fC.\n", temperature_s);
164 sht2x_interface_debug_print("sht2x: humidity is %0.2f%%.\n", humidity_s);
165
166 /* wait 1000 ms */
168 }
169
170 /* set resolution rh 8bit, t 12bit */
172 if (res != 0)
173 {
174 sht2x_interface_debug_print("sht2x: set resolution failed.\n");
175 (void)sht2x_deinit(&gs_handle);
176
177 return 1;
178 }
179
180 /* output */
181 sht2x_interface_debug_print("sht2x: set resolution rh 8bit, t 12bit.\n");
182
183 for (i = 0; i < times; i++)
184 {
185 uint16_t temperature_raw;
186 float temperature_s;
187 uint16_t humidity_raw;
188 float humidity_s;
189
190 res = sht2x_read(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature_s, (uint16_t *)&humidity_raw, (float *)&humidity_s);
191 if (res != 0)
192 {
193 sht2x_interface_debug_print("sht2x: read failed.\n");
194 (void)sht2x_deinit(&gs_handle);
195
196 return 1;
197 }
198 sht2x_interface_debug_print("sht2x: temperature is %0.2fC.\n", temperature_s);
199 sht2x_interface_debug_print("sht2x: humidity is %0.2f%%.\n", humidity_s);
200
201 /* wait 1000 ms */
203 }
204
205 /* set resolution rh 10bit, t 13bit */
207 if (res != 0)
208 {
209 sht2x_interface_debug_print("sht2x: set resolution failed.\n");
210 (void)sht2x_deinit(&gs_handle);
211
212 return 1;
213 }
214
215 /* output */
216 sht2x_interface_debug_print("sht2x: set resolution rh 10bit, t 13bit.\n");
217
218 for (i = 0; i < times; i++)
219 {
220 uint16_t temperature_raw;
221 float temperature_s;
222 uint16_t humidity_raw;
223 float humidity_s;
224
225 res = sht2x_read(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature_s, (uint16_t *)&humidity_raw, (float *)&humidity_s);
226 if (res != 0)
227 {
228 sht2x_interface_debug_print("sht2x: read failed.\n");
229 (void)sht2x_deinit(&gs_handle);
230
231 return 1;
232 }
233 sht2x_interface_debug_print("sht2x: temperature is %0.2fC.\n", temperature_s);
234 sht2x_interface_debug_print("sht2x: humidity is %0.2f%%.\n", humidity_s);
235
236 /* wait 1000 ms */
238 }
239
240 /* set resolution rh 11bit, t 11bit */
242 if (res != 0)
243 {
244 sht2x_interface_debug_print("sht2x: set resolution failed.\n");
245 (void)sht2x_deinit(&gs_handle);
246
247 return 1;
248 }
249
250 /* output */
251 sht2x_interface_debug_print("sht2x: set resolution rh 11bit, t 11bit.\n");
252
253 for (i = 0; i < times; i++)
254 {
255 uint16_t temperature_raw;
256 float temperature_s;
257 uint16_t humidity_raw;
258 float humidity_s;
259
260 res = sht2x_read(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature_s, (uint16_t *)&humidity_raw, (float *)&humidity_s);
261 if (res != 0)
262 {
263 sht2x_interface_debug_print("sht2x: read failed.\n");
264 (void)sht2x_deinit(&gs_handle);
265
266 return 1;
267 }
268 sht2x_interface_debug_print("sht2x: temperature is %0.2fC.\n", temperature_s);
269 sht2x_interface_debug_print("sht2x: humidity is %0.2f%%.\n", humidity_s);
270
271 /* wait 1000 ms */
273 }
274
275 /* set no hold master mode */
276 res = sht2x_set_mode(&gs_handle, SHT2X_MODE_NO_HOLD_MASTER);
277 if (res != 0)
278 {
279 sht2x_interface_debug_print("sht2x: set mode failed.\n");
280 (void)sht2x_deinit(&gs_handle);
281
282 return 1;
283 }
284
285 /* output */
286 sht2x_interface_debug_print("sht2x: set no hold master mode.\n");
287
288 /* set resolution rh 12bit, t 14bit */
290 if (res != 0)
291 {
292 sht2x_interface_debug_print("sht2x: set resolution failed.\n");
293 (void)sht2x_deinit(&gs_handle);
294
295 return 1;
296 }
297
298 /* output */
299 sht2x_interface_debug_print("sht2x: set resolution rh 12bit, t 14bit.\n");
300
301 for (i = 0; i < times; i++)
302 {
303 uint16_t temperature_raw;
304 float temperature_s;
305 uint16_t humidity_raw;
306 float humidity_s;
307
308 res = sht2x_read(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature_s, (uint16_t *)&humidity_raw, (float *)&humidity_s);
309 if (res != 0)
310 {
311 sht2x_interface_debug_print("sht2x: read failed.\n");
312 (void)sht2x_deinit(&gs_handle);
313
314 return 1;
315 }
316 sht2x_interface_debug_print("sht2x: temperature is %0.2fC.\n", temperature_s);
317 sht2x_interface_debug_print("sht2x: humidity is %0.2f%%.\n", humidity_s);
318
319 /* wait 1000 ms */
321 }
322
323 /* set resolution rh 8bit, t 12bit */
325 if (res != 0)
326 {
327 sht2x_interface_debug_print("sht2x: set resolution failed.\n");
328 (void)sht2x_deinit(&gs_handle);
329
330 return 1;
331 }
332
333 /* output */
334 sht2x_interface_debug_print("sht2x: set resolution rh 8bit, t 12bit.\n");
335
336 for (i = 0; i < times; i++)
337 {
338 uint16_t temperature_raw;
339 float temperature_s;
340 uint16_t humidity_raw;
341 float humidity_s;
342
343 res = sht2x_read(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature_s, (uint16_t *)&humidity_raw, (float *)&humidity_s);
344 if (res != 0)
345 {
346 sht2x_interface_debug_print("sht2x: read failed.\n");
347 (void)sht2x_deinit(&gs_handle);
348
349 return 1;
350 }
351 sht2x_interface_debug_print("sht2x: temperature is %0.2fC.\n", temperature_s);
352 sht2x_interface_debug_print("sht2x: humidity is %0.2f%%.\n", humidity_s);
353
354 /* wait 1000 ms */
356 }
357
358 /* set resolution rh 10bit, t 13bit */
360 if (res != 0)
361 {
362 sht2x_interface_debug_print("sht2x: set resolution failed.\n");
363 (void)sht2x_deinit(&gs_handle);
364
365 return 1;
366 }
367
368 /* output */
369 sht2x_interface_debug_print("sht2x: set resolution rh 10bit, t 13bit.\n");
370
371 for (i = 0; i < times; i++)
372 {
373 uint16_t temperature_raw;
374 float temperature_s;
375 uint16_t humidity_raw;
376 float humidity_s;
377
378 res = sht2x_read(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature_s, (uint16_t *)&humidity_raw, (float *)&humidity_s);
379 if (res != 0)
380 {
381 sht2x_interface_debug_print("sht2x: read failed.\n");
382 (void)sht2x_deinit(&gs_handle);
383
384 return 1;
385 }
386 sht2x_interface_debug_print("sht2x: temperature is %0.2fC.\n", temperature_s);
387 sht2x_interface_debug_print("sht2x: humidity is %0.2f%%.\n", humidity_s);
388
389 /* wait 1000 ms */
391 }
392
393 /* set resolution rh 11bit, t 11bit */
395 if (res != 0)
396 {
397 sht2x_interface_debug_print("sht2x: set resolution failed.\n");
398 (void)sht2x_deinit(&gs_handle);
399
400 return 1;
401 }
402
403 /* output */
404 sht2x_interface_debug_print("sht2x: set resolution rh 11bit, t 11bit.\n");
405
406 for (i = 0; i < times; i++)
407 {
408 uint16_t temperature_raw;
409 float temperature_s;
410 uint16_t humidity_raw;
411 float humidity_s;
412
413 res = sht2x_read(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature_s, (uint16_t *)&humidity_raw, (float *)&humidity_s);
414 if (res != 0)
415 {
416 sht2x_interface_debug_print("sht2x: read failed.\n");
417 (void)sht2x_deinit(&gs_handle);
418
419 return 1;
420 }
421 sht2x_interface_debug_print("sht2x: temperature is %0.2fC.\n", temperature_s);
422 sht2x_interface_debug_print("sht2x: humidity is %0.2f%%.\n", humidity_s);
423
424 /* wait 1000 ms */
426 }
427
428 /* finish read test */
429 sht2x_interface_debug_print("sht2x: finish read test.\n");
430 (void)sht2x_deinit(&gs_handle);
431
432 return 0;
433}
driver sht2x read test header file
struct sht2x_info_s sht2x_info_t
sht2x information structure definition
uint8_t sht2x_set_heater(sht2x_handle_t *handle, sht2x_bool_t enable)
enable or disable heater
uint8_t sht2x_read(sht2x_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read data
uint8_t sht2x_init(sht2x_handle_t *handle)
initialize the chip
uint8_t sht2x_info(sht2x_info_t *info)
get chip's information
uint8_t sht2x_set_resolution(sht2x_handle_t *handle, sht2x_resolution_t resolution)
set resolution
struct sht2x_handle_s sht2x_handle_t
sht2x handle structure definition
uint8_t sht2x_set_disable_otp_reload(sht2x_handle_t *handle, sht2x_bool_t enable)
enable or disable disable otp reload
uint8_t sht2x_deinit(sht2x_handle_t *handle)
close the chip
uint8_t sht2x_set_mode(sht2x_handle_t *handle, sht2x_mode_t mode)
set chip mode
@ SHT2X_MODE_HOLD_MASTER
@ SHT2X_MODE_NO_HOLD_MASTER
@ SHT2X_RESOLUTION_RH_11BIT_T_11BIT
@ SHT2X_RESOLUTION_RH_12BIT_T_14BIT
@ SHT2X_RESOLUTION_RH_10BIT_T_13BIT
@ SHT2X_RESOLUTION_RH_8BIT_T_12BIT
@ SHT2X_BOOL_FALSE
@ SHT2X_BOOL_TRUE
uint8_t sht2x_interface_iic_read_command(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read command
void sht2x_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t sht2x_interface_iic_init(void)
interface iic bus init
uint8_t sht2x_interface_iic_deinit(void)
interface iic bus deinit
void sht2x_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t sht2x_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t sht2x_interface_iic_write_command(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write command
uint8_t sht2x_interface_iic_read_with_wait(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read with wait
uint8_t sht2x_interface_iic_read_address16(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
interface iic bus read with 16 bits register address
uint8_t sht2x_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t sht2x_read_test(uint32_t times)
read test
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]