99久久全国免费观看_国产一区二区三区四区五区VM_久久www人成免费看片中文_国产高清在线a视频大全_深夜福利www_日韩一级成人av

基于simulink的s-function的PWM生成

基于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.
 * 
 *   This S-function illustrates the use of the S-function macro:
 *       
 *        ssSetControllableSampleTime(S, 0, resolution)
 *
 *   to register a controllable sample time in mdlInitializeSampleTimes(). The resolution must be a
 *   positive finite number that defines the fundamental step size that the S-function can schedule
 *   the next hit for this sample time.
 *
 *   This S-function illustrates the use of the S-function macro:
 *
 *       ssSetNumTicksToNextHitForControllableSampleTime(S, 0, numTick)
 *  
 *   to schedule the next hit of the controllable sample time. The next hit happens after t =
 *   t_current + numTick * resolution. numTick must be a positive integer. The S-function must use
 *   this macro to schedule the execution of the controllable sample time in
 *   mdlInitializeConditions() and mdlOutputs().
 *
 * Copyright 2017 The MathWorks, Inc.
 */

#define S_FUNCTION_NAME mysfun_generate                          //函數名
#define S_FUNCTION_LEVEL 2                                       //sfun的深度

#include "simstruc.h"
#include "assert.h"

/* Function: mdlInitializeSizes ================================================
 * Abstract:
 *
 *   Register an S-function with two input ports, one output port, and three DWorks. Specify the
 *   number of sample times to 1.
 *
 */
static void mdlInitializeSizes(SimStruct* S)
{
    //這個函數用來初始化的,主要是設置輸入、輸出和參數的。
    
    if (!ssSetNumInputPorts(S, 2)) return;//設置輸入信號2個
    ssSetInputPortWidth(S, 0, 1);//設置輸入變量0的維數為1
    ssSetInputPortDirectFeedThrough(S, 0, 1);// 設置輸入端口的信號是否mdlOutputs函數中使用,這兒設置為true
    ssSetInputPortWidth(S, 1, 1);//設置輸入變量1的維數為1
    ssSetInputPortDirectFeedThrough(S, 1, 1);// 設置輸入端口的信號是否mdlOutputs函數中使用,這兒設置為true
    
    if (!ssSetNumOutputPorts(S, 1)) return;//設置輸出變量的個數
    ssSetOutputPortWidth(S, 0, 1);//設置輸出變量0的維數為1維

    ssSetNumSFcnParams(S, 2);//設置參數2個
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        return;
    }
    
    ssSetNumContStates(S, 0);//設置連續狀態變量的
    ssSetNumDiscStates(S, 0);
    ssSetNumDWork(S, 3);
    ssSetDWorkWidth(S, 0, 1);//設置離散狀態變量的
    ssSetDWorkDataType(S, 0, SS_BOOLEAN);
    ssSetDWorkWidth(S, 1, 1);
    ssSetDWorkDataType(S, 1, SS_DOUBLE);
    ssSetDWorkWidth(S, 2, 1);
    ssSetDWorkDataType(S, 2, SS_UINT32);
    
    ssSetNumSampleTimes(S, 1);//設置采樣時間,此處為1s,后面有修改

    ssSetOperatingPointCompliance(S, USE_DEFAULT_OPERATING_POINT);
    
    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE |
                    SS_OPTION_USE_TLC_WITH_ACCELERATOR);//默認
}

/* Function: mdlInitializeSampleTimes ========================================== 
 * Abstract: 
 *   Register a controllable sample time with the specified resolution.
 */
static void mdlInitializeSampleTimes(SimStruct* S)
{
    real_T resolution = (real_T) *mxGetPr(ssGetSFcnParam(S,1));//設置采樣時間
    ssSetControllableSampleTime(S, 0, resolution);//將參數分辨率設置成采樣時間
}

/* Function: mdlInitializeConditions============================================
 * Abstract:
 *   Initialize the DWorks related to the generation of the PWM signals. Force a hit for the
 *   controllable sample time.
 */
#define MDL_INITIALIZE_CONDITIONS
static void mdlInitializeConditions(SimStruct *S)
{
    //初始化離散狀態變量的值
    /*
     * Force a sample hit whenever the system is initialized.
     */
    ssSetNumTicksToNextHitForControllableSampleTime(S, 0, 1);

    boolean_T * x1  = (boolean_T*)ssGetDWork(S,0);    
    real_T *x2 = (real_T*)ssGetDWork(S,1);
    uint32_T *x3 = (uint32_T*)ssGetDWork(S,2);    
    *x1 = true;
    *x2 = 0.0;
    *x3 = 0;    
 }

/* Function: mdlOutputs ========================================================
 * Abstract:
 *   Generate the PWM signals based on the input duty cycle and period signals.
 */
static void mdlOutputs(SimStruct* S, int_T tid)
{
    //這個函數就是輸出的函數,所有的代碼動作在這里執行
    
    real_T* y = ssGetOutputPortRealSignal(S, 0);//獲取輸出端口
    boolean_T* x1 = (boolean_T*)ssGetDWork(S,0);//獲取離散變量端口
    real_T *x2 = (real_T*)ssGetDWork(S,1);//獲取離散變量端口
    uint32_T *x3 = (uint32_T*)ssGetDWork(S,2);    //獲取離散變量端口  
        
    size_t numTicksToNextHit = 0;//設置值
    real_T yout_value = 0.0;   
    
    if (*x1) {
        real_T amplitude_prm = (real_T) *mxGetPr(ssGetSFcnParam(S,0));
        real_T period_prm = (real_T) *mxGetPr(ssGetSFcnParam(S,1));

        InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
        real_T duty = *uPtrs[0];
        
        real_T p = *uPtrs[1];
        size_t numSamples = (size_t) (p/period_prm);
        numTicksToNextHit = (size_t) (duty*numSamples);                
        
        if (numTicksToNextHit == 0) {
            numTicksToNextHit = numSamples;
            yout_value = 0;
        } else if (numTicksToNextHit == numSamples) {
            numTicksToNextHit = numSamples;
            yout_value = amplitude_prm;
        } else {
            *x1 = false;
            *x2 = duty;
            *x3 = (uint32_T)(numSamples - numTicksToNextHit);
            yout_value = amplitude_prm;
        }
    } else {
        *x1 = true;
        numTicksToNextHit = (size_t) *x3;
        yout_value = 0;
    }    
    
    *y = yout_value;//輸出
    ssSetNumTicksToNextHitForControllableSampleTime(S, 0, numTicksToNextHit);    //更新下一次的采樣時間
    
} /* mdlOutputs */

static void mdlTerminate(SimStruct *S)
{
}

/* Required S-Function trailer */

#ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
#include "simulink.c"      /* MEX-file interface mechanism */
#else
#include "cg_sfun.h"       /* Code generation registration function */
#endif
聲明:本內容為作者獨立觀點,不代表電子星球立場。未經允許不得轉載。授權事宜與稿件投訴,請聯系:editor@netbroad.com
本篇所含全部資料,點擊此處留下郵箱我會發給你
資料明細:基于simulink的s-function的PWM生成.rar
覺得內容不錯的朋友,別忘了一鍵三連哦!
贊 4
收藏 10
關注 136
成為作者 賺取收益
全部留言
0/200
  • dy-fH1WEBzQ 2024-03-20 12:45
    老師,能不能發我一下資料,謝謝! 11****@****.com
    回復 1條回復
  • yangwenlong 2023-11-25 15:38
    老師,能不能發我一下資料,謝謝! ya****@****.com
    回復 1條回復
  • yangwenlong 2023-04-21 14:24
    老師,能不能發我一下資料,謝謝! ya****@****.com
    回復 1條回復
  • sdll825 2023-03-16 22:17
    老師,能不能發我一下資料,謝謝! sd****@****.com
    回復 1條回復
  • 治國平天下 2021-09-10 03:12
    老師,能不能發我一下資料,謝謝! 78****@****.com
    回復 1條回復
  • 丶誰來 2021-07-11 12:09
    老師,能不能發我一下資料,謝謝! 55****@****.com
    回復 1條回復
  • 平行的世界 2021-06-17 12:51
    老師,能不能發我一下資料,謝謝! l9****@****.com
    回復 1條回復
  • wkhn 2021-04-26 15:37
    老師,能不能發我一下資料,謝謝! wa****@****.com
    回復 1條回復
  • 電力電子愛好者 2021-03-07 17:23
    老師,能不能發我一下資料,謝謝! 31****@****.com
    回復 1條回復
  • 電力電子愛好者 2021-03-07 17:23
    老師,能不能發我一下資料,謝謝! 31****@****.com
    回復 1條回復
  • 溫水煮青蛙 2021-02-22 05:44
    老師,能不能發我一下資料,謝謝! 92****@****.com
    回復 1條回復
主站蜘蛛池模板: 久热综合网 | 国产成人啪精品视频免费网站 | 深夜看国产毛片在线视频香蕉 | 亚洲有吗在线观看 | 一色一伦一区二区三区 | 国产亚洲精品AA片在线不卡 | 国产乱对白刺激视频在线观看 | 成人天堂 | 成人国产亚洲精品A区天堂 欧美四虎影院 | 毛片a在线 | 国产999精品久久 | 91女神在线视频 | 无码人妻丰满熟妇区毛片 | 宝宝好涨水快流出来免费视频 | 天天爽天天狠久久久综合麻豆 | 激情综合色综合啪啪五月丁香搜索 | 不卡一二区 | 三区影院| 前夜40集连续剧免费观看国语高清 | 免费视频啪啪 | 四虎影视www | 欧美成人一区在线 | 四虎看片 | 久久久久亚洲一区二区三区 | 日韩国产免费一区二区三区 | 性伦欧美刺激片在线观看 | 国产女同2互磨高潮在线观看 | 寡妇张开腿让黑人捅爽 | wwwwww日本| 国产伦一区二区三区色一情 | 色一乱一伦一图一区二区精品 | 波多野结衣无码免费视频 | 天天干天天摸天天操 | 夜色太晚 | 在线看v| 成人免费无码大片A毛片 | 午夜福利无码不卡在线观看 | 狠狠干成人网 | 色亚洲天堂 | 中文字幕一区二区三区乱码在线 | 日本精品成人一区二区三区视频 |