LibDriver HX711
Loading...
Searching...
No Matches
driver_hx711.c
Go to the documentation of this file.
1
37
38#include "driver_hx711.h"
39
43#define CHIP_NAME "Aviaic HX711"
44#define MANUFACTURER_NAME "Aviaic"
45#define SUPPLY_VOLTAGE_MIN 2.6f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 1.5f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
51
62static uint8_t a_hx711_read_ad(hx711_handle_t *handle, uint8_t len, int32_t *value)
63{
64 uint32_t val = 0;
65 uint32_t cnt = 0;
66 uint8_t i;
67 uint8_t v;
68
69 if (handle->clock_write(0) != 0) /* write clock 0 */
70 {
71 handle->debug_print("hx711: clock write 0 failed.\n"); /* clock write failed */
72
73 return 1; /* return error */
74 }
75
76 while (1) /* loop */
77 {
78 handle->delay_us(100); /* wait 100 us */
79 if (handle->bus_read((uint8_t *)&v) != 0) /* bus read failed */
80 {
81 handle->debug_print("hx711: bus read failed.\n"); /* bus read failed */
82
83 return 1; /* return error */
84 }
85 cnt++; /* increase timeout cnt */
86 if (v == 1) /* if v==1 */
87 {
88 if (cnt >= 50000) /* check timeout */
89 {
90 handle->debug_print("hx711: bus no response.\n"); /* bus no response */
91
92 return 1; /* return error */
93 }
94 }
95 else
96 {
97 break; /* break */
98 }
99 }
100 handle->disable_irq(); /* disable interrupt */
101 handle->delay_us(1); /* wait 1 us */
102 for (i = 0; i < 24; i++) /* read 24 bits */
103 {
104 if (handle->clock_write(1) != 0) /* write clock 1 */
105 {
106 handle->enable_irq(); /* enable interrupt */
107 handle->debug_print("hx711: clock write 1 failed.\n"); /* clock write failed */
108
109 return 1; /* return error */
110 }
111 val = val << 1; /* left shift 1 */
112 handle->delay_us(1); /* wait 1 us */
113 if (handle->clock_write(0) != 0) /* write clock 0 */
114 {
115 handle->enable_irq(); /* enable interrupt */
116 handle->debug_print("hx711: clock write 0 failed.\n"); /* clock write failed */
117
118 return 1; /* return error */
119 }
120 if (handle->bus_read((uint8_t *)&v) != 0) /* read 1 bit */
121 {
122 handle->enable_irq(); /* enable interrupt */
123 handle->debug_print("hx711: bus read failed.\n"); /* bus read failed */
124
125 return 1; /* return error */
126 }
127 if (v != 0) /* check v */
128 {
129 val++; /* value++ */
130 }
131 handle->delay_us(1); /* wait 1 us */
132 }
133 while (len != 0) /* send pulses */
134 {
135 if (handle->clock_write(1) != 0) /* write clock 1 */
136 {
137 handle->enable_irq(); /* enable interrupt */
138 handle->debug_print("hx711: clock write 1 failed.\n"); /* clock write failed */
139
140 return 1; /* return error */
141 }
142 handle->delay_us(1); /* wait 1 us */
143 if (handle->clock_write(0) != 0) /* write clock 0 */
144 {
145 handle->enable_irq(); /* enable interrupt */
146 handle->debug_print("hx711: clock write 0 failed.\n"); /* clock write failed */
147
148 return 1; /* return error */
149 }
150 handle->delay_us(1); /* wait 1 us */
151 len--; /* length-- */
152 }
153 handle->enable_irq(); /* enable interrupt */
154 if ((val & 0x800000) != 0) /* check negative bit */
155 {
156 union
157 {
158 int32_t i_f;
159 uint32_t u_f;
160 } u;
161 val = 0xFF000000U | val; /* set negative value */
162 u.u_f = val; /* set negative value */
163 *value = (int32_t)u.i_f; /* set negative value */
164 }
165 else
166 {
167 *value = (int32_t)val; /* set positive value */
168 }
169
170 return 0; /* success return 0 */
171}
172
184{
185 if (handle == NULL) /* check handle */
186 {
187 return 2; /* return error */
188 }
189 if (handle->debug_print == NULL) /* check debug_print */
190 {
191 return 3; /* return error */
192 }
193 if (handle->bus_init == NULL) /* check bus_init */
194 {
195 handle->debug_print("hx711: bus_init is null.\n"); /* bus_init is null */
196
197 return 3; /* return error */
198 }
199 if (handle->bus_deinit == NULL) /* check bus_deinit */
200 {
201 handle->debug_print("hx711: bus_deinit is null.\n"); /* bus_deinit is null */
202
203 return 3; /* return error */
204 }
205 if (handle->bus_read == NULL) /* check bus_read */
206 {
207 handle->debug_print("hx711: bus_read is null.\n"); /* bus_read is null */
208
209 return 3; /* return error */
210 }
211 if (handle->clock_init == NULL) /* check clock_init */
212 {
213 handle->debug_print("hx711: clock_init is null.\n"); /* clock_init is null */
214
215 return 3; /* return error */
216 }
217 if (handle->clock_deinit == NULL) /* check clock_deinit */
218 {
219 handle->debug_print("hx711: clock_deinit is null.\n"); /* clock_deinit is null */
220
221 return 3; /* return error */
222 }
223 if (handle->clock_write == NULL) /* check clock_write */
224 {
225 handle->debug_print("hx711: clock_write is null.\n"); /* clock_write is null */
226
227 return 3; /* return error */
228 }
229 if (handle->delay_us == NULL) /* check delay_us */
230 {
231 handle->debug_print("hx711: delay_us is null.\n"); /* delay_us is null */
232
233 return 3; /* return error */
234 }
235 if (handle->enable_irq == NULL) /* check enable_irq */
236 {
237 handle->debug_print("hx711: enable_irq is null.\n"); /* enable_irq is null */
238
239 return 3; /* return error */
240 }
241 if (handle->disable_irq == NULL) /* check disable_irq */
242 {
243 handle->debug_print("hx711: disable_irq is null.\n"); /* disable_irq is null */
244
245 return 3; /* return error */
246 }
247
248 if (handle->clock_init() != 0) /* initialize clock */
249 {
250 handle->debug_print("hx711: clock init failed.\n"); /* clock init failed */
251
252 return 1; /* return error */
253 }
254 if (handle->bus_init() != 0) /* initialize bus */
255 {
256 handle->debug_print("hx711: bus init failed.\n"); /* bus init failed */
257 (void)handle->clock_deinit(); /* deinit clock */
258
259 return 1; /* return error */
260 }
261 handle->inited = 1; /* flag finish initialization */
262 handle->mode = 1; /* set mode */
263
264 return 0; /* success return 0 */
265}
266
278{
279 if (handle == NULL) /* check handle */
280 {
281 return 2; /* return error */
282 }
283 if (handle->inited != 1) /* check handle initialization */
284 {
285 return 3; /* return error */
286 }
287
288 if (handle->bus_deinit() != 0) /* deinit bus */
289 {
290 handle->debug_print("hx711: bus deinit failed.\n"); /* bus deinit failed */
291
292 return 1; /* return error */
293 }
294 if (handle->clock_deinit() != 0) /* deinit clock */
295 {
296 handle->debug_print("hx711: clock deinit failed.\n"); /* clock deinit failed */
297
298 return 1; /* return error */
299 }
300 handle->inited = 0; /* flag close */
301
302 return 0; /* success return 0 */
303}
304
317{
318 int32_t value;
319
320 if (handle == NULL) /* check handle */
321 {
322 return 2; /* return error */
323 }
324 if (handle->inited != 1) /* check handle initialization */
325 {
326 return 3; /* return error */
327 }
328
329 handle->mode = (uint8_t)mode; /* set mode */
330 if (a_hx711_read_ad(handle, handle->mode, (int32_t *)&value) != 0) /* make mode valid */
331 {
332 handle->debug_print("hx711: read ad failed.\n"); /* read ad failed */
333
334 return 1; /* return error */
335 }
336
337 return 0; /* success return 0 */
338}
339
352{
353 if (handle == NULL) /* check handle */
354 {
355 return 2; /* return error */
356 }
357 if (handle->inited != 1) /* check handle initialization */
358 {
359 return 3; /* return error */
360 }
361
362 *mode = (hx711_mode_t)(handle->mode); /* get mode */
363
364 return 0; /* success return 0 */
365}
366
380uint8_t hx711_read(hx711_handle_t *handle, int32_t *raw, double *voltage_v)
381{
382 if (handle == NULL) /* check handle */
383 {
384 return 2; /* return error */
385 }
386 if (handle->inited != 1) /* check handle initialization */
387 {
388 return 3; /* return error */
389 }
390
391 if (a_hx711_read_ad(handle, handle->mode, (int32_t *)raw) != 0) /* read ad */
392 {
393 handle->debug_print("hx711: read voltage failed.\n"); /* read voltage failed */
394
395 return 1; /* return error */
396 }
397 if (handle->mode == (uint8_t)HX711_MODE_CHANNEL_A_GAIN_128) /* if gain 128 */
398 {
399 *voltage_v = (double)(*raw) * (20.0 / (pow(2.0, 24.0))) / 1000.0; /* calculate gain 128 */
400
401 return 0; /* success return 0 */
402 }
403 else if (handle->mode == (uint8_t)HX711_MODE_CHANNEL_B_GAIN_32) /* if gain 32 */
404 {
405 *voltage_v = (double)(*raw) * (80.0 / (pow(2.0, 24.0))) / 1000.0; /* calculate gain 32 */
406
407 return 0; /* success return 0 */
408 }
409 else if (handle->mode == (uint8_t)HX711_MODE_CHANNEL_A_GAIN_64) /* if gain 64 */
410 {
411 *voltage_v = (double)(*raw) * (40.0 / (pow(2.0, 24.0))) / 1000.0; /* calculate gain 64 */
412
413 return 0; /* success return 0 */
414 }
415 else
416 {
417 handle->debug_print("hx711: mode error.\n"); /* mode error */
418
419 return 4; /* return error */
420 }
421}
422
432{
433 if (info == NULL) /* check handle */
434 {
435 return 2; /* return error */
436 }
437
438 memset(info, 0, sizeof(hx711_info_t)); /* initialize hx711 info structure */
439 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
440 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
441 strncpy(info->interface, "GPIO", 8); /* copy interface name */
442 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
443 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
444 info->max_current_ma = MAX_CURRENT; /* set maximum current */
445 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
446 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
447 info->driver_version = DRIVER_VERSION; /* set driver version */
448
449 return 0; /* success return 0 */
450}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver hx711 header file
uint8_t hx711_deinit(hx711_handle_t *handle)
close the chip
uint8_t hx711_get_mode(hx711_handle_t *handle, hx711_mode_t *mode)
get the chip mode
hx711_mode_t
hx711 mode enumeration definition
struct hx711_handle_s hx711_handle_t
hx711 handle structure definition
uint8_t hx711_init(hx711_handle_t *handle)
initialize the chip
uint8_t hx711_set_mode(hx711_handle_t *handle, hx711_mode_t mode)
set the chip mode
struct hx711_info_s hx711_info_t
hx711 info structure definition
uint8_t hx711_info(hx711_info_t *info)
get chip's information
uint8_t hx711_read(hx711_handle_t *handle, int32_t *raw, double *voltage_v)
read the 24 bits raw ad from the chip
@ HX711_MODE_CHANNEL_A_GAIN_64
@ HX711_MODE_CHANNEL_A_GAIN_128
@ HX711_MODE_CHANNEL_B_GAIN_32
void(* enable_irq)(void)
uint8_t(* clock_deinit)(void)
uint8_t(* bus_deinit)(void)
void(* debug_print)(const char *const fmt,...)
void(* delay_us)(uint32_t us)
void(* disable_irq)(void)
uint8_t(* bus_read)(uint8_t *value)
uint8_t(* bus_init)(void)
uint8_t(* clock_init)(void)
uint8_t(* clock_write)(uint8_t value)
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]