| Digital Power Starter Kit 3 Firmware dsPIC33C Boost Converter Voltage Mode Control Example |

p33c_dac.c
1 /* Microchip Technology Inc. and its subsidiaries. You may use this software
2 * and any derivatives exclusively with Microchip products.
3 *
4 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
5 * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
6 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
7 * PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
8 * WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
9 *
10 * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
11 * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
12 * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
13 * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
14 * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
15 * IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
16 * ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
17 *
18 * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
19 * TERMS.
20 */
21
22 // Include standard header files
23 #include <xc.h> // include processor files - each processor file is guarded.
24 #include <stdint.h> // include standard integer data types
25 #include <stdbool.h> // include standard boolean data types
26 #include <stddef.h> // include standard definition data types
27
28 #include "p33c_dac.h"
29
30 // Determine number of available DAC instances on the selected device
31 #if defined (DAC8CONL)
32 // Array of pointers to first register of DAC instances on this device
33 volatile uint16_t* p33cDacInstanceHandles[8]={
34 &DAC1CONL, &DAC2CONL, &DAC3CONL, &DAC4CONL,
35 &DAC5CONL, &DAC6CONL, &DAC7CONL, &DAC8CONL
36 };
37 #define P33C_DAC_COUNT 8
38 #elif defined (DAC7CONL)
39 // Array of pointers to first register of DAC instances on this device
40 volatile uint16_t* p33cDacInstanceHandles[7]={
41 &DAC1CONL, &DAC2CONL, &DAC3CONL, &DAC4CONL,
42 &DAC5CONL, &DAC6CONL, &DAC7CONL
43 };
44 #define P33C_DAC_COUNT 7
45 #elif defined (DAC6CONL)
46 // Array of pointers to first register of DAC instances on this device
47 volatile uint16_t* p33cDacInstanceHandles[6]={
48 &DAC1CONL, &DAC2CONL, &DAC3CONL, &DAC4CONL,
49 &DAC5CONL, &DAC6CONL
50 };
51 #define P33C_DAC_COUNT 6
52 #elif defined (DAC5CONL)
53 // Array of pointers to first register of DAC instances on this device
54 volatile uint16_t* p33cDacInstanceHandles[5]={
55 &DAC1CONL, &DAC2CONL, &DAC3CONL, &DAC4CONL,
56 &DAC5CONL
57 };
58 #define P33C_DAC_COUNT 5
59 #elif defined (DAC4CONL)
60 // Array of pointers to first register of DAC instances on this device
61 volatile uint16_t* p33cDacInstanceHandles[4]={
62 &DAC1CONL, &DAC2CONL, &DAC3CONL, &DAC4CONL
63 };
64 #define P33C_DAC_COUNT 4
65 #elif defined (DAC3CONL)
66 // Array of pointers to first register of DAC instances on this device
67 volatile uint16_t* p33cDacInstanceHandles[3]={
68 &DAC1CONL, &DAC2CONL, &DAC3CONL
69 };
70 #define P33C_DAC_COUNT 3
71 #elif defined (DAC2CONL)
72 // Array of pointers to first register of DAC instances on this device
73 volatile uint16_t* p33cDacInstanceHandles[2]={
74 &DAC1CONL, &DAC2CONL
75 };
76 #define P33C_DAC_COUNT 2
77 #elif defined (DAC1CONL)
78 // Array of pointers to first register of DAC instances on this device
79 volatile uint16_t* p33cDacInstanceHandles[2]={
80 &DAC1CONL
81 };
82 #define P33C_DAC_COUNT 1
83 #endif
84
85
86 /*********************************************************************************
87 * @fn volatile uint16_t p33c_DacModule_Dispose(void)
88 * @ingroup lib-layer-pral-functions-public-dac
89 * @brief Resets all DAC Module registers to their RESET default values
90 * @param void
91 * @return 0 = failure, disposing DAC module was not successful
92 * @return 1 = success, disposing DAC module was successful
93 *
94 * @details
95 * This function clears all DAC module registers to their
96 * default values set when the device comes out of RESET.
97 *
98 * Default configuration:
99 * - all outputs are set to logic functions
100 * - all analog functions are disabled
101 * - all pull-up and pull-down resistors are disabled
102 * - all DACs are operating in push-pull mode (open drain disabled)
103 * - all DACs are configured as input with their signal level HIGH
104 *
105 *********************************************************************************/
106
107 volatile uint16_t p33c_DacModule_Dispose(void)
108 {
109 volatile uint16_t retval=1;
110
111 retval = p33c_DacModule_ConfigWrite(dacModuleConfigClear);
112
113 return(retval);
114 }
115
116 /*********************************************************************************
117 * @fn volatile struct P33C_DAC_MODULE_s p33c_DacModule_ConfigRead(void)
118 * @ingroup lib-layer-pral-functions-public-dac
119 * @brief Read the current configuration from the DAC module base registers
120 * @param void
121 * @return 0 = failure, reading DAC module was not successful (returns NULL)
122 * @return n = success, reading DAC module was successful (returns 16-bit wide pointer)
123 *
124 * @details
125 * This function reads all registers with their current configuration into
126 * a data structure of type P33C_DAC_MODULE_s. Users can read and
127 * verify or modify the configuration to write it back to the DAC module
128 * registers.
129 *
130 *********************************************************************************/
131
132 volatile struct P33C_DAC_MODULE_s p33c_DacModule_ConfigRead(void)
133 {
134 volatile struct P33C_DAC_MODULE_s* dac;
135
136 // Set pointer to memory address of desired DAC instance
137 dac = p33c_DacModule_GetHandle();
138
139 return(*dac);
140
141 }
142
143 /*********************************************************************************
144 * @fn volatile uint16_t p33c_DacModule_ConfigWrite(volatile struct P33C_DAC_MODULE_s dacModuleConfig)
145 * @ingroup lib-layer-pral-functions-public-dac
146 * @brief Writes a user-defined configuration to the DAC module base registers
147 * @param dacModuleConfig Digital-to-Analog converter module configuration of type struct P33C_DAC_MODULE_s
148 * @return 0 = failure, writing DAC module was not successful
149 * @return 1 = success, writing DAC module was successful
150 *
151 * @details
152 * This function writes a user-defined DAC module configuration of type
153 * P33C_DAC_MODULE_s to the DAC module base registers. The
154 * individual register configurations have to be set in user-code
155 * before calling this function. To simplify the configuration process
156 * of standard functions, this driver provides templates, which can be
157 * loaded and written directly.
158 *
159 *********************************************************************************/
160
161 volatile uint16_t p33c_DacModule_ConfigWrite(
162 volatile struct P33C_DAC_MODULE_s dacModuleConfig
163 )
164 {
165 volatile uint16_t retval=1;
166 volatile struct P33C_DAC_MODULE_s* dac;
167
168 // Set pointer to memory address of the DAC module base registers
169 dac = p33c_DacModule_GetHandle();
170 *dac = dacModuleConfig;
171
172 return(retval);
173
174 }
175
176
177 /* ============================================================================== */
178 /* ============================================================================== */
179 /* ============================================================================== */
180
181 /*********************************************************************************
182 * @fn volatile uint16_t p33c_DacInstance_Dispose(volatile uint16_t dacInstance)
183 * @ingroup lib-layer-pral-functions-public-dac
184 * @brief Resets all DAC Instance registers to their RESET default values
185 * @param dacInstance Index of the selected DAC Instance (1=DAC1, 2=DAC2, etc.)
186 * @return 0 = failure, disposing DAC instance was not successful
187 * @return 1 = success, disposing DAC instance was successful
188 *
189 * @details
190 * This function clears all DAC instance registers to their
191 * default values set when the device comes out of RESET.
192 *
193 * Default configuration:
194 * - all outputs are set to logic functions
195 * - all analog functions are disabled
196 * - all pull-up and pull-down resistors are disabled
197 * - all DACs are operating in push-pull mode (open drain disabled)
198 * - all DACs are configured as input with their signal level HIGH
199 *
200 *********************************************************************************/
201
202 volatile uint16_t p33c_DacInstance_Dispose(volatile uint16_t dacInstance)
203 {
204 volatile uint16_t retval=1;
205
206 retval = p33c_DacInstance_ConfigWrite(dacInstance, dacConfigClear);
207
208 return(retval);
209 }
210
211 /*********************************************************************************
212 * @fn volatile struct P33C_DAC_INSTANCE_s p33c_DacInstance_ConfigRead(volatile uint16_t dacInstance)
213 * @ingroup lib-layer-pral-functions-public-dac
214 * @brief Read the current configuration from the DAC instance registers
215 * @param dacInstance Index of the selected DAC Instance (1=DAC1, 2=DAC2, etc.)
216 * @return DAC instance object of type struct P33C_DAC_INSTANCE_s of the selected DAC instance
217 *
218 * @details
219 * This function reads all registers with their current configuration into
220 * a data structure of type P33C_DAC_INSTANCE_s. Users can read and
221 * verify or modify the configuration to write it back to the DAC instance
222 * registers or copy configurations to other instances of the same type.
223 *
224 *********************************************************************************/
225
226 volatile struct P33C_DAC_INSTANCE_s p33c_DacInstance_ConfigRead(volatile uint16_t dacInstance)
227 {
228 volatile struct P33C_DAC_INSTANCE_s* dac;
229
230 // Set pointer to memory address of desired DAC instance
231 dac = p33c_DacInstance_GetHandle(dacInstance);
232
233 return(*dac);
234
235 }
236
237 /*********************************************************************************
238 * @fn volatile uint16_t p33c_DacInstance_ConfigWrite(volatile uint16_t dacInstance, volatile struct P33C_DAC_INSTANCE_s dacConfig)
239 * @ingroup lib-layer-pral-functions-public-dac
240 * @brief Writes a user-defined configuration to the DAC instance registers
241 * @param dacInstance Index of the selected DAC Instance (1=DAC1, 2=DAC2, etc.)
242 * @param dacConfig DAC instance object of type struct P33C_DAC_INSTANCE_s of the selected DAC instance
243 * @return 0 = failure, writing DAC instance was not successful
244 * @return 1 = success, writing DAC instance was successful
245 *
246 * @details
247 * This function writes a user-defined DAC instance configuration of type
248 * P33C_DAC_INSTANCE_s to the DAC instance registers. The
249 * individual register configurations have to be set in user-code
250 * before calling this function. To simplify the configuration process
251 * of standard functions, this driver provides templates, which can be
252 * loaded and written directly.
253 *
254 *********************************************************************************/
255
256 volatile uint16_t p33c_DacInstance_ConfigWrite(
257 volatile uint16_t dacInstance,
258 volatile struct P33C_DAC_INSTANCE_s dacConfig)
259 {
260 volatile uint16_t retval=1;
261 volatile struct P33C_DAC_INSTANCE_s* dac;
262
263 // Set pointer to memory address of desired DAC instance
264 dac = p33c_DacInstance_GetHandle(dacInstance);
265 *dac = dacConfig;
266
267 return(retval);
268
269 }
270
271
272 /* ============================================================================== */
273 /* ============================================================================== */
274 /* ============================================================================== */
275
276 /*********************************************************************************
277 * @var dacModuleConfigClear
278 * @ingroup lib-layer-pral-properties-private-dac
279 * @brief Default RESET configuration of the DAC module base SFRs
280 *
281 * @details
282 * Default configuration of the DAC module SFRs with all its registers
283 * being reset to their default state when the device comes out of RESET.
284 * Programmers can use this template to reset (dispose) a previously used
285 * DAC module when it's not used anymore or to ensure a known startup
286 * condition before writing individual configurations to its SFRs.
287 *
288 ********************************************************************************/
289
290 volatile struct P33C_DAC_MODULE_s dacModuleConfigClear = {
291
292 .DacModuleCtrl1L.value = 0x0000,
293 .DacModuleCtrl2L.value = 0x0000,
294 .DacModuleCtrl2H.value = 0x0000
295
296 };
297
298 /*********************************************************************************
299 * @var dacModuleDefault
300 * @ingroup lib-layer-pral-properties-private-dac
301 * @brief Default configuration of DAC module running from 500 MHz input clock
302 *
303 * @details
304 * Default configuration of the DAC module SFRs with all its registers
305 * being reset to their default state when the device comes out of RESET.
306 * The timing settings for settling time and transition mode time of the
307 * built-in Pulse-Density Modulator ramp generator are reset to their
308 * recommended default values when operated from a 500 MHz clock input.
309 * (Please read the device data sheet for details)
310 *
311 * Programmers can use this template to reset a previously used
312 * DAC module when it's not used anymore or to ensure a known startup
313 * condition before writing individual configurations to its SFRs.
314 *
315 ********************************************************************************/
316
317 volatile struct P33C_DAC_MODULE_s dacModuleDefault = {
318 .DacModuleCtrl1L.value = 0x0080,
319 .DacModuleCtrl2H.bits.SSTIME = 0x008A,
320 .DacModuleCtrl2L.bits.TMODTIME = 0x0055
321 };
322
323 /*********************************************************************************
324 * @var dacConfigClear
325 * @ingroup lib-layer-pral-properties-private-dac
326 * @brief Default RESET configuration of one DAC instance channel SFRs
327 * @param dacConfigClear DAC instance Special Function Register (SFR) set
328 *
329 * @details
330 * Default configuration of the DAC instance SFRs with all its registers
331 * being reset to their default state when the device comes out of RESET.
332 * Programmers can use this template to reset (dispose) a previously used
333 * DAC instance when it's not used anymore or to secure a known startup
334 * condition before writing individual configurations to its SFRs.
335 *
336 * *******************************************************************************/
337
338 volatile struct P33C_DAC_INSTANCE_s dacConfigClear = {
339
340 .DACxCONL.value = 0x0000,
341 .DACxCONH.value = 0x0000,
342 .DACxDATL.value = 0x0000,
343 .DACxDATH.value = 0x0000,
344 .SLPxCONL.value = 0x0000,
345 .SLPxCONH.value = 0x0000,
346 .SLPxDAT.value = 0x0000
347
348 };
349
350
351 // end of file
P33C_DAC_INSTANCE_s::DACxCONL
union P33C_DAC_INSTANCE_s::@78 DACxCONL
P33C_DAC_MODULE_s::value
volatile uint16_t value
Definition: p33c_dac.h:63
P33C_DAC_MODULE_s
Definition: p33c_dac.h:60
P33C_DAC_INSTANCE_s::value
volatile uint16_t value
Definition: p33c_dac.h:112
P33C_DAC_INSTANCE_s
Definition: p33c_dac.h:109
P33C_DAC_MODULE_s::DacModuleCtrl1L
union P33C_DAC_MODULE_s::@72 DacModuleCtrl1L