adc26  2.1.0.0
adc26.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef ADC26_H
29 #define ADC26_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 
73 #define ADC26_REG_CONVERSION 0x00
74 #define ADC26_REG_CONFIG 0x01
75 #define ADC26_REG_LO_THRESH 0x02
76 #define ADC26_REG_HI_THRESH 0x03
77  // adc26_reg
79 
94 #define ADC26_CONFIG_OS_NO_EFFECT 0x0000
95 #define ADC26_CONFIG_OS_START_CONVERSION 0x8000
96 #define ADC26_CONFIG_OS_MASK 0x8000
97 #define ADC26_CONFIG_MUX_P_AIN0_N_AIN1 0x0000
98 #define ADC26_CONFIG_MUX_P_AIN0_N_AIN3 0x1000
99 #define ADC26_CONFIG_MUX_P_AIN1_N_AIN3 0x2000
100 #define ADC26_CONFIG_MUX_P_AIN2_N_AIN3 0x3000
101 #define ADC26_CONFIG_MUX_P_AIN0_N_GND 0x4000
102 #define ADC26_CONFIG_MUX_P_AIN1_N_GND 0x5000
103 #define ADC26_CONFIG_MUX_P_AIN2_N_GND 0x6000
104 #define ADC26_CONFIG_MUX_P_AIN3_N_GND 0x7000
105 #define ADC26_CONFIG_MUX_MASK 0x7000
106 #define ADC26_CONFIG_PGA_6_144V 0x0000
107 #define ADC26_CONFIG_PGA_4_096V 0x0200
108 #define ADC26_CONFIG_PGA_2_048V 0x0400
109 #define ADC26_CONFIG_PGA_1_024V 0x0600
110 #define ADC26_CONFIG_PGA_0_512V 0x0800
111 #define ADC26_CONFIG_PGA_0_256V 0x0A00
112 #define ADC26_CONFIG_PGA_MASK 0x0E00
113 #define ADC26_CONFIG_MODE_CONTINUOUS 0x0000
114 #define ADC26_CONFIG_MODE_SINGLE_SHOT 0x0100
115 #define ADC26_CONFIG_MODE_MASK 0x0100
116 #define ADC26_CONFIG_DR_128_SPS 0x0000
117 #define ADC26_CONFIG_DR_250_SPS 0x0020
118 #define ADC26_CONFIG_DR_490_SPS 0x0040
119 #define ADC26_CONFIG_DR_920_SPS 0x0060
120 #define ADC26_CONFIG_DR_1600_SPS 0x0080
121 #define ADC26_CONFIG_DR_2400_SPS 0x00A0
122 #define ADC26_CONFIG_DR_3300_SPS 0x00C0
123 #define ADC26_CONFIG_DR_MASK 0x00E0
124 #define ADC26_CONFIG_COMP_MODE_TRADITIONAL 0x0000
125 #define ADC26_CONFIG_COMP_MODE_WINDOW 0x0010
126 #define ADC26_CONFIG_COMP_MODE_MASK 0x0010
127 #define ADC26_CONFIG_COMP_POL_ACTIVE_LOW 0x0000
128 #define ADC26_CONFIG_COMP_POL_ACTIVE_HIGH 0x0008
129 #define ADC26_CONFIG_COMP_POL_MASK 0x0008
130 #define ADC26_CONFIG_COMP_LAT_NONLATCHING 0x0000
131 #define ADC26_CONFIG_COMP_LAT_LATCHING 0x0004
132 #define ADC26_CONFIG_COMP_LAT_MASK 0x0004
133 #define ADC26_CONFIG_COMP_QUE_ONE_CONV 0x0000
134 #define ADC26_CONFIG_COMP_QUE_TWO_CONV 0x0001
135 #define ADC26_CONFIG_COMP_QUE_FOUR_CONV 0x0002
136 #define ADC26_CONFIG_COMP_QUE_DISABLE 0x0003
137 #define ADC26_CONFIG_COMP_QUE_MASK 0x0003
138 
143 #define ADC26_MUX_P_AIN0_N_AIN1 0
144 #define ADC26_MUX_P_AIN0_N_AIN3 1
145 #define ADC26_MUX_P_AIN1_N_AIN3 2
146 #define ADC26_MUX_P_AIN2_N_AIN3 3
147 #define ADC26_MUX_P_AIN0_N_GND 4
148 #define ADC26_MUX_P_AIN1_N_GND 5
149 #define ADC26_MUX_P_AIN2_N_GND 6
150 #define ADC26_MUX_P_AIN3_N_GND 7
151 
156 #define ADC26_PGA_0_256V 0
157 #define ADC26_PGA_0_512V 1
158 #define ADC26_PGA_1_024V 2
159 #define ADC26_PGA_2_048V 3
160 #define ADC26_PGA_4_096V 4
161 
166 #define ADC26_ADC_MIN ( -2048 )
167 #define ADC26_ADC_MAX ( 2047 )
168 
173 #define ADC26_ADC_RESOLUTION 2048.0f
174 #define ADC26_ADC_GAIN_MIN 0.256f
175 
181 #define ADC26_DEVICE_ADDRESS_GND 0x48
182 #define ADC26_DEVICE_ADDRESS_VCC 0x49
183 #define ADC26_DEVICE_ADDRESS_SDA 0x4A
184 #define ADC26_DEVICE_ADDRESS_SCL 0x4B
185  // adc26_set
187 
202 #define ADC26_MAP_MIKROBUS( cfg, mikrobus ) \
203  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
204  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
205  cfg.alert = MIKROBUS( mikrobus, MIKROBUS_INT )
206  // adc26_map // adc26
209 
214 typedef struct
215 {
216  // Input pins
217  digital_in_t alert;
219  // Modules
220  i2c_master_t i2c;
222  // I2C slave address
223  uint8_t slave_address;
225 } adc26_t;
226 
231 typedef struct
232 {
233  pin_name_t scl;
234  pin_name_t sda;
236  pin_name_t alert;
238  uint32_t i2c_speed;
239  uint8_t i2c_address;
241 } adc26_cfg_t;
242 
247 typedef enum
248 {
249  ADC26_OK = 0,
250  ADC26_ERROR = -1
251 
253 
270 
284 err_t adc26_init ( adc26_t *ctx, adc26_cfg_t *cfg );
285 
298 err_t adc26_default_cfg ( adc26_t *ctx );
299 
313 err_t adc26_write_reg ( adc26_t *ctx, uint8_t reg, uint16_t data_in );
314 
328 err_t adc26_read_reg ( adc26_t *ctx, uint8_t reg, uint16_t *data_out );
329 
338 uint8_t adc26_get_alert_pin ( adc26_t *ctx );
339 
353 err_t adc26_start_conversion ( adc26_t *ctx, uint8_t mux, uint8_t pga );
354 
367 err_t adc26_write_threshold ( adc26_t *ctx, int16_t lo_thresh, int16_t hi_thresh );
368 
380 err_t adc26_read_adc ( adc26_t *ctx, int16_t *data_out );
381 
393 err_t adc26_read_voltage ( adc26_t *ctx, float *voltage );
394 
395 #ifdef __cplusplus
396 }
397 #endif
398 #endif // ADC26_H
399  // adc26
401 
402 // ------------------------------------------------------------------------ END
adc26_t::alert
digital_in_t alert
Definition: adc26.h:217
adc26_t
ADC 26 Click context object.
Definition: adc26.h:215
adc26_cfg_t::scl
pin_name_t scl
Definition: adc26.h:233
adc26_read_adc
err_t adc26_read_adc(adc26_t *ctx, int16_t *data_out)
ADC 26 read ADC function.
adc26_cfg_t::sda
pin_name_t sda
Definition: adc26.h:234
adc26_cfg_t::alert
pin_name_t alert
Definition: adc26.h:236
adc26_get_alert_pin
uint8_t adc26_get_alert_pin(adc26_t *ctx)
ADC 26 get alert pin function.
adc26_read_reg
err_t adc26_read_reg(adc26_t *ctx, uint8_t reg, uint16_t *data_out)
ADC 26 read reg function.
adc26_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: adc26.h:238
ADC26_ERROR
@ ADC26_ERROR
Definition: adc26.h:250
adc26_return_value_t
adc26_return_value_t
ADC 26 Click return value data.
Definition: adc26.h:248
adc26_cfg_t
ADC 26 Click configuration object.
Definition: adc26.h:232
adc26_t::i2c
i2c_master_t i2c
Definition: adc26.h:220
adc26_t::slave_address
uint8_t slave_address
Definition: adc26.h:223
adc26_start_conversion
err_t adc26_start_conversion(adc26_t *ctx, uint8_t mux, uint8_t pga)
ADC 26 start conversion function.
adc26_cfg_setup
void adc26_cfg_setup(adc26_cfg_t *cfg)
ADC 26 configuration object setup function.
ADC26_OK
@ ADC26_OK
Definition: adc26.h:249
adc26_write_threshold
err_t adc26_write_threshold(adc26_t *ctx, int16_t lo_thresh, int16_t hi_thresh)
ADC 26 write threshold function.
adc26_write_reg
err_t adc26_write_reg(adc26_t *ctx, uint8_t reg, uint16_t data_in)
ADC 26 write reg function.
adc26_init
err_t adc26_init(adc26_t *ctx, adc26_cfg_t *cfg)
ADC 26 initialization function.
adc26_read_voltage
err_t adc26_read_voltage(adc26_t *ctx, float *voltage)
ADC 26 read voltage function.
adc26_default_cfg
err_t adc26_default_cfg(adc26_t *ctx)
ADC 26 default configuration function.
adc26_cfg_t::i2c_address
uint8_t i2c_address
Definition: adc26.h:239