3.5.03 MC_PWM
Last modified by xingzhi lin on 2025/12/03 23:06
Instruction format
| Instruction | MC_PWM (FB) | |
| Name | Pulse width modulation | |
| Graphical performance | ST performance | |
| MC_PWM( Enable: = OutputPort: = PulsePeriod: = PulseWidth: = InVelocity=>, Busy=>, Error=>, ErrorID=>); | |
Related variables
| Input variables | Name | Data type | Effective range | Initial value | Description |
| Enable | Enable | BOOL | [FALSE, TRUE] | FALSE | Level trigger |
| OutputPort | Pulse output port | BYTE | [0, 2, 4, 6] | 0 | PWM output port, support D0, D2, D4, D6 only |
| PulsePeriod | Pulse width | UINT | [0,65535] | 0 | Period: The width of the complete square wave. Unit: 1us |
| PulseWidth | High-level pulse width | UINT | [0,65535] | 0 | The width of the high level of the square wave. Unit: 1us |
| Output variables | Name | Data type | Effective range | Initial value | Description |
| InVelocity | Reaching set value flag | BOOL | [FALSE, TRUE] | FALSE | The pulse width frequency reaches the set value. |
| Busy | Execution flag | BOOL | [FALSE, TRUE] | FALSE | Execute output PWM square wave. |
| Error | Error flag | BOOL | [FALSE, TRUE] | FALSE | TRUE when an error occurred inside the function block. |
| ErrorID | Error ID | MC_ERROR | - | MC_NO_ERROR | Error ID, refer to MC_ERROR. |
Function description
(1) This function block realizes dynamic pulse width modulation;
(2) After D0/D2/D4/D6 is configured as PWM function, control of D0 + D1/ D2 + D3/ D4 + D5 /D6 + D7 outputs is not allowed, during the execution of MC_PWM function block.
Sequence diagram

Error description
ErrorID in MC_ERROR describes the cause of the corresponding error.
| Error ID | Initial value | Description |
| 0 | MC_NO_ERROR | No error |
| 5 | MC_PWM_PORT_ERROR | Port input error |
| 6 | MC_PWM_PORT_WAS_RUN | The corresponding port is running |
| 7 | MC_PWM_PERIOD_LESS_THAN_WIDTH | The pulse width is smaller than the high level pulse width |
| 8 | MC_PWM_PORT_WASNT_ENABLE | Port is not enabled |
