An external DLL (dynamic link library) block allows users to write code in C/C++, compile it into DLL using Microsoft Visual C/C++, and link it with PSIM. These blocks can be used in either the power circuit or the control circuit.
A DLL block receives values from PSIM as inputs, performs the calculation, and sends the results back to PSIM. PSIM calls the DLL routine at each simulation time step.
However, when the inputs of the DLL block are connected to one of these discrete elements (zero-order hold, unit delay, discrete integrators and differentiators, z-domain
Area for custom code Function selection Input/output ports
transfer function blocks, and digital filters), the DLL block is called only at the discrete sampling times.
Two types of DLL blocks are provided: Simple DLL Block and General DLL Block.
The simple DLL block has a fixed number of inputs and outputs, and the DLL file name is the only parameter that needs to be defined. On the other hand, the general DLL block allows users to define arbitrary number of inputs/outputs and additional parameters.
Users can also customize the DLL block image.
In general, the simple DLL block is easier to program and easier to use.
The images and parameters of the simple DLL blocks are shown below.
Images:
Attribute:
The node with a dot is for the first input (in[0]). The sequence of the input/output nodes is from the top to the bottom.
The images and parameters of the simple DLL blocks are shown below.
Image (for a block with 2 inputs and 3 outputs):
Parameter Description
File Name Name of the DLL file
1-input 3-input 6-input
Attribute:
The node with a dot is for the first input (in[0]). The sequence of the input/output nodes is from the top to the bottom.
By default, users define the number of inputs and outputs. But the number of inputs and outputs, the node names, as well as the number of parameters and the parameter names can all be defined inside the DLL routine. For more details on defining and programming for the general DLL block, please refer to the help file "Help General DLL Block.pdf" and related examples.
The name of the DLL file can be arbitrary. The DLL file can be placed in one of the three places, in the order of precedence: in the PSIM directory, in the same directory as the schematic file that uses the DLL file, or in the directory as defined in the Options ->
Set Path function in PSIM.
Sample DLL files are provided in PSIM, and users can use these files as the templates to Parameter Description
DLL File Name of the DLL file
Input Data File Name of the input data file that the DLL routine reads (optional)
Number of Input Nodes
Number of input nodes (optional) Number of Output
Nodes
Number of output nodes (optional)
IN Nodes List of input nodes (optional) OUT Nodes List of output nodes (optional)
Parameter 1 Parameter to be passed from PSIM into the DLL routine (optional)
Parameter 2 Parameter to be passed from PSIM into the DLL routine (optional)
Edit Image (button) Click this button to edit and customize the image of the DLL block.
Display File (button) Click this button to display the content of the Input Data File (optional).
Read File (button) If the Input Data File is modified, click this button to reload the data file (optional).
write their own. Procedures on how to compile the DLL routine and link with PSIM are provided in these files and in the on-line help.
Example:
The following shows a power factor correction circuit with the inductor current and the load voltage feedback. The input voltage is used to generate the current reference. The control scheme is implemented in a digital environment, with a sampling rate of 30 kHz.
The control scheme is implemented in an external C code and is interfaced to the power circuit through a simple DLL block.
The input of the DLL block are the sampled input voltage, inductor current, and output voltage. One of the DLL block outputs is the modulation wave Vm, which is compared with the carrier wave to generate the PWM gating signal for the switch. The other output is the inductor current reference for monitoring purpose.
Part of the source code, which is in the file “pfc_vi_dll.c”, is shown below. Both the inner current loop and the outer voltage loop use a PI controller. Trapezoidal rule is used to discretize the controllers. Discretization using Backward Euler is also implemented but the codes are commented out.
// This sample program implement the control of the circuit "pfc-vi-dll.sch" in a C routine.
// Input: in[0]=Vin; in[1]=iL; in[2]=Vo // Output: Vm=out[0]; iref=out[1]
// You may change the variable names (say from "t" to "Time").
// But DO NOT change the function name, number of variables, variable type, and sequence.
// Variables:
// t: Time, passed from PSIM by value // delt: Time step, passed from PSIM by value // in: input array, passed from PSIM by reference
// out: output array, sent back to PSIM (Note: the values of out[*] can be modified in PSIM) // The maximum length of the input and output array "in" and "out" is 20.
// Warning: Global variables above the function simuser (t,delt,in,out) are not allowed!!!
#include <math.h>
__declspec(dllexport) void simuser (t, delt, in, out) // Note that all the variables must be defined as "double"
double t, delt;
double *in, *out;
{
// Place your code here...begin
double Voref=10.5, Va, iref, iL, Vo, Vm, errv, erri, Ts=33.33e-6;
static double yv=0., yi=0., uv=0., ui=0.;
// Input
// Store old values uv=33.33*errv;
ui=4761.9*erri;
// Output out[0]=Vm;
out[1]=iref;
// Place your code here...end }