LibDriver MCP4725
Loading...
Searching...
No Matches
driver_mcp4725.c
Go to the documentation of this file.
1
37
38#include "driver_mcp4725.h"
39#include <math.h>
40
44#define CHIP_NAME "Microchip MCP4725"
45#define MANUFACTURER_NAME "Microchip"
46#define SUPPLY_VOLTAGE_MIN 2.7f
47#define SUPPLY_VOLTAGE_MAX 5.5f
48#define MAX_CURRENT 0.4f
49#define TEMPERATURE_MIN -40.0f
50#define TEMPERATURE_MAX 125.0f
51#define DRIVER_VERSION 2000
52
64{
65 if (handle == NULL) /* check handle */
66 {
67 return 2; /* return error */
68 }
69 if (handle->debug_print == NULL) /* check debug_print */
70 {
71 return 3; /* return error */
72 }
73 if (handle->iic_init == NULL) /* check iic_init */
74 {
75 handle->debug_print("mcp4725: iic_init is null.\n"); /* iic_init is null */
76
77 return 3; /* return error */
78 }
79 if (handle->iic_deinit == NULL) /* check iic_deinit */
80 {
81 handle->debug_print("mcp4725: iic_deinit is null.\n"); /* iic_deinit is null */
82
83 return 3; /* return error */
84 }
85 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
86 {
87 handle->debug_print("mcp4725: iic_read_cmd is null.\n"); /* iic_read is null */
88
89 return 3; /* return error */
90 }
91 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
92 {
93 handle->debug_print("mcp4725: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
94
95 return 3; /* return error */
96 }
97 if (handle->delay_ms == NULL) /* check delay_ms */
98 {
99 handle->debug_print("mcp4725: delay_ms is null.\n"); /* delay_ms is null */
100
101 return 3; /* return error */
102 }
103
104 if (handle->iic_init() != 0) /* iic init */
105 {
106 handle->debug_print("mcp4725: iic init failed.\n"); /* iic init failed */
107
108 return 1; /* return error */
109 }
110 handle->inited = 1; /* flag finish initialization */
111
112 return 0; /* success return 0 */
113}
114
126{
127 if (handle == NULL) /* check handle */
128 {
129 return 2; /* return error */
130 }
131 if (handle->inited != 1) /* check handle initialization */
132 {
133 return 3; /* return error */
134 }
135
136 if (handle->iic_deinit() != 0) /* iic deinit */
137 {
138 handle->debug_print("mcp4725: iic deinit failed.\n"); /* iic deinit failed */
139
140 return 1; /* return error */
141 }
142 handle->inited = 0; /* flag close */
143
144 return 0; /* success return 0 */
145}
146
157{
158 if (handle == NULL) /* check handle */
159 {
160 return 2; /* return error */
161 }
162
163 handle->iic_addr = (uint8_t)((0x60 | addr_pin) << 1); /* set iic address */
164
165 return 0; /* success return 0 */
166}
167
178{
179 if (handle == NULL) /* check handle */
180 {
181 return 2; /* return error */
182 }
183
184 if (((handle->iic_addr >> 1) & ~0x60) != 0)
185 {
186 *addr_pin = (mcp4725_address_t)(0x01); /* get iic address */
187 }
188 else
189 {
190 *addr_pin = (mcp4725_address_t)(0x00); /* get iic address */
191 }
192
193 return 0; /* success return 0 */
194}
195
207{
208 if (handle == NULL) /* check handle */
209 {
210 return 2; /* return error */
211 }
212 if (handle->inited != 1) /* check handle initialization */
213 {
214 return 3; /* return error */
215 }
216
217 handle->mode = (uint8_t)mode; /* set mode */
218
219 return 0; /* success return 0 */
220}
221
233{
234 if (handle == NULL) /* check handle */
235 {
236 return 2; /* return error */
237 }
238 if (handle->inited != 1) /* check handle initialization */
239 {
240 return 3; /* return error */
241 }
242
243 *mode = (mcp4725_mode_t)(handle->mode); /* get mode */
244
245 return 0; /* success return 0 */
246}
247
259{
260 if (handle == NULL) /* check handle */
261 {
262 return 2; /* return error */
263 }
264 if (handle->inited != 1) /* check handle initialization */
265 {
266 return 3; /* return error */
267 }
268
269 handle->power_mode = (uint8_t)mode; /* set power mode */
270
271 return 0; /* success return 0 */
272}
273
285{
286 if (handle == NULL) /* check handle */
287 {
288 return 2; /* return error */
289 }
290 if (handle->inited != 1) /* check handle initialization */
291 {
292 return 3; /* return error */
293 }
294
295 *mode = (mcp4725_power_down_mode_t)(handle->power_mode); /* get power mode */
296
297 return 0; /* success return 0 */
298}
299
310uint8_t mcp4725_set_reference_voltage(mcp4725_handle_t *handle, float ref_voltage)
311{
312 if (handle == NULL) /* check handle */
313 {
314 return 2; /* return error */
315 }
316 if (handle->inited != 1) /* check handle initialization */
317 {
318 return 3; /* return error */
319 }
320
321 handle->ref_voltage = ref_voltage; /* set reference voltage */
322
323 return 0; /* success return 0 */
324}
325
336uint8_t mcp4725_get_reference_voltage(mcp4725_handle_t *handle, float *ref_voltage)
337{
338 if (handle == NULL) /* check handle */
339 {
340 return 2; /* return error */
341 }
342 if (handle->inited != 1) /* check handle initialization */
343 {
344 return 3; /* return error */
345 }
346
347 *ref_voltage = handle->ref_voltage; /* get reference voltage */
348
349 return 0; /* success return 0 */
350}
351
363uint8_t mcp4725_read(mcp4725_handle_t *handle, uint16_t *value)
364{
365 uint8_t buf[5];
366
367 if (handle == NULL) /* check handle */
368 {
369 return 2; /* return error */
370 }
371 if (handle->inited != 1) /* check handle initialization */
372 {
373 return 3; /* return error */
374 }
375
376 if (handle->iic_read_cmd(handle->iic_addr, (uint8_t *)buf, 5) != 0) /* read data */
377 {
378 handle->debug_print("mcp4725: read failed.\n"); /* read data failed */
379
380 return 1; /* return error */
381 }
382 if (handle->mode == MCP4725_MODE_DAC) /* if use dac mode */
383 {
384 *value = (uint16_t)(((uint16_t)buf[1]) << 8 | buf[2]); /* get value */
385 *value = (*value) >> 4; /* right shift 4 */
386
387 return 0; /* success return 0 */
388 }
389 else if (handle->mode == MCP4725_MODE_EEPROM) /* if use eeprom mode */
390 {
391 *value = (uint16_t)((uint16_t)(buf[3] & 0x0F) << 8 | buf[4]); /* get value */
392
393 return 0; /* success return 0 */
394 }
395 else
396 {
397 handle->debug_print("mcp4725: mode is invalid.\n"); /* mode is invalid */
398
399 return 1; /* return error */
400 }
401}
402
414uint8_t mcp4725_write(mcp4725_handle_t *handle, uint16_t value)
415{
416 uint8_t buf[6];
417
418 if (handle == NULL) /* check handle */
419 {
420 return 2; /* return error */
421 }
422 if (handle->inited != 1) /* check handle initialization */
423 {
424 return 3; /* return error */
425 }
426
427 value = value & 0xFFF; /* get valid part */
428 if (handle->mode == MCP4725_MODE_DAC) /* dac mode */
429 {
430 buf[0] = (uint8_t)((value >> 8) & 0xFF); /* set msb */
431 buf[0] = (uint8_t)(buf[0] | (handle->power_mode << 4)); /* set power mode */
432 buf[1] = (uint8_t)(value & 0xFF); /* set lsb */
433 buf[2] = (uint8_t)((value >> 8) & 0xFF); /* set msb */
434 buf[2] = (uint8_t)(buf[2] | (handle->power_mode << 4)); /* set power mode */
435 buf[3] = (uint8_t)(value & 0xFF); /* set lsb */
436
437 return handle->iic_write_cmd(handle->iic_addr, (uint8_t *)buf, 4); /* write command */
438 }
439 else if (handle->mode == MCP4725_MODE_EEPROM) /* eeprom mode */
440 {
441 buf[0] = (uint8_t)(0x03 << 5); /* set mode */
442 buf[0] = (uint8_t)(buf[0] | (handle->power_mode << 1)); /* set power mode */
443 value = value << 4; /* right shift 4 */
444 buf[1] = (uint8_t)((value >> 8) & 0xFF); /* set msb */
445 buf[2] = (uint8_t)(value & 0xFF); /* set lsb */
446 buf[3] = (uint8_t)(0x03 << 5); /* set mode */
447 buf[3] = (uint8_t)(buf[3] | (handle->power_mode << 1)); /* set power mode */
448 value = value << 4; /* right shift 4 */
449 buf[4] = (uint8_t)((value >> 8) & 0xFF); /* set msb */
450 buf[5] = (uint8_t)(value & 0xFF); /* set lsb */
451
452 return handle->iic_write_cmd(handle->iic_addr, (uint8_t *)buf, 6); /* write command */
453 }
454 else
455 {
456 handle->debug_print("mcp4725: mode is invalid.\n"); /* mode is invalid */
457
458 return 1; /* return error */
459 }
460}
461
474uint8_t mcp4725_convert_to_register(mcp4725_handle_t *handle, float s, uint16_t *reg)
475{
476 if (handle == NULL) /* check handle */
477 {
478 return 2; /* return error */
479 }
480 if (handle->inited != 1) /* check handle initialization */
481 {
482 return 3; /* return error */
483 }
484
485 if (fabsf(handle->ref_voltage - 0.0f) < 1e-6f) /* check reference voltage */
486 {
487 handle->debug_print("mcp4725: reference voltage can't be zero.\n"); /* reference voltage can't be zero */
488
489 return 1; /* return error */
490 }
491 *reg = (uint16_t)(s * 4096.0000000f / handle->ref_voltage); /* calculate register */
492
493 return 0; /* success return 0 */
494}
495
508uint8_t mcp4725_convert_to_data(mcp4725_handle_t *handle, uint16_t reg, float *s)
509{
510 if (handle == NULL) /* check handle */
511 {
512 return 2; /* return error */
513 }
514 if (handle->inited != 1) /* check handle initialization */
515 {
516 return 3; /* return error */
517 }
518
519 *s = (float)(reg) * handle->ref_voltage / 4096.00000000f; /* calculate data */
520
521 return 0; /* success return 0 */
522}
523
536uint8_t mcp4725_set_reg(mcp4725_handle_t *handle, uint8_t *buf, uint16_t len)
537{
538 if (handle == NULL) /* check handle */
539 {
540 return 2; /* return error */
541 }
542 if (handle->inited != 1) /* check handle initialization */
543 {
544 return 3; /* return error */
545 }
546
547 return handle->iic_write_cmd(handle->iic_addr, buf, len); /* write data */
548}
549
562uint8_t mcp4725_get_reg(mcp4725_handle_t *handle, uint8_t *buf, uint16_t len)
563{
564 if (handle == NULL) /* check handle */
565 {
566 return 2; /* return error */
567 }
568 if (handle->inited != 1) /* check handle initialization */
569 {
570 return 3; /* return error */
571 }
572
573 return handle->iic_read_cmd(handle->iic_addr, buf, len); /* read data */
574}
575
585{
586 if (info == NULL) /* check handle */
587 {
588 return 2; /* return error */
589 }
590
591 memset(info, 0, sizeof(mcp4725_info_t)); /* initialize mcp4725 info structure */
592 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
593 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
594 strncpy(info->interface, "IIC", 8); /* copy interface name */
595 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
596 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
597 info->max_current_ma = MAX_CURRENT; /* set maximum current */
598 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
599 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
600 info->driver_version = DRIVER_VERSION; /* set driver version */
601
602 return 0; /* success return 0 */
603}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver mcp4725 header file
struct mcp4725_handle_s mcp4725_handle_t
mcp4725 handle structure definition
uint8_t mcp4725_get_reference_voltage(mcp4725_handle_t *handle, float *ref_voltage)
get the chip reference voltage
mcp4725_power_down_mode_t
mcp4725 power down enumeration definition
uint8_t mcp4725_convert_to_data(mcp4725_handle_t *handle, uint16_t reg, float *s)
convert the register data to the dac value
uint8_t mcp4725_deinit(mcp4725_handle_t *handle)
close the chip
uint8_t mcp4725_info(mcp4725_info_t *info)
get chip's information
uint8_t mcp4725_read(mcp4725_handle_t *handle, uint16_t *value)
read the dac value
uint8_t mcp4725_set_power_mode(mcp4725_handle_t *handle, mcp4725_power_down_mode_t mode)
set the chip power mode
uint8_t mcp4725_get_power_mode(mcp4725_handle_t *handle, mcp4725_power_down_mode_t *mode)
get the chip power mode
mcp4725_address_t
mcp4725 address enumeration definition
struct mcp4725_info_s mcp4725_info_t
mcp4725 information structure definition
uint8_t mcp4725_write(mcp4725_handle_t *handle, uint16_t value)
write the dac value
uint8_t mcp4725_convert_to_register(mcp4725_handle_t *handle, float s, uint16_t *reg)
convert the dac value to the register data
uint8_t mcp4725_get_addr_pin(mcp4725_handle_t *handle, mcp4725_address_t *addr_pin)
get the chip address pin
uint8_t mcp4725_init(mcp4725_handle_t *handle)
initialize the chip
uint8_t mcp4725_set_reference_voltage(mcp4725_handle_t *handle, float ref_voltage)
set the chip reference voltage
uint8_t mcp4725_set_mode(mcp4725_handle_t *handle, mcp4725_mode_t mode)
set the chip mode
mcp4725_mode_t
mcp4725 mode enumeration definition
uint8_t mcp4725_get_mode(mcp4725_handle_t *handle, mcp4725_mode_t *mode)
get the chip mode
uint8_t mcp4725_set_addr_pin(mcp4725_handle_t *handle, mcp4725_address_t addr_pin)
set the chip address pin
@ MCP4725_MODE_DAC
@ MCP4725_MODE_EEPROM
uint8_t mcp4725_set_reg(mcp4725_handle_t *handle, uint8_t *buf, uint16_t len)
set the chip register
uint8_t mcp4725_get_reg(mcp4725_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_read_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint32_t driver_version
char manufacturer_name[32]