43#define CHIP_NAME "NXP PCF8591"
44#define MANUFACTURER_NAME "NXP"
45#define SUPPLY_VOLTAGE_MIN 2.5f
46#define SUPPLY_VOLTAGE_MAX 6.0f
47#define MAX_CURRENT 50.0f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
120 prev_conf = handle->
conf;
121 handle->
conf &= ~(3 << 0);
122 handle->
conf |= channel << 0;
126 handle->
debug_print(
"pcf8591: write command failed.\n");
127 handle->
conf = prev_conf;
186 prev_conf = handle->
conf;
187 handle->
conf &= ~(3 << 4);
188 handle->
conf |= mode << 4;
192 handle->
debug_print(
"pcf8591: write command failed.\n");
193 handle->
conf = prev_conf;
252 prev_conf = handle->
conf;
253 handle->
conf &= ~(1 << 2);
254 handle->
conf |= enable << 2;
257 handle->
conf |= 0x40;
261 handle->
conf &= ~0x40;
266 handle->
debug_print(
"pcf8591: write command failed.\n");
267 handle->
conf = prev_conf;
375 handle->
debug_print(
"pcf8591: iic_init is null.\n");
381 handle->
debug_print(
"pcf8591: iic_deinit is null.\n");
387 handle->
debug_print(
"pcf8591: iic_read_cmd is null.\n");
393 handle->
debug_print(
"pcf8591: iic_write_cmd is null.\n");
399 handle->
debug_print(
"pcf8591: delay_ms is null.\n");
406 handle->
debug_print(
"pcf8591: iic init failed.\n");
444 handle->
debug_print(
"pcf8591: write command failed.\n");
451 handle->
debug_print(
"pcf8591: iic deinit failed.\n");
486 buf[0] = handle->
conf;
491 handle->
debug_print(
"pcf8591: write command failed.\n");
521 *reg = (uint8_t)(dac / handle->
ref_voltage * 256.0f);
556 if ((handle->
conf & (1 << 2)) != 0)
558 handle->
debug_print(
"pcf8591: can't use this function.\n");
562 mode = (handle->
conf >> 4) & 0x03;
563 channel = handle->
conf & 0x03;
564 if ((mode == 1) || (mode == 2))
568 handle->
debug_print(
"pcf8591: channel is invalid.\n");
577 handle->
debug_print(
"pcf8591: channel is invalid.\n");
585 handle->
debug_print(
"pcf8591: read command failed.\n");
596 *raw = (int16_t)(u_data);
597 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
600 else if (channel == 1)
603 *raw = (int16_t)(u_data);
604 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
607 else if (channel == 2)
610 *raw = (int16_t)(u_data);
611 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
617 *raw = (int16_t)(u_data);
618 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
628 s_data = (int8_t)(buf[1]);
629 *raw = (int16_t)(s_data);
630 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
633 else if (channel == 1)
635 s_data = (int8_t)(buf[1]);
636 *raw = (int16_t)(s_data);
637 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
640 else if (channel == 2)
642 s_data = (int8_t)(buf[1]);
643 *raw = (int16_t)(s_data);
644 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
649 handle->
debug_print(
"pcf8591: channel is invalid.\n");
660 *raw = (int16_t)(u_data);
661 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
664 else if (channel == 1)
667 *raw = (int16_t)(u_data);
668 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
671 else if (channel == 2)
673 s_data = (int8_t)(buf[1]);
674 *raw = (int16_t)(s_data);
675 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
680 handle->
debug_print(
"pcf8591: channel is invalid.\n");
690 s_data = (int8_t)(buf[1]);
691 *raw = (int16_t)(s_data);
692 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
695 else if (channel == 1)
697 s_data = (int8_t)(buf[1]);
698 *raw = (int16_t)(s_data);
699 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
704 handle->
debug_print(
"pcf8591: channel is invalid.\n");
712 handle->
debug_print(
"pcf8591: mode is invalid.\n");
753 if ((handle->
conf & (1 << 2)) == 0)
755 handle->
debug_print(
"pcf8591: can't use this function.\n");
759 mode = (handle->
conf >> 4) & 0x03;
763 i = i<(*len) ? i : (*len);
768 i = i<(*len) ? i : (*len);
773 i = i<(*len) ? i : (*len);
778 i = i<(*len) ? i : (*len);
781 memset(buf, 0,
sizeof(uint8_t) * 5);
785 handle->
debug_print(
"pcf8591: read command failed.\n");
794 *raw = (int16_t)(u_data);
795 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
799 *raw = (int16_t)(u_data);
800 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
804 *raw = (int16_t)(u_data);
805 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
809 *raw = (int16_t)(u_data);
810 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
818 s_data = (int8_t)(buf[1]);
819 *raw = (int16_t)(s_data);
820 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
823 s_data = (int8_t)(buf[2]);
824 *raw = (int16_t)(s_data);
825 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
828 s_data = (int8_t)(buf[3]);
829 *raw = (int16_t)(s_data);
830 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
839 *raw = (int16_t)(u_data);
840 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
844 *raw = (int16_t)(u_data);
845 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
848 s_data = (int8_t)(buf[3]);
849 *raw = (int16_t)(s_data);
850 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
858 s_data = (int8_t)(buf[1]);
859 *raw = (int16_t)(s_data);
860 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
863 s_data = (int8_t)(buf[2]);
864 *raw = (int16_t)(s_data);
865 *adc = (float)(*raw) / 256.0f * handle->
ref_voltage;
873 handle->
debug_print(
"pcf8591: mode is invalid.\n");
#define SUPPLY_VOLTAGE_MAX
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
driver pcf8591 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_get_channel(pcf8591_handle_t *handle, pcf8591_channel_t *channel)
get the adc channel
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
pcf8591_mode_t
pcf8591 mode definition
uint8_t pcf8591_get_auto_increment(pcf8591_handle_t *handle, pcf8591_bool_t *enable)
get the adc auto increment read mode
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
pcf8591_bool_t
pcf8591 bool enumeration definition
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
uint8_t pcf8591_get_addr_pin(pcf8591_handle_t *handle, pcf8591_address_t *addr_pin)
get the address pin
pcf8591_address_t
pcf8591 address enumeration definition
uint8_t pcf8591_get_mode(pcf8591_handle_t *handle, pcf8591_mode_t *mode)
get the adc mode
uint8_t pcf8591_get_reference_voltage(pcf8591_handle_t *handle, float *ref_voltage)
get the adc reference voltage
uint8_t pcf8591_deinit(pcf8591_handle_t *handle)
close the chip
struct pcf8591_handle_s pcf8591_handle_t
pcf8591 handle structure definition
pcf8591_channel_t
pcf8591 channel definition
uint8_t pcf8591_set_auto_increment(pcf8591_handle_t *handle, pcf8591_bool_t enable)
set the adc auto increment read mode
uint8_t pcf8591_get_reg(pcf8591_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
uint8_t pcf8591_set_reg(pcf8591_handle_t *handle, uint8_t *buf, uint16_t len)
set 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)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v