voltmeter  2.0.0.0
voltmeter.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 VOLTMETER_H
36 #define VOLTMETER_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_spi_master.h"
51 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define VOLTMETER_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
66  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
67  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
68  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
69  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN )
70 
76 #define VOLTMETER_OK 0
77 #define VOLTMETER_ERROR -1
78 
84 #define VOLTMETER_DATA_RESOLUTION 0x0FFF
85 #define VOLTMETER_VREF_ISOLATED_GND 17.05f
86 #define VOLTMETER_VREF_SHARED_GND 8.525f
87 #define VOLTMETER_NUM_ADC_CONVERSIONS 100
88 #define VOLTMETER_DATA_OFFSET 15
89 #define VOLTMETER_GND_ISO 1
90 #define VOLTMETER_GND_SHARED 0
91  // End group macro
94 // --------------------------------------------------------------- PUBLIC TYPES
103 typedef struct
104 {
105  // Input pins
106  digital_in_t an;
107 
108  // Modules
109  spi_master_t spi;
110  pin_name_t chip_select;
111 
112 } voltmeter_t;
113 
117 typedef struct
118 {
119  // Communication gpio pins
120  pin_name_t miso;
121  pin_name_t mosi;
122  pin_name_t sck;
123  pin_name_t cs;
124 
125  // Additional gpio pins
126  pin_name_t an;
127 
128  // static variable
129  uint32_t spi_speed;
130  spi_master_mode_t spi_mode;
131  spi_master_chip_select_polarity_t cs_polarity;
132 
134  // End types group
136 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
137 
142 #ifdef __cplusplus
143 extern "C"{
144 #endif
145 
155 
164 
173 
183 float voltmeter_calculate_voltage ( voltmeter_t *ctx, int16_t raw_adc, uint8_t iso_gnd );
184 
185 #ifdef __cplusplus
186 }
187 #endif
188 #endif // _VOLTMETER_H_
189  // End public_function group
192 
193 // ------------------------------------------------------------------------- END
voltmeter_t::spi
spi_master_t spi
Definition: voltmeter.h:109
voltmeter_init
err_t voltmeter_init(voltmeter_t *ctx, voltmeter_cfg_t *cfg)
Initialization function.
voltmeter_cfg_t::an
pin_name_t an
Definition: voltmeter.h:126
voltmeter_cfg_t::mosi
pin_name_t mosi
Definition: voltmeter.h:121
voltmeter_cfg_t::miso
pin_name_t miso
Definition: voltmeter.h:120
voltmeter_calculate_voltage
float voltmeter_calculate_voltage(voltmeter_t *ctx, int16_t raw_adc, uint8_t iso_gnd)
Voltage calculation function.
voltmeter_t::an
digital_in_t an
Definition: voltmeter.h:106
voltmeter_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: voltmeter.h:130
voltmeter_cfg_t::cs
pin_name_t cs
Definition: voltmeter.h:123
voltmeter_cfg_t::sck
pin_name_t sck
Definition: voltmeter.h:122
voltmeter_t::chip_select
pin_name_t chip_select
Definition: voltmeter.h:110
voltmeter_read_raw_data
int16_t voltmeter_read_raw_data(voltmeter_t *ctx)
Read raw ADC value function.
voltmeter_cfg_setup
void voltmeter_cfg_setup(voltmeter_cfg_t *cfg)
Config Object Initialization function.
voltmeter_t
Click ctx object definition.
Definition: voltmeter.h:104
voltmeter_cfg_t::spi_speed
uint32_t spi_speed
Definition: voltmeter.h:129
voltmeter_cfg_t
Click configuration structure definition.
Definition: voltmeter.h:118
voltmeter_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: voltmeter.h:131