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

init_dac.c
1 /*
2 * File: init_opa.c
3 * Author: M91406
4 *
5 * Created on May 19, 2020, 1:19 PM
6 */
7
8
9 #include <xc.h> // include processor files - each processor file is guarded.
10 #include <stdint.h> // include standard integer types header file
11 #include <stdbool.h> // include standard boolean types header file
12
13 #include "config/hal.h"
14 #include "init_dac.h"
15 #include "power_control/devices/dev_boost_typedef.h"
16
21 /***********************************************************************************
22 * @fn uint16_t sysDacModule_Initialize
23 * @brief DAC Module Base Register Initialization
24 * @return unsigned integer
25 * @return 0=failure
26 * @return 1=success
27 *
28 * @details
29 * This function initializes the DAC module base registers for being operated
30 * by the auxiliary oscillator at an input clock frequency of 500 MHz.
31 *
32 * In this application the DAC is used to provide a constant reference voltage
33 * of VDD/2 = 1.65 V to the external current sense shunt amplifier devices.
34 * Neither, built-in comparator nor Pulse-Density Modulator (PDM) ramp generator
35 * are used.
36 *
37 **********************************************************************************/
38
39 volatile uint16_t sysDacModule_Initialize(void)
40 {
41 volatile uint16_t retval=1;
42 volatile struct P33C_DAC_MODULE_s* dac;
43
44 dac = p33c_DacModule_GetHandle();
45 retval &= p33c_DacModule_Dispose();
46 retval &= p33c_DacModule_ConfigWrite(dacModuleDefault);
47
48 return (retval);
49
50 }
51
52 /***********************************************************************************
53 * @fn uint16_t sysDacOutput_Initialize(volatile uint16_t dacInstance, volatile uint16_t initValue)
54 * @brief Initializes a user specified DAC instance with DAC output enabled
55 * @param dacInstance Index of the selected DAC Instance (1=DAC1, 2=DAC2, etc.)
56 * @param initValue Integer value at startup of the DAC peripheral instance
57 * @return unsigned integer (0=failure, 1=success)
58 *
59 * @details
60 * This function clears all registers of the declared DAC instance and
61 * initializes the registers required by a static Digital-to-Analog Converter
62 * output with the given default values.
63 *
64 * @note
65 * The initialization function does not enable the DAC instance. Please use
66 * function sysDacOutput_Enable() to turn on the DAC instance at the appropriate
67 * time.
68 *
69 **********************************************************************************/
70
71 volatile uint16_t sysDacOutput_Initialize(volatile uint16_t dacInstance, volatile uint16_t initValue)
72 {
73 volatile uint16_t retval=1;
74 volatile struct P33C_DAC_INSTANCE_s* dac;
75
76 dac = p33c_DacInstance_GetHandle(dacInstance);
77 retval &= p33c_DacInstance_Dispose(dacInstance);
78
79 dac->DACxCONL.bits.DACOEN = 1; // Enable DAC output pin
80 dac->DACxCONH.bits.TMCB = BOOST_LEB_PERIOD; // Set Leading Edge blanking interval period
81 dac->DACxDATH.value = initValue; // Set DAC output to initial value
82
83 return (retval);
84 }
85
86 /***********************************************************************************
87 * @fn uint16_t sysDacOutput_Enable
88 * @brief Enables a user specified DAC output
89 * @param dacInstance Index of the selected DAC Instance (1=DAC1, 2=DAC2, etc.)
90 * @return unsigned integer (0=failure, 1=success)
91 *
92 * @details
93 * This function enables the DAC output and DAC module specified in parameter
94 * dacInstance. This function should be called after the DAC instance has been
95 * initialized by calling function sysDacOutput_Initialize().
96 *
97 **********************************************************************************/
98
99 volatile uint16_t sysDacOutput_Enable(volatile uint16_t dacInstance) {
100
101 volatile uint16_t retval=1;
102 volatile struct P33C_DAC_MODULE_s* dacmod;
103 volatile struct P33C_DAC_INSTANCE_s* dacinst;
104
105 // Capture DAC module SFR set
106 dacmod = p33c_DacModule_GetHandle();
107 dacmod->DacModuleCtrl1L.bits.DACON = true; // Ensure DAC module is enabled
108
109 // Capture specified DAC instance SFR set
110 dacinst = p33c_DacInstance_GetHandle(dacInstance);
111 dacinst->DACxCONL.bits.DACEN = true; // Enable DAC instance
112 dacinst->DACxCONL.bits.DACOEN = true; // Enable DAC output pin
113
114 return (retval);
115 }
116
117 /***********************************************************************************
118 * @fn uint16_t sysDacOutput_Disable
119 * @brief Disables a user specified DAC output
120 * @param dacInstance Index of the selected DAC Instance (1=DAC1, 2=DAC2, etc.)
121 * @return unsigned integer (0=failure, 1=success)
122 *
123 * @details
124 * This function disables the DAC output and DAC module specified in parameter
125 * dacInstance.
126 *
127 **********************************************************************************/
128
129 volatile uint16_t sysDacOutput_Disable(volatile uint16_t dacInstance) {
130
131 volatile uint16_t retval=1;
132 volatile struct P33C_DAC_INSTANCE_s* dacinst;
133
134 // Capture specified DAC instance SFR set
135 dacinst = p33c_DacInstance_GetHandle(dacInstance);
136 dacinst->DACxCONL.bits.DACOEN = false; // Disable DAC output pin
137 dacinst->DACxCONL.bits.DACEN = false; // Disable DAC instance
138
139 return (retval);
140 } // end of group dac-initialization
142
143 // end of file
sysDacOutput_Initialize
volatile uint16_t sysDacOutput_Initialize(volatile uint16_t dacInstance, volatile uint16_t initValue)
Definition: init_dac.c:71
P33C_DAC_INSTANCE_s::DACxDATH
union P33C_DAC_INSTANCE_s::@81 DACxDATH
sysDacModule_Initialize
volatile uint16_t sysDacModule_Initialize(void)
Definition: init_dac.c:39
P33C_DAC_INSTANCE_s::bits
volatile struct tagDAC1CONLBITS bits
Definition: p33c_dac.h:111
P33C_DAC_INSTANCE_s::DACxCONL
union P33C_DAC_INSTANCE_s::@78 DACxCONL
P33C_DAC_INSTANCE_s::DACxCONH
union P33C_DAC_INSTANCE_s::@79 DACxCONH
BOOST_LEB_PERIOD
#define BOOST_LEB_PERIOD
Leading Edge Blanking = n x PWM resolution (here: 50 x 2ns = 100ns)
Definition: dpsk3_hwdescr.h:988
P33C_DAC_MODULE_s
Definition: p33c_dac.h:60
P33C_DAC_MODULE_s::bits
volatile struct tagDACCTRL1LBITS bits
Definition: p33c_dac.h:62
sysDacOutput_Disable
volatile uint16_t sysDacOutput_Disable(volatile uint16_t dacInstance)
Definition: init_dac.c:129
P33C_DAC_INSTANCE_s::value
volatile uint16_t value
Definition: p33c_dac.h:112
sysDacOutput_Enable
volatile uint16_t sysDacOutput_Enable(volatile uint16_t dacInstance)
Definition: init_dac.c:99
P33C_DAC_INSTANCE_s
Definition: p33c_dac.h:109
P33C_DAC_MODULE_s::DacModuleCtrl1L
union P33C_DAC_MODULE_s::@72 DacModuleCtrl1L