LibDriver MCP3421
Loading...
Searching...
No Matches
driver_mcp3421_read_test.c
Go to the documentation of this file.
1
36
38
39static mcp3421_handle_t gs_handle;
40
49uint8_t mcp3421_read_test(uint32_t times)
50{
51 uint8_t res;
52 uint32_t i;
53 mcp3421_info_t info;
54
55 /* link interface function */
63
64 /* get information */
65 res = mcp3421_info(&info);
66 if (res != 0)
67 {
68 mcp3421_interface_debug_print("mcp3421: get info failed.\n");
69
70 return 1;
71 }
72 else
73 {
74 /* print chip info */
75 mcp3421_interface_debug_print("mcp3421: chip is %s.\n", info.chip_name);
76 mcp3421_interface_debug_print("mcp3421: manufacturer is %s.\n", info.manufacturer_name);
77 mcp3421_interface_debug_print("mcp3421: interface is %s.\n", info.interface);
78 mcp3421_interface_debug_print("mcp3421: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
79 mcp3421_interface_debug_print("mcp3421: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
80 mcp3421_interface_debug_print("mcp3421: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
81 mcp3421_interface_debug_print("mcp3421: max current is %0.2fmA.\n", info.max_current_ma);
82 mcp3421_interface_debug_print("mcp3421: max temperature is %0.1fC.\n", info.temperature_max);
83 mcp3421_interface_debug_print("mcp3421: min temperature is %0.1fC.\n", info.temperature_min);
84 }
85
86 /* start read test */
87 mcp3421_interface_debug_print("mcp3421: start read test.\n");
88
89 /* mcp3421 init */
90 res = mcp3421_init(&gs_handle);
91 if (res != 0)
92 {
93 mcp3421_interface_debug_print("mcp3421: init failed.\n");
94
95 return 1;
96 }
97
98 /* set pag 1 */
99 res = mcp3421_set_pga(&gs_handle, MCP3421_PGA_1);
100 if (res != 0)
101 {
102 mcp3421_interface_debug_print("mcp3421: set pga failed.\n");
103 (void)mcp3421_deinit(&gs_handle);
104
105 return 1;
106 }
107
108 /* set adc bit */
109 res = mcp3421_set_adc_bit(&gs_handle, MCP3421_BIT_12);
110 if (res != 0)
111 {
112 mcp3421_interface_debug_print("mcp3421: set adc bit failed.\n");
113 (void)mcp3421_deinit(&gs_handle);
114
115 return 1;
116 }
117
118 /* output */
119 mcp3421_interface_debug_print("mcp3421: set adc 12bits.\n");
120
121 for (i = 0; i < times; i++)
122 {
123 int32_t raw;
124 double v;
125
126 /* read once */
127 res = mcp3421_single_read(&gs_handle, &raw, &v);
128 if (res != 0)
129 {
130 mcp3421_interface_debug_print("mcp3421: single read failed.\n");
131 (void)mcp3421_deinit(&gs_handle);
132
133 return 1;
134 }
135
136 /* output */
137 mcp3421_interface_debug_print("mcp3421: adc is %0.6fV.\n", v);
138
139 /* delay 1000ms */
141 }
142
143 /* set adc bit */
144 res = mcp3421_set_adc_bit(&gs_handle, MCP3421_BIT_14);
145 if (res != 0)
146 {
147 mcp3421_interface_debug_print("mcp3421: set adc bit failed.\n");
148 (void)mcp3421_deinit(&gs_handle);
149
150 return 1;
151 }
152
153 /* output */
154 mcp3421_interface_debug_print("mcp3421: set adc 14bits.\n");
155
156 for (i = 0; i < times; i++)
157 {
158 int32_t raw;
159 double v;
160
161 /* read once */
162 res = mcp3421_single_read(&gs_handle, &raw, &v);
163 if (res != 0)
164 {
165 mcp3421_interface_debug_print("mcp3421: single read failed.\n");
166 (void)mcp3421_deinit(&gs_handle);
167
168 return 1;
169 }
170
171 /* output */
172 mcp3421_interface_debug_print("mcp3421: adc is %0.6fV.\n", v);
173
174 /* delay 1000ms */
176 }
177
178 /* set adc bit */
179 res = mcp3421_set_adc_bit(&gs_handle, MCP3421_BIT_16);
180 if (res != 0)
181 {
182 mcp3421_interface_debug_print("mcp3421: set adc bit failed.\n");
183 (void)mcp3421_deinit(&gs_handle);
184
185 return 1;
186 }
187
188 /* output */
189 mcp3421_interface_debug_print("mcp3421: set adc 16bits.\n");
190
191 for (i = 0; i < times; i++)
192 {
193 int32_t raw;
194 double v;
195
196 /* read once */
197 res = mcp3421_single_read(&gs_handle, &raw, &v);
198 if (res != 0)
199 {
200 mcp3421_interface_debug_print("mcp3421: single read failed.\n");
201 (void)mcp3421_deinit(&gs_handle);
202
203 return 1;
204 }
205
206 /* output */
207 mcp3421_interface_debug_print("mcp3421: adc is %0.6fV.\n", v);
208
209 /* delay 1000ms */
211 }
212
213 /* set adc bit */
214 res = mcp3421_set_adc_bit(&gs_handle, MCP3421_BIT_18);
215 if (res != 0)
216 {
217 mcp3421_interface_debug_print("mcp3421: set adc bit failed.\n");
218 (void)mcp3421_deinit(&gs_handle);
219
220 return 1;
221 }
222
223 /* output */
224 mcp3421_interface_debug_print("mcp3421: set adc 18bits.\n");
225
226 for (i = 0; i < times; i++)
227 {
228 int32_t raw;
229 double v;
230
231 /* read once */
232 res = mcp3421_single_read(&gs_handle, &raw, &v);
233 if (res != 0)
234 {
235 mcp3421_interface_debug_print("mcp3421: single read failed.\n");
236 (void)mcp3421_deinit(&gs_handle);
237
238 return 1;
239 }
240
241 /* output */
242 mcp3421_interface_debug_print("mcp3421: adc is %0.6fV.\n", v);
243
244 /* delay 1000ms */
246 }
247
248 /* set pga 2 */
249 res = mcp3421_set_pga(&gs_handle, MCP3421_PGA_2);
250 if (res != 0)
251 {
252 mcp3421_interface_debug_print("mcp3421: set pga failed.\n");
253 (void)mcp3421_deinit(&gs_handle);
254
255 return 1;
256 }
257
258 /* output */
259 mcp3421_interface_debug_print("mcp3421: set pga 2.\n");
260
261 for (i = 0; i < times; i++)
262 {
263 int32_t raw;
264 double v;
265
266 /* read once */
267 res = mcp3421_single_read(&gs_handle, &raw, &v);
268 if (res != 0)
269 {
270 mcp3421_interface_debug_print("mcp3421: single read failed.\n");
271 (void)mcp3421_deinit(&gs_handle);
272
273 return 1;
274 }
275
276 /* output */
277 mcp3421_interface_debug_print("mcp3421: adc is %0.6fV.\n", v);
278
279 /* delay 1000ms */
281 }
282
283 /* set pga 4 */
284 res = mcp3421_set_pga(&gs_handle, MCP3421_PGA_4);
285 if (res != 0)
286 {
287 mcp3421_interface_debug_print("mcp3421: set pga failed.\n");
288 (void)mcp3421_deinit(&gs_handle);
289
290 return 1;
291 }
292
293 /* output */
294 mcp3421_interface_debug_print("mcp3421: set pga 4.\n");
295
296 for (i = 0; i < times; i++)
297 {
298 int32_t raw;
299 double v;
300
301 /* read once */
302 res = mcp3421_single_read(&gs_handle, &raw, &v);
303 if (res != 0)
304 {
305 mcp3421_interface_debug_print("mcp3421: single read failed.\n");
306 (void)mcp3421_deinit(&gs_handle);
307
308 return 1;
309 }
310
311 /* output */
312 mcp3421_interface_debug_print("mcp3421: adc is %0.6fV.\n", v);
313
314 /* delay 1000ms */
316 }
317
318 /* set pga 8 */
319 res = mcp3421_set_pga(&gs_handle, MCP3421_PGA_8);
320 if (res != 0)
321 {
322 mcp3421_interface_debug_print("mcp3421: set pga failed.\n");
323 (void)mcp3421_deinit(&gs_handle);
324
325 return 1;
326 }
327
328 /* output */
329 mcp3421_interface_debug_print("mcp3421: set pga 8.\n");
330
331 for (i = 0; i < times; i++)
332 {
333 int32_t raw;
334 double v;
335
336 /* read once */
337 res = mcp3421_single_read(&gs_handle, &raw, &v);
338 if (res != 0)
339 {
340 mcp3421_interface_debug_print("mcp3421: single read failed.\n");
341 (void)mcp3421_deinit(&gs_handle);
342
343 return 1;
344 }
345
346 /* output */
347 mcp3421_interface_debug_print("mcp3421: adc is %0.6fV.\n", v);
348
349 /* delay 1000ms */
351 }
352
353 /* set pga 1 */
354 res = mcp3421_set_pga(&gs_handle, MCP3421_PGA_1);
355 if (res != 0)
356 {
357 mcp3421_interface_debug_print("mcp3421: set pga failed.\n");
358 (void)mcp3421_deinit(&gs_handle);
359
360 return 1;
361 }
362
363 /* output */
364 mcp3421_interface_debug_print("mcp3421: continuous read test.\n");
365
366 /* start continuous read */
367 res = mcp3421_start_continuous_read(&gs_handle);
368 if (res != 0)
369 {
370 mcp3421_interface_debug_print("mcp3421: start continuous read failed.\n");
371 (void)mcp3421_deinit(&gs_handle);
372
373 return 1;
374 }
375
376 /* delay 1000ms */
378
379 for (i = 0; i < times; i++)
380 {
381 int32_t raw;
382 double v;
383
384 /* continuous read */
385 res = mcp3421_continuous_read(&gs_handle, &raw, &v);
386 if (res != 0)
387 {
388 mcp3421_interface_debug_print("mcp3421: single read failed.\n");
389 (void)mcp3421_deinit(&gs_handle);
390
391 return 1;
392 }
393
394 /* output */
395 mcp3421_interface_debug_print("mcp3421: adc is %0.6fV.\n", v);
396
397 /* delay 1000ms */
399 }
400
401 /* stop continuous read */
402 res = mcp3421_stop_continuous_read(&gs_handle);
403 if (res != 0)
404 {
405 mcp3421_interface_debug_print("mcp3421: stop continuous read failed.\n");
406 (void)mcp3421_deinit(&gs_handle);
407
408 return 1;
409 }
410
411 /* finish read test */
412 mcp3421_interface_debug_print("mcp3421: finish read test.\n");
413 (void)mcp3421_deinit(&gs_handle);
414
415 return 0;
416}
driver mcp3421 read test header file
uint8_t mcp3421_set_pga(mcp3421_handle_t *handle, mcp3421_pga_t pga)
set the adc pga
uint8_t mcp3421_set_adc_bit(mcp3421_handle_t *handle, mcp3421_bit_t adc_bit)
set the adc bit
uint8_t mcp3421_start_continuous_read(mcp3421_handle_t *handle)
start the chip reading
uint8_t mcp3421_single_read(mcp3421_handle_t *handle, int32_t *raw, double *v)
read data from the chip once
uint8_t mcp3421_init(mcp3421_handle_t *handle)
initialize the chip
uint8_t mcp3421_deinit(mcp3421_handle_t *handle)
close the chip
uint8_t mcp3421_stop_continuous_read(mcp3421_handle_t *handle)
stop the chip reading
uint8_t mcp3421_continuous_read(mcp3421_handle_t *handle, int32_t *raw, double *v)
read data from the chip continuously
struct mcp3421_handle_s mcp3421_handle_t
mcp3421 handle structure definition
struct mcp3421_info_s mcp3421_info_t
mcp3421 information structure definition
uint8_t mcp3421_info(mcp3421_info_t *info)
get chip's information
@ MCP3421_PGA_2
@ MCP3421_PGA_4
@ MCP3421_PGA_1
@ MCP3421_PGA_8
@ MCP3421_BIT_16
@ MCP3421_BIT_12
@ MCP3421_BIT_18
@ MCP3421_BIT_14
uint8_t mcp3421_interface_iic_deinit(void)
interface iic bus deinit
uint8_t mcp3421_interface_iic_init(void)
interface iic bus init
uint8_t mcp3421_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write command
uint8_t mcp3421_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read command
void mcp3421_interface_delay_ms(uint32_t ms)
interface delay ms
void mcp3421_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t mcp3421_read_test(uint32_t times)
read test
uint32_t driver_version
char manufacturer_name[32]