LibDriver ADS1115
Loading...
Searching...
No Matches
driver_ads1115_read_test.c
Go to the documentation of this file.
1
37
39
40static ads1115_handle_t gs_handle;
41
51uint8_t ads1115_read_test(ads1115_address_t addr, uint32_t times)
52{
53 uint8_t res;
54 int16_t high_threshold;
55 int16_t low_threshold;
56 uint32_t i;
57 ads1115_info_t info;
58
59 /* link interface function */
67
68 /* get information */
69 res = ads1115_info(&info);
70 if (res != 0)
71 {
72 ads1115_interface_debug_print("ads1115: get info failed.\n");
73
74 return 1;
75 }
76 else
77 {
78 /* print chip info */
79 ads1115_interface_debug_print("ads1115: chip is %s.\n", info.chip_name);
80 ads1115_interface_debug_print("ads1115: manufacturer is %s.\n", info.manufacturer_name);
81 ads1115_interface_debug_print("ads1115: interface is %s.\n", info.interface);
82 ads1115_interface_debug_print("ads1115: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
83 ads1115_interface_debug_print("ads1115: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
84 ads1115_interface_debug_print("ads1115: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
85 ads1115_interface_debug_print("ads1115: max current is %0.2fmA.\n", info.max_current_ma);
86 ads1115_interface_debug_print("ads1115: max temperature is %0.1fC.\n", info.temperature_max);
87 ads1115_interface_debug_print("ads1115: min temperature is %0.1fC.\n", info.temperature_min);
88 }
89
90 /* set addr pin */
91 res = ads1115_set_addr_pin(&gs_handle, addr);
92 if (res != 0)
93 {
94 ads1115_interface_debug_print("ads1115: set addr failed.\n");
95
96 return 1;
97 }
98
99 /* ads1115 init */
100 res = ads1115_init(&gs_handle);
101 if (res != 0)
102 {
103 ads1115_interface_debug_print("ads1115: init failed.\n");
104
105 return 1;
106 }
107
108 /* set channel AIN0 GND */
110 if (res != 0)
111 {
112 ads1115_interface_debug_print("ads1115: set channel failed.\n");
113 (void)ads1115_deinit(&gs_handle);
114
115 return 1;
116 }
117
118 /* set range 6.144V */
119 res = ads1115_set_range(&gs_handle, ADS1115_RANGE_6P144V);
120 if (res != 0)
121 {
122 ads1115_interface_debug_print("ads1115: set range failed.\n");
123 (void)ads1115_deinit(&gs_handle);
124
125 return 1;
126 }
127
128 /* set alert pin low */
129 res = ads1115_set_alert_pin(&gs_handle, ADS1115_PIN_LOW);
130 if (res != 0)
131 {
132 ads1115_interface_debug_print("ads1115: set alert pin failed.\n");
133 (void)ads1115_deinit(&gs_handle);
134
135 return 1;
136 }
137
138 /* set threshold compare mode */
140 if (res != 0)
141 {
142 ads1115_interface_debug_print("ads1115: set compare mode failed.\n");
143 (void)ads1115_deinit(&gs_handle);
144
145 return 1;
146 }
147
148 /* set 128sps rate */
149 res = ads1115_set_rate(&gs_handle, ADS1115_RATE_128SPS);
150 if (res != 0)
151 {
152 ads1115_interface_debug_print("ads1115: set rate failed.\n");
153 (void)ads1115_deinit(&gs_handle);
154
155 return 1;
156 }
157
158 /* set queue none conv */
160 if (res != 0)
161 {
162 ads1115_interface_debug_print("ads1115: set comparator queue failed.\n");
163 (void)ads1115_deinit(&gs_handle);
164
165 return 1;
166 }
167
168 /* disable compare */
169 res = ads1115_set_compare(&gs_handle, ADS1115_BOOL_FALSE);
170 if (res != 0)
171 {
172 ads1115_interface_debug_print("ads1115: set compare failed.\n");
173 (void)ads1115_deinit(&gs_handle);
174
175 return 1;
176 }
177
178 /* convert to register */
179 res = ads1115_convert_to_register(&gs_handle, 3.3f, (int16_t *)&high_threshold);
180 if (res != 0)
181 {
182 ads1115_interface_debug_print("ads1115: convert to high threshold register failed.\n");
183 (void)ads1115_deinit(&gs_handle);
184
185 return 1;
186 }
187
188 /* convert to register */
189 res = ads1115_convert_to_register(&gs_handle, 1.8f, (int16_t *)&low_threshold);
190 if (res != 0)
191 {
192 ads1115_interface_debug_print("ads1115: convert to low threshold register failed.\n");
193 (void)ads1115_deinit(&gs_handle);
194
195 return 1;
196 }
197
198 /* set compare threshold */
199 res = ads1115_set_compare_threshold(&gs_handle, high_threshold, low_threshold);
200 if (res != 0)
201 {
202 ads1115_interface_debug_print("ads1115: set compare threshold failed.\n");
203 (void)ads1115_deinit(&gs_handle);
204
205 return 1;
206 }
207
208 /* start read test */
209 ads1115_interface_debug_print("ads1115: start read test.\n");
210
211 /* AIN0 to GND */
212 ads1115_interface_debug_print("ads1115: AIN0 to GND.\n");
213
214 /* start continuous read */
215 ads1115_interface_debug_print("ads1115: continuous read test.\n");
216 res = ads1115_start_continuous_read(&gs_handle);
217 if (res != 0)
218 {
219 ads1115_interface_debug_print("ads1115: start continues read mode failed.\n");
220 (void)ads1115_deinit(&gs_handle);
221
222 return 1;
223 }
224
225 /* delay 100 ms */
227 for (i = 0; i < times; i++)
228 {
229 int16_t raw;
230 float s;
231
232 /* continuous read */
233 res = ads1115_continuous_read(&gs_handle, (int16_t *)&raw, (float *)&s);
234 if (res != 0)
235 {
236 ads1115_interface_debug_print("ads1115: read failed.\n");
237 (void)ads1115_deinit(&gs_handle);
238
239 return 1;
240 }
241 ads1115_interface_debug_print("ads1115: %d continues mode %0.3fV.\n", i+1, s);
243 }
244
245 /* stop continuous read */
246 res = ads1115_stop_continuous_read(&gs_handle);
247 if (res != 0)
248 {
249 ads1115_interface_debug_print("ads1115: stop continues read mode failed.\n");
250 (void)ads1115_deinit(&gs_handle);
251
252 return 1;
253 }
254
255 /* start single read */
256 ads1115_interface_debug_print("ads1115: single read test.\n");
257 for (i = 0; i < times; i++)
258 {
259 int16_t raw;
260 float s;
261
262 /* single read */
263 res = ads1115_single_read(&gs_handle, (int16_t *)&raw, (float *)&s);
264 if (res != 0)
265 {
266 ads1115_interface_debug_print("ads1115: read failed.\n");
267 (void)ads1115_deinit(&gs_handle);
268
269 return 1;
270 }
271 ads1115_interface_debug_print("ads1115: %d single mode %0.3fV.\n", i+1, s);
273 }
274
275 /* finish read test */
276 ads1115_interface_debug_print("ads1115: finish read test.\n");
277 (void)ads1115_deinit(&gs_handle);
278
279 return 0;
280}
driver ads1115 read test header file
uint8_t ads1115_set_addr_pin(ads1115_handle_t *handle, ads1115_address_t addr_pin)
set the iic address pin
uint8_t ads1115_start_continuous_read(ads1115_handle_t *handle)
start the chip reading
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_stop_continuous_read(ads1115_handle_t *handle)
stop the chip reading
uint8_t ads1115_set_channel(ads1115_handle_t *handle, ads1115_channel_t channel)
set the adc channel
uint8_t ads1115_continuous_read(ads1115_handle_t *handle, int16_t *raw, float *v)
read data from the chip continuously
@ ADS1115_CHANNEL_AIN0_GND
@ 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_read_test(ads1115_address_t addr, uint32_t times)
read test
uint32_t driver_version
char manufacturer_name[32]