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 "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  pin_name_t chip_select;
110 
111 } voltmeter_t;
112 
116 typedef struct
117 {
118  // Communication gpio pins
119 
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 
127  pin_name_t an;
128 
129  // static variable
130 
131  uint32_t spi_speed;
132  uint8_t spi_mode;
133  spi_master_chip_select_polarity_t cs_polarity;
134 
136  // End types group
138 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
139 
144 #ifdef __cplusplus
145 extern "C"{
146 #endif
147 
157 
166 
179 (
180  voltmeter_t *ctx,
181  uint8_t *wr_buf,
182  uint16_t wr_len,
183  uint8_t *rd_buf,
184  uint16_t rd_len
185 );
186 void voltmeter_write_data ( voltmeter_t *ctx, uint16_t write_data );
196 uint8_t voltmeter_read_byte ( voltmeter_t *ctx );
205 uint16_t voltmeter_read_data ( voltmeter_t *ctx );
215 void voltmeter_calibration ( voltmeter_t *ctx );
224 float voltmeter_get_voltage ( voltmeter_t *ctx, uint8_t coefficient );
233 
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 #endif // _VOLTMETER_H_
239  // End public_function group
242 
243 // ------------------------------------------------------------------------- END
pin_name_t mosi
Definition: voltmeter.h:121
pin_name_t miso
Definition: voltmeter.h:120
uint8_t voltmeter_read_byte(voltmeter_t *ctx)
Generic read the byte of the data function.
digital_in_t an
Definition: voltmeter.h:104
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.
VOLTMETER_RETVAL voltmeter_init(voltmeter_t *ctx, voltmeter_cfg_t *cfg)
Initialization function.
Click configuration structure definition.
Definition: voltmeter.h:116
uint8_t spi_mode
Definition: voltmeter.h:132
Click ctx object definition.
Definition: voltmeter.h:96
#define VOLTMETER_RETVAL
Definition: voltmeter.h:66
spi_master_chip_select_polarity_t cs_polarity
Definition: voltmeter.h:133
uint32_t spi_speed
Definition: voltmeter.h:131
pin_name_t sck
Definition: voltmeter.h:122
void voltmeter_generic_transfer(voltmeter_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
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:123
pin_name_t chip_select
Definition: voltmeter.h:109
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:127
void voltmeter_calibration(voltmeter_t *ctx)
Calibration reading function.