LibDriver CS1237
Loading...
Searching...
No Matches
driver_cs1237_read_test.c
Go to the documentation of this file.
1
36
38#include <stdlib.h>
39
43static cs1237_handle_t gs_handle;
44
56uint8_t cs1237_read_test(float reference_voltage_v, int32_t temperature_raw, float temperature_deg, uint32_t times)
57{
58 uint8_t res;
59 uint32_t i;
60 cs1237_info_t info;
61
62 /* link interface function */
74
75 /* get information */
76 res = cs1237_info(&info);
77 if (res != 0)
78 {
79 cs1237_interface_debug_print("cs1237: get info failed.\n");
80
81 return 1;
82 }
83 else
84 {
85 /* print chip info */
86 cs1237_interface_debug_print("cs1237: chip is %s.\n", info.chip_name);
87 cs1237_interface_debug_print("cs1237: manufacturer is %s.\n", info.manufacturer_name);
88 cs1237_interface_debug_print("cs1237: interface is %s.\n", info.interface);
89 cs1237_interface_debug_print("cs1237: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
90 cs1237_interface_debug_print("cs1237: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
91 cs1237_interface_debug_print("cs1237: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
92 cs1237_interface_debug_print("cs1237: max current is %0.2fmA.\n", info.max_current_ma);
93 cs1237_interface_debug_print("cs1237: max temperature is %0.1fC.\n", info.temperature_max);
94 cs1237_interface_debug_print("cs1237: min temperature is %0.1fC.\n", info.temperature_min);
95 }
96
97 /* start read test */
98 cs1237_interface_debug_print("cs1237: start read test.\n");
99
100 /* cs1237 init */
101 res = cs1237_init(&gs_handle);
102 if (res != 0)
103 {
104 cs1237_interface_debug_print("cs1237: init failed.\n");
105
106 return 1;
107 }
108
109 /* set reference voltage */
110 res = cs1237_set_reference_voltage(&gs_handle, reference_voltage_v);
111 if (res != 0)
112 {
113 cs1237_interface_debug_print("cs1237: set reference voltage failed.\n");
114 (void)cs1237_deinit(&gs_handle);
115
116 return 1;
117 }
118
119 /* set temperature calibration point */
120 res = cs1237_set_temperature_calibration_point(&gs_handle, temperature_raw, temperature_deg);
121 if (res != 0)
122 {
123 cs1237_interface_debug_print("cs1237: set temperature calibration point failed.\n");
124 (void)cs1237_deinit(&gs_handle);
125
126 return 1;
127 }
128
129 /* wake up */
130 res = cs1237_wake_up(&gs_handle);
131 if (res != 0)
132 {
133 cs1237_interface_debug_print("cs1237: wake up failed.\n");
134 (void)cs1237_deinit(&gs_handle);
135
136 return 1;
137 }
138
139 /* enable reference voltage output */
141 if (res != 0)
142 {
143 cs1237_interface_debug_print("cs1237: set reference voltage output failed.\n");
144 (void)cs1237_deinit(&gs_handle);
145
146 return 1;
147 }
148
149 /* set adc rate 10hz */
150 res = cs1237_set_adc_rate(&gs_handle, CS1237_ADC_RATE_10HZ);
151 if (res != 0)
152 {
153 cs1237_interface_debug_print("cs1237: set adc rate failed.\n");
154 (void)cs1237_deinit(&gs_handle);
155
156 return 1;
157 }
158
159 /* set channel a */
160 res = cs1237_set_channel(&gs_handle, CS1237_CHANNEL_A);
161 if (res != 0)
162 {
163 cs1237_interface_debug_print("cs1237: set channel failed.\n");
164 (void)cs1237_deinit(&gs_handle);
165
166 return 1;
167 }
168
169 /* output */
170 cs1237_interface_debug_print("cs1237: set channel a.\n");
171
172 /* output */
173 cs1237_interface_debug_print("cs1237: set pga 1.\n");
174
175 /* set pga 1 */
176 res = cs1237_set_pga(&gs_handle, CS1237_PGA_1);
177 if (res != 0)
178 {
179 cs1237_interface_debug_print("cs1237: set pga failed.\n");
180 (void)cs1237_deinit(&gs_handle);
181
182 return 1;
183 }
184
185 for (i = 0; i < times; i++)
186 {
187 int32_t raw;
188 double mv;
189
190 /* delay 1000ms */
192
193 /* read data */
194 res = cs1237_read(&gs_handle, &raw, &mv);
195 if (res != 0)
196 {
197 cs1237_interface_debug_print("cs1237: read failed.\n");
198 (void)cs1237_deinit(&gs_handle);
199
200 return 1;
201 }
202
203 /* output */
204 cs1237_interface_debug_print("cs1237: adc is %fmV.\n", mv);
205 }
206
207 /* output */
208 cs1237_interface_debug_print("cs1237: set pga 2.\n");
209
210 /* set pga 2 */
211 res = cs1237_set_pga(&gs_handle, CS1237_PGA_2);
212 if (res != 0)
213 {
214 cs1237_interface_debug_print("cs1237: set pga failed.\n");
215 (void)cs1237_deinit(&gs_handle);
216
217 return 1;
218 }
219
220 for (i = 0; i < times; i++)
221 {
222 int32_t raw;
223 double mv;
224
225 /* delay 1000ms */
227
228 /* read data */
229 res = cs1237_read(&gs_handle, &raw, &mv);
230 if (res != 0)
231 {
232 cs1237_interface_debug_print("cs1237: read failed.\n");
233 (void)cs1237_deinit(&gs_handle);
234
235 return 1;
236 }
237
238 /* output */
239 cs1237_interface_debug_print("cs1237: adc is %fmV.\n", mv);
240 }
241
242 /* output */
243 cs1237_interface_debug_print("cs1237: set pga 64.\n");
244
245 /* set pga 64 */
246 res = cs1237_set_pga(&gs_handle, CS1237_PGA_64);
247 if (res != 0)
248 {
249 cs1237_interface_debug_print("cs1237: set pga failed.\n");
250 (void)cs1237_deinit(&gs_handle);
251
252 return 1;
253 }
254
255 for (i = 0; i < times; i++)
256 {
257 int32_t raw;
258 double mv;
259
260 /* delay 1000ms */
262
263 /* read data */
264 res = cs1237_read(&gs_handle, &raw, &mv);
265 if (res != 0)
266 {
267 cs1237_interface_debug_print("cs1237: read failed.\n");
268 (void)cs1237_deinit(&gs_handle);
269
270 return 1;
271 }
272
273 /* output */
274 cs1237_interface_debug_print("cs1237: adc is %fmV.\n", mv);
275 }
276
277 /* output */
278 cs1237_interface_debug_print("cs1237: set pga 128.\n");
279
280 /* set pga 128 */
281 res = cs1237_set_pga(&gs_handle, CS1237_PGA_128);
282 if (res != 0)
283 {
284 cs1237_interface_debug_print("cs1237: set pga failed.\n");
285 (void)cs1237_deinit(&gs_handle);
286
287 return 1;
288 }
289
290 for (i = 0; i < times; i++)
291 {
292 int32_t raw;
293 double mv;
294
295 /* delay 1000ms */
297
298 /* read data */
299 res = cs1237_read(&gs_handle, &raw, &mv);
300 if (res != 0)
301 {
302 cs1237_interface_debug_print("cs1237: read failed.\n");
303 (void)cs1237_deinit(&gs_handle);
304
305 return 1;
306 }
307
308 /* output */
309 cs1237_interface_debug_print("cs1237: adc is %fmV.\n", mv);
310 }
311
312 /* output */
313 cs1237_interface_debug_print("cs1237: set adc rate 10hz.\n");
314
315 /* set adc rate 10hz */
316 res = cs1237_set_adc_rate(&gs_handle, CS1237_ADC_RATE_10HZ);
317 if (res != 0)
318 {
319 cs1237_interface_debug_print("cs1237: set adc rate failed.\n");
320 (void)cs1237_deinit(&gs_handle);
321
322 return 1;
323 }
324
325 for (i = 0; i < times; i++)
326 {
327 int32_t raw;
328 double mv;
329
330 /* delay 1000ms */
332
333 /* read data */
334 res = cs1237_read(&gs_handle, &raw, &mv);
335 if (res != 0)
336 {
337 cs1237_interface_debug_print("cs1237: read failed.\n");
338 (void)cs1237_deinit(&gs_handle);
339
340 return 1;
341 }
342
343 /* output */
344 cs1237_interface_debug_print("cs1237: adc is %fmV.\n", mv);
345 }
346
347 /* output */
348 cs1237_interface_debug_print("cs1237: set adc rate 40hz.\n");
349
350 /* set adc rate 40hz */
351 res = cs1237_set_adc_rate(&gs_handle, CS1237_ADC_RATE_40HZ);
352 if (res != 0)
353 {
354 cs1237_interface_debug_print("cs1237: set adc rate failed.\n");
355 (void)cs1237_deinit(&gs_handle);
356
357 return 1;
358 }
359
360 for (i = 0; i < times; i++)
361 {
362 int32_t raw;
363 double mv;
364
365 /* delay 1000ms */
367
368 /* read data */
369 res = cs1237_read(&gs_handle, &raw, &mv);
370 if (res != 0)
371 {
372 cs1237_interface_debug_print("cs1237: read failed.\n");
373 (void)cs1237_deinit(&gs_handle);
374
375 return 1;
376 }
377
378 /* output */
379 cs1237_interface_debug_print("cs1237: adc is %fmV.\n", mv);
380 }
381
382 /* output */
383 cs1237_interface_debug_print("cs1237: set adc rate 640hz.\n");
384
385 /* set adc rate 640hz */
387 if (res != 0)
388 {
389 cs1237_interface_debug_print("cs1237: set adc rate failed.\n");
390 (void)cs1237_deinit(&gs_handle);
391
392 return 1;
393 }
394
395 for (i = 0; i < times; i++)
396 {
397 int32_t raw;
398 double mv;
399
400 /* delay 1000ms */
402
403 /* read data */
404 res = cs1237_read(&gs_handle, &raw, &mv);
405 if (res != 0)
406 {
407 cs1237_interface_debug_print("cs1237: read failed.\n");
408 (void)cs1237_deinit(&gs_handle);
409
410 return 1;
411 }
412
413 /* output */
414 cs1237_interface_debug_print("cs1237: adc is %fmV.\n", mv);
415 }
416
417 /* output */
418 cs1237_interface_debug_print("cs1237: set adc rate 1280hz.\n");
419
420 /* set adc rate 1280hz */
422 if (res != 0)
423 {
424 cs1237_interface_debug_print("cs1237: set adc rate failed.\n");
425 (void)cs1237_deinit(&gs_handle);
426
427 return 1;
428 }
429
430 for (i = 0; i < times; i++)
431 {
432 int32_t raw;
433 double mv;
434
435 /* delay 1000ms */
437
438 /* read data */
439 res = cs1237_read(&gs_handle, &raw, &mv);
440 if (res != 0)
441 {
442 cs1237_interface_debug_print("cs1237: read failed.\n");
443 (void)cs1237_deinit(&gs_handle);
444
445 return 1;
446 }
447
448 /* output */
449 cs1237_interface_debug_print("cs1237: adc is %fmV.\n", mv);
450 }
451
452 /* set pga 1 */
453 res = cs1237_set_pga(&gs_handle, CS1237_PGA_1);
454 if (res != 0)
455 {
456 cs1237_interface_debug_print("cs1237: set pga failed.\n");
457 (void)cs1237_deinit(&gs_handle);
458
459 return 1;
460 }
461
462 /* set channel temperature */
464 if (res != 0)
465 {
466 cs1237_interface_debug_print("cs1237: set channel failed.\n");
467 (void)cs1237_deinit(&gs_handle);
468
469 return 1;
470 }
471
472 /* output */
473 cs1237_interface_debug_print("cs1237: read temperature test.\n");
474
475 for (i = 0; i < times; i++)
476 {
477 int32_t raw;
478 float deg;
479
480 /* delay 1000ms */
482
483 /* read data */
484 res = cs1237_read_temperature(&gs_handle, &raw, &deg);
485 if (res != 0)
486 {
487 cs1237_interface_debug_print("cs1237: read temperature failed.\n");
488 (void)cs1237_deinit(&gs_handle);
489
490 return 1;
491 }
492
493 /* output */
494 cs1237_interface_debug_print("cs1237: raw is 0x%0X, temperature is %0.2fC.\n", raw, deg);
495 }
496
497 /* finish read test */
498 cs1237_interface_debug_print("cs1237: finish read test.\n");
499 (void)cs1237_deinit(&gs_handle);
500
501 return 0;
502}
driver cs1237 read test header file
uint8_t cs1237_set_adc_rate(cs1237_handle_t *handle, cs1237_adc_rate_t rate)
set adc rate
uint8_t cs1237_read_temperature(cs1237_handle_t *handle, int32_t *raw, float *degrees)
read temperature
uint8_t cs1237_set_reference_voltage_output(cs1237_handle_t *handle, cs1237_bool_t enable)
enable or disable reference voltage output
struct cs1237_handle_s cs1237_handle_t
cs1237 handle structure definition
uint8_t cs1237_deinit(cs1237_handle_t *handle)
close the chip
uint8_t cs1237_info(cs1237_info_t *info)
get chip's information
uint8_t cs1237_read(cs1237_handle_t *handle, int32_t *raw, double *mv)
read voltage
uint8_t cs1237_set_reference_voltage(cs1237_handle_t *handle, float volt)
set reference voltage
uint8_t cs1237_wake_up(cs1237_handle_t *handle)
wake up
uint8_t cs1237_set_pga(cs1237_handle_t *handle, cs1237_pga_t pga)
set adc pga
uint8_t cs1237_set_temperature_calibration_point(cs1237_handle_t *handle, int32_t temperature_raw, float temperature_deg)
set temperature calibration point
struct cs1237_info_s cs1237_info_t
cs1237 information structure definition
uint8_t cs1237_set_channel(cs1237_handle_t *handle, cs1237_channel_t channel)
set adc channel
uint8_t cs1237_init(cs1237_handle_t *handle)
initialize the chip
@ CS1237_BOOL_TRUE
@ CS1237_PGA_2
@ CS1237_PGA_1
@ CS1237_PGA_128
@ CS1237_PGA_64
@ CS1237_ADC_RATE_1280HZ
@ CS1237_ADC_RATE_640HZ
@ CS1237_ADC_RATE_10HZ
@ CS1237_ADC_RATE_40HZ
@ CS1237_CHANNEL_TEMPERATURE
@ CS1237_CHANNEL_A
void cs1237_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t cs1237_interface_dout_gpio_deinit(void)
interface dout gpio deinit
uint8_t cs1237_interface_sclk_gpio_write(uint8_t level)
interface sclk gpio write
uint8_t cs1237_interface_sclk_gpio_deinit(void)
interface sclk gpio deinit
uint8_t cs1237_interface_dout_gpio_init(void)
interface dout gpio init
uint8_t cs1237_interface_sclk_gpio_init(void)
interface sclk gpio init
void cs1237_interface_delay_ms(uint32_t ms)
interface delay ms
void cs1237_interface_delay_us(uint32_t us)
interface delay us
uint8_t cs1237_interface_dout_gpio_read(uint8_t *level)
interface dout gpio read
uint8_t cs1237_interface_dout_gpio_write(uint8_t level)
interface dout gpio write
uint8_t cs1237_read_test(float reference_voltage_v, int32_t temperature_raw, float temperature_deg, uint32_t times)
read test
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]