Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Voltage Mode Control Example
v_loop.c
1 /* *********************************************************************************
2  * PowerSmartâ„¢ Digital Control Library Designer, Version 0.9.12.672
3  * *********************************************************************************
4  * 4p4z controller function declarations and compensation filter coefficients
5  * derived for following operating conditions:
6  * *********************************************************************************
7  *
8  * Controller Type: 4P4Z - Advanced High-Q Compensator
9  * Sampling Frequency: 500000 Hz
10  * Fixed Point Format: Q15
11  * Scaling Mode: 4 - Fast Floating Point Coefficient Scaling
12  * Input Gain: 0.5
13  *
14  * *********************************************************************************
15  * CGS Version: 3.0.7
16  * CGS Date: 03/07/2021
17  * *********************************************************************************
18  * User: M91406
19  * Date/Time: 03/08/2021 12:42:58
20  * ********************************************************************************/
21 
22 #include "v_loop.h"
23 
24 /* *********************************************************************************
25  * Data Arrays:
26  * ============
27  *
28  * This source file declares the default parameters of the z-domain compensation
29  * filter. The NPNZ16b_s data structure contains two pointers to A- and B-
30  * coefficient arrays and two pointers to control and error history arrays.
31  *
32  * For optimized data processing during DSP computations, these arrays must be
33  * located in specific memory locations (X-space for coefficient arrays and
34  * Y-space for control and error history arrays).
35  *
36  * The following declarations are used to define the array data contents, their
37  * length and memory location. These declarations are made publicly accessible
38  * through extern declarations in header file v_loop.h
39  * ********************************************************************************/
40 
41 volatile struct V_LOOP_CONTROL_LOOP_COEFFICIENTS_s __attribute__((space(xmemory), near)) v_loop_coefficients; // A/B-Coefficients
42 volatile uint16_t v_loop_ACoefficients_size = (sizeof(v_loop_coefficients.ACoefficients)/sizeof(v_loop_coefficients.ACoefficients[0])); // A-coefficient array size
43 volatile uint16_t v_loop_BCoefficients_size = (sizeof(v_loop_coefficients.BCoefficients)/sizeof(v_loop_coefficients.BCoefficients[0])); // B-coefficient array size
44 
45 volatile struct V_LOOP_CONTROL_LOOP_HISTORIES_s __attribute__((space(ymemory), far)) v_loop_histories; // Control/Error Histories
46 volatile uint16_t v_loop_ControlHistory_size = (sizeof(v_loop_histories.ControlHistory)/sizeof(v_loop_histories.ControlHistory[0])); // Control history array size
47 volatile uint16_t v_loop_ErrorHistory_size = (sizeof(v_loop_histories.ErrorHistory)/sizeof(v_loop_histories.ErrorHistory[0])); // Error history array size
48 
49 /* *********************************************************************************
50  * Pole&Zero Placement:
51  * *********************************************************************************
52  *
53  * fP0: 604 Hz
54  * fP1: 82540 Hz
55  * fP2: 104860 Hz
56  * fP3: 220000 Hz
57  * fZ1: 2021 Hz
58  * fZ2: 3833 Hz
59  * fZ3: 49866 Hz
60  *
61  * *********************************************************************************
62  * Filter Coefficients and Parameters:
63  * ********************************************************************************/
64 
65 volatile int32_t v_loop_ACoefficients [4] =
66 {
67  0x572EFFFF, // Coefficient A1 will be multiplied with controller output u(n-1)
68  0xA8120001, // Coefficient A2 will be multiplied with controller output u(n-2)
69  0x88A20005, // Coefficient A3 will be multiplied with controller output u(n-3)
70  0x55B30006 // Coefficient A4 will be multiplied with controller output u(n-4)
71 };
72 
73 volatile int32_t v_loop_BCoefficients [5] =
74 {
75  0x43F2FFFC, // Coefficient B0 will be multiplied with error input e(n-0)
76  0x9D6FFFFC, // Coefficient B1 will be multiplied with error input e(n-1)
77  0xBA34FFFD, // Coefficient B2 will be multiplied with error input e(n-2)
78  0x629CFFFC, // Coefficient B3 will be multiplied with error input e(n-3)
79  0xBDFEFFFD // Coefficient B4 will be multiplied with error input e(n-4)
80 };
81 
82 // Coefficient normalization factors
83 volatile int16_t v_loop_pre_scaler = 3; // Bit-shift value used to perform input value normalization
84 volatile int16_t v_loop_post_shift_A = 0; // Bit-shift value A used to perform control output value backward normalization
85 volatile int16_t v_loop_post_shift_B = 0; // Bit-shift value B used to perform control output value backward normalization
86 volatile fractional v_loop_post_scaler = 0x0000; // Q15 fractional factor used to perform control output value backward normalization
87 
88 // P-Term Coefficient for Plant Measurements
89 volatile int16_t v_loop_pterm_factor = 0x639F; // Q15 fractional of the P-Term factor
90 volatile int16_t v_loop_pterm_scaler = 0xFFFF; // Bit-shift scaler of the P-Term factor
91 
92 
93 // User-defined NPNZ16b_s controller data object
94 volatile struct NPNZ16b_s v_loop; // user-controller data object
95 
96 /* ********************************************************************************/
97 
98 /* *********************************************************************************
99  * Controller Initialization:
100  * ==========================
101  *
102  * Public controller initialization function loading known default settings
103  * into the NPNZ16b data structure.
104  *
105  * ********************************************************************************/
106 
107 volatile uint16_t v_loop_Initialize(volatile struct NPNZ16b_s* controller)
108 {
109  volatile uint16_t i=0;
110 
111  // Initialize controller data structure at runtime with pre-defined default values
112  controller->status.value = NPNZ_STATUS_CLEAR; // clear all status flag bits (will turn off execution))
113 
114  controller->Filter.ptrACoefficients = &v_loop_coefficients.ACoefficients[0]; // initialize pointer to A-coefficients array
115  controller->Filter.ptrBCoefficients = &v_loop_coefficients.BCoefficients[0]; // initialize pointer to B-coefficients array
116  controller->Filter.ptrControlHistory = &v_loop_histories.ControlHistory[0]; // initialize pointer to control history array
117  controller->Filter.ptrErrorHistory = &v_loop_histories.ErrorHistory[0]; // initialize pointer to error history array
118  controller->Filter.normPostShiftA = v_loop_post_shift_A; // initialize A-coefficients/single bit-shift scaler
119  controller->Filter.normPostShiftB = v_loop_post_shift_B; // initialize B-coefficients/dual/post scale factor bit-shift scaler
120  controller->Filter.normPostScaler = v_loop_post_scaler; // initialize control output value normalization scaling factor
121  controller->Filter.normPreShift = v_loop_pre_scaler; // initialize A-coefficients/single bit-shift scaler
122 
123  controller->Filter.ACoefficientsArraySize = v_loop_ACoefficients_size; // initialize A-coefficients array size
124  controller->Filter.BCoefficientsArraySize = v_loop_BCoefficients_size; // initialize A-coefficients array size
125  controller->Filter.ControlHistoryArraySize = v_loop_ControlHistory_size; // initialize control history array size
126  controller->Filter.ErrorHistoryArraySize = v_loop_ErrorHistory_size; // initialize error history array size
127 
128  // Load default set of A-coefficients from user RAM into controller A-array located in X-Space
129  for(i=0; i<controller->Filter.ACoefficientsArraySize; i++)
130  {
131  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A1 value into v_loop coefficient data space
132  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A2 value into v_loop coefficient data space
133  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A3 value into v_loop coefficient data space
134  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A4 value into v_loop coefficient data space
135  }
136 
137  // Load default set of B-coefficients from user RAM into controller B-array located in X-Space
138  for(i=0; i<controller->Filter.BCoefficientsArraySize; i++)
139  {
140  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B0 value into v_loop coefficient data space
141  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B1 value into v_loop coefficient data space
142  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B2 value into v_loop coefficient data space
143  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B3 value into v_loop coefficient data space
144  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B4 value into v_loop coefficient data space
145  }
146 
147  // Clear error and control histories of the 3P3Z controller
149 
150  // Load P-Term factor and scaler into data structure
151  controller->Filter.PTermFactor = v_loop_pterm_factor;
152  controller->Filter.PTermScaler = v_loop_pterm_scaler;
153 
154  return(1);
155 }
156 
157 
158 //**********************************************************************************
159 // Download latest version of this tool here: https://areiter128.github.io/DCLD
160 //**********************************************************************************
161 
v_loop
volatile struct NPNZ16b_s v_loop
External reference to user-defined NPNZ16b controller data object 'v_loop'.
Definition: v_loop.c:94
NPNZ_FILTER_PARAMS_s::ptrACoefficients
volatile int32_t * ptrACoefficients
Pointer to A coefficients located in X-space.
Definition: npnz16b.h:286
NPNZ16b_s
Global NPNZ controller data object.
Definition: npnz16b.h:502
NPNZ_FILTER_PARAMS_s::ControlHistoryArraySize
volatile uint16_t ControlHistoryArraySize
Size of the control history array in Y-space.
Definition: npnz16b.h:294
NPNZ_STATUS_s::value
volatile uint16_t value
Controller status full register access.
Definition: npnz16b.h:204
NPNZ_FILTER_PARAMS_s::ptrErrorHistory
volatile fractional * ptrErrorHistory
Pointer to n+1 delay-line samples located in Y-space with first sample being the most recent.
Definition: npnz16b.h:289
NPNZ16b_s::Filter
volatile struct NPNZ_FILTER_PARAMS_s Filter
Filter parameters such as pointer to history and coefficient arrays and number scaling.
Definition: npnz16b.h:506
NPNZ16b_s::status
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
v_loop_pterm_scaler
volatile int16_t v_loop_pterm_scaler
Bit-shift scaler of the P-Term Coefficient for Plant Measurements.
Definition: v_loop.c:90
v_loop_Initialize
volatile uint16_t v_loop_Initialize(volatile struct NPNZ16b_s *controller)
Initializes controller coefficient arrays and normalization factors.
Definition: v_loop.c:107
V_LOOP_CONTROL_LOOP_HISTORIES_s
Data structure packing A- and B- coefficient arrays in a linear memory space for optimized DSP code e...
Definition: v_loop.h:66
v_loop_Reset
void v_loop_Reset(volatile struct NPNZ16b_s *controller)
Prototype of the Assembly routine '_v_loop_Reset' clearing the NPNZ16b controller output and error hi...
NPNZ_FILTER_PARAMS_s::PTermScaler
volatile int16_t PTermScaler
Q15 P-Term Coefficient Bit-Shift Scaler (R/W)
Definition: npnz16b.h:304
v_loop_pterm_factor
volatile int16_t v_loop_pterm_factor
Q15 fractional of the P-Term Coefficient for Plant Measurements.
Definition: v_loop.c:89
NPNZ_FILTER_PARAMS_s::normPreShift
volatile int16_t normPreShift
Normalization of ADC-resolution to Q15 (R/W)
Definition: npnz16b.h:298
NPNZ_FILTER_PARAMS_s::BCoefficientsArraySize
volatile uint16_t BCoefficientsArraySize
Size of the B coefficients array in X-space.
Definition: npnz16b.h:293
V_LOOP_CONTROL_LOOP_COEFFICIENTS_s
Data structure packing A- and B- coefficient arrays in a linear memory space for optimized DSP code e...
Definition: v_loop.h:54
NPNZ_FILTER_PARAMS_s::normPostShiftA
volatile int16_t normPostShiftA
Normalization of A-term control output to Q15 (R/W)
Definition: npnz16b.h:299
NPNZ_FILTER_PARAMS_s::ptrControlHistory
volatile fractional * ptrControlHistory
Pointer to n delay-line samples located in Y-space with first sample being the most recent.
Definition: npnz16b.h:288
NPNZ_FILTER_PARAMS_s::ACoefficientsArraySize
volatile uint16_t ACoefficientsArraySize
Size of the A coefficients array in X-space.
Definition: npnz16b.h:292
NPNZ_FILTER_PARAMS_s::normPostScaler
volatile int16_t normPostScaler
Control output normalization factor (Q15) (R/W)
Definition: npnz16b.h:301
NPNZ_FILTER_PARAMS_s::ErrorHistoryArraySize
volatile uint16_t ErrorHistoryArraySize
Size of the error history array in Y-space.
Definition: npnz16b.h:295
NPNZ_FILTER_PARAMS_s::normPostShiftB
volatile int16_t normPostShiftB
Normalization of B-term control output to Q15 (R/W)
Definition: npnz16b.h:300
NPNZ_FILTER_PARAMS_s::PTermFactor
volatile int16_t PTermFactor
Q15 P-Term Coefficient Factor (R/W)
Definition: npnz16b.h:305
NPNZ_FILTER_PARAMS_s::ptrBCoefficients
volatile int32_t * ptrBCoefficients
Pointer to B coefficients located in X-space.
Definition: npnz16b.h:287