A C block allows users to enter custom C code directly without compiling the code, unlike in the case of external DLL blocks where a compiler is needed to compile the code into a DLL. The C block code will be interpreted and executed at runtime by a built-in C interpreter in PSIM.
This block makes it very easy for users to define any functions for the block through the powerful C programming language, and to test their control algorithms in C code implementation.
There are two types of c block:
- The Simplified C Block for simple functions, which is embedded code generation compatible.
- The C Block for more advanced functionality, includes custom function definition and library inclusion.
The interface of the C block dialog window is shown below.
Basic usage of c block
Using the C block is relatively straightforward, you are able to set the number of inputs and outputs to the block for signals to connect to the external simulation. Users are also able to use the predefined variables:
- ‘t‘ for simulation time starting at 0
- ‘delt‘ for simulation time step
- ‘x1, x2…xn’ for input variables
- ‘y1, y2…yn’ for output variables
Both ‘t’ and ‘delt’ variables are set in simulation control in the PSIM schematic.
You are able to declare other variables and data types along with regular conditional logic like ‘if’ or ‘for loops’. The math library is included so that you can use function like ‘sin()’ etc. Regular C code syntax needs to be followed and there is a button to check to make sure that your code will compile.
Further instructions and details can be found in the PSIM User Manual – C Block section.