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

Jallin
認證:VIP會員
作者動態
精華-電力電子實戰之PLL鎖相環的仿真和C代碼演示
2021-04-26 20:10
PID在頻域的表現形式
2021-04-18 12:22
ccs低版本打開高版本的工程問題
2021-02-20 21:23
基于simulink的buck仿真和simulink的掃頻儀的使用,設計環路時極其方便
2021-02-20 21:21
基于simulink的s-function的PWM生成
2021-02-20 21:19

基于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
關注 135
成為作者 賺取收益
全部留言
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條回復
主站蜘蛛池模板: 欧美重口另类在线播放二区 | 脱了老师内裤猛烈进入在线观看 | 亚洲第一免费视频 | 亚洲欧美日韩Aⅴ在线观看 日本成人影院 | 龙珠z免费观看国语版 | 欧美国产综合在线 | 波多野结衣在线视频一区二区三区 | 国产精品人人爽人人做av片 | 人妻少妇波多野结衣 | 91尤物国产福利在线观看 | 无码少妇一区二区浪潮AV | 91精品国产综合久久男男 | 亚洲韩国一区二区三区 | 中文字幕一二 | 刘亦菲一区二区三区免费看 | 99re免费视频 | 日韩一区二区三区四区在线 | 无套内射视频囯产 | 第一区在线观看免费国语入口 | 欧美日韩色网专区 | 欧美黑人狂躁日本寡妇 | 很黄的网站在线观看 | 成人午夜爽爽爽免费视频 | 国产福利在线观看视频 | 亚洲国产成人精品女人久久久久 | 久久av资源网 | 亚洲欧美精品一中文字幕 | 亚洲国产精品视频一区二区 | 国内黄色一级片 | 国产真实乱对白精彩久久老熟妇女 | 男女啪啪做爰高潮全过有网站 | 国内在线高清免费视频 | JIZZ成熟丰满韩国女人少妇 | 一边捏奶头一边高潮视频 | 色视频在线观看免费视频 | 欧美亚洲另类在线观看 | 国产精品影视在线 | 欧美性福 | 内射白嫩少妇超碰 | 成人午夜爽爽爽免费视频 | 亚洲日本精品一区 |