adc12  2.0.0.0
adc12.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 ADC12_H
29 #define ADC12_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 ADC12_CMD_SD_MASK 0x80
74 #define ADC12_CMD_SD_DIFF 0x00
75 #define ADC12_CMD_SD_SINGLE_END 0x80
76 
77 #define ADC12_CMD_PD_MASK 0x0C
78 #define ADC12_CMD_PD_PDADCONV 0x00
79 #define ADC12_CMD_PD_IROFF_ADON 0x04
80 #define ADC12_CMD_PD_IRON_ADOFF 0x08
81 #define ADC12_CMD_PD_IRON_ADON 0x0C
82  // adc12_cmd
84 
99 #define ADC12_SINGLE_END_CH0 0
100 #define ADC12_SINGLE_END_CH1 1
101 #define ADC12_SINGLE_END_CH2 2
102 #define ADC12_SINGLE_END_CH3 3
103 #define ADC12_SINGLE_END_CH4 4
104 #define ADC12_SINGLE_END_CH5 5
105 #define ADC12_SINGLE_END_CH6 6
106 #define ADC12_SINGLE_END_CH7 7
107 
112 #define ADC12_DIFF_CH0_P_CH1_N 0
113 #define ADC12_DIFF_CH1_P_CH0_N 1
114 #define ADC12_DIFF_CH2_P_CH3_N 2
115 #define ADC12_DIFF_CH3_P_CH2_N 3
116 #define ADC12_DIFF_CH4_P_CH5_N 4
117 #define ADC12_DIFF_CH5_P_CH4_N 5
118 #define ADC12_DIFF_CH6_P_CH7_N 6
119 #define ADC12_DIFF_CH7_P_CH6_N 7
120 
125 #define ADC12_INTERNAL_VREF 2500
126 
131 #define ADC12_RES 4096.0
132 
138 #define ADC12_DEV_ADDR_DEFAULT 0x48
139 #define ADC12_DEV_ADDR_01 0x49
140 #define ADC12_DEV_ADDR_10 0x4A
141 #define ADC12_DEV_ADDR_11 0x4B
142  // adc12_set
144 
159 #define ADC12_MAP_MIKROBUS( cfg, mikrobus ) \
160  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
161  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA );
162  // adc12_map // adc12
165 
170 typedef struct
171 {
172  // Modules
173 
174  i2c_master_t i2c;
176  // I2C slave address
177 
178  uint8_t slave_address;
180  uint8_t adc12_sd_mode;
181  uint8_t adc12_pd_mode;
183 } adc12_t;
184 
189 typedef struct
190 {
191  pin_name_t scl;
192  pin_name_t sda;
194  uint32_t i2c_speed;
195  uint8_t i2c_address;
197 } adc12_cfg_t;
198 
203 typedef enum
204 {
205  ADC12_OK = 0,
206  ADC12_ERROR = -1
207 
209 
228 
245 err_t adc12_init ( adc12_t *ctx, adc12_cfg_t *cfg );
246 
261 err_t adc12_send_cmd ( adc12_t *ctx, uint8_t cmd_byte );
262 
277 err_t adc12_read_data ( adc12_t *ctx, uint16_t *data_out );
278 
290 void adc12_set_sd_mode ( adc12_t *ctx, uint8_t sd_mode );
291 
302 uint8_t adc12_get_sd_mode ( adc12_t *ctx );
303 
315 void adc12_set_pd_mode ( adc12_t *ctx, uint8_t pd_mode );
316 
327 uint8_t adc12_get_pd_mode ( adc12_t *ctx );
328 
344 err_t adc12_read_raw_data ( adc12_t *ctx, uint8_t ch, uint16_t *data_out );
345 
362 err_t adc12_read_voltage ( adc12_t *ctx, uint8_t ch, uint16_t vref, float *data_out );
363 
364 #ifdef __cplusplus
365 }
366 #endif
367 #endif // ADC12_H
368  // adc12
370 
371 // ------------------------------------------------------------------------ END
adc12_read_raw_data
err_t adc12_read_raw_data(adc12_t *ctx, uint8_t ch, uint16_t *data_out)
ADC 12 read raw ADC data function.
adc12_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: adc12.h:194
adc12_init
err_t adc12_init(adc12_t *ctx, adc12_cfg_t *cfg)
ADC 12 initialization function.
adc12_set_sd_mode
void adc12_set_sd_mode(adc12_t *ctx, uint8_t sd_mode)
ADC 12 set the Single-Ended/Differential Inputs function.
adc12_t::adc12_pd_mode
uint8_t adc12_pd_mode
Definition: adc12.h:181
adc12_send_cmd
err_t adc12_send_cmd(adc12_t *ctx, uint8_t cmd_byte)
ADC 12 send command function.
ADC12_ERROR
@ ADC12_ERROR
Definition: adc12.h:206
adc12_get_pd_mode
uint8_t adc12_get_pd_mode(adc12_t *ctx)
ADC 12 get the Power-Down mode function.
adc12_set_pd_mode
void adc12_set_pd_mode(adc12_t *ctx, uint8_t pd_mode)
ADC 12 set the Power-Down mode function.
adc12_cfg_setup
void adc12_cfg_setup(adc12_cfg_t *cfg)
ADC 12 configuration object setup function.
adc12_t::slave_address
uint8_t slave_address
Definition: adc12.h:178
adc12_t::adc12_sd_mode
uint8_t adc12_sd_mode
Definition: adc12.h:180
adc12_cfg_t::i2c_address
uint8_t i2c_address
Definition: adc12.h:195
adc12_read_voltage
err_t adc12_read_voltage(adc12_t *ctx, uint8_t ch, uint16_t vref, float *data_out)
ADC 12 read voltage function.
adc12_cfg_t
ADC 12 Click configuration object.
Definition: adc12.h:190
adc12_cfg_t::scl
pin_name_t scl
Definition: adc12.h:191
adc12_read_data
err_t adc12_read_data(adc12_t *ctx, uint16_t *data_out)
ADC 12 read data function.
adc12_cfg_t::sda
pin_name_t sda
Definition: adc12.h:192
adc12_t::i2c
i2c_master_t i2c
Definition: adc12.h:174
adc12_t
ADC 12 Click context object.
Definition: adc12.h:171
ADC12_OK
@ ADC12_OK
Definition: adc12.h:205
adc12_get_sd_mode
uint8_t adc12_get_sd_mode(adc12_t *ctx)
ADC 12 get the Single-Ended/Differential Inputs function.
adc12_return_value_t
adc12_return_value_t
ADC 12 Click return value data.
Definition: adc12.h:204