dac  2.0.0.0
dac.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  */
32 // ----------------------------------------------------------------------------
33 
34 #ifndef DAC_H
35 #define DAC_H
36 
41 #ifdef PREINIT_SUPPORTED
42 #include "preinit.h"
43 #endif
44 
45 #ifdef MikroCCoreVersion
46  #if MikroCCoreVersion >= 1
47  #include "delays.h"
48  #endif
49 #endif
50 
51 #include "drv_digital_out.h"
52 #include "drv_spi_master.h"
53 
54 
55 // -------------------------------------------------------------- PUBLIC MACROS
66 #define DAC_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
68  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
69  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
70  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
71 
77 #define DAC_RETVAL uint8_t
78 
79 #define DAC_OK 0x00
80 #define DAC_INIT_ERROR 0xFF
81 
87 #define DAC_APPLY_SETTINGS 0x30
88 
89 #define DAC_4_BIT_DATA 0x0F
90 #define DAC_8_BIT_DATA 0xFF
91 #define DAC_PERCENTAGE 100
92 #define DAC_ONE_STEP_VAL 819
93 #define DAC_CLAC_VAL 20
94 #define DAC_CALIB_VAL_1 220
95 #define DAC_CALIB_VAL_2 273
96 #define DAC_RESOLUTION 4096
97 #define DAC_STEP_VALUE 1000
98  // End group macro
101 // --------------------------------------------------------------- PUBLIC TYPES
110 typedef struct
111 {
112  digital_out_t cs;
113 
114  // Modules
115 
116  spi_master_t spi;
117  pin_name_t chip_select;
118 
119 } dac_t;
120 
124 typedef struct
125 {
126  // Communication gpio pins
127 
128  pin_name_t miso;
129  pin_name_t mosi;
130  pin_name_t sck;
131  pin_name_t cs;
132 
133  // static variable
134 
135  uint32_t spi_speed;
136  spi_master_mode_t spi_mode;
137  spi_master_chip_select_polarity_t cs_polarity;
138 
139 } dac_cfg_t;
140  // End types group
142 // ------------------------------------------------------------------ CONSTANTS // End constants group
151 // ------------------------------------------------------------------ VARIABLES // End variable group
159 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
160 
165 #ifdef __cplusplus
166 extern "C"{
167 #endif
168 
178 void dac_cfg_setup ( dac_cfg_t *cfg );
179 
189 
198 void dac_set_voltage ( dac_t *ctx, uint16_t v_out );
199 
209 void dac_set_voltage_pct ( dac_t *ctx, uint8_t v_out_pct );
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 #endif // _DAC_H_
215  // End public_function group
218 
219 // ------------------------------------------------------------------------- END
dac_cfg_t::spi_speed
uint32_t spi_speed
Definition: dac.h:135
dac_cfg_t::cs
pin_name_t cs
Definition: dac.h:131
dac_t
Click ctx object definition.
Definition: dac.h:111
DAC_RETVAL
#define DAC_RETVAL
Definition: dac.h:77
dac_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: dac.h:137
dac_cfg_t
Click configuration structure definition.
Definition: dac.h:125
dac_cfg_t::sck
pin_name_t sck
Definition: dac.h:130
dac_cfg_setup
void dac_cfg_setup(dac_cfg_t *cfg)
Config Object Initialization function.
dac_cfg_t::mosi
pin_name_t mosi
Definition: dac.h:129
dac_init
DAC_RETVAL dac_init(dac_t *ctx, dac_cfg_t *cfg)
Initialization function.
dac_t::cs
digital_out_t cs
Definition: dac.h:112
dac_cfg_t::miso
pin_name_t miso
Definition: dac.h:128
dac_t::chip_select
pin_name_t chip_select
Definition: dac.h:117
dac_set_voltage_pct
void dac_set_voltage_pct(dac_t *ctx, uint8_t v_out_pct)
Function write procentage value.
dac_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: dac.h:136
dac_t::spi
spi_master_t spi
Definition: dac.h:116
dac_set_voltage
void dac_set_voltage(dac_t *ctx, uint16_t v_out)
Set voltage function.