ammeter  2.0.0.0
ammeter.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 AMMETER_H
36 #define AMMETER_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_spi_master.h"
40 
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
53 #define AMMETER_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
55  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
56  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
57  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
58 
64 #define AMMETER_RETVAL uint8_t
65 
66 #define AMMETER_OK 0x00
67 #define AMMETER_INIT_ERROR 0xFF
68 
74 #define AMMETER_SAMPLE_NUM 10
75 #define AMMETER_FILTER_USEFULL_DATA 0x1FFE
76 #define AMMETER_CALC_COEF 1024.0
77 #define AMMETER_STABULISATION_COEF 2.0
78  // End group macro
81 // --------------------------------------------------------------- PUBLIC TYPES
90 typedef struct
91 {
92  // Modules
93 
94  spi_master_t spi;
95 
96 } ammeter_t;
97 
101 typedef struct
102 {
103  // Communication gpio pins
104 
105  pin_name_t miso;
106  pin_name_t mosi;
107  pin_name_t sck;
108  pin_name_t cs;
109 
110  // static variable
111 
112  hal_spi_speed_t spi_speed;
113  hal_spi_mode_t spi_mode;
114 
115 } ammeter_cfg_t;
116  // End types group
118 // ------------------------------------------------------------------ CONSTANTS // End constants group
127 // ------------------------------------------------------------------ VARIABLES // End variable group
135 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
136 
141 #ifdef __cplusplus
142 extern "C"{
143 #endif
144 
153 void ammeter_cfg_setup ( ammeter_cfg_t *cfg );
154 
163 
173 uint16_t ammeter_data_read ( ammeter_t *ctx );
174 
184 uint16_t ammeter_get_raw_data ( ammeter_t *ctx );
185 
197 float ammeter_get_avg_data ( ammeter_t *ctx );
198 
210 float ammeter_amperage ( ammeter_t *ctx );
211 
212 #ifdef __cplusplus
213 }
214 #endif
215 #endif // _AMMETER_H_
216  // End public_function group
219 
220 // ------------------------------------------------------------------------- END
ammeter_cfg_t::spi_mode
hal_spi_mode_t spi_mode
Definition: ammeter.h:113
ammeter_cfg_t
Click configuration structure definition.
Definition: ammeter.h:101
ammeter_cfg_t::spi_speed
hal_spi_speed_t spi_speed
Definition: ammeter.h:112
AMMETER_RETVAL
#define AMMETER_RETVAL
Definition: ammeter.h:64
ammeter_cfg_t::mosi
pin_name_t mosi
Definition: ammeter.h:106
ammeter_t
Click ctx object definition.
Definition: ammeter.h:90
ammeter_cfg_setup
void ammeter_cfg_setup(ammeter_cfg_t *cfg)
Config Object Initialization function.
ammeter_data_read
uint16_t ammeter_data_read(ammeter_t *ctx)
Data read function.
ammeter_get_raw_data
uint16_t ammeter_get_raw_data(ammeter_t *ctx)
Get raw data function.
ammeter_get_avg_data
float ammeter_get_avg_data(ammeter_t *ctx)
Get average average data function.
ammeter_t::spi
spi_master_t spi
Definition: ammeter.h:94
ammeter_cfg_t::cs
pin_name_t cs
Definition: ammeter.h:108
ammeter_init
AMMETER_RETVAL ammeter_init(ammeter_t *ctx, ammeter_cfg_t *cfg)
Initialization function.
ammeter_cfg_t::sck
pin_name_t sck
Definition: ammeter.h:107
ammeter_cfg_t::miso
pin_name_t miso
Definition: ammeter.h:105
ammeter_amperage
float ammeter_amperage(ammeter_t *ctx)
Get amperage function.