Introduction
A monostable multivibrator, also known as a one-shot pulse generator, is a circuit that produces a single output pulse in response to an external trigger. It has one stable state and temporarily switches to an unstable state for a defined time before returning. Commonly used for timing, pulse generation, and signal conditioning.
This article demonstrates how to model and implement a monostable multivibrator using PSIM and generate C code for embedded applications.
Error #4023
While PSIM provides a built-in block for simulating monostable behavior, this block was not specifically designed with code generation in mind. As a result, attempting to generate C code from a design that uses this block may lead to errors (Error #4023)
— particularly when it’s driven by signals from the digital side of an ADC.
The core issue lies in the internal handling of the pulse width in the standard block, which is incompatible with PSIM’s code generation engine. You can verify whether a block supports code generation by enabling the option:
Options → Settings → Advanced → Show image next to elements that can be used for code generation
You’ll notice that the Monostable Multivibrator block is not marked as compatible.
Despite this limitation, the block still functions correctly within PSIM's simulation environment when used appropriately—i.e., with analog signals or signals from the left (analog) side of the ADC block. However, this restriction makes it impractical for embedded code development or integration in digital workflows.
C Block Implementation as a Workaround
To overcome this limitation, I recreated the monostable behavior using a Simplified C block, which is compatible with PSIM’s code generation. This custom C block allows the user to define the pulse width in steps, giving more flexibility and enabling full code generation capabilities.
Here’s how the workaround works:
- The C block is connected to the digital (right) side of the ADC, unlike the standard block.
- You can easily modify the
pulse_width
variable inside the block by double-clicking it. - For example, if you want a 1ms pulse width and your PWM carrier is running at 10 kHz (also the ADC sampling rate), you would set
pulse_width = 1ms * 10kHz = 10 steps
.
In PSIM, you can conveniently use units like m
(milli) and k
(kilo), so 1m * 10k
would correctly evaluate to 10
.
The results from this C block match exactly with those of the original library block during simulation. Additionally, because the logic is implemented in C, it can be verified during simulation as part of a Model-in-the-Loop (MiL) workflow—ensuring your design is ready for embedded deployment.
Simulation example attached here: