LibDriver PCF8591
Loading...
Searching...
No Matches
driver_pcf8591_basic.c
Go to the documentation of this file.
1
37
39
40static pcf8591_handle_t gs_handle;
41
52{
53 uint8_t res;
54
55 /* link functions */
63
64 /* set addr pin */
65 res = pcf8591_set_addr_pin(&gs_handle, addr);
66 if (res != 0)
67 {
68 pcf8591_interface_debug_print("pcf8591: set addr pin failed.\n");
69
70 return 1;
71 }
72
73 /* pcf8591 init */
74 res = pcf8591_init(&gs_handle);
75 if (res != 0)
76 {
77 pcf8591_interface_debug_print("pcf8591: init failed.\n");
78
79 return 1;
80 }
81
82 /* set mode */
83 res = pcf8591_set_mode(&gs_handle, mode);
84 if (res != 0)
85 {
86 pcf8591_interface_debug_print("pcf8591: set mode failed.\n");
87 (void)pcf8591_deinit(&gs_handle);
88
89 return 1;
90 }
91
92 /* disable auto increment */
94 if (res != 0)
95 {
96 pcf8591_interface_debug_print("pcf8591: set auto increment failed.\n");
97 (void)pcf8591_deinit(&gs_handle);
98
99 return 1;
100 }
101
102 /* set default reference voltage */
104 if (res != 0)
105 {
106 pcf8591_interface_debug_print("pcf8591: set reference voltage failed.\n");
107 (void)pcf8591_deinit(&gs_handle);
108
109 return 1;
110 }
111
112 return 0;
113}
114
123{
124 /* close pcf8591 */
125 if (pcf8591_deinit(&gs_handle) != 0)
126 {
127 return 1;
128 }
129 else
130 {
131 return 0;
132 }
133}
134
144{
145 uint8_t res;
146
147 /* set channel */
148 res = pcf8591_set_channel(&gs_handle, channel);
149 if (res != 0)
150 {
151 return 1;
152 }
153
154 return 0;
155}
156
166uint8_t pcf8591_basic_read(int16_t *raw, float *adc)
167{
168 /* read data */
169 if (pcf8591_read(&gs_handle, raw, adc) != 0)
170 {
171 return 1;
172 }
173 else
174 {
175 return 0;
176 }
177}
178
187uint8_t pcf8591_basic_write(float dac)
188{
189 uint8_t res;
190 uint8_t data;
191
192 /* convert to register data */
193 res = pcf8591_dac_convert_to_register(&gs_handle, dac, (uint8_t *)&data);
194 if (res != 0)
195 {
196 return 1;
197 }
198
199 /* write dac */
200 if (pcf8591_write(&gs_handle, data) != 0)
201 {
202 return 1;
203 }
204 else
205 {
206 return 0;
207 }
208}
driver pcf8591 basic include 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
uint8_t pcf8591_read(pcf8591_handle_t *handle, int16_t *raw, float *adc)
read data from the chip
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
pcf8591_mode_t
pcf8591 mode 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
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
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
@ PCF8591_BOOL_FALSE
uint8_t pcf8591_basic_read(int16_t *raw, float *adc)
basic example read the adc
#define PCF8591_BASIC_DEFAULT_REFERENCE_VOLTAGE
pcf8591 basic example default definition
uint8_t pcf8591_basic_set_channel(pcf8591_channel_t channel)
basic example set the channel
uint8_t pcf8591_basic_init(pcf8591_address_t addr, pcf8591_mode_t mode)
basic example init
uint8_t pcf8591_basic_write(float dac)
basic example write the dac
uint8_t pcf8591_basic_deinit(void)
basic example deinit
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