LibDriver ADS1115
Loading...
Searching...
No Matches
driver_ads1115.c
Go to the documentation of this file.
1
37
38#include "driver_ads1115.h"
39
43#define CHIP_NAME "Texas Instruments ADS1115"
44#define MANUFACTURER_NAME "Texas Instruments"
45#define SUPPLY_VOLTAGE_MIN 2.0f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 0.2f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 125.0f
50#define DRIVER_VERSION 2000
51
55#define ADS1115_REG_CONVERT 0x00
56#define ADS1115_REG_CONFIG 0x01
57#define ADS1115_REG_LOWRESH 0x02
58#define ADS1115_REG_HIGHRESH 0x03
59
63#define ADS1115_ADDRESS1 (0x48 << 1)
64#define ADS1115_ADDRESS2 (0x49 << 1)
65#define ADS1115_ADDRESS3 (0x4A << 1)
66#define ADS1115_ADDRESS4 (0x4B << 1)
67
78static uint8_t a_ads1115_iic_multiple_read(ads1115_handle_t *handle, uint8_t reg, int16_t *data)
79{
80 uint8_t buf[2];
81
82 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
83 if (handle->iic_read(handle->iic_addr, reg, (uint8_t *)buf, 2) == 0) /* read data */
84 {
85 *data = (uint16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set data */
86
87 return 0; /* success return 0 */
88 }
89 else
90 {
91 return 1; /* return error */
92 }
93}
94
105static uint8_t a_ads1115_iic_multiple_write(ads1115_handle_t *handle, uint8_t reg, uint16_t data)
106{
107 uint8_t buf[2];
108
109 buf[0] = (data >> 8) & 0xFF; /* set MSB */
110 buf[1] = data & 0xFF; /* set LSB */
111 if (handle->iic_write(handle->iic_addr, reg, (uint8_t *)buf, 2) != 0) /* write data */
112 {
113 return 1; /* return error */
114 }
115 else
116 {
117 return 0; /* success return 0 */
118 }
119}
120
132{
133 if (handle == NULL) /* check handle */
134 {
135 return 2; /* return error */
136 }
137 if (handle->debug_print == NULL) /* check debug_print */
138 {
139 return 3; /* return error */
140 }
141 if (handle->iic_init == NULL) /* check iic_init */
142 {
143 handle->debug_print("ads1115: iic_init is null.\n"); /* iic_init is null */
144
145 return 3; /* return error */
146 }
147 if (handle->iic_deinit == NULL) /* check iic_deinit */
148 {
149 handle->debug_print("ads1115: iic_deinit is null.\n"); /* iic_deinit is null */
150
151 return 3; /* return error */
152 }
153 if (handle->iic_read == NULL) /* check iic_read */
154 {
155 handle->debug_print("ads1115: iic_read is null.\n"); /* iic_read is null */
156
157 return 3; /* return error */
158 }
159 if (handle->iic_write == NULL) /* check iic_write */
160 {
161 handle->debug_print("ads1115: iic_write is null.\n"); /* iic_write is null */
162
163 return 3; /* return error */
164 }
165 if (handle->delay_ms == NULL) /* check delay_ms */
166 {
167 handle->debug_print("ads1115: delay_ms is null.\n"); /* delay_ms is null */
168
169 return 3; /* return error */
170 }
171
172 if (handle->iic_init() != 0) /* iic init */
173 {
174 handle->debug_print("ads1115: iic init failed.\n"); /* iic init failed */
175
176 return 1; /* return error */
177 }
178 handle->inited = 1; /* flag inited */
179
180 return 0; /* success return 0 */
181}
182
195{
196 uint8_t res;
197 uint16_t conf;
198
199 if (handle == NULL) /* check handle */
200 {
201 return 2; /* return error */
202 }
203 if (handle->inited != 1) /* check handle initialization */
204 {
205 return 3; /* return error */
206 }
207
208 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
209 if (res != 0) /* check error */
210 {
211 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
212
213 return 4; /* return error */
214 }
215 conf &= ~(0x01 << 8); /* clear bit */
216 conf |= 1 << 8; /* set stop continues read */
217 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
218 if (res != 0) /* check error */
219 {
220 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
221
222 return 4; /* return error */
223 }
224 res = handle->iic_deinit(); /* close iic */
225 if (res != 0) /* check the result */
226 {
227 handle->debug_print("ads1115: iic deinit failed.\n"); /* iic deinit failed */
228
229 return 1; /* return error */
230 }
231 handle->inited = 0; /* flag close */
232
233 return 0; /* success return 0 */
234}
235
248{
249 uint8_t res;
250 uint16_t conf;
251
252 if (handle == NULL) /* check handle */
253 {
254 return 2; /* return error */
255 }
256 if (handle->inited != 1) /* check handle initialization */
257 {
258 return 3; /* return error */
259 }
260
261 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
262 if (res != 0) /* check error */
263 {
264 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
265
266 return 1; /* return error */
267 }
268 conf &= ~(0x07 << 12); /* clear channel */
269 conf |= (channel & 0x07) << 12; /* set channel */
270 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
271 if (res != 0) /* check error */
272 {
273 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
274
275 return 1; /* return error */
276 }
277
278 return 0; /* success return 0 */
279}
280
293{
294 uint8_t res;
295 uint16_t conf;
296
297 if (handle == NULL) /* check handle */
298 {
299 return 2; /* return error */
300 }
301 if (handle->inited != 1) /* check handle initialization */
302 {
303 return 3; /* return error */
304 }
305
306 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
307 if (res != 0) /* check error */
308 {
309 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
310
311 return 1; /* return error */
312 }
313 *channel = (ads1115_channel_t)((conf >> 12) & 0x07); /* get channel */
314
315 return 0; /* success return 0 */
316}
317
330{
331 uint8_t res;
332 uint16_t conf;
333
334 if (handle == NULL) /* check handle */
335 {
336 return 2; /* return error */
337 }
338 if (handle->inited != 1) /* check handle initialization */
339 {
340 return 3; /* return error */
341 }
342
343 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
344 if (res != 0) /* check error */
345 {
346 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
347
348 return 1; /* return error */
349 }
350 conf &= ~(0x07 << 9); /* clear range */
351 conf |= (range & 0x07) << 9; /* set range */
352 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
353 if (res != 0) /* check error */
354 {
355 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
356
357 return 1; /* return error */
358 }
359
360 return 0; /* success return 0 */
361}
362
375{
376 uint8_t res;
377 uint16_t conf;
378
379 if (handle == NULL) /* check handle */
380 {
381 return 2; /* return error */
382 }
383 if (handle->inited != 1) /* check handle initialization */
384 {
385 return 3; /* return error */
386 }
387
388 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
389 if (res != 0) /* check error */
390 {
391 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
392
393 return 1; /* return error */
394 }
395 *range = (ads1115_range_t)((conf >> 9) & 0x07); /* get range */
396
397 return 0; /* success return 0 */
398}
399
412{
413 uint8_t res;
414 uint16_t conf;
415
416 if (handle == NULL) /* check handle */
417 {
418 return 2; /* return error */
419 }
420 if (handle->inited != 1) /* check handle initialization */
421 {
422 return 3; /* return error */
423 }
424
425 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
426 if (res != 0) /* check error */
427 {
428 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
429
430 return 1; /* return error */
431 }
432 conf &= ~(1 << 3); /* clear alert pin */
433 conf |= (pin & 0x01) << 3; /* set alert pin */
434 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
435 if (res != 0) /* check error */
436 {
437 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
438
439 return 1; /* return error */
440 }
441
442 return 0; /* success return 0 */
443}
444
457{
458 uint8_t res;
459 uint16_t conf;
460
461 if (handle == NULL) /* check handle */
462 {
463 return 2; /* return error */
464 }
465 if (handle->inited != 1) /* check handle initialization */
466 {
467 return 3; /* return error */
468 }
469
470 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
471 if (res != 0) /* check error */
472 {
473 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
474
475 return 1; /* return error */
476 }
477 *pin = (ads1115_pin_t)((conf >> 3) & 0x01); /* get alert pin */
478
479 return 0; /* success return 0 */
480}
481
494{
495 uint8_t res;
496 uint16_t conf;
497
498 if (handle == NULL) /* check handle */
499 {
500 return 2; /* return error */
501 }
502 if (handle->inited != 1) /* check handle initialization */
503 {
504 return 3; /* return error */
505 }
506
507 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
508 if (res != 0) /* check error */
509 {
510 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
511
512 return 1; /* return error */
513 }
514 conf &= ~(1 << 4); /* clear compare mode */
515 conf |= (compare & 0x01) << 4; /* set compare mode */
516 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
517 if (res != 0) /* check error */
518 {
519 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
520
521 return 1; /* return error */
522 }
523
524 return 0; /* success return 0 */
525}
526
539{
540 uint8_t res;
541 uint16_t conf;
542
543 if (handle == NULL) /* check handle */
544 {
545 return 2; /* return error */
546 }
547 if (handle->inited != 1) /* check handle initialization */
548 {
549 return 3; /* return error */
550 }
551
552 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
553 if (res != 0) /* check error */
554 {
555 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
556
557 return 1; /* return error */
558 }
559 *compare = (ads1115_compare_t)((conf >> 4) & 0x01); /* get compare mode */
560
561 return 0; /* success return 0 */
562}
563
576{
577 uint8_t res;
578 uint16_t conf;
579
580 if (handle == NULL) /* check handle */
581 {
582 return 2; /* return error */
583 }
584 if (handle->inited != 1) /* check handle initialization */
585 {
586 return 3; /* return error */
587 }
588
589 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
590 if (res != 0) /* check error */
591 {
592 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
593
594 return 1; /* return error */
595 }
596 conf &= ~(0x07 << 5); /* clear rate */
597 conf |= (rate & 0x07) << 5; /* set rate */
598 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
599 if (res != 0) /* check error */
600 {
601 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
602
603 return 1; /* return error */
604 }
605
606 return 0; /* success return */
607}
608
621{
622 uint8_t res;
623 uint16_t conf;
624
625 if (handle == NULL) /* check handle */
626 {
627 return 2; /* return error */
628 }
629 if (handle->inited != 1) /* check handle initialization */
630 {
631 return 3; /* return error */
632 }
633
634 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
635 if (res != 0) /* check error */
636 {
637 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
638
639 return 1; /* return error */
640 }
641 *rate = (ads1115_rate_t)((conf >> 5) & 0x07); /* get rate */
642
643 return 0; /* success return 0 */
644}
645
658{
659 uint8_t res;
660 uint16_t conf;
661
662 if (handle == NULL) /* check handle */
663 {
664 return 2; /* return error */
665 }
666 if (handle->inited != 1) /* check handle initialization */
667 {
668 return 3; /* return error */
669 }
670
671 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
672 if (res != 0) /* check error */
673 {
674 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
675
676 return 1; /* return error */
677 }
678 conf &= ~(0x03 << 0); /* clear comparator queue */
679 conf |= (comparator_queue & 0x03) << 0; /* set comparator queue */
680 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
681 if (res != 0) /* check error */
682 {
683 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
684
685 return 1; /* return error */
686 }
687
688 return 0; /* success return 0 */
689}
690
703{
704 uint8_t res;
705 uint16_t conf;
706
707 if (handle == NULL) /* check handle */
708 {
709 return 2; /* return error */
710 }
711 if (handle->inited != 1) /* check handle initialization */
712 {
713 return 3; /* return error */
714 }
715
716 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
717 if (res != 0) /* check error */
718 {
719 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
720
721 return 1; /* return error */
722 }
723 *comparator_queue = (ads1115_comparator_queue_t)((conf >> 0) & 0x03); /* get comparator queue */
724
725 return 0; /* success return 0 */
726}
727
739{
740 if (handle == NULL) /* check handle */
741 {
742 return 2; /* return error */
743 }
744
745 if (addr_pin == ADS1115_ADDR_GND) /* gnd */
746 {
747 handle->iic_addr = ADS1115_ADDRESS1; /* set address 1 */
748 }
749 else if (addr_pin == ADS1115_ADDR_VCC) /* vcc */
750 {
751 handle->iic_addr = ADS1115_ADDRESS2; /* set address 2 */
752 }
753 else if (addr_pin == ADS1115_ADDR_SDA) /* sda */
754 {
755 handle->iic_addr = ADS1115_ADDRESS3; /* set address 3 */
756 }
757 else if (addr_pin == ADS1115_ADDR_SCL) /* scl */
758 {
759 handle->iic_addr = ADS1115_ADDRESS4; /* set address 4 */
760 }
761 else
762 {
763 handle->debug_print("ads1115: addr_pin is invalid.\n"); /* addr_pin is invalid */
764
765 return 1; /* return error */
766 }
767
768 return 0; /* success return 0 */
769}
770
782{
783 if (handle == NULL) /* check handle */
784 {
785 return 2; /* return error */
786 }
787
788 if (handle->iic_addr == ADS1115_ADDRESS1) /* if address 1 */
789 {
790 *addr_pin = ADS1115_ADDR_GND; /* set gnd */
791 }
792 else if (handle->iic_addr == ADS1115_ADDRESS2) /* if address 2 */
793 {
794 *addr_pin = ADS1115_ADDR_VCC; /* set vcc */
795 }
796 else if (handle->iic_addr == ADS1115_ADDRESS3) /* if address 3 */
797 {
798 *addr_pin = ADS1115_ADDR_SDA; /* set sda */
799 }
800 else if (handle->iic_addr == ADS1115_ADDRESS4) /* set address 4 */
801 {
802 *addr_pin = ADS1115_ADDR_SCL; /* set scl */
803 }
804 else
805 {
806 handle->debug_print("ads1115: addr_pin is invalid.\n"); /* addr_pin is invalid */
807
808 return 1; /* return error */
809 }
810
811 return 0; /* success return 0 */
812}
813
826uint8_t ads1115_single_read(ads1115_handle_t *handle, int16_t *raw, float *v)
827{
828 uint8_t res;
829 uint8_t range;
830 uint16_t conf;
831 uint32_t timeout = 500;
832
833 if (handle == NULL) /* check handle */
834 {
835 return 2; /* return error */
836 }
837 if (handle->inited != 1) /* check handle initialization */
838 {
839 return 3; /* return error */
840 }
841
842 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
843 if (res != 0) /* check error */
844 {
845 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
846
847 return 1; /* return error */
848 }
849 range = (ads1115_range_t)((conf >> 9) & 0x07); /* get range conf */
850 conf &= ~(1 << 8); /* clear bit */
851 conf |= 1 << 8; /* set single read */
852 conf |= 1 << 15; /* start single read */
853 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
854 if (res != 0) /* check error */
855 {
856 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
857
858 return 1; /* return error */
859 }
860 while (timeout != 0) /* check timeout */
861 {
862 handle->delay_ms(8); /* wait 8 ms */
863 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
864 if (res != 0) /* check error */
865 {
866 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
867
868 return 1; /* return error */
869 }
870 if ((conf & (1 << 15)) == (1 << 15)) /* check finished */
871 {
872 break; /* break */
873 }
874 timeout--; /* timeout-- */
875 }
876 if (timeout == 0) /* check timeout */
877 {
878 handle->debug_print("ads1115: read timeout.\n"); /* timeout */
879
880 return 1; /* return error */
881 }
882 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONVERT, raw); /* read data */
883 if (res != 0) /* check the result */
884 {
885 handle->debug_print("ads1115: continues read failed.\n"); /* continues read failed */
886
887 return 1; /* return error */
888 }
889 if (range == ADS1115_RANGE_6P144V) /* if 6.144V */
890 {
891 *v = (float)(*raw) * 6.144f / 32768.0f; /* get convert adc */
892 }
893 else if (range == ADS1115_RANGE_4P096V) /* if 4.096V */
894 {
895 *v = (float)(*raw) * 4.096f / 32768.0f; /* get convert adc */
896 }
897 else if (range == ADS1115_RANGE_2P048V) /* if 2.048V */
898 {
899 *v = (float)(*raw) * 2.048f / 32768.0f; /* get convert adc */
900 }
901 else if (range == ADS1115_RANGE_1P024V) /* if 1.024V */
902 {
903 *v = (float)(*raw) * 1.024f / 32768.0f; /* get convert adc */
904 }
905 else if (range == ADS1115_RANGE_0P512V) /* if 0.512V */
906 {
907 *v = (float)(*raw) * 0.512f / 32768.0f; /* get convert adc */
908 }
909 else if (range == ADS1115_RANGE_0P256V) /* if 0.256V */
910 {
911 *v = (float)(*raw) * 0.256f / 32768.0f; /* get convert adc */
912 }
913 else
914 {
915 handle->debug_print("ads1115: range is invalid.\n"); /* range is invalid */
916
917 return 1; /* return error */
918 }
919
920 return 0; /* success return 0 */
921}
922
936uint8_t ads1115_continuous_read(ads1115_handle_t *handle,int16_t *raw, float *v)
937{
938 uint8_t res;
939 uint8_t range;
940 uint16_t conf;
941
942 if (handle == NULL) /* check handle */
943 {
944 return 2; /* return error */
945 }
946 if (handle->inited != 1) /* check handle initialization */
947 {
948 return 3; /* return error */
949 }
950
951 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
952 if (res != 0) /* check error */
953 {
954 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
955
956 return 1; /* return error */
957 }
958 range = (ads1115_range_t)((conf >> 9) & 0x07); /* get range conf */
959 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONVERT, raw); /* read data */
960 if (res != 0) /* check error */
961 {
962 handle->debug_print("ads1115: continuous read failed.\n"); /* continuous read failed */
963
964 return 1; /* return error */
965 }
966 if (range == ADS1115_RANGE_6P144V) /* if 6.144V */
967 {
968 *v = (float)(*raw) * 6.144f / 32768.0f; /* get convert adc */
969 }
970 else if (range == ADS1115_RANGE_4P096V) /* if 4.096V */
971 {
972 *v = (float)(*raw) * 4.096f / 32768.0f; /* get convert adc */
973 }
974 else if (range == ADS1115_RANGE_2P048V) /* if 2.048V */
975 {
976 *v = (float)(*raw) * 2.048f / 32768.0f; /* get convert adc */
977 }
978 else if (range == ADS1115_RANGE_1P024V) /* if 1.024V */
979 {
980 *v = (float)(*raw) * 1.024f / 32768.0f; /* get convert adc */
981 }
982 else if (range == ADS1115_RANGE_0P512V) /* if 0.512V */
983 {
984 *v = (float)(*raw) * 0.512f / 32768.0f; /* get convert adc */
985 }
986 else if (range == ADS1115_RANGE_0P256V) /* if 0.256V */
987 {
988 *v = (float)(*raw) * 0.256f / 32768.0f; /* get convert adc */
989 }
990 else
991 {
992 handle->debug_print("ads1115: range is invalid.\n"); /* range is invalid */
993
994 return 1; /* return error */
995 }
996
997 return 0; /* success return 0 */
998}
999
1011{
1012 uint8_t res;
1013 uint16_t conf;
1014
1015 if (handle == NULL) /* check handle */
1016 {
1017 return 2; /* return error */
1018 }
1019 if (handle->inited != 1) /* check handle initialization */
1020 {
1021 return 3; /* return error */
1022 }
1023
1024 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
1025 if (res != 0) /* check error */
1026 {
1027 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
1028
1029 return 1; /* return error */
1030 }
1031 conf &= ~(0x01 << 8); /* set start continuous read */
1032 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
1033 if (res != 0) /* check error */
1034 {
1035 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
1036
1037 return 1; /* return error */
1038 }
1039
1040 return 0; /* success return 0 */
1041}
1042
1054{
1055 uint8_t res;
1056 uint16_t conf;
1057
1058 if (handle == NULL) /* check handle */
1059 {
1060 return 2; /* return error */
1061 }
1062 if (handle->inited != 1) /* check handle initialization */
1063 {
1064 return 3; /* return error */
1065 }
1066
1067 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
1068 if (res != 0) /* check error */
1069 {
1070 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
1071
1072 return 1; /* return error */
1073 }
1074 conf &= ~(0x01 << 8); /* clear bit */
1075 conf |= 1 << 8; /* set stop continues read */
1076 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
1077 if (res != 0) /* check error */
1078 {
1079 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
1080
1081 return 1; /* return error */
1082 }
1083
1084 return 0; /* success return 0 */
1085}
1086
1099{
1100 uint8_t res;
1101 uint16_t conf;
1102
1103 if (handle == NULL) /* check handle */
1104 {
1105 return 2; /* return error */
1106 }
1107 if (handle->inited != 1) /* check handle initialization */
1108 {
1109 return 3; /* return error */
1110 }
1111
1112 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
1113 if (res != 0) /* check error */
1114 {
1115 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
1116
1117 return 1; /* return error */
1118 }
1119 conf &= ~(0x01 << 2); /* clear compare */
1120 conf |= enable << 2; /* set compare */
1121 res = a_ads1115_iic_multiple_write(handle, ADS1115_REG_CONFIG, conf); /* write config */
1122 if (res != 0) /* check error */
1123 {
1124 handle->debug_print("ads1115: write config failed.\n"); /* write config failed */
1125
1126 return 1; /* return error */
1127 }
1128
1129 return 0; /* success return 0 */
1130}
1131
1144{
1145 uint8_t res;
1146 uint16_t conf;
1147
1148 if (handle == NULL) /* check handle */
1149 {
1150 return 2; /* return error */
1151 }
1152 if (handle->inited != 1) /* check handle initialization */
1153 {
1154 return 3; /* return error */
1155 }
1156
1157 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
1158 if (res != 0) /* check error */
1159 {
1160 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
1161
1162 return 1; /* return error */
1163 }
1164 *enable = (ads1115_bool_t)((conf >> 2) & 0x01); /* get compare */
1165
1166 return 0; /* success return 0 */
1167}
1168
1181uint8_t ads1115_set_compare_threshold(ads1115_handle_t *handle, int16_t high_threshold, int16_t low_threshold)
1182{
1183 if (handle == NULL) /* check handle */
1184 {
1185 return 2; /* return error */
1186 }
1187 if (handle->inited != 1) /* check handle initialization */
1188 {
1189 return 3; /* return error */
1190 }
1191
1192 if (a_ads1115_iic_multiple_write(handle, ADS1115_REG_HIGHRESH, high_threshold) != 0) /* write high threshold */
1193 {
1194 handle->debug_print("ads1115: write high threshold failed.\n"); /* write high threshold failed */
1195
1196 return 1; /* return error */
1197 }
1198 if (a_ads1115_iic_multiple_write(handle, ADS1115_REG_LOWRESH, low_threshold) != 0) /* write low threshold */
1199 {
1200 handle->debug_print("ads1115: write low threshold failed.\n"); /* write low threshold failed */
1201
1202 return 1; /* return error */
1203 }
1204
1205 return 0; /* success return 0 */
1206}
1207
1220uint8_t ads1115_get_compare_threshold(ads1115_handle_t *handle, int16_t *high_threshold, int16_t *low_threshold)
1221{
1222 if (handle == NULL) /* check handle */
1223 {
1224 return 2; /* return error */
1225 }
1226 if (handle->inited != 1) /* check handle initialization */
1227 {
1228 return 3; /* return error */
1229 }
1230
1231 if (a_ads1115_iic_multiple_read(handle, ADS1115_REG_HIGHRESH, high_threshold) != 0) /* read high threshold */
1232 {
1233 handle->debug_print("ads1115: read high threshold failed.\n"); /* read high threshold failed */
1234
1235 return 1; /* return error */
1236 }
1237 if (a_ads1115_iic_multiple_read(handle, ADS1115_REG_LOWRESH, low_threshold) != 0) /* read low threshold */
1238 {
1239 handle->debug_print("ads1115: read low threshold failed.\n"); /* read low threshold failed */
1240
1241 return 1; /* return error */
1242 }
1243
1244 return 0; /* success return 0 */
1245}
1246
1259uint8_t ads1115_convert_to_register(ads1115_handle_t *handle, float s, int16_t *reg)
1260{
1261 uint8_t res;
1262 uint8_t range;
1263 uint16_t conf;
1264
1265 if (handle == NULL) /* check handle */
1266 {
1267 return 2; /* return error */
1268 }
1269 if (handle->inited != 1) /* check handle initialization */
1270 {
1271 return 3; /* return error */
1272 }
1273
1274 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
1275 if (res != 0) /* check error */
1276 {
1277 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
1278
1279 return 1; /* return error */
1280 }
1281 range = (ads1115_range_t)((conf >> 9) & 0x07); /* get range conf */
1282 if (range == ADS1115_RANGE_6P144V) /* if 6.144V */
1283 {
1284 *reg = (int16_t)(s * 32768.0f / 6.144f); /* convert to reg */
1285 }
1286 else if (range == ADS1115_RANGE_4P096V) /* if 4.096V */
1287 {
1288 *reg = (int16_t)(s * 32768.0f / 4.096f); /* convert to reg */
1289 }
1290 else if (range == ADS1115_RANGE_2P048V) /* if 2.048V */
1291 {
1292 *reg = (int16_t)(s * 32768.0f / 2.048f); /* convert to reg */
1293 }
1294 else if (range == ADS1115_RANGE_1P024V) /* if 1.024V */
1295 {
1296 *reg = (int16_t)(s * 32768.0f / 1.024f); /* convert to reg */
1297 }
1298 else if (range == ADS1115_RANGE_0P512V) /* if 0.512V */
1299 {
1300 *reg = (int16_t)(s * 32768.0f / 0.512f); /* convert to reg */
1301 }
1302 else if (range == ADS1115_RANGE_0P256V) /* if 0.256V */
1303 {
1304 *reg = (int16_t)(s * 32768.0f / 0.256f); /* convert to reg */
1305 }
1306 else
1307 {
1308 handle->debug_print("ads1115: range is invalid.\n"); /* range is invalid */
1309
1310 return 1; /* return error */
1311 }
1312
1313 return 0; /* success return 0 */
1314}
1315
1328uint8_t ads1115_convert_to_data(ads1115_handle_t *handle, int16_t reg, float *s)
1329{
1330 uint8_t res;
1331 uint8_t range;
1332 uint16_t conf;
1333
1334 if (handle == NULL) /* check handle */
1335 {
1336 return 2; /* return error */
1337 }
1338 if (handle->inited != 1) /* check handle initialization */
1339 {
1340 return 3; /* return error */
1341 }
1342
1343 res = a_ads1115_iic_multiple_read(handle, ADS1115_REG_CONFIG, (int16_t *)&conf); /* read config */
1344 if (res != 0) /* check error */
1345 {
1346 handle->debug_print("ads1115: read config failed.\n"); /* read config failed */
1347
1348 return 1; /* return error */
1349 }
1350 range = (ads1115_range_t)((conf >> 9) & 0x07); /* get range conf */
1351 if (range == ADS1115_RANGE_6P144V) /* if 6.144V */
1352 {
1353 *s = (float)(reg) * 6.144f / 32768.0f; /* convert to data */
1354 }
1355 else if (range == ADS1115_RANGE_4P096V) /* if 4.096V */
1356 {
1357 *s = (float)(reg) * 4.096f / 32768.0f; /* convert to data */
1358 }
1359 else if (range == ADS1115_RANGE_2P048V) /* if 2.048V */
1360 {
1361 *s = (float)(reg) * 2.048f / 32768.0f; /* convert to data */
1362 }
1363 else if (range == ADS1115_RANGE_1P024V) /* if 1.024V */
1364 {
1365 *s = (float)(reg) * 1.024f / 32768.0f; /* convert to data */
1366 }
1367 else if (range == ADS1115_RANGE_0P512V) /* if 0.512V */
1368 {
1369 *s = (float)(reg) * 0.512f / 32768.0f; /* convert to data */
1370 }
1371 else if (range == ADS1115_RANGE_0P256V) /* if 0.256V */
1372 {
1373 *s = (float)(reg) * 0.256f / 32768.0f; /* convert to data */
1374 }
1375 else
1376 {
1377 handle->debug_print("ads1115: range is invalid.\n"); /* range is invalid */
1378
1379 return 1; /* return error */
1380 }
1381
1382 return 0; /* success return 0 */
1383}
1384
1397uint8_t ads1115_set_reg(ads1115_handle_t *handle, uint8_t reg, int16_t value)
1398{
1399 if (handle == NULL) /* check handle */
1400 {
1401 return 2; /* return error */
1402 }
1403 if (handle->inited != 1) /* check handle initialization */
1404 {
1405 return 3; /* return error */
1406 }
1407
1408 return a_ads1115_iic_multiple_write(handle, reg, value); /* write reg */
1409}
1410
1423uint8_t ads1115_get_reg(ads1115_handle_t *handle, uint8_t reg, int16_t *value)
1424{
1425 if (handle == NULL) /* check handle */
1426 {
1427 return 2; /* return error */
1428 }
1429 if (handle->inited != 1) /* check handle initialization */
1430 {
1431 return 3; /* return error */
1432 }
1433
1434 return a_ads1115_iic_multiple_read(handle, reg, value); /* read reg */
1435}
1436
1446{
1447 if (info == NULL) /* check handle */
1448 {
1449 return 2; /* return error */
1450 }
1451
1452 memset(info, 0, sizeof(ads1115_info_t)); /* initialize ads1115 info structure */
1453 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1454 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1455 strncpy(info->interface, "IIC", 8); /* copy interface name */
1456 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1457 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1458 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1459 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1460 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1461 info->driver_version = DRIVER_VERSION; /* set driver version */
1462
1463 return 0; /* success return 0 */
1464}
#define ADS1115_REG_LOWRESH
#define ADS1115_ADDRESS2
#define ADS1115_ADDRESS1
iic address definition
#define MAX_CURRENT
#define ADS1115_ADDRESS3
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define ADS1115_REG_CONFIG
#define TEMPERATURE_MIN
#define ADS1115_REG_HIGHRESH
#define SUPPLY_VOLTAGE_MIN
#define ADS1115_REG_CONVERT
chip register definition
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define ADS1115_ADDRESS4
driver ads1115 header file
uint8_t ads1115_get_channel(ads1115_handle_t *handle, ads1115_channel_t *channel)
get the adc channel
uint8_t ads1115_set_addr_pin(ads1115_handle_t *handle, ads1115_address_t addr_pin)
set the iic address pin
uint8_t ads1115_get_addr_pin(ads1115_handle_t *handle, ads1115_address_t *addr_pin)
get the iic address pin
uint8_t ads1115_start_continuous_read(ads1115_handle_t *handle)
start the chip reading
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_get_rate(ads1115_handle_t *handle, ads1115_rate_t *rate)
get the sample rate
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
ads1115_rate_t
ads1115 channel rate enumeration definition
uint8_t ads1115_get_range(ads1115_handle_t *handle, ads1115_range_t *range)
get the adc range
uint8_t ads1115_single_read(ads1115_handle_t *handle, int16_t *raw, float *v)
read data from the chip once
ads1115_bool_t
ads1115 bool enumeration definition
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
ads1115_range_t
ads1115 range enumeration definition
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_ADDR_VCC
@ ADS1115_ADDR_SDA
@ ADS1115_ADDR_SCL
@ ADS1115_ADDR_GND
@ ADS1115_RANGE_0P256V
@ ADS1115_RANGE_0P512V
@ ADS1115_RANGE_6P144V
@ ADS1115_RANGE_1P024V
@ ADS1115_RANGE_2P048V
@ ADS1115_RANGE_4P096V
uint8_t ads1115_get_reg(ads1115_handle_t *handle, uint8_t reg, int16_t *value)
get the chip register
uint8_t ads1115_set_reg(ads1115_handle_t *handle, uint8_t reg, int16_t value)
set the chip register
uint8_t ads1115_set_alert_pin(ads1115_handle_t *handle, ads1115_pin_t pin)
set the alert pin active status
uint8_t ads1115_get_alert_pin(ads1115_handle_t *handle, ads1115_pin_t *pin)
get 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_get_comparator_queue(ads1115_handle_t *handle, ads1115_comparator_queue_t *comparator_queue)
get the interrupt comparator queue
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
ads1115_comparator_queue_t
ads1115 comparator queue enumeration definition
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
uint8_t ads1115_get_compare_threshold(ads1115_handle_t *handle, int16_t *high_threshold, int16_t *low_threshold)
get the interrupt compare threshold
uint8_t ads1115_get_compare_mode(ads1115_handle_t *handle, ads1115_compare_t *compare)
get the interrupt compare mode
uint8_t ads1115_get_compare(ads1115_handle_t *handle, ads1115_bool_t *enable)
get the interrupt compare status
uint8_t ads1115_convert_to_data(ads1115_handle_t *handle, int16_t reg, float *s)
convert a register raw data to a converted adc data
ads1115_pin_t
ads1115 pin enumeration definition
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
uint32_t driver_version
char manufacturer_name[32]