voltmeter  2.0.0.0
voltmeter.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright (c) 2019, MikroElektronika - www.mikroe.com
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef VOLTMETER_H
36 #define VOLTMETER_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_spi_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
54 #define VOLTMETER_MAP_MIKROBUS( cfg, mikrobus ) \
55  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
56  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
57  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
58  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
59  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN )
60 
66 #define VOLTMETER_RETVAL uint8_t
67 
68 #define VOLTMETER_OK 0x00
69 #define VOLTMETER_INIT_ERROR 0xFF
70 
76 #define VOLTMETER_VALUE_5_BIT 0x001F
77 #define VOLTMETER_VALUE_12_BIT 0x0FFF
78 #define VOLTMETER_VCC_5V_COEFF_0 0
79 #define VOLTMETER_VCC_5V_COEFF_1 1
80 #define VOLTMETER_VCC_5V_COEFF_2 2
81 #define VOLTMETER_VCC_5V_COEFF_VAL 5.6487
82 #define VOLTMETER_VCC_5V_COEFFICIENT_VAL 6.1576
83 #define VOLTMETER_VCC_5V_COEFF_PIC32_VAL 10.6857
84  // End group macro
87 // --------------------------------------------------------------- PUBLIC TYPES
96 typedef struct
97 {
98  // Output pins
99 
100  digital_out_t cs;
101 
102  // Input pins
103 
104  digital_in_t an;
105 
106  // Modules
107 
108  spi_master_t spi;
109 
110 } voltmeter_t;
111 
115 typedef struct
116 {
117  // Communication gpio pins
118 
119  pin_name_t miso;
120  pin_name_t mosi;
121  pin_name_t sck;
122  pin_name_t cs;
123 
124  // Additional gpio pins
125 
126  pin_name_t an;
127 
128  // static variable
129 
130  hal_spi_speed_t spi_speed;
131  hal_spi_mode_t spi_mode;
132 
134  // End types group
136 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
137 
142 #ifdef __cplusplus
143 extern "C"{
144 #endif
145 
155 
164 
173 void voltmeter_generic_transfer ( voltmeter_t *ctx, spi_master_transfer_data_t *block );
174 void voltmeter_write_data ( voltmeter_t *ctx, uint16_t write_data );
184 uint8_t voltmeter_read_byte ( voltmeter_t *ctx );
193 uint16_t voltmeter_read_data ( voltmeter_t *ctx );
203 void voltmeter_calibration ( voltmeter_t *ctx );
212 float voltmeter_get_voltage ( voltmeter_t *ctx, uint8_t coefficient );
221 
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 #endif // _VOLTMETER_H_
227  // End public_function group
230 
231 // ------------------------------------------------------------------------- END
pin_name_t mosi
Definition: voltmeter.h:120
pin_name_t miso
Definition: voltmeter.h:119
uint8_t voltmeter_read_byte(voltmeter_t *ctx)
Generic read the byte of the data function.
digital_in_t an
Definition: voltmeter.h:104
hal_spi_speed_t spi_speed
Definition: voltmeter.h:130
hal_spi_mode_t spi_mode
Definition: voltmeter.h:131
uint16_t voltmeter_read_data(voltmeter_t *ctx)
Generic reading 16-bit data function.
float voltmeter_get_voltage(voltmeter_t *ctx, uint8_t coefficient)
Voltage reading function.
void voltmeter_generic_transfer(voltmeter_t *ctx, spi_master_transfer_data_t *block)
Generic transfer function.
VOLTMETER_RETVAL voltmeter_init(voltmeter_t *ctx, voltmeter_cfg_t *cfg)
Initialization function.
Click configuration structure definition.
Definition: voltmeter.h:115
Click ctx object definition.
Definition: voltmeter.h:96
#define VOLTMETER_RETVAL
Definition: voltmeter.h:66
pin_name_t sck
Definition: voltmeter.h:121
spi_master_t spi
Definition: voltmeter.h:108
void voltmeter_cfg_setup(voltmeter_cfg_t *cfg)
Config Object Initialization function.
pin_name_t cs
Definition: voltmeter.h:122
digital_out_t cs
Definition: voltmeter.h:100
void voltmeter_write_data(voltmeter_t *ctx, uint16_t write_data)
Generic write the byte of the data function.
pin_name_t an
Definition: voltmeter.h:126
void voltmeter_calibration(voltmeter_t *ctx)
Calibration reading function.