LibDriver ADS1115
Loading...
Searching...
No Matches
driver_ads1115_mutichannel_test.c
Go to the documentation of this file.
1
37
39
40static ads1115_handle_t gs_handle;
41
53{
54 uint8_t res;
55 uint32_t i;
56 int16_t high_threshold;
57 int16_t low_threshold;
58 ads1115_info_t info;
59
60 /* link interface function */
68
69 /* get information */
70 res = ads1115_info(&info);
71 if (res != 0)
72 {
73 ads1115_interface_debug_print("ads1115: get info failed.\n");
74
75 return 1;
76 }
77 else
78 {
79 /* print chip info */
80 ads1115_interface_debug_print("ads1115: chip is %s.\n", info.chip_name);
81 ads1115_interface_debug_print("ads1115: manufacturer is %s.\n", info.manufacturer_name);
82 ads1115_interface_debug_print("ads1115: interface is %s.\n", info.interface);
83 ads1115_interface_debug_print("ads1115: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
84 ads1115_interface_debug_print("ads1115: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
85 ads1115_interface_debug_print("ads1115: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
86 ads1115_interface_debug_print("ads1115: max current is %0.2fmA.\n", info.max_current_ma);
87 ads1115_interface_debug_print("ads1115: max temperature is %0.1fC.\n", info.temperature_max);
88 ads1115_interface_debug_print("ads1115: min temperature is %0.1fC.\n", info.temperature_min);
89 }
90
91 /* set addr pin */
92 res = ads1115_set_addr_pin(&gs_handle, addr);
93 if (res != 0)
94 {
95 ads1115_interface_debug_print("ads1115: set addr failed.\n");
96
97 return 1;
98 }
99
100 /* ads1115 init */
101 res = ads1115_init(&gs_handle);
102 if (res != 0)
103 {
104 ads1115_interface_debug_print("ads1115: init failed.\n");
105
106 return 1;
107 }
108
109 /* set range 6.144V */
110 res = ads1115_set_range(&gs_handle, ADS1115_RANGE_6P144V);
111 if (res != 0)
112 {
113 ads1115_interface_debug_print("ads1115: set range failed.\n");
114 (void)ads1115_deinit(&gs_handle);
115
116 return 1;
117 }
118
119 /* set alert pin low */
120 res = ads1115_set_alert_pin(&gs_handle, ADS1115_PIN_LOW);
121 if (res != 0)
122 {
123 ads1115_interface_debug_print("ads1115: set alert pin failed.\n");
124 (void)ads1115_deinit(&gs_handle);
125
126 return 1;
127 }
128
129 /* set threshold compare mode */
131 if (res != 0)
132 {
133 ads1115_interface_debug_print("ads1115: set compare mode failed.\n");
134 (void)ads1115_deinit(&gs_handle);
135
136 return 1;
137 }
138
139 /* set 128SPS rate */
140 res = ads1115_set_rate(&gs_handle, ADS1115_RATE_128SPS);
141 if (res != 0)
142 {
143 ads1115_interface_debug_print("ads1115: set rate failed.\n");
144 (void)ads1115_deinit(&gs_handle);
145
146 return 1;
147 }
148
149 /* set comparator queue none */
151 if (res != 0)
152 {
153 ads1115_interface_debug_print("ads1115: set comparator queue failed.\n");
154 (void)ads1115_deinit(&gs_handle);
155
156 return 1;
157 }
158
159 /* disable compare */
160 res = ads1115_set_compare(&gs_handle, ADS1115_BOOL_FALSE);
161 if (res != 0)
162 {
163 ads1115_interface_debug_print("ads1115: set compare failed.\n");
164 (void)ads1115_deinit(&gs_handle);
165
166 return 1;
167 }
168
169 /* convert to register */
170 res = ads1115_convert_to_register(&gs_handle, 3.3f, (int16_t *)&high_threshold);
171 if (res != 0)
172 {
173 ads1115_interface_debug_print("ads1115: convert to high threshold register failed.\n");
174 (void)ads1115_deinit(&gs_handle);
175
176 return 1;
177 }
178
179 /* convert to register */
180 res = ads1115_convert_to_register(&gs_handle, 1.8f, (int16_t *)&low_threshold);
181 if (res != 0)
182 {
183 ads1115_interface_debug_print("ads1115: convert to low threshold register failed.\n");
184 (void)ads1115_deinit(&gs_handle);
185
186 return 1;
187 }
188
189 /* set compare threshold */
190 res = ads1115_set_compare_threshold(&gs_handle, high_threshold, low_threshold);
191 if (res != 0)
192 {
193 ads1115_interface_debug_print("ads1115: set compare threshold failed.\n");
194 (void)ads1115_deinit(&gs_handle);
195
196 return 1;
197 }
198
199 /* start multichannel test */
200 ads1115_interface_debug_print("ads1115: start multichannel test.\n");
201 res = ads1115_set_channel(&gs_handle, channel);
202 if (res != 0)
203 {
204 ads1115_interface_debug_print("ads1115: set channel failed.\n");
205 (void)ads1115_deinit(&gs_handle);
206
207 return 1;
208 }
209 for (i = 0; i < times; i++)
210 {
211 int16_t raw;
212 float s;
213
214 /* single read */
215 res = ads1115_single_read(&gs_handle, (int16_t *)&raw, (float *)&s);
216 if (res != 0)
217 {
218 ads1115_interface_debug_print("ads1115: read failed.\n");
219 (void)ads1115_deinit(&gs_handle);
220
221 return 1;
222 }
223 ads1115_interface_debug_print("ads1115: %0.3fV.\n", s);
225 }
226
227 /* finish multichannel test */
228 ads1115_interface_debug_print("ads1115: finish multichannel test.\n");
229 (void)ads1115_deinit(&gs_handle);
230
231 return 0;
232}
driver ads1115 mutichannel test header file
uint8_t ads1115_set_addr_pin(ads1115_handle_t *handle, ads1115_address_t addr_pin)
set the iic address pin
ads1115_channel_t
ads1115 channel enumeration definition
uint8_t ads1115_init(ads1115_handle_t *handle)
initialize the chip
uint8_t ads1115_set_rate(ads1115_handle_t *handle, ads1115_rate_t rate)
set the sample rate
ads1115_address_t
ads1115 address enumeration definition
uint8_t ads1115_deinit(ads1115_handle_t *handle)
close the chip
uint8_t ads1115_set_range(ads1115_handle_t *handle, ads1115_range_t range)
set the adc range
struct ads1115_info_s ads1115_info_t
ads1115 information structure definition
uint8_t ads1115_single_read(ads1115_handle_t *handle, int16_t *raw, float *v)
read data from the chip once
struct ads1115_handle_s ads1115_handle_t
ads1115 handle structure definition
uint8_t ads1115_info(ads1115_info_t *info)
get chip's information
uint8_t ads1115_set_channel(ads1115_handle_t *handle, ads1115_channel_t channel)
set the adc channel
@ ADS1115_RATE_128SPS
@ ADS1115_BOOL_FALSE
@ ADS1115_RANGE_6P144V
uint8_t ads1115_interface_iic_init(void)
interface iic bus init
void ads1115_interface_delay_ms(uint32_t ms)
interface delay ms
void ads1115_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t ads1115_interface_iic_deinit(void)
interface iic bus deinit
uint8_t ads1115_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t ads1115_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t ads1115_set_alert_pin(ads1115_handle_t *handle, ads1115_pin_t pin)
set the alert pin active status
uint8_t ads1115_set_compare(ads1115_handle_t *handle, ads1115_bool_t enable)
enable or disable the interrupt compare
uint8_t ads1115_convert_to_register(ads1115_handle_t *handle, float s, int16_t *reg)
convert a adc value to a register raw data
uint8_t ads1115_set_compare_threshold(ads1115_handle_t *handle, int16_t high_threshold, int16_t low_threshold)
set the interrupt compare threshold
uint8_t ads1115_set_compare_mode(ads1115_handle_t *handle, ads1115_compare_t compare)
set the interrupt compare mode
uint8_t ads1115_set_comparator_queue(ads1115_handle_t *handle, ads1115_comparator_queue_t comparator_queue)
set the interrupt comparator queue
@ ADS1115_COMPARATOR_QUEUE_NONE_CONV
@ ADS1115_COMPARE_THRESHOLD
@ ADS1115_PIN_LOW
uint8_t ads1115_multichannel_test(ads1115_address_t addr, ads1115_channel_t channel, uint32_t times)
multichannel test
uint32_t driver_version
char manufacturer_name[32]