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 conf = handle->
conf | 0x40;
492 handle->
debug_print(
"pcf8591: write command failed.\n");
522 *reg = (uint8_t)(dac / handle->
ref_voltage * 255.0f);
557 if ((handle->
conf & (1 << 2)) != 0)
559 handle->
debug_print(
"pcf8591: can't use this function.\n");
563 mode = (handle->
conf >> 4) & 0x03;
564 channel = handle->
conf & 0x03;
565 if ((mode == 1) || (mode == 2))
569 handle->
debug_print(
"pcf8591: channel is invalid.\n");
578 handle->
debug_print(
"pcf8591: channel is invalid.\n");
586 handle->
debug_print(
"pcf8591: read command failed.\n");
597 *raw = (int16_t)(u_data);
598 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
601 else if (channel == 1)
604 *raw = (int16_t)(u_data);
605 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
608 else if (channel == 2)
611 *raw = (int16_t)(u_data);
612 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
618 *raw = (int16_t)(u_data);
619 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
629 s_data = (int8_t)(buf[0]);
630 *raw = (int16_t)(s_data);
631 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
634 else if (channel == 1)
636 s_data = (int8_t)(buf[0]);
637 *raw = (int16_t)(s_data);
638 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
641 else if (channel == 2)
643 s_data = (int8_t)(buf[0]);
644 *raw = (int16_t)(s_data);
645 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
650 handle->
debug_print(
"pcf8591: channel is invalid.\n");
661 *raw = (int16_t)(u_data);
662 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
665 else if (channel == 1)
668 *raw = (int16_t)(u_data);
669 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
672 else if (channel == 2)
674 s_data = (int8_t)(buf[0]);
675 *raw = (int16_t)(s_data);
676 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
681 handle->
debug_print(
"pcf8591: channel is invalid.\n");
691 s_data = (int8_t)(buf[0]);
692 *raw = (int16_t)(s_data);
693 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
696 else if (channel == 1)
698 s_data = (int8_t)(buf[0]);
699 *raw = (int16_t)(s_data);
700 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
705 handle->
debug_print(
"pcf8591: channel is invalid.\n");
713 handle->
debug_print(
"pcf8591: mode is invalid.\n");
754 if ((handle->
conf & (1 << 2)) == 0)
756 handle->
debug_print(
"pcf8591: can't use this function.\n");
760 mode = (handle->
conf >> 4) & 0x03;
764 i = i<(*len) ? i : (*len);
769 i = i<(*len) ? i : (*len);
774 i = i<(*len) ? i : (*len);
779 i = i<(*len) ? i : (*len);
782 memset(buf, 0,
sizeof(uint8_t) * 4);
783 for (j = 0; j < i; j++)
788 handle->
debug_print(
"pcf8591: read command failed.\n");
799 *raw = (int16_t)(u_data);
800 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
804 *raw = (int16_t)(u_data);
805 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
809 *raw = (int16_t)(u_data);
810 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
814 *raw = (int16_t)(u_data);
815 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
823 s_data = (int8_t)(buf[1]);
824 *raw = (int16_t)(s_data);
825 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
828 s_data = (int8_t)(buf[2]);
829 *raw = (int16_t)(s_data);
830 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
833 s_data = (int8_t)(buf[0]);
834 *raw = (int16_t)(s_data);
835 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
844 *raw = (int16_t)(u_data);
845 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
849 *raw = (int16_t)(u_data);
850 *adc = (float)(*raw) / 255.0f * handle->
ref_voltage;
853 s_data = (int8_t)(buf[0]);
854 *raw = (int16_t)(s_data);
855 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
863 s_data = (int8_t)(buf[1]);
864 *raw = (int16_t)(s_data);
865 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
868 s_data = (int8_t)(buf[0]);
869 *raw = (int16_t)(s_data);
870 *adc = (float)(*raw) / 128.0f * handle->
ref_voltage;
878 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