LibDriver PCF8591
Loading...
Searching...
No Matches
driver_pcf8591_read_write_test.c
Go to the documentation of this file.
1
37
39#include <stdlib.h>
40
41static pcf8591_handle_t gs_handle;
42
52uint8_t pcf8591_read_write_test(pcf8591_address_t addr, uint32_t times)
53{
54 uint8_t res;
55 uint8_t reg;
56 uint32_t i, j;
57 int16_t raws[4];
58 float adcs[4];
59 int16_t raw;
60 float adc;
61 uint8_t len;
62 pcf8591_info_t info;
63
64 /* link functions */
72
73 /* pcf8591 info */
74 res = pcf8591_info(&info);
75 if (res != 0)
76 {
77 pcf8591_interface_debug_print("pcf8591: get info failed.\n");
78
79 return 1;
80 }
81 else
82 {
83 /* print chip information */
84 pcf8591_interface_debug_print("pcf8591: chip is %s.\n", info.chip_name);
85 pcf8591_interface_debug_print("pcf8591: manufacturer is %s.\n", info.manufacturer_name);
86 pcf8591_interface_debug_print("pcf8591: interface is %s.\n", info.interface);
87 pcf8591_interface_debug_print("pcf8591: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
88 pcf8591_interface_debug_print("pcf8591: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
89 pcf8591_interface_debug_print("pcf8591: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
90 pcf8591_interface_debug_print("pcf8591: max current is %0.2fmA.\n", info.max_current_ma);
91 pcf8591_interface_debug_print("pcf8591: max temperature is %0.1fC.\n", info.temperature_max);
92 pcf8591_interface_debug_print("pcf8591: min temperature is %0.1fC.\n", info.temperature_min);
93 }
94
95 /* set addr pin */
96 res = pcf8591_set_addr_pin(&gs_handle, addr);
97 if (res != 0)
98 {
99 pcf8591_interface_debug_print("pcf8591: set addr pin failed.\n");
100
101 return 1;
102 }
103
104 /* pcf8591 init */
105 res = pcf8591_init(&gs_handle);
106 if (res != 0)
107 {
108 pcf8591_interface_debug_print("pcf8591: init failed.\n");
109
110 return 1;
111 }
112
113 /* start read test */
114 pcf8591_interface_debug_print("pcf8591: start read write test.\n");
115
116 /* enable auto increment */
118 if (res != 0)
119 {
120 pcf8591_interface_debug_print("pcf8591: set auto increment failed.\n");
121 (void)pcf8591_deinit(&gs_handle);
122
123 return 1;
124 }
125
126 /* set default reference voltage */
127 res = pcf8591_set_reference_voltage(&gs_handle, 3.3f);
128 if (res != 0)
129 {
130 pcf8591_interface_debug_print("pcf8591: set reference voltage failed.\n");
131 (void)pcf8591_deinit(&gs_handle);
132
133 return 1;
134 }
135
136 /* mode test */
137 pcf8591_interface_debug_print("pcf8591: mode test.\n");
138
139 /* set mode AIN0123_GND */
140 pcf8591_interface_debug_print("pcf8591: set mode AIN0123_GND.\n");
142 if (res != 0)
143 {
144 pcf8591_interface_debug_print("pcf8591: set mode failed.\n");
145 (void)pcf8591_deinit(&gs_handle);
146
147 return 1;
148 }
150 for (i=0; i<times; i++)
151 {
152 len = 4;
153
154 /* read data */
155 res = pcf8591_multiple_read(&gs_handle, (int16_t *)raws, (float *)adcs, (uint8_t *)&len);
156 if (res != 0)
157 {
158 pcf8591_interface_debug_print("pcf8591: multiple read failed.\n");
159 (void)pcf8591_deinit(&gs_handle);
160
161 return 1;
162 }
163 for (j=0; j<len; j++)
164 {
165 pcf8591_interface_debug_print("pcf8591: raw is %d.\n", raws[j]);
166 pcf8591_interface_debug_print("pcf8591: adc is %0.3f.\n", adcs[j]);
167 }
169 }
170
171 /* set mode AIN012_AIN3 */
172 pcf8591_interface_debug_print("pcf8591: set mode AIN012_AIN3.\n");
174 if (res != 0)
175 {
176 pcf8591_interface_debug_print("pcf8591: set mode failed.\n");
177 (void)pcf8591_deinit(&gs_handle);
178
179 return 1;
180 }
182 for (i=0; i<times; i++)
183 {
184 len = 4;
185
186 /* read data */
187 res = pcf8591_multiple_read(&gs_handle, (int16_t *)raws, (float *)adcs, (uint8_t *)&len);
188 if (res != 0)
189 {
190 pcf8591_interface_debug_print("pcf8591: multiple read failed.\n");
191 (void)pcf8591_deinit(&gs_handle);
192
193 return 1;
194 }
195 for (j=0; j<len; j++)
196 {
197 pcf8591_interface_debug_print("pcf8591: raw is %d.\n", raws[j]);
198 pcf8591_interface_debug_print("pcf8591: adc is %0.3f.\n", adcs[j]);
199 }
201 }
202
203 /* set mode AIN0_GND_AND_AIN1_GND_AND_AIN2_AIN */
204 pcf8591_interface_debug_print("pcf8591: set mode AIN0_GND_AND_AIN1_GND_AND_AIN2_AIN.\n");
206 if (res != 0)
207 {
208 pcf8591_interface_debug_print("pcf8591: set mode failed.\n");
209 (void)pcf8591_deinit(&gs_handle);
210
211 return 1;
212 }
214 for (i=0; i<times; i++)
215 {
216 len = 4;
217
218 /* read data */
219 res = pcf8591_multiple_read(&gs_handle, (int16_t *)raws, (float *)adcs, (uint8_t *)&len);
220 if (res != 0)
221 {
222 pcf8591_interface_debug_print("pcf8591: multiple read failed.\n");
223 (void)pcf8591_deinit(&gs_handle);
224
225 return 1;
226 }
227 for (j=0; j<len; j++)
228 {
229 pcf8591_interface_debug_print("pcf8591: raw is %d.\n", raws[j]);
230 pcf8591_interface_debug_print("pcf8591: adc is %0.3f.\n", adcs[j]);
231 }
233 }
234
235 /* set mode AIN0_AIN1_AND_ANI2_AIN3 */
236 pcf8591_interface_debug_print("pcf8591: set mode AIN0_AIN1_AND_ANI2_AIN3.\n");
238 if (res != 0)
239 {
240 pcf8591_interface_debug_print("pcf8591: set mode failed.\n");
241 (void)pcf8591_deinit(&gs_handle);
242
243 return 1;
244 }
246 for (i=0; i<times; i++)
247 {
248 len = 4;
249
250 /* read data */
251 res = pcf8591_multiple_read(&gs_handle, (int16_t *)raws, (float *)adcs, (uint8_t *)&len);
252 if (res != 0)
253 {
254 pcf8591_interface_debug_print("pcf8591: multiple read failed.\n");
255 (void)pcf8591_deinit(&gs_handle);
256
257 return 1;
258 }
259 for (j=0; j<len; j++)
260 {
261 pcf8591_interface_debug_print("pcf8591: raw is %d.\n", raws[j]);
262 pcf8591_interface_debug_print("pcf8591: adc is %0.3f.\n", adcs[j]);
263 }
265 }
266
267 /* channel test */
268 pcf8591_interface_debug_print("pcf8591: channel test.\n");
269
270 /* disable auto increment */
272 if (res != 0)
273 {
274 pcf8591_interface_debug_print("pcf8591: set auto increment failed.\n");
275 (void)pcf8591_deinit(&gs_handle);
276
277 return 1;
278 }
279
280 /* set mode AIN0123_GND */
282 if (res != 0)
283 {
284 pcf8591_interface_debug_print("pcf8591: set mode failed.\n");
285 (void)pcf8591_deinit(&gs_handle);
286
287 return 1;
288 }
289
290 /* set channel 0 */
291 pcf8591_interface_debug_print("pcf8591: set channel 0.\n");
292 res = pcf8591_set_channel(&gs_handle, PCF8591_CHANNEL_0);
293 if (res != 0)
294 {
295 pcf8591_interface_debug_print("pcf8591: set channel failed.\n");
296 (void)pcf8591_deinit(&gs_handle);
297
298 return 1;
299 }
301 for (i=0; i<times; i++)
302 {
303 /* read data */
304 res = pcf8591_read(&gs_handle, (int16_t *)&raw, (float *)&adc);
305 if (res != 0)
306 {
307 pcf8591_interface_debug_print("pcf8591: read failed.\n");
308 (void)pcf8591_deinit(&gs_handle);
309
310 return 1;
311 }
312 pcf8591_interface_debug_print("pcf8591: raw is %d.\n", raw);
313 pcf8591_interface_debug_print("pcf8591: adc is %0.3f.\n", adc);
315 }
316
317 /* set channel 1 */
318 pcf8591_interface_debug_print("pcf8591: set channel 1.\n");
319 res = pcf8591_set_channel(&gs_handle, PCF8591_CHANNEL_1);
320 if (res != 0)
321 {
322 pcf8591_interface_debug_print("pcf8591: set channel failed.\n");
323 (void)pcf8591_deinit(&gs_handle);
324
325 return 1;
326 }
328 for (i=0; i<times; i++)
329 {
330 /* read data */
331 res = pcf8591_read(&gs_handle, (int16_t *)&raw, (float *)&adc);
332 if (res != 0)
333 {
334 pcf8591_interface_debug_print("pcf8591: read failed.\n");
335 (void)pcf8591_deinit(&gs_handle);
336
337 return 1;
338 }
339 pcf8591_interface_debug_print("pcf8591: raw is %d.\n", raw);
340 pcf8591_interface_debug_print("pcf8591: adc is %0.3f.\n", adc);
342 }
343
344 /* set channel 2 */
345 pcf8591_interface_debug_print("pcf8591: set channel 2.\n");
346 res = pcf8591_set_channel(&gs_handle, PCF8591_CHANNEL_2);
347 if (res != 0)
348 {
349 pcf8591_interface_debug_print("pcf8591: set channel failed.\n");
350 (void)pcf8591_deinit(&gs_handle);
351
352 return 1;
353 }
355 for (i=0; i<times; i++)
356 {
357 /* read data */
358 res = pcf8591_read(&gs_handle, (int16_t *)&raw, (float *)&adc);
359 if (res != 0)
360 {
361 pcf8591_interface_debug_print("pcf8591: read failed.\n");
362 (void)pcf8591_deinit(&gs_handle);
363
364 return 1;
365 }
366 pcf8591_interface_debug_print("pcf8591: raw is %d.\n", raw);
367 pcf8591_interface_debug_print("pcf8591: adc is %0.3f.\n", adc);
369 }
370
371 /* set channel 3 */
372 pcf8591_interface_debug_print("pcf8591: set channel 3.\n");
373 res = pcf8591_set_channel(&gs_handle, PCF8591_CHANNEL_3);
374 if (res != 0)
375 {
376 pcf8591_interface_debug_print("pcf8591: set channel failed.\n");
377 (void)pcf8591_deinit(&gs_handle);
378
379 return 1;
380 }
381
382 for (i=0; i<times; i++)
383 {
384 /* read data */
385 res = pcf8591_read(&gs_handle, (int16_t *)&raw, (float *)&adc);
386 if (res != 0)
387 {
388 pcf8591_interface_debug_print("pcf8591: read failed.\n");
389 (void)pcf8591_deinit(&gs_handle);
390
391 return 1;
392 }
393 pcf8591_interface_debug_print("pcf8591: raw is %d.\n", raw);
394 pcf8591_interface_debug_print("pcf8591: adc is %0.3f.\n", adc);
396 }
397
398 /* write test */
399 pcf8591_interface_debug_print("pcf8591: write test.\n");
400 for (i=0; i<times; i++)
401 {
402 adc = (float)(rand() % 3300) / 1000.0f;
403 res = pcf8591_dac_convert_to_register(&gs_handle, adc, (uint8_t *)&reg);
404 if (res != 0)
405 {
406 pcf8591_interface_debug_print("pcf8591: dac convert to register failed.\n");
407
408 return 1;
409 }
410 res = pcf8591_write(&gs_handle, reg);
411 if (res != 0)
412 {
413 pcf8591_interface_debug_print("pcf8591: write failed.\n");
414
415 return 1;
416 }
417 pcf8591_interface_debug_print("pcf8591: adc is %0.3f.\n", adc);
418 pcf8591_interface_debug_print("pcf8591: reg is 0x%02X.\n", reg);
420 }
421
422 /* finish read test */
423 pcf8591_interface_debug_print("pcf8591: finish read test.\n");
424 (void)pcf8591_deinit(&gs_handle);
425
426 return 0;
427}
driver pcf8591 read write test header file
uint8_t pcf8591_dac_convert_to_register(pcf8591_handle_t *handle, float dac, uint8_t *reg)
convert a dac value to a register raw data
struct pcf8591_info_s pcf8591_info_t
pcf8591 information structure definition
uint8_t pcf8591_read(pcf8591_handle_t *handle, int16_t *raw, float *adc)
read data from the chip
uint8_t pcf8591_info(pcf8591_info_t *info)
get chip's information
uint8_t pcf8591_set_addr_pin(pcf8591_handle_t *handle, pcf8591_address_t addr_pin)
set the address pin
uint8_t pcf8591_write(pcf8591_handle_t *handle, uint8_t data)
write to the dac
uint8_t pcf8591_init(pcf8591_handle_t *handle)
initialize the chip
uint8_t pcf8591_multiple_read(pcf8591_handle_t *handle, int16_t *raw, float *adc, uint8_t *len)
read the multiple channel data from the chip
uint8_t pcf8591_set_mode(pcf8591_handle_t *handle, pcf8591_mode_t mode)
set the adc mode
uint8_t pcf8591_set_reference_voltage(pcf8591_handle_t *handle, float ref_voltage)
set the adc reference voltage
uint8_t pcf8591_set_channel(pcf8591_handle_t *handle, pcf8591_channel_t channel)
set the adc channel
pcf8591_address_t
pcf8591 address enumeration definition
uint8_t pcf8591_deinit(pcf8591_handle_t *handle)
close the chip
struct pcf8591_handle_s pcf8591_handle_t
pcf8591 handle structure definition
uint8_t pcf8591_set_auto_increment(pcf8591_handle_t *handle, pcf8591_bool_t enable)
set the adc auto increment read mode
@ PCF8591_MODE_AIN0_AIN1_AND_ANI2_AIN3
@ PCF8591_MODE_AIN012_AIN3
@ PCF8591_MODE_AIN0123_GND
@ PCF8591_MODE_AIN0_GND_AND_AIN1_GND_AND_AIN2_AIN3
@ PCF8591_BOOL_FALSE
@ PCF8591_BOOL_TRUE
@ PCF8591_CHANNEL_3
@ PCF8591_CHANNEL_1
@ PCF8591_CHANNEL_0
@ PCF8591_CHANNEL_2
uint8_t pcf8591_interface_iic_init(void)
interface iic bus init
void pcf8591_interface_delay_ms(uint32_t ms)
interface delay ms
void pcf8591_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t pcf8591_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write command
uint8_t pcf8591_interface_iic_deinit(void)
interface iic bus deinit
uint8_t pcf8591_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read command
uint8_t pcf8591_read_write_test(pcf8591_address_t addr, uint32_t times)
read write test
uint32_t driver_version
char manufacturer_name[32]