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 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_i2c_master.h"
48 
69 #define ADC16_OPCODE_SINGLE_READ 0x10
70 #define ADC16_OPCODE_SINGLE_WRITE 0x08
71 #define ADC16_OPCODE_SET_BIT 0x18
72 #define ADC16_OPCODE_CLEAR_BIT 0x20
73 #define ADC16_OPCODE_CONTINUOUS_READ 0x30
74 #define ADC16_OPCODE_CONTINUOUS_WRITE 0x28
75 
80 #define ADC16_REG_OPMODE_I2CMODE_STATUS 0x00
81 #define ADC16_REG_DATA_BUFFER_STATUS 0x01
82 #define ADC16_REG_ACCUMULATOR_STATUS 0x02
83 #define ADC16_REG_ALERT_TRIG_CHID 0x03
84 #define ADC16_REG_SEQUENCE_STATUS 0x04
85 #define ADC16_REG_ACC_CH0_LSB 0x08
86 #define ADC16_REG_ACC_CH0_MSB 0x09
87 #define ADC16_REG_ACC_CH1_LSB 0x0A
88 #define ADC16_REG_ACC_CH1_MSB 0x0B
89 #define ADC16_REG_ALERT_LOW_FLAGS 0x0C
90 #define ADC16_REG_ALERT_HIGH_FLAGS 0x0E
91 #define ADC16_REG_DEVICE_RESET 0x14
92 #define ADC16_REG_OFFSET_CAL 0x15
93 #define ADC16_REG_WKEY 0x17
94 #define ADC16_REG_OSC_SEL 0x18
95 #define ADC16_REG_NCLK_SEL 0x19
96 #define ADC16_REG_OPMODE_SEL 0x1C
97 #define ADC16_REG_START_SEQUENCE 0x1E
98 #define ADC16_REG_ABORT_SEQUENCE 0x1F
99 #define ADC16_REG_AUTO_SEQ_CHEN 0x20
100 #define ADC16_REG_CH_INPUT_CFG 0x24
101 #define ADC16_REG_DOUT_FORMAT_CFG 0x28
102 #define ADC16_REG_DATA_BUFFER_OPMODE 0x2C
103 #define ADC16_REG_ACC_EN 0x30
104 #define ADC16_REG_ALERT_CHEN 0x34
105 #define ADC16_REG_PRE_ALT_MAX_EVENT_COUNT 0x36
106 #define ADC16_REG_ALERT_DWC_EN 0x37
107 #define ADC16_REG_DWC_HTH_CH0_LSB 0x38
108 #define ADC16_REG_DWC_HTH_CH0_MSB 0x39
109 #define ADC16_REG_DWC_LTH_CH0_LSB 0x3A
110 #define ADC16_REG_DWC_LTH_CH0_MSB 0x3B
111 #define ADC16_REG_DWC_HTH_CH1_LSB 0x3C
112 #define ADC16_REG_DWC_HTH_CH1_MSB 0x3D
113 #define ADC16_REG_DWC_LTH_CH1_LSB 0x3E
114 #define ADC16_REG_DWC_LTH_CH1_MSB 0x3F
115 #define ADC16_REG_DWC_HYS_CH0 0x40
116 #define ADC16_REG_DWC_HYS_CH1 0x41
117  // adc16_reg
119 
134 #define ADC16_WKEY_UNLOCK 0x0A
135 #define ADC16_WKEY_LOCK 0x00
136 
141 #define ADC16_DEVICE_RESET 0x01
142 
147 #define ADC16_START_CALIBRATION 0x01
148 
153 #define ADC16_CH_CFG_TWO_CH_SINGLE_END 0x00
154 #define ADC16_CH_CFG_SIN_CH_SINGLE_END_RMT_GND 0x01
155 #define ADC16_CH_CFG_SIN_CH_SINGLE_END_PSEUDO_DIFF 0x02
156 
161 #define ADC16_OPMODE_SEL_MAN_WITH_CH0 0x00
162 #define ADC16_OPMODE_SEL_MAN_WITH_AUTO_SEQ 0x04
163 #define ADC16_OPMODE_SEL_AUTO_WITH_AUTO_SEQ 0x06
164 #define ADC16_OPMODE_SEL_HIGH_PREC_WITH_AUTO_SEQ 0x07
165 
170 #define ADC16_AUTOSEQ_EN_CH0 0x01
171 #define ADC16_AUTOSEQ_EN_CH1 0x02
172 #define ADC16_AUTOSEQ_EN_BOTH_CH 0x03
173 
178 #define ADC16_START_SEQUENCE 0x01
179 #define ADC16_ABORT_SEQUENCE 0x01
180 
185 #define ADC16_RESOLUTION 0x0FFF
186 #define ADC16_VREF 3.3
187 
193 #define ADC16_DEV_ADDR_R1_0_R2_DNP 0x1F
194 #define ADC16_DEV_ADDR_R1_11K_R2_DNP 0x1E
195 #define ADC16_DEV_ADDR_R1_33K_R2_DNP 0x1D
196 #define ADC16_DEV_ADDR_R1_100K_R2_DNP 0x1C
197 #define ADC16_DEV_ADDR_R1_DNP_R2_DNP 0x18
198 #define ADC16_DEV_ADDR_R1_DNP_R2_11K 0x19
199 #define ADC16_DEV_ADDR_R1_DNP_R2_33K 0x1A
200 #define ADC16_DEV_ADDR_R1_DNP_R2_100K 0x1B
201  // adc16_set
203 
218 #define ADC16_MAP_MIKROBUS( cfg, mikrobus ) \
219  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
220  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
221  cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_AN ); \
222  cfg.alr = MIKROBUS( mikrobus, MIKROBUS_INT )
223  // adc16_map // adc16
226 
231 typedef struct
232 {
233  // Input pins
234  digital_in_t rdy;
235  digital_in_t alr;
237  // Modules
238  i2c_master_t i2c;
240  // I2C slave address
241  uint8_t slave_address;
243 } adc16_t;
244 
249 typedef struct
250 {
251  pin_name_t scl;
252  pin_name_t sda;
254  pin_name_t rdy;
255  pin_name_t alr;
257  uint32_t i2c_speed;
258  uint8_t i2c_address;
260 } adc16_cfg_t;
261 
266 typedef enum
267 {
268  ADC16_OK = 0,
269  ADC16_ERROR = -1
270 
272 
289 
303 err_t adc16_init ( adc16_t *ctx, adc16_cfg_t *cfg );
304 
317 err_t adc16_default_cfg ( adc16_t *ctx );
318 
331 err_t adc16_single_register_write ( adc16_t *ctx, uint8_t reg, uint8_t data_in );
332 
345 err_t adc16_single_register_read ( adc16_t *ctx, uint8_t reg, uint8_t *data_out );
346 
360 err_t adc16_set_register_bits ( adc16_t *ctx, uint8_t reg, uint8_t bit_mask );
361 
375 err_t adc16_clear_register_bits ( adc16_t *ctx, uint8_t reg, uint8_t bit_mask );
376 
391 err_t adc16_continuous_register_write ( adc16_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
392 
407 err_t adc16_continuous_register_read ( adc16_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
408 
417 uint8_t adc16_get_rdy_pin ( adc16_t *ctx );
418 
427 uint8_t adc16_get_alr_pin ( adc16_t *ctx );
428 
441 err_t adc16_get_voltage ( adc16_t *ctx, float *ain0_voltage, float *ain1_voltage );
442 
443 #ifdef __cplusplus
444 }
445 #endif
446 #endif // ADC16_H
447  // adc16
449 
450 // ------------------------------------------------------------------------ END
adc16_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: adc16.h:257
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:267
adc16_cfg_t::alr
pin_name_t alr
Definition: adc16.h:255
ADC16_OK
@ ADC16_OK
Definition: adc16.h:268
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:238
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:250
adc16_t::slave_address
uint8_t slave_address
Definition: adc16.h:241
adc16_t
ADC 16 Click context object.
Definition: adc16.h:232
adc16_cfg_t::i2c_address
uint8_t i2c_address
Definition: adc16.h:258
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:251
adc16_t::alr
digital_in_t alr
Definition: adc16.h:235
adc16_cfg_t::rdy
pin_name_t rdy
Definition: adc16.h:254
adc16_t::rdy
digital_in_t rdy
Definition: adc16.h:234
adc16_cfg_setup
void adc16_cfg_setup(adc16_cfg_t *cfg)
ADC 16 configuration object setup function.
ADC16_ERROR
@ ADC16_ERROR
Definition: adc16.h:269
adc16_cfg_t::sda
pin_name_t sda
Definition: adc16.h:252
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.