adc21  2.1.0.0
adc21.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 ADC21_H
29 #define ADC21_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_spi_master.h"
52 #include "spi_specifics.h"
53 
74 #define ADC21_CHANNEL_0 0
75 #define ADC21_CHANNEL_1 1
76 #define ADC21_CHANNEL_2 2
77 #define ADC21_CHANNEL_3 3
78 #define ADC21_CHANNEL_4 4
79 #define ADC21_CHANNEL_5 5
80 #define ADC21_CHANNEL_6 6
81 #define ADC21_CHANNEL_7 7
82 
87 #define ADC21_DATA_RESOLUTION 0x0FFF
88 #define ADC21_AVCC_3V3 3.3
89 #define ADC21_AVCC_5V 5.0
90 
99 #define ADC21_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
100 #define ADC21_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
101  // adc21_set
103 
118 #define ADC21_MAP_MIKROBUS( cfg, mikrobus ) \
119  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
120  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
121  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
122  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS );
123  // adc21_map // adc21
126 
131 typedef struct
132 {
133  // Modules
134  spi_master_t spi;
136  pin_name_t chip_select;
138 } adc21_t;
139 
144 typedef struct
145 {
146  // Communication gpio pins
147  pin_name_t miso;
148  pin_name_t mosi;
149  pin_name_t sck;
150  pin_name_t cs;
152  // static variable
153  uint32_t spi_speed;
154  spi_master_mode_t spi_mode;
155  spi_master_chip_select_polarity_t cs_polarity;
157 } adc21_cfg_t;
158 
163 typedef enum
164 {
165  ADC21_OK = 0,
166  ADC21_ERROR = -1
167 
169 
186 
200 err_t adc21_init ( adc21_t *ctx, adc21_cfg_t *cfg );
201 
214 err_t adc21_read_raw_adc ( adc21_t *ctx, uint8_t channel, uint16_t *raw_adc );
215 
230 err_t adc21_read_voltage ( adc21_t *ctx, uint8_t channel, float avcc, float *voltage );
231 
232 #ifdef __cplusplus
233 }
234 #endif
235 #endif // ADC21_H
236  // adc21
238 
239 // ------------------------------------------------------------------------ END
ADC21_OK
@ ADC21_OK
Definition: adc21.h:165
adc21_t
ADC 21 Click context object.
Definition: adc21.h:132
adc21_init
err_t adc21_init(adc21_t *ctx, adc21_cfg_t *cfg)
ADC 21 initialization function.
adc21_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc21.h:153
spi_specifics.h
This file contains SPI specific macros, functions, etc.
adc21_cfg_t::sck
pin_name_t sck
Definition: adc21.h:149
adc21_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc21.h:155
adc21_cfg_t::mosi
pin_name_t mosi
Definition: adc21.h:148
adc21_t::chip_select
pin_name_t chip_select
Definition: adc21.h:136
adc21_read_voltage
err_t adc21_read_voltage(adc21_t *ctx, uint8_t channel, float avcc, float *voltage)
ADC 21 read voltage function.
adc21_cfg_t
ADC 21 Click configuration object.
Definition: adc21.h:145
adc21_return_value_t
adc21_return_value_t
ADC 21 Click return value data.
Definition: adc21.h:164
adc21_cfg_t::miso
pin_name_t miso
Definition: adc21.h:147
ADC21_ERROR
@ ADC21_ERROR
Definition: adc21.h:166
adc21_read_raw_adc
err_t adc21_read_raw_adc(adc21_t *ctx, uint8_t channel, uint16_t *raw_adc)
ADC 21 read raw adc function.
adc21_cfg_t::cs
pin_name_t cs
Definition: adc21.h:150
adc21_t::spi
spi_master_t spi
Definition: adc21.h:134
adc21_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc21.h:154
adc21_cfg_setup
void adc21_cfg_setup(adc21_cfg_t *cfg)
ADC 21 configuration object setup function.