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 
37 #include "mikrosdk_version.h"
38 
39 #ifdef __GNUC__
40 #if mikroSDK_GET_VERSION < 20800ul
41 #include "rcu_delays.h"
42 #else
43 #include "delays.h"
44 #endif
45 #endif
46 
47 #include "drv_digital_out.h"
48 #include "drv_spi_master.h"
49 
50 
51 // -------------------------------------------------------------- PUBLIC MACROS
62 #define DAC_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
64  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
65  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
66  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
67 
73 #define DAC_RETVAL uint8_t
74 
75 #define DAC_OK 0x00
76 #define DAC_INIT_ERROR 0xFF
77 
83 #define DAC_APPLY_SETTINGS 0x30
84 
85 #define DAC_4_BIT_DATA 0x0F
86 #define DAC_8_BIT_DATA 0xFF
87 #define DAC_PERCENTAGE 100
88 #define DAC_ONE_STEP_VAL 819
89 #define DAC_CLAC_VAL 20
90 #define DAC_CALIB_VAL_1 220
91 #define DAC_CALIB_VAL_2 273
92 #define DAC_RESOLUTION 4096
93 #define DAC_STEP_VALUE 1000
94  // End group macro
97 // --------------------------------------------------------------- PUBLIC TYPES
106 typedef struct
107 {
108  digital_out_t cs;
109 
110  // Modules
111 
112  spi_master_t spi;
113  pin_name_t chip_select;
114 
115 } dac_t;
116 
120 typedef struct
121 {
122  // Communication gpio pins
123 
124  pin_name_t miso;
125  pin_name_t mosi;
126  pin_name_t sck;
127  pin_name_t cs;
128 
129  // static variable
130 
131  uint32_t spi_speed;
132  spi_master_mode_t spi_mode;
133  spi_master_chip_select_polarity_t cs_polarity;
134 
135 } dac_cfg_t;
136  // End types group
138 // ------------------------------------------------------------------ CONSTANTS // End constants group
147 // ------------------------------------------------------------------ VARIABLES // End variable group
155 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
156 
161 #ifdef __cplusplus
162 extern "C"{
163 #endif
164 
174 void dac_cfg_setup ( dac_cfg_t *cfg );
175 
185 
194 void dac_set_voltage ( dac_t *ctx, uint16_t v_out );
195 
205 void dac_set_voltage_pct ( dac_t *ctx, uint8_t v_out_pct );
206 
207 #ifdef __cplusplus
208 }
209 #endif
210 #endif // _DAC_H_
211  // End public_function group
214 
215 // ------------------------------------------------------------------------- END
dac_cfg_t::spi_speed
uint32_t spi_speed
Definition: dac.h:131
dac_cfg_t::cs
pin_name_t cs
Definition: dac.h:127
dac_t
Click ctx object definition.
Definition: dac.h:107
DAC_RETVAL
#define DAC_RETVAL
Definition: dac.h:73
dac_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: dac.h:133
dac_cfg_t
Click configuration structure definition.
Definition: dac.h:121
dac_cfg_t::sck
pin_name_t sck
Definition: dac.h:126
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:125
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:108
dac_cfg_t::miso
pin_name_t miso
Definition: dac.h:124
dac_t::chip_select
pin_name_t chip_select
Definition: dac.h:113
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:132
dac_t::spi
spi_master_t spi
Definition: dac.h:112
dac_set_voltage
void dac_set_voltage(dac_t *ctx, uint16_t v_out)
Set voltage function.