基于simulink的s-function的PWM生成
simulink真是无所不能,不仅可以仿真电路,生成代码,还可以将自己的代码放在仿真里运行,这里基于sfun写一个PWM生成器,便于sfun的学习
模型
设置sfun的函数名和参数,其中有两个参数和两个输入信号,
两个参数分别是输出波形的幅值和模型运行的分辨率,这里设置幅值为5,分辨率为10k
两个输入信号分别是占空比和周期值,这里设置占空比为50%,周期为40ms(25hz)
注:分辨率要大于PWM的周期值
仿真结果如下:
代码
/*
* File : sfun_pwm.c
* Abstract:
*
* This file represents an S-function example which demonstrates the S-function macros for using a
* controllable sample time. This S-function generates PWM (Pulse-width modulation) signals based
* on the input period and duty cycle signals.
*
* This S-function registers a controllable sample time with which the S-function can schedule the
* next hit when changing the output value. The S-function has two input ports and one output
* port. The first input port is the duty cycle signal and the second is the period signal. The
* S-function has two block parameters: the amplitude of the generated PWM signal and the
* resolution of the controllable sample time.
*