SELKIELogger  1.0.0
I2C-ADS1015.h.in
1 #ifndef SELKIELoggerI2C_ADS1015
2 #define SELKIELoggerI2C_ADS1015
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 
7 #include <math.h>
8 
20 #define ADS1015_ADDR_DEFAULT 0x48
21 #define ADS1015_ADDR_ALT1 0x49
22 #define ADS1015_ADDR_ALT2 0x4A
23 #define ADS1015_ADDR_ALT3 0x4B
24 
25 #define ADS1015_REG_CONFIG 0x01
26 #define ADS1015_REG_RESULT 0x00
27 
28 #define ADS1015_CONFIG_STATE_CLEAR 0x7FFF
29 #define ADS1015_CONFIG_STATE_SELECT 0x8000
30 #define ADS1015_CONFIG_STATE_IDLE 0x0000
31 #define ADS1015_CONFIG_STATE_CONVERT 0x8000
32 
33 #define ADS1015_CONFIG_MUX_CLEAR 0x8FFF
34 #define ADS1015_CONFIG_MUX_SELECT 0x7000
35 #define ADS1015_CONFIG_MUX_DIFF_0_1 0x0000
36 #define ADS1015_CONFIG_MUX_DIFF_0_3 0x1000
37 #define ADS1015_CONFIG_MUX_DIFF_1_3 0x2000
38 #define ADS1015_CONFIG_MUX_DIFF_2_3 0x3000
39 #define ADS1015_CONFIG_MUX_SINGLE_0 0x4000
40 #define ADS1015_CONFIG_MUX_SINGLE_1 0x5000
41 #define ADS1015_CONFIG_MUX_SINGLE_2 0x6000
42 #define ADS1015_CONFIG_MUX_SINGLE_3 0x7000
43 
44 #define ADS1015_CONFIG_PGA_CLEAR 0xF1FF
45 #define ADS1015_CONFIG_PGA_SELECT 0x0E00
46 #define ADS1015_CONFIG_PGA_6144MV 0x0000
47 #define ADS1015_CONFIG_PGA_4096MV 0x0200
48 #define ADS1015_CONFIG_PGA_2048MV 0x0400
49 #define ADS1015_CONFIG_PGA_1024MV 0x0600
50 #define ADS1015_CONFIG_PGA_0512MV 0x0800
51 #define ADS1015_CONFIG_PGA_0256MV 0x0A00
52 #define ADS1015_CONFIG_PGA_0256MV2 0x0C00
53 #define ADS1015_CONFIG_PGA_0256MV3 0x0E00
54 
55 #define ADS1015_CONFIG_PGA_DEFAULT @DEFAULT_ADS1015_GAIN@
56 
57 #define ADS1015_CONFIG_MODE_CLEAR 0xFEFF
58 #define ADS1015_CONFIG_MODE_SELECT 0x0100
59 #define ADS1015_CONFIG_MODE_CONTIN 0x0000
60 #define ADS1015_CONFIG_MODE_SINGLE 0x0100
61 
62 #define ADS1015_CONFIG_DRATE_CLEAR 0xFF1F
63 #define ADS1015_CONFIG_DRATE_SELECT 0x00E0
64 #define ADS1015_CONFIG_DRATE_0128 0x0000
65 #define ADS1015_CONFIG_DRATE_0250 0x0020
66 #define ADS1015_CONFIG_DRATE_0490 0x0040
67 #define ADS1015_CONFIG_DRATE_0920 0x0060
68 #define ADS1015_CONFIG_DRATE_1600 0x0080
69 #define ADS1015_CONFIG_DRATE_2400 0x00A0
70 #define ADS1015_CONFIG_DRATE_3300 0x00C0
71 #define ADS1015_CONFIG_DRATE_33002 0x00E0
72 
73 #define ADS1015_CONFIG_COMP_CLEAR 0xFFEF
74 #define ADS1015_CONFIG_COMP_SELECT 0x0010
75 #define ADS1015_CONFIG_COMP_TRAD 0x0000
76 #define ADS1015_CONFIG_COMP_WINDOW 0x0010
77 
78 #define ADS1015_CONFIG_CPOL_CLEAR 0xFFF7
79 #define ADS1015_CONFIG_CPOL_SELECT 0x0008
80 #define ADS1015_CONFIG_CPOL_ALOW 0x0000
81 #define ADS1015_CONFIG_CPOL_AHIGH 0x0008
82 
83 #define ADS1015_CONFIG_CLATCH_CLEAR 0xFFFB
84 #define ADS1015_CONFIG_CLATCH_SELECT 0x0004
85 #define ADS1015_CONFIG_CLATCH_NO 0x0000
86 #define ADS1015_CONFIG_CLATCH_YES 0x0004
87 
88 #define ADS1015_CONFIG_QUEUE_CLEAR 0xFFFC
89 #define ADS1015_CONFIG_QUEUE_SELECT 0x0003
90 #define ADS1015_CONFIG_QUEUE_LEN1 0x0000
91 #define ADS1015_CONFIG_QUEUE_LEN2 0x0001
92 #define ADS1015_CONFIG_QUEUE_LEN4 0x0002
93 #define ADS1015_CONFIG_QUEUE_DISABLE 0x0003
94 
95 #define ADS1015_CONFIG_DEFAULT ((ADS1015_CONFIG_STATE_IDLE | ADS1015_CONFIG_MUX_DIFF_0_1 | ADS1015_CONFIG_PGA_6144MV | ADS1015_CONFIG_MODE_SINGLE | ADS1015_CONFIG_DRATE_0250 | ADS1015_CONFIG_COMP_TRAD | ADS1015_CONFIG_CPOL_ALOW | ADS1015_CONFIG_CLATCH_NO | ADS1015_CONFIG_QUEUE_DISABLE))
96 
97 
104 typedef struct {
105  float scale;
106  float offset;
107  float min;
108  float max;
109  uint16_t pga;
111 
112 #define I2C_ADS1015_DEFAULTS {.scale = 1.0, .offset=0.0, .min=-INFINITY, .max=INFINITY}
113 
115 uint16_t i2c_ads1015_read_configuration(const int busHandle, const int devAddr);
116 
118 float i2c_ads1015_read_mux(const int busHandle, const int devAddr, const uint16_t mux, const i2c_ads1015_options *opts);
119 
121 float i2c_ads1015_pga_to_scale_factor(const uint16_t config);
122 
124 float i2c_ads1015_read_ch0(const int busHandle, const int devAddr, const void *opts);
125 
127 float i2c_ads1015_read_ch1(const int busHandle, const int devAddr, const void *opts);
128 
130 float i2c_ads1015_read_ch2(const int busHandle, const int devAddr, const void *opts);
131 
133 float i2c_ads1015_read_ch3(const int busHandle, const int devAddr, const void *opts);
134 
136 float i2c_ads1015_read_diff_ch0_ch1(const int busHandle, const int devAddr, const void *opts);
137 
139 float i2c_ads1015_read_diff_ch0_ch3(const int busHandle, const int devAddr, const void *opts);
140 
142 float i2c_ads1015_read_diff_ch1_ch3(const int busHandle, const int devAddr, const void *opts);
143 
145 float i2c_ads1015_read_diff_ch2_ch3(const int busHandle, const int devAddr, const void *opts);
147 #endif
float i2c_ads1015_read_ch1(const int busHandle, const int devAddr, const void *opts)
Get single-ended voltage measurement from channel 1.
Definition: I2C-ADS1015.c:177
float i2c_ads1015_read_ch2(const int busHandle, const int devAddr, const void *opts)
Get single-ended voltage measurement from channel 2.
Definition: I2C-ADS1015.c:191
float i2c_ads1015_read_mux(const int busHandle, const int devAddr, const uint16_t mux, const i2c_ads1015_options *opts)
Generic ADS1015 read function.
Definition: I2C-ADS1015.c:91
float i2c_ads1015_pga_to_scale_factor(const uint16_t config)
Get number of millivolts represented by LSB at a given PGA value.
Definition: I2C-ADS1015.c:55
float i2c_ads1015_read_diff_ch0_ch3(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 0 and 3.
Definition: I2C-ADS1015.c:234
float i2c_ads1015_read_diff_ch0_ch1(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 0 and 1.
Definition: I2C-ADS1015.c:220
float i2c_ads1015_read_ch0(const int busHandle, const int devAddr, const void *opts)
Get single-ended voltage measurement from channel 0.
Definition: I2C-ADS1015.c:163
float i2c_ads1015_read_ch3(const int busHandle, const int devAddr, const void *opts)
Get single-ended voltage measurement from channel 3.
Definition: I2C-ADS1015.c:206
float i2c_ads1015_read_diff_ch2_ch3(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 2 and 3.
Definition: I2C-ADS1015.c:262
uint16_t i2c_ads1015_read_configuration(const int busHandle, const int devAddr)
Read configuration from device.
Definition: I2C-ADS1015.c:40
float i2c_ads1015_read_diff_ch1_ch3(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 1 and 3.
Definition: I2C-ADS1015.c:248
float offset
Add this amount to received value.
float scale
Scale received value by this quantity.
float max
If not NaN, largest value considered valid.
float min
If not NaN, smallest value considered valid.
uint16_t pga
PGA setting.