I have create a C++ block to generate PWM signals (high-side & low-side) with inputs as frequency, duty cycle, delay …
This block alone works perfectly fine !
Then I would like to copy paste this block to drive several half-bridges together, with different delays for example, they wont work together correctly.
I think I might know where the problem is. However, is there way to easily duplicate a c++ block already coded ?
It would help if you could post the schematic and code. Since this is a first-time post, you don’t have sufficient forum rights yet so I’ll make a guess.
If I understand the issue correctly, your C-Block code works fine as long as there is only one instance of the component on the schematic. When you add a second component instance, things work incorrectly.
I suspect that you are using global variables in your component code. If so, you’ll need to use the “per-instance data” structure to save the component state information for each schematic instance separately.
If “per-instance data” is not clear, you might want to check out the C-Block Basics papers on my QSpice Github repository.
I assume that by “did not work,” you mean that it ran but the results weren’t what you expected. This is due to using global variables as I previously guessed.
In the first case, there is a single DLL called by all four schematic instances. QSpice loads the DLL once and calls it four times for each simulation point generated. There is only one copy of the global variables shared by all four instance calls. Each call modifies the same global variables.
In the second case, you’ve created four separate DLLs. QSpice loads each of the four DLLs. Each DLL has a separate copy of the global variables so they don’t interfere with each other.
The solution is to have a single DLL and use “per-instance data.” Move the global variables into the per-instance structure as detailed in the first of the C-Block Basics papers in the repo. That should solve it.