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 
38 #include "drv_digital_out.h"
39 #include "drv_spi_master.h"
40 
41 // -------------------------------------------------------------- PUBLIC MACROS
51 #define ADC_MAP_MIKROBUS( cfg, mikrobus ) \
52  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
53  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
54  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
55  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
56  // End macros group
59 // --------------------------------------------------------------- PUBLIC TYPES
68 #define ADC_RETVAL uint8_t
69 
70 #define ADC_OK 0x00
71 #define ADC_INIT_ERROR 0xFF
72 
76 #define ADC_VREF_3300MV 3300
77 #define ADC_VREF_4096MV 4096
78 #define ADC_VREF_5000MV 5000
79 
83 #define ADC_CH0_OR_CH01 0x00
84 #define ADC_CH1_OR_CH10 0x01
85 #define ADC_CH2_OR_CH23 0x02
86 #define ADC_CH3_OR_CH32 0x03
87 
91 typedef struct
92 {
93  digital_out_t cs;
94 
95  // SPI Module.
96  spi_master_t spi;
97  pin_name_t chip_select;
98 
99  // Single-Ended Channels Data.
100  uint16_t ch0;
101  uint16_t ch1;
102  uint16_t ch2;
103  uint16_t ch3;
104 
105  // Differential Channels Data.
106  uint16_t ch01;
107  uint16_t ch10;
108  uint16_t ch23;
109  uint16_t ch32;
110 
111  // Reference Voltage.
112  uint16_t vref;
113 
114 } adc_t;
115 
119 typedef struct
120 {
121  // Communication GPIO Pins.
122  pin_name_t miso;
123  pin_name_t mosi;
124  pin_name_t sck;
125  pin_name_t cs;
126 
127  // SPI Configuration Variables.
128  uint32_t spi_speed;
129  spi_master_mode_t spi_mode;
130  spi_master_chip_select_polarity_t cs_polarity;
131 
132  // Reference Voltage Selection.
133  uint16_t vref;
134 
135 } adc_cfg_t;
136  // End types group
138 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
144 #ifdef __cplusplus
145 extern "C"{
146 #endif
147 
159 
173 
187 uint16_t adc_get_single_ended_ch( adc_t *ctx, uint8_t channel );
188 
202 uint16_t adc_get_differential_ch( adc_t *ctx, uint8_t channel );
203 
204 #ifdef __cplusplus
205 }
206 #endif
207 #endif // _ADC_H_
208  // End public_function group
211 
212 // ------------------------------------------------------------------------ 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:101
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:100
adc_t::ch23
uint16_t ch23
Definition: adc.h:108
adc_cfg_t::cs
pin_name_t cs
Definition: adc.h:125
adc_t
Click context object definition.
Definition: adc.h:92
adc_t::vref
uint16_t vref
Definition: adc.h:112
adc_cfg_t::sck
pin_name_t sck
Definition: adc.h:124
adc_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc.h:130
adc_cfg_t::mosi
pin_name_t mosi
Definition: adc.h:123
adc_t::ch3
uint16_t ch3
Definition: adc.h:103
adc_cfg_t::vref
uint16_t vref
Definition: adc.h:133
adc_cfg_t::miso
pin_name_t miso
Definition: adc.h:122
adc_t::cs
digital_out_t cs
Definition: adc.h:93
adc_cfg_t
Click configuration structure definition.
Definition: adc.h:120
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:102
adc_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc.h:129
adc_t::chip_select
pin_name_t chip_select
Definition: adc.h:97
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:96
adc_t::ch01
uint16_t ch01
Definition: adc.h:106
ADC_RETVAL
#define ADC_RETVAL
Click error code definition.
Definition: adc.h:68
adc_t::ch32
uint16_t ch32
Definition: adc.h:109
adc_t::ch10
uint16_t ch10
Definition: adc.h:107
adc_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc.h:128