LibDriver ADS1115
Loading...
Searching...
No Matches
driver_ads1115_compare_test.c
Go to the documentation of this file.
1
37
39
40static ads1115_handle_t gs_handle;
41
56 float f_high_threshold, float f_low_threshold, uint32_t times)
57{
58 uint8_t res;
59 int16_t high_threshold;
60 int16_t low_threshold;
61 uint32_t i;
62 ads1115_info_t info;
63
64 /* link interface function */
72
73 /* get information */
74 res = ads1115_info(&info);
75 if (res != 0)
76 {
77 ads1115_interface_debug_print("ads1115: get info failed.\n");
78
79 return 1;
80 }
81 else
82 {
83 /* print chip info */
84 ads1115_interface_debug_print("ads1115: chip is %s.\n", info.chip_name);
85 ads1115_interface_debug_print("ads1115: manufacturer is %s.\n", info.manufacturer_name);
86 ads1115_interface_debug_print("ads1115: interface is %s.\n", info.interface);
87 ads1115_interface_debug_print("ads1115: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
88 ads1115_interface_debug_print("ads1115: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
89 ads1115_interface_debug_print("ads1115: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
90 ads1115_interface_debug_print("ads1115: max current is %0.2fmA.\n", info.max_current_ma);
91 ads1115_interface_debug_print("ads1115: max temperature is %0.1fC.\n", info.temperature_max);
92 ads1115_interface_debug_print("ads1115: min temperature is %0.1fC.\n", info.temperature_min);
93 }
94
95 /* set addr pin */
96 res = ads1115_set_addr_pin(&gs_handle, addr);
97 if (res != 0)
98 {
99 ads1115_interface_debug_print("ads1115: set addr failed.\n");
100
101 return 1;
102 }
103
104 /* ads1115 init */
105 res = ads1115_init(&gs_handle);
106 if (res != 0)
107 {
108 ads1115_interface_debug_print("ads1115: init failed.\n");
109
110 return 1;
111 }
112
113 /* set channel */
114 res = ads1115_set_channel(&gs_handle, channel);
115 if (res != 0)
116 {
117 ads1115_interface_debug_print("ads1115: set channel failed.\n");
118 (void)ads1115_deinit(&gs_handle);
119
120 return 1;
121 }
122
123 /* set range 6.144V */
124 res = ads1115_set_range(&gs_handle, ADS1115_RANGE_6P144V);
125 if (res != 0)
126 {
127 ads1115_interface_debug_print("ads1115: set range failed.\n");
128 (void)ads1115_deinit(&gs_handle);
129
130 return 1;
131 }
132
133 /* set alert pin low */
134 res = ads1115_set_alert_pin(&gs_handle, ADS1115_PIN_LOW);
135 if (res != 0)
136 {
137 ads1115_interface_debug_print("ads1115: set alert pin failed.\n");
138 (void)ads1115_deinit(&gs_handle);
139
140 return 1;
141 }
142
143 /* set compare mode */
144 res = ads1115_set_compare_mode(&gs_handle, compare);
145 if (res != 0)
146 {
147 ads1115_interface_debug_print("ads1115: set compare mode failed.\n");
148 (void)ads1115_deinit(&gs_handle);
149
150 return 1;
151 }
152
153 /* set rate 128SPS */
154 res = ads1115_set_rate(&gs_handle, ADS1115_RATE_128SPS);
155 if (res != 0)
156 {
157 ads1115_interface_debug_print("ads1115: set rate failed.\n");
158 (void)ads1115_deinit(&gs_handle);
159
160 return 1;
161 }
162
163 /* set comparator queue 2 conv */
165 if (res != 0)
166 {
167 ads1115_interface_debug_print("ads1115: set comparator queue failed.\n");
168 (void)ads1115_deinit(&gs_handle);
169
170 return 1;
171 }
172
173 /* enable compare */
174 res = ads1115_set_compare(&gs_handle, ADS1115_BOOL_TRUE);
175 if (res != 0)
176 {
177 ads1115_interface_debug_print("ads1115: set compare failed.\n");
178 (void)ads1115_deinit(&gs_handle);
179
180 return 1;
181 }
182
183 /* convert to register */
184 res = ads1115_convert_to_register(&gs_handle, f_high_threshold, (int16_t *)&high_threshold);
185 if (res != 0)
186 {
187 ads1115_interface_debug_print("ads1115: convert to high threshold register failed.\n");
188 (void)ads1115_deinit(&gs_handle);
189
190 return 1;
191 }
192
193 /* convert to register */
194 res = ads1115_convert_to_register(&gs_handle, f_low_threshold, (int16_t *)&low_threshold);
195 if (res != 0)
196 {
197 ads1115_interface_debug_print("ads1115: convert to low threshold register failed.\n");
198 (void)ads1115_deinit(&gs_handle);
199
200 return 1;
201 }
202
203 /* set compare threshold */
204 res = ads1115_set_compare_threshold(&gs_handle, high_threshold, low_threshold);
205 if (res != 0)
206 {
207 ads1115_interface_debug_print("ads1115: set compare threshold failed.\n");
208 (void)ads1115_deinit(&gs_handle);
209
210 return 1;
211 }
212
213 /* start compare mode test */
214 ads1115_interface_debug_print("ads1115: start compare mode test.\n");
215 for (i = 0; i < times; i++)
216 {
217 int16_t raw;
218 float s;
219
220 ads1115_interface_debug_print("ads1115: run %d times.\n", i+1);
221
222 /* single read */
223 res = ads1115_single_read(&gs_handle, (int16_t *)&raw, (float *)&s);
224 if (res != 0)
225 {
226 ads1115_interface_debug_print("ads1115: read failed.\n");
227 (void)ads1115_deinit(&gs_handle);
228
229 return 1;
230 }
231 if (s > f_high_threshold)
232 {
233 ads1115_interface_debug_print("ads1115: voltage is over high threshold, please see alert pin.\n");
234 }
235 if (s < f_low_threshold)
236 {
237 ads1115_interface_debug_print("ads1115: voltage is below low threshold, please see alert pin.\n");
238 }
240 }
241
242 /* finish compare mode test */
243 ads1115_interface_debug_print("ads1115: finish compare mode test.\n");
244 (void)ads1115_deinit(&gs_handle);
245
246 return 0;
247}
driver ads1115 compare 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_TRUE
@ 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_compare_t
ads1115 compare mode enumeration definition
@ ADS1115_COMPARATOR_QUEUE_2_CONV
@ ADS1115_PIN_LOW
uint8_t ads1115_compare_test(ads1115_address_t addr, ads1115_channel_t channel, ads1115_compare_t compare, float f_high_threshold, float f_low_threshold, uint32_t times)
compare test
uint32_t driver_version
char manufacturer_name[32]