LibDriver ADS1118
Loading...
Searching...
No Matches
driver_ads1118_read_test.c
Go to the documentation of this file.
1
36
38
39static ads1118_handle_t gs_handle;
40
49uint8_t ads1118_read_test(uint32_t times)
50{
51 uint8_t res;
52 uint8_t i;
53 ads1118_info_t info;
54
55 /* link interface function */
62
63 /* get information */
64 res = ads1118_info(&info);
65 if (res != 0)
66 {
67 ads1118_interface_debug_print("ads1118: get info failed.\n");
68
69 return 1;
70 }
71 else
72 {
73 /* print chip info */
74 ads1118_interface_debug_print("ads1118: chip is %s.\n", info.chip_name);
75 ads1118_interface_debug_print("ads1118: manufacturer is %s.\n", info.manufacturer_name);
76 ads1118_interface_debug_print("ads1118: interface is %s.\n", info.interface);
77 ads1118_interface_debug_print("ads1118: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
78 ads1118_interface_debug_print("ads1118: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
79 ads1118_interface_debug_print("ads1118: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
80 ads1118_interface_debug_print("ads1118: max current is %0.2fmA.\n", info.max_current_ma);
81 ads1118_interface_debug_print("ads1118: max temperature is %0.1fC.\n", info.temperature_max);
82 ads1118_interface_debug_print("ads1118: min temperature is %0.1fC.\n", info.temperature_min);
83 }
84
85 /* ads1118 init */
86 res = ads1118_init(&gs_handle);
87 if (res != 0)
88 {
89 ads1118_interface_debug_print("ads1118: init failed.\n");
90
91 return 1;
92 }
93
94 /* set channel AIN0 GND */
96 if (res != 0)
97 {
98 ads1118_interface_debug_print("ads1118: set channel failed.\n");
99 (void)ads1118_deinit(&gs_handle);
100
101 return 1;
102 }
103
104 /* set range 6.144V */
105 res = ads1118_set_range(&gs_handle, ADS1118_RANGE_6P144V);
106 if (res != 0)
107 {
108 ads1118_interface_debug_print("ads1118: set range failed.\n");
109 (void)ads1118_deinit(&gs_handle);
110
111 return 1;
112 }
113
114 /* set 128sps rate */
115 res = ads1118_set_rate(&gs_handle, ADS1118_RATE_128SPS);
116 if (res != 0)
117 {
118 ads1118_interface_debug_print("ads1118: set rate failed.\n");
119 (void)ads1118_deinit(&gs_handle);
120
121 return 1;
122 }
123
124 /* disable dout pull up */
126 if (res != 0)
127 {
128 ads1118_interface_debug_print("ads1118: set dout pull up failed.\n");
129 (void)ads1118_deinit(&gs_handle);
130
131 return 1;
132 }
133
134 /* set adc mode */
135 res = ads1118_set_mode(&gs_handle, ADS1118_MODE_ADC);
136 if (res != 0)
137 {
138 ads1118_interface_debug_print("ads1118: set mode failed.\n");
139 (void)ads1118_deinit(&gs_handle);
140
141 return 1;
142 }
143
144 /* start read test */
145 ads1118_interface_debug_print("ads1118: start read test.\n");
146
147 /* AIN0 to GND */
148 ads1118_interface_debug_print("ads1118: AIN0 to GND.\n");
149
150 /* start continuous read */
151 ads1118_interface_debug_print("ads1118: continuous read test.\n");
152 res = ads1118_start_continuous_read(&gs_handle);
153 if (res != 0)
154 {
155 ads1118_interface_debug_print("ads1118: start continues read mode failed.\n");
156 (void)ads1118_deinit(&gs_handle);
157
158 return 1;
159 }
160
161 /* delay 100 ms */
163 for (i = 0; i < times; i++)
164 {
165 int16_t raw;
166 float s;
167
168 /* continuous read */
169 res = ads1118_continuous_read(&gs_handle, (int16_t *)&raw, (float *)&s);
170 if (res != 0)
171 {
172 ads1118_interface_debug_print("ads1118: read failed.\n");
173 (void)ads1118_deinit(&gs_handle);
174
175 return 1;
176 }
177 ads1118_interface_debug_print("ads1118: %d continues mode %0.3fV.\n", i+1, s);
179 }
180
181 /* stop continuous read */
182 res = ads1118_stop_continuous_read(&gs_handle);
183 if (res != 0)
184 {
185 ads1118_interface_debug_print("ads1118: stop continues read mode failed.\n");
186 (void)ads1118_deinit(&gs_handle);
187
188 return 1;
189 }
190
191 /* start single read */
192 ads1118_interface_debug_print("ads1118: single read test.\n");
193 for (i = 0; i < times; i++)
194 {
195 int16_t raw;
196 float s;
197
198 /* single read */
199 res = ads1118_single_read(&gs_handle, (int16_t *)&raw, (float *)&s);
200 if (res != 0)
201 {
202 ads1118_interface_debug_print("ads1118: read failed.\n");
203 (void)ads1118_deinit(&gs_handle);
204
205 return 1;
206 }
207 ads1118_interface_debug_print("ads1118: %d single mode %0.3fV.\n", i+1, s);
209 }
210
211 /* set temperature mode */
213 if (res != 0)
214 {
215 ads1118_interface_debug_print("ads1118: set mode failed.\n");
216 (void)ads1118_deinit(&gs_handle);
217
218 return 1;
219 }
220
221 /* temperature read test */
222 ads1118_interface_debug_print("ads1118: temperature read test.\n");
223 for (i = 0; i < times; i++)
224 {
225 int16_t raw;
226 float s;
227
228 /* single read */
229 res = ads1118_single_read(&gs_handle, (int16_t *)&raw, (float *)&s);
230 if (res != 0)
231 {
232 ads1118_interface_debug_print("ads1118: read failed.\n");
233 (void)ads1118_deinit(&gs_handle);
234
235 return 1;
236 }
237 res = ads1118_temperature_convert(&gs_handle, raw, &s);
238 if (res != 0)
239 {
240 ads1118_interface_debug_print("ads1118: temperature convert failed.\n");
241 (void)ads1118_deinit(&gs_handle);
242
243 return 1;
244 }
245 ads1118_interface_debug_print("ads1118: %d temperature is %0.3fC.\n", i + 1, s);
247 }
248
249 /* finish read test */
250 ads1118_interface_debug_print("ads1118: finish read test.\n");
251 (void)ads1118_deinit(&gs_handle);
252
253 return 0;
254}
driver ads1118 read test header file
uint8_t ads1118_stop_continuous_read(ads1118_handle_t *handle)
stop the chip reading
uint8_t ads1118_single_read(ads1118_handle_t *handle, int16_t *raw, float *v)
read data from the chip once
uint8_t ads1118_set_channel(ads1118_handle_t *handle, ads1118_channel_t channel)
set the adc channel
uint8_t ads1118_set_range(ads1118_handle_t *handle, ads1118_range_t range)
set the adc range
uint8_t ads1118_info(ads1118_info_t *info)
get chip's information
uint8_t ads1118_continuous_read(ads1118_handle_t *handle, int16_t *raw, float *v)
read data from the chip continuously
struct ads1118_info_s ads1118_info_t
ads1118 information structure definition
uint8_t ads1118_init(ads1118_handle_t *handle)
initialize the chip
uint8_t ads1118_set_dout_pull_up(ads1118_handle_t *handle, ads1118_bool_t enable)
enable or disable dout pull up
uint8_t ads1118_deinit(ads1118_handle_t *handle)
close the chip
struct ads1118_handle_s ads1118_handle_t
ads1118 handle structure definition
uint8_t ads1118_set_rate(ads1118_handle_t *handle, ads1118_rate_t rate)
set the sample rate
uint8_t ads1118_temperature_convert(ads1118_handle_t *handle, int16_t raw, float *deg)
temperature convert
uint8_t ads1118_start_continuous_read(ads1118_handle_t *handle)
start the chip reading
uint8_t ads1118_set_mode(ads1118_handle_t *handle, ads1118_mode_t mode)
set the chip mode
@ ADS1118_MODE_TEMPERATURE
@ ADS1118_MODE_ADC
@ ADS1118_BOOL_FALSE
@ ADS1118_RANGE_6P144V
@ ADS1118_CHANNEL_AIN0_GND
@ ADS1118_RATE_128SPS
void ads1118_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t ads1118_interface_spi_transmit(uint8_t *tx, uint8_t *rx, uint16_t len)
interface spi bus transmit
uint8_t ads1118_interface_spi_deinit(void)
interface spi bus deinit
uint8_t ads1118_interface_spi_init(void)
interface spi bus init
void ads1118_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t ads1118_read_test(uint32_t times)
read test
uint32_t driver_version
char manufacturer_name[32]