Digital Power Starter Kit 3 Firmware  DM330017-3, Rev.3.0
dsPIC33C Buck Converter Peak Current Mode Control Example
v_loop.c
1 /* *********************************************************************************
2  * PowerSmartâ„¢ Digital Control Library Designer, Version 0.9.14.678
3  * *********************************************************************************
4  * 2p2z controller function declarations and compensation filter coefficients
5  * derived for following operating conditions:
6  * *********************************************************************************
7  *
8  * Controller Type: 2P2Z - Basic Current Mode 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.8
16  * CGS Date: 03/12/2021
17  * *********************************************************************************
18  * User: M91406
19  * Date/Time: 04/25/2021 13:39:05
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: 704 Hz
54  * fP1: 132540 Hz
55  * fZ1: 606 Hz
56  *
57  * *********************************************************************************
58  * Filter Coefficients and Parameters:
59  * ********************************************************************************/
60 
61 volatile int32_t v_loop_ACoefficients [2] =
62 {
63  0x45D7FFFF, // Coefficient A1 will be multiplied with controller output u(n-1)
64  0xA2920003 // Coefficient A2 will be multiplied with controller output u(n-2)
65 };
66 
67 volatile int32_t v_loop_BCoefficients [3] =
68 {
69  0x43D7FFFF, // Coefficient B0 will be multiplied with error input e(n-0)
70  0x41E00006, // Coefficient B1 will be multiplied with error input e(n-1)
71  0xBCAEFFFF // Coefficient B2 will be multiplied with error input e(n-2)
72 };
73 
74 // Coefficient normalization factors
75 volatile int16_t v_loop_pre_scaler = 3; // Bit-shift value used to perform input value normalization
76 volatile int16_t v_loop_post_shift_A = 0; // Bit-shift value A used to perform control output value backward normalization
77 volatile int16_t v_loop_post_shift_B = 0; // Bit-shift value B used to perform control output value backward normalization
78 volatile fractional v_loop_post_scaler = 0x0000; // Q15 fractional factor used to perform control output value backward normalization
79 
80 // P-Term Coefficient for Plant Measurements
81 volatile int16_t v_loop_pterm_factor = 0x639F; // Q15 fractional of the P-Term factor
82 volatile int16_t v_loop_pterm_scaler = 0xFFFF; // Bit-shift scaler of the P-Term factor
83 
84 
85 // User-defined NPNZ16b_s controller data object
86 volatile struct NPNZ16b_s v_loop; // user-controller data object
87 
88 /* ********************************************************************************/
89 
90 /* *********************************************************************************
91  * Controller Initialization:
92  * ==========================
93  *
94  * Public controller initialization function loading known default settings
95  * into the NPNZ16b data structure.
96  *
97  * ********************************************************************************/
98 
99 volatile uint16_t v_loop_Initialize(volatile struct NPNZ16b_s* controller)
100 {
101  volatile uint16_t i=0;
102 
103  // Initialize controller data structure at runtime with pre-defined default values
104  controller->status.value = NPNZ_STATUS_CLEAR; // clear all status flag bits (will turn off execution))
105 
106  controller->Filter.ptrACoefficients = &v_loop_coefficients.ACoefficients[0]; // initialize pointer to A-coefficients array
107  controller->Filter.ptrBCoefficients = &v_loop_coefficients.BCoefficients[0]; // initialize pointer to B-coefficients array
108  controller->Filter.ptrControlHistory = &v_loop_histories.ControlHistory[0]; // initialize pointer to control history array
109  controller->Filter.ptrErrorHistory = &v_loop_histories.ErrorHistory[0]; // initialize pointer to error history array
110  controller->Filter.normPostShiftA = v_loop_post_shift_A; // initialize A-coefficients/single bit-shift scaler
111  controller->Filter.normPostShiftB = v_loop_post_shift_B; // initialize B-coefficients/dual/post scale factor bit-shift scaler
112  controller->Filter.normPostScaler = v_loop_post_scaler; // initialize control output value normalization scaling factor
113  controller->Filter.normPreShift = v_loop_pre_scaler; // initialize A-coefficients/single bit-shift scaler
114 
115  controller->Filter.ACoefficientsArraySize = v_loop_ACoefficients_size; // initialize A-coefficients array size
116  controller->Filter.BCoefficientsArraySize = v_loop_BCoefficients_size; // initialize A-coefficients array size
117  controller->Filter.ControlHistoryArraySize = v_loop_ControlHistory_size; // initialize control history array size
118  controller->Filter.ErrorHistoryArraySize = v_loop_ErrorHistory_size; // initialize error history array size
119 
120  // Load default set of A-coefficients from user RAM into controller A-array located in X-Space
121  for(i=0; i<controller->Filter.ACoefficientsArraySize; i++)
122  {
123  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A1 value into v_loop coefficient data space
124  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A2 value into v_loop coefficient data space
125  }
126 
127  // Load default set of B-coefficients from user RAM into controller B-array located in X-Space
128  for(i=0; i<controller->Filter.BCoefficientsArraySize; i++)
129  {
130  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B0 value into v_loop coefficient data space
131  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B1 value into v_loop coefficient data space
132  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B2 value into v_loop coefficient data space
133  }
134 
135  // Clear error and control histories of the 3P3Z controller
137 
138  // Load P-Term factor and scaler into data structure
139  controller->Filter.PTermFactor = v_loop_pterm_factor;
140  controller->Filter.PTermScaler = v_loop_pterm_scaler;
141 
142  return(1);
143 }
144 
145 
146 //**********************************************************************************
147 // Download latest version of this tool here: https://microchip-pic-avr-tools.github.io/powersmart-dcld/
148 //**********************************************************************************
149 
volatile uint16_t v_loop_Initialize(volatile struct NPNZ16b_s *controller)
Initializes controller coefficient arrays and normalization factors.
Definition: v_loop.c:99
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...
volatile struct NPNZ16b_s v_loop
External reference to user-defined NPNZ16b controller data object 'v_loop'.
Definition: v_loop.c:86
volatile int16_t v_loop_pterm_scaler
Bit-shift scaler of the P-Term Coefficient for Plant Measurements.
Definition: v_loop.c:82
volatile int16_t v_loop_pterm_factor
Q15 fractional of the P-Term Coefficient for Plant Measurements.
Definition: v_loop.c:81
volatile uint16_t value
Controller status full register access.
Definition: npnz16b.h:204
volatile uint16_t BCoefficientsArraySize
Size of the B coefficients array in X-space.
Definition: npnz16b.h:293
volatile fractional * ptrControlHistory
Pointer to n delay-line samples located in Y-space with first sample being the most recent.
Definition: npnz16b.h:288
volatile int16_t normPostShiftB
Normalization of B-term control output to Q15 (R/W)
Definition: npnz16b.h:300
volatile uint16_t ErrorHistoryArraySize
Size of the error history array in Y-space.
Definition: npnz16b.h:295
volatile int16_t PTermFactor
Q15 P-Term Coefficient Factor (R/W)
Definition: npnz16b.h:305
volatile int16_t normPostScaler
Control output normalization factor (Q15) (R/W)
Definition: npnz16b.h:301
volatile int32_t * ptrBCoefficients
Pointer to B coefficients located in X-space.
Definition: npnz16b.h:287
volatile int16_t PTermScaler
Q15 P-Term Coefficient Bit-Shift Scaler (R/W)
Definition: npnz16b.h:304
volatile int32_t * ptrACoefficients
Pointer to A coefficients located in X-space.
Definition: npnz16b.h:286
volatile uint16_t ACoefficientsArraySize
Size of the A coefficients array in X-space.
Definition: npnz16b.h:292
volatile uint16_t ControlHistoryArraySize
Size of the control history array in Y-space.
Definition: npnz16b.h:294
volatile int16_t normPostShiftA
Normalization of A-term control output to Q15 (R/W)
Definition: npnz16b.h:299
volatile int16_t normPreShift
Normalization of ADC-resolution to Q15 (R/W)
Definition: npnz16b.h:298
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
Generic NPNZ16b Controller Object. This data structure is the main API data object providing single-p...
Definition: npnz16b.h:502
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
volatile struct NPNZ_FILTER_PARAMS_s Filter
Filter parameters such as pointer to history and coefficient arrays and number scaling.
Definition: npnz16b.h:506
Data structure packing A- and B- coefficient arrays in a linear memory space for optimized DSP code e...
Definition: v_loop.h:54
volatile int32_t ACoefficients[2]
Definition: v_loop.h:55
volatile int32_t BCoefficients[3]
Definition: v_loop.h:56
Data structure packing A- and B- coefficient arrays in a linear memory space for optimized DSP code e...
Definition: v_loop.h:66
volatile fractional ControlHistory[2]
Definition: v_loop.h:67
volatile fractional ErrorHistory[3]
Definition: v_loop.h:68