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 
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_spi_master.h"
48 #include "spi_specifics.h"
49 
70 #define ADC21_CHANNEL_0 0
71 #define ADC21_CHANNEL_1 1
72 #define ADC21_CHANNEL_2 2
73 #define ADC21_CHANNEL_3 3
74 #define ADC21_CHANNEL_4 4
75 #define ADC21_CHANNEL_5 5
76 #define ADC21_CHANNEL_6 6
77 #define ADC21_CHANNEL_7 7
78 
83 #define ADC21_DATA_RESOLUTION 0x0FFF
84 #define ADC21_AVCC_3V3 3.3
85 #define ADC21_AVCC_5V 5.0
86 
95 #define ADC21_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
96 #define ADC21_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
97  // adc21_set
99 
114 #define ADC21_MAP_MIKROBUS( cfg, mikrobus ) \
115  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
116  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
117  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
118  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS );
119  // adc21_map // adc21
122 
127 typedef struct
128 {
129  // Modules
130  spi_master_t spi;
132  pin_name_t chip_select;
134 } adc21_t;
135 
140 typedef struct
141 {
142  // Communication gpio pins
143  pin_name_t miso;
144  pin_name_t mosi;
145  pin_name_t sck;
146  pin_name_t cs;
148  // static variable
149  uint32_t spi_speed;
150  spi_master_mode_t spi_mode;
151  spi_master_chip_select_polarity_t cs_polarity;
153 } adc21_cfg_t;
154 
159 typedef enum
160 {
161  ADC21_OK = 0,
162  ADC21_ERROR = -1
163 
165 
182 
196 err_t adc21_init ( adc21_t *ctx, adc21_cfg_t *cfg );
197 
210 err_t adc21_read_raw_adc ( adc21_t *ctx, uint8_t channel, uint16_t *raw_adc );
211 
226 err_t adc21_read_voltage ( adc21_t *ctx, uint8_t channel, float avcc, float *voltage );
227 
228 #ifdef __cplusplus
229 }
230 #endif
231 #endif // ADC21_H
232  // adc21
234 
235 // ------------------------------------------------------------------------ END
ADC21_OK
@ ADC21_OK
Definition: adc21.h:161
adc21_t
ADC 21 Click context object.
Definition: adc21.h:128
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:149
spi_specifics.h
This file contains SPI specific macros, functions, etc.
adc21_cfg_t::sck
pin_name_t sck
Definition: adc21.h:145
adc21_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc21.h:151
adc21_cfg_t::mosi
pin_name_t mosi
Definition: adc21.h:144
adc21_t::chip_select
pin_name_t chip_select
Definition: adc21.h:132
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:141
adc21_return_value_t
adc21_return_value_t
ADC 21 Click return value data.
Definition: adc21.h:160
adc21_cfg_t::miso
pin_name_t miso
Definition: adc21.h:143
ADC21_ERROR
@ ADC21_ERROR
Definition: adc21.h:162
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:146
adc21_t::spi
spi_master_t spi
Definition: adc21.h:130
adc21_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc21.h:150
adc21_cfg_setup
void adc21_cfg_setup(adc21_cfg_t *cfg)
ADC 21 configuration object setup function.