필터 지우기
필터 지우기

How do you generate a PWM signal through S function in C.I am facing a build error while building.Could anyone help me in this issue.

조회 수: 7 (최근 30일)
#define S_FUNCTION_NAME pwm_sfunction
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#define PARAM_FREQUENCY 0
#define PARAM_DUTY_CYCLE 1
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S,2);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S))
{
return;
}
ssSetNumContStates(S,0);
ssSetNumDiscStates(S,0);
if (!ssSetNumInputPorts(S,0)) return;
if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S,0,1);
ssSetNumSampleTimes(S,1);
ssSetSimStateCompliance(S,USE_DEFAULT_SIM_STATE);
ssSetOptions(S,0);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S,0,INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S,0,0.0);
}
static void mdlStart(SimStruct *S)
{
}
static void mdlOutputs(SimStruct *S,int_T tid)
{
real_T *y=ssGetOutputPortRealSignal(S,0);
real_T frequency = *mxGetPr(ssGetSFcnParam(S,PARAM_FREQUENCY));
real_T duty_cycle = *mxGetPr(ssGetSFcnParam(S,PARAM_DUTY_CYCLE));
double T_pwm =1.0/frequency;
double time = fmod(ssGetT(S),T_pwm);
*y =(time < T_pwm * duty_cycle)? 1.0:0.0;
}
static void mdlTerminate(SimStruct *S)
{
}
#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif

답변 (1개)

Mark McBroom
Mark McBroom 2024년 2월 18일
Why are you writing an S-Function for this simple C algorithm. If you must write this in C, I suggest using the C Function block. https://www.mathworks.com/help/simulink/slref/cfunction.html
Or even easier, just write this code in MATLAB using a MATLAB Function Block: https://www.mathworks.com/help/simulink/ug/what-is-a-matlab-function-block.html#

카테고리

Help CenterFile Exchange에서 Block Libraries에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by