I would recommend avoiding the C-Script unless you absolutely cannot achieve what you need using standard PLECS control blocks.
Also, while the code snippet is helpful, it is difficult to understand the full context without seeing the rest of your simulation setup. Sharing the model would make it much easier to provide useful guidance.
Here is a very simple solution for on-time measurement using basic PLECS blocks. Maybe this is sufficient for your application?
Hello, what I mainly want to do is experiment with measuring the on-time and period of PWM signals. However, I don’t know why the output is always zero. Could you help me figure this out? My core purpose is to test this capture method to achieve a function similar to the eCAP module in DSPs. I wonder if it can be used for variable-frequency PWM signal capture?
max step size: I would recommend leaving this at the default. With the relative tolerance you can control the accuracy that PLECS will need to ensure for each simulation step. by setting the max step size to 1e-8 you are effectively running the simulation at 10ns step sizes which will result in a much slower simulation with no benefits in terms of accuracy improvements.
if you look at Scope2 you’ll notice that the integrator output is never above zero. So when you cast Input(1) to an unsigned long count variable, it will always be zero.
If you have access to our code generation tools then an eCapture module is already available and so if the final goal is to mimic what you would see in HW when using either a TI or STM DSP, then I would recommend using that. All of our peripheral models come with an offline implementation that closely mimics the behavior of the HW.
If however you want/have to implement the module yourself then again I would recommend staying away from the C-Script unless absolutely necessary. Have a look at the implementation that I had sent earlier and you can easily translate that to something that measures both on-time and period.
The module you mentioned seems to only measure the on-time of the PWM signal—it still can’t measure the full PWM period. I changed the integrator’s reset to rising-edge mode, and the trigger module to rising-edge mode too, but the final output is 0. How can I fix this issue? I’m trying to build a closed-loop controlled interleaved CRM-mode totem-pole PFC, so I need a function similar to eCAP.
The reason you are obtaining zero is because you are trying to measure the integrator output and resetting the integrator at the same time. The solution to that is to use the integrator state. To do this you will need to Enable the Show state port parameter on the integrator. This will create a little port at the top of the integrator and instead of using the output value you’ll need to use the state value. See the documentation for the integrator, specifically this part:
Show state port
Specifies whether to show an additional state output port. The state port is updated at a slightly different point in the block execution order (i.e. before the reset and initial condition inputs are evaluated) and may therefore be used to calculate an input signal for the external reset input or the initial condition input.
Here is an updated simulation with both on time and period measurement. Hope this helps!