adc  2.0.0.0
adc.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef _ADC_H_
36 #define _ADC_H_
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_spi_master.h"
54 
55 // -------------------------------------------------------------- PUBLIC MACROS
65 #define ADC_MAP_MIKROBUS( cfg, mikrobus ) \
66  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
67  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
68  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
69  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
70  // End macros group
73 // --------------------------------------------------------------- PUBLIC TYPES
82 #define ADC_RETVAL uint8_t
83 
84 #define ADC_OK 0x00
85 #define ADC_INIT_ERROR 0xFF
86 
90 #define ADC_VREF_3300MV 3300
91 #define ADC_VREF_4096MV 4096
92 #define ADC_VREF_5000MV 5000
93 
97 #define ADC_CH0_OR_CH01 0x00
98 #define ADC_CH1_OR_CH10 0x01
99 #define ADC_CH2_OR_CH23 0x02
100 #define ADC_CH3_OR_CH32 0x03
101 
105 typedef struct
106 {
107  digital_out_t cs;
108 
109  // SPI Module.
110  spi_master_t spi;
111  pin_name_t chip_select;
112 
113  // Single-Ended Channels Data.
114  uint16_t ch0;
115  uint16_t ch1;
116  uint16_t ch2;
117  uint16_t ch3;
118 
119  // Differential Channels Data.
120  uint16_t ch01;
121  uint16_t ch10;
122  uint16_t ch23;
123  uint16_t ch32;
124 
125  // Reference Voltage.
126  uint16_t vref;
127 
128 } adc_t;
129 
133 typedef struct
134 {
135  // Communication GPIO Pins.
136  pin_name_t miso;
137  pin_name_t mosi;
138  pin_name_t sck;
139  pin_name_t cs;
140 
141  // SPI Configuration Variables.
142  uint32_t spi_speed;
143  spi_master_mode_t spi_mode;
144  spi_master_chip_select_polarity_t cs_polarity;
145 
146  // Reference Voltage Selection.
147  uint16_t vref;
148 
149 } adc_cfg_t;
150  // End types group
152 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
158 #ifdef __cplusplus
159 extern "C"{
160 #endif
161 
173 
187 
201 uint16_t adc_get_single_ended_ch( adc_t *ctx, uint8_t channel );
202 
216 uint16_t adc_get_differential_ch( adc_t *ctx, uint8_t channel );
217 
218 #ifdef __cplusplus
219 }
220 #endif
221 #endif // _ADC_H_
222  // End public_function group
225 
226 // ------------------------------------------------------------------------ END
adc_get_differential_ch
uint16_t adc_get_differential_ch(adc_t *ctx, uint8_t channel)
Get Pseudo-Differential Pair function.
adc_t::ch1
uint16_t ch1
Definition: adc.h:115
adc_get_single_ended_ch
uint16_t adc_get_single_ended_ch(adc_t *ctx, uint8_t channel)
Get Single-Ended Channel function.
adc_t::ch0
uint16_t ch0
Definition: adc.h:114
adc_t::ch23
uint16_t ch23
Definition: adc.h:122
adc_cfg_t::cs
pin_name_t cs
Definition: adc.h:139
adc_t
Click context object definition.
Definition: adc.h:106
adc_t::vref
uint16_t vref
Definition: adc.h:126
adc_cfg_t::sck
pin_name_t sck
Definition: adc.h:138
adc_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc.h:144
adc_cfg_t::mosi
pin_name_t mosi
Definition: adc.h:137
adc_t::ch3
uint16_t ch3
Definition: adc.h:117
adc_cfg_t::vref
uint16_t vref
Definition: adc.h:147
adc_cfg_t::miso
pin_name_t miso
Definition: adc.h:136
adc_t::cs
digital_out_t cs
Definition: adc.h:107
adc_cfg_t
Click configuration structure definition.
Definition: adc.h:134
adc_init
ADC_RETVAL adc_init(adc_t *ctx, adc_cfg_t *cfg)
Click Initialization function.
adc_t::ch2
uint16_t ch2
Definition: adc.h:116
adc_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc.h:143
adc_t::chip_select
pin_name_t chip_select
Definition: adc.h:111
adc_cfg_setup
void adc_cfg_setup(adc_cfg_t *cfg)
Configuration Object Setup function.
adc_t::spi
spi_master_t spi
Definition: adc.h:110
adc_t::ch01
uint16_t ch01
Definition: adc.h:120
ADC_RETVAL
#define ADC_RETVAL
Click error code definition.
Definition: adc.h:82
adc_t::ch32
uint16_t ch32
Definition: adc.h:123
adc_t::ch10
uint16_t ch10
Definition: adc.h:121
adc_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc.h:142