adc16  2.0.0.0
adc16.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 ADC16_H
29 #define ADC16_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 ADC16_OPCODE_SINGLE_READ 0x10
74 #define ADC16_OPCODE_SINGLE_WRITE 0x08
75 #define ADC16_OPCODE_SET_BIT 0x18
76 #define ADC16_OPCODE_CLEAR_BIT 0x20
77 #define ADC16_OPCODE_CONTINUOUS_READ 0x30
78 #define ADC16_OPCODE_CONTINUOUS_WRITE 0x28
79 
84 #define ADC16_REG_OPMODE_I2CMODE_STATUS 0x00
85 #define ADC16_REG_DATA_BUFFER_STATUS 0x01
86 #define ADC16_REG_ACCUMULATOR_STATUS 0x02
87 #define ADC16_REG_ALERT_TRIG_CHID 0x03
88 #define ADC16_REG_SEQUENCE_STATUS 0x04
89 #define ADC16_REG_ACC_CH0_LSB 0x08
90 #define ADC16_REG_ACC_CH0_MSB 0x09
91 #define ADC16_REG_ACC_CH1_LSB 0x0A
92 #define ADC16_REG_ACC_CH1_MSB 0x0B
93 #define ADC16_REG_ALERT_LOW_FLAGS 0x0C
94 #define ADC16_REG_ALERT_HIGH_FLAGS 0x0E
95 #define ADC16_REG_DEVICE_RESET 0x14
96 #define ADC16_REG_OFFSET_CAL 0x15
97 #define ADC16_REG_WKEY 0x17
98 #define ADC16_REG_OSC_SEL 0x18
99 #define ADC16_REG_NCLK_SEL 0x19
100 #define ADC16_REG_OPMODE_SEL 0x1C
101 #define ADC16_REG_START_SEQUENCE 0x1E
102 #define ADC16_REG_ABORT_SEQUENCE 0x1F
103 #define ADC16_REG_AUTO_SEQ_CHEN 0x20
104 #define ADC16_REG_CH_INPUT_CFG 0x24
105 #define ADC16_REG_DOUT_FORMAT_CFG 0x28
106 #define ADC16_REG_DATA_BUFFER_OPMODE 0x2C
107 #define ADC16_REG_ACC_EN 0x30
108 #define ADC16_REG_ALERT_CHEN 0x34
109 #define ADC16_REG_PRE_ALT_MAX_EVENT_COUNT 0x36
110 #define ADC16_REG_ALERT_DWC_EN 0x37
111 #define ADC16_REG_DWC_HTH_CH0_LSB 0x38
112 #define ADC16_REG_DWC_HTH_CH0_MSB 0x39
113 #define ADC16_REG_DWC_LTH_CH0_LSB 0x3A
114 #define ADC16_REG_DWC_LTH_CH0_MSB 0x3B
115 #define ADC16_REG_DWC_HTH_CH1_LSB 0x3C
116 #define ADC16_REG_DWC_HTH_CH1_MSB 0x3D
117 #define ADC16_REG_DWC_LTH_CH1_LSB 0x3E
118 #define ADC16_REG_DWC_LTH_CH1_MSB 0x3F
119 #define ADC16_REG_DWC_HYS_CH0 0x40
120 #define ADC16_REG_DWC_HYS_CH1 0x41
121  // adc16_reg
123 
138 #define ADC16_WKEY_UNLOCK 0x0A
139 #define ADC16_WKEY_LOCK 0x00
140 
145 #define ADC16_DEVICE_RESET 0x01
146 
151 #define ADC16_START_CALIBRATION 0x01
152 
157 #define ADC16_CH_CFG_TWO_CH_SINGLE_END 0x00
158 #define ADC16_CH_CFG_SIN_CH_SINGLE_END_RMT_GND 0x01
159 #define ADC16_CH_CFG_SIN_CH_SINGLE_END_PSEUDO_DIFF 0x02
160 
165 #define ADC16_OPMODE_SEL_MAN_WITH_CH0 0x00
166 #define ADC16_OPMODE_SEL_MAN_WITH_AUTO_SEQ 0x04
167 #define ADC16_OPMODE_SEL_AUTO_WITH_AUTO_SEQ 0x06
168 #define ADC16_OPMODE_SEL_HIGH_PREC_WITH_AUTO_SEQ 0x07
169 
174 #define ADC16_AUTOSEQ_EN_CH0 0x01
175 #define ADC16_AUTOSEQ_EN_CH1 0x02
176 #define ADC16_AUTOSEQ_EN_BOTH_CH 0x03
177 
182 #define ADC16_START_SEQUENCE 0x01
183 #define ADC16_ABORT_SEQUENCE 0x01
184 
189 #define ADC16_RESOLUTION 0x0FFF
190 #define ADC16_VREF 3.3
191 
197 #define ADC16_DEV_ADDR_R1_0_R2_DNP 0x1F
198 #define ADC16_DEV_ADDR_R1_11K_R2_DNP 0x1E
199 #define ADC16_DEV_ADDR_R1_33K_R2_DNP 0x1D
200 #define ADC16_DEV_ADDR_R1_100K_R2_DNP 0x1C
201 #define ADC16_DEV_ADDR_R1_DNP_R2_DNP 0x18
202 #define ADC16_DEV_ADDR_R1_DNP_R2_11K 0x19
203 #define ADC16_DEV_ADDR_R1_DNP_R2_33K 0x1A
204 #define ADC16_DEV_ADDR_R1_DNP_R2_100K 0x1B
205  // adc16_set
207 
222 #define ADC16_MAP_MIKROBUS( cfg, mikrobus ) \
223  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
224  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
225  cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_AN ); \
226  cfg.alr = MIKROBUS( mikrobus, MIKROBUS_INT )
227  // adc16_map // adc16
230 
235 typedef struct
236 {
237  // Input pins
238  digital_in_t rdy;
239  digital_in_t alr;
241  // Modules
242  i2c_master_t i2c;
244  // I2C slave address
245  uint8_t slave_address;
247 } adc16_t;
248 
253 typedef struct
254 {
255  pin_name_t scl;
256  pin_name_t sda;
258  pin_name_t rdy;
259  pin_name_t alr;
261  uint32_t i2c_speed;
262  uint8_t i2c_address;
264 } adc16_cfg_t;
265 
270 typedef enum
271 {
272  ADC16_OK = 0,
273  ADC16_ERROR = -1
274 
276 
293 
307 err_t adc16_init ( adc16_t *ctx, adc16_cfg_t *cfg );
308 
321 err_t adc16_default_cfg ( adc16_t *ctx );
322 
335 err_t adc16_single_register_write ( adc16_t *ctx, uint8_t reg, uint8_t data_in );
336 
349 err_t adc16_single_register_read ( adc16_t *ctx, uint8_t reg, uint8_t *data_out );
350 
364 err_t adc16_set_register_bits ( adc16_t *ctx, uint8_t reg, uint8_t bit_mask );
365 
379 err_t adc16_clear_register_bits ( adc16_t *ctx, uint8_t reg, uint8_t bit_mask );
380 
395 err_t adc16_continuous_register_write ( adc16_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
396 
411 err_t adc16_continuous_register_read ( adc16_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
412 
421 uint8_t adc16_get_rdy_pin ( adc16_t *ctx );
422 
431 uint8_t adc16_get_alr_pin ( adc16_t *ctx );
432 
445 err_t adc16_get_voltage ( adc16_t *ctx, float *ain0_voltage, float *ain1_voltage );
446 
447 #ifdef __cplusplus
448 }
449 #endif
450 #endif // ADC16_H
451  // adc16
453 
454 // ------------------------------------------------------------------------ END
adc16_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: adc16.h:261
adc16_single_register_read
err_t adc16_single_register_read(adc16_t *ctx, uint8_t reg, uint8_t *data_out)
ADC 16 single register read function.
adc16_init
err_t adc16_init(adc16_t *ctx, adc16_cfg_t *cfg)
ADC 16 initialization function.
adc16_set_register_bits
err_t adc16_set_register_bits(adc16_t *ctx, uint8_t reg, uint8_t bit_mask)
ADC 16 set register bits function.
adc16_return_value_t
adc16_return_value_t
ADC 16 Click return value data.
Definition: adc16.h:271
adc16_cfg_t::alr
pin_name_t alr
Definition: adc16.h:259
ADC16_OK
@ ADC16_OK
Definition: adc16.h:272
adc16_get_alr_pin
uint8_t adc16_get_alr_pin(adc16_t *ctx)
ADC 16 get alr pin function.
adc16_continuous_register_read
err_t adc16_continuous_register_read(adc16_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len)
ADC 16 continuous register read function.
adc16_get_voltage
err_t adc16_get_voltage(adc16_t *ctx, float *ain0_voltage, float *ain1_voltage)
ADC 16 get voltage function.
adc16_get_rdy_pin
uint8_t adc16_get_rdy_pin(adc16_t *ctx)
ADC 16 get rdy pin function.
adc16_t::i2c
i2c_master_t i2c
Definition: adc16.h:242
adc16_default_cfg
err_t adc16_default_cfg(adc16_t *ctx)
ADC 16 default configuration function.
adc16_continuous_register_write
err_t adc16_continuous_register_write(adc16_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len)
ADC 16 continuous register write function.
adc16_cfg_t
ADC 16 Click configuration object.
Definition: adc16.h:254
adc16_t::slave_address
uint8_t slave_address
Definition: adc16.h:245
adc16_t
ADC 16 Click context object.
Definition: adc16.h:236
adc16_cfg_t::i2c_address
uint8_t i2c_address
Definition: adc16.h:262
adc16_clear_register_bits
err_t adc16_clear_register_bits(adc16_t *ctx, uint8_t reg, uint8_t bit_mask)
ADC 16 clear register bits function.
adc16_cfg_t::scl
pin_name_t scl
Definition: adc16.h:255
adc16_t::alr
digital_in_t alr
Definition: adc16.h:239
adc16_cfg_t::rdy
pin_name_t rdy
Definition: adc16.h:258
adc16_t::rdy
digital_in_t rdy
Definition: adc16.h:238
adc16_cfg_setup
void adc16_cfg_setup(adc16_cfg_t *cfg)
ADC 16 configuration object setup function.
ADC16_ERROR
@ ADC16_ERROR
Definition: adc16.h:273
adc16_cfg_t::sda
pin_name_t sda
Definition: adc16.h:256
adc16_single_register_write
err_t adc16_single_register_write(adc16_t *ctx, uint8_t reg, uint8_t data_in)
ADC 16 single register write function.