[技术问答]

HC32F460 如何动态调节PWM占空比

[复制链接]
1673|12
手机看帖
扫描二维码
随时随地手机跟帖
NIH奥|  楼主 | 2020-7-4 12:13 | 显示全部楼层 |阅读模式
我正在使用HC32F460 ,但是在示列程序中修改输出占空比,但是没有效果
stc_timer4_cnt_init_t stcCntInit;
    stc_timer4_oco_init_t stcOcoInit;
    stc_timer4_pwm_init_t stcPwmInit;
    stc_oco_low_ch_compare_mode_t stcLowChCmpMode;
    stc_oco_high_ch_compare_mode_t stcHighChCmpMode;
    uint16_t u8OcoOccrVal = TIMER4_CNT_CYCLE_VAL/2u;
    en_timer4_oco_ch_t enOcoLowCh = (en_timer4_oco_ch_t)(TIMER4_OCO_HIGH_CH + 1);

    /* Clear structures */
    MEM_ZERO_STRUCT(stcCntInit);
    MEM_ZERO_STRUCT(stcOcoInit);
    MEM_ZERO_STRUCT(stcLowChCmpMode);
    MEM_ZERO_STRUCT(stcHighChCmpMode);
    MEM_ZERO_STRUCT(stcPwmInit);

    /* Enable peripheral clock */
    PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM41 | PWC_FCG2_PERIPH_TIM42 | PWC_FCG2_PERIPH_TIM43, Enable);

    /* Timer4 CNT : Initialize CNT configuration structure */
    stcCntInit.enBufferCmd = Enable;//Disable;
    stcCntInit.enClk = Timer4CntPclk;
    stcCntInit.enClkDiv = Timer4CntPclkDiv16;  /* CNT clock divide */
    stcCntInit.u16Cycle = TIMER4_CNT_CYCLE_VAL;
    stcCntInit.enCntMode = Timer4CntSawtoothWave;
    stcCntInit.enZeroIntCmd = Disable;
    stcCntInit.enPeakIntCmd = Disable;
    stcCntInit.enZeroIntMsk = Timer4CntIntMask0;
    stcCntInit.enPeakIntMsk = Timer4CntIntMask0;
    TIMER4_CNT_Init(TIMER4_UNIT, &stcCntInit); /* Initialize CNT */

    /* Timer4 OCO : Initialize OCO configuration structure */
    stcOcoInit.enOccrBufMode = OccrBufTrsfByCntZeroOrCntPeak;//OccrBufDisable;
    stcOcoInit.enOcmrBufMode = OcmrBufTrsfByCntZeroOrCntPeak;//OcmrBufDisable;
    stcOcoInit.enPortLevel = OcPortLevelLow;
    stcOcoInit.enOcoIntCmd = Disable;
    TIMER4_OCO_Init(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, &stcOcoInit); /* Initialize OCO high channel */
    TIMER4_OCO_Init(TIMER4_UNIT, enOcoLowCh, &stcOcoInit);         /* Initialize OCO low channel */

    if (!(TIMER4_OCO_HIGH_CH % 2))
    {
        /* ocmr[15:0] = 0x0FFF */
        stcHighChCmpMode.enCntZeroMatchOpState = OcoOpOutputReverse;
        stcHighChCmpMode.enCntZeroNotMatchOpState = OcoOpOutputHold;
        stcHighChCmpMode.enCntUpCntMatchOpState = OcoOpOutputReverse;
        stcHighChCmpMode.enCntPeakMatchOpState = OcoOpOutputReverse;
        stcHighChCmpMode.enCntPeakNotMatchOpState = OcoOpOutputHold;
        stcHighChCmpMode.enCntDownCntMatchOpState = OcoOpOutputReverse;

        stcHighChCmpMode.enCntZeroMatchOcfState = OcoOcfSet;
        stcHighChCmpMode.enCntUpCntMatchOcfState = OcoOcfSet;
        stcHighChCmpMode.enCntPeakMatchOcfState = OcoOcfSet;
        stcHighChCmpMode.enCntDownCntMatchOcfState = OcoOcfSet;

        stcHighChCmpMode.enMatchConditionExtendCmd = Disable;

        TIMER4_OCO_SetHighChCompareMode(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, &stcHighChCmpMode);  /* Set OCO high channel compare mode */
    }

    /*************Timer4 OCO ocmr1[31:0] = 0x0FF0 0FFF*****************************/
    if (enOcoLowCh % 2)
    {
        /* OCMR[31:0] Ox 0FF0 0FFF    0000 1111 1111 0000   0000 1111 1111 1111 */
        stcLowChCmpMode.enCntZeroLowMatchHighMatchLowChOpState = OcoOpOutputReverse;         /* bit[27:26]  11 */
        stcLowChCmpMode.enCntZeroLowMatchHighNotMatchLowChOpState = OcoOpOutputReverse;      /* bit[11:10]  11 */
        stcLowChCmpMode.enCntZeroLowNotMatchHighMatchLowChOpState = OcoOpOutputHold;         /* bit[31:30]  00 */
        stcLowChCmpMode.enCntZeroLowNotMatchHighNotMatchLowChOpState = OcoOpOutputHold;      /* bit[15:14]  00 */

        stcLowChCmpMode.enCntUpCntLowMatchHighMatchLowChOpState = OcoOpOutputReverse;        /* bit[25:24]  11 */
        stcLowChCmpMode.enCntUpCntLowMatchHighNotMatchLowChOpState = OcoOpOutputReverse;     /* bit[9:8]    11 */
        stcLowChCmpMode.enCntUpCntLowNotMatchHighMatchLowChOpState = OcoOpOutputHold;        /* bit[19:18]  00 */

        stcLowChCmpMode.enCntPeakLowMatchHighMatchLowChOpState = OcoOpOutputReverse;         /* bit[23:22]  11 */
        stcLowChCmpMode.enCntPeakLowMatchHighNotMatchLowChOpState = OcoOpOutputReverse;      /* bit[7:6]    11 */
        stcLowChCmpMode.enCntPeakLowNotMatchHighMatchLowChOpState = OcoOpOutputHold;         /* bit[29:28]  00 */
        stcLowChCmpMode.enCntPeakLowNotMatchHighNotMatchLowChOpState = OcoOpOutputHold;      /* bit[13:12]  00 */

        stcLowChCmpMode.enCntDownLowMatchHighMatchLowChOpState = OcoOpOutputReverse;         /* bit[21:20]  11 */
        stcLowChCmpMode.enCntDownLowMatchHighNotMatchLowChOpState = OcoOpOutputReverse;      /* bit[5:4]    11 */
        stcLowChCmpMode.enCntDownLowNotMatchHighMatchLowChOpState = OcoOpOutputHold;         /* bit[17:16]  00 */

        stcLowChCmpMode.enCntZeroMatchOcfState = OcoOcfSet;    /* bit[3] 1 */
        stcLowChCmpMode.enCntUpCntMatchOcfState = OcoOcfSet;   /* bit[2] 1 */
        stcLowChCmpMode.enCntPeakMatchOcfState = OcoOcfSet;    /* bit[1] 1 */
        stcLowChCmpMode.enCntDownCntMatchOcfState = OcoOcfSet; /* bit[0] 1 */

        TIMER4_OCO_SetLowChCompareMode(TIMER4_UNIT, enOcoLowCh, &stcLowChCmpMode);  /* Set OCO low channel compare mode */
    }

    /* Set OCO compare value */
    TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, u8OcoOccrVal/2);
    TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, u8OcoOccrVal);

    /* Enable OCO */
    TIMER4_OCO_OutputCompareCmd(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, Enable);
    TIMER4_OCO_OutputCompareCmd(TIMER4_UNIT, enOcoLowCh, Enable);

    /* Initialize PWM I/O */
    PORT_SetFunc(TIMER4_PWM_H_PORT, TIMER4_PWM_H_PIN, Func_Tim4, Disable);
    PORT_SetFunc(TIMER4_PWM_L_PORT, TIMER4_PWM_L_PIN, Func_Tim4, Disable);

    /* Timer4 PWM: Initialize PWM configuration structure */
    stcPwmInit.enRtIntMaskCmd = Enable;
    stcPwmInit.enClkDiv = PwmPlckDiv1;
    stcPwmInit.enOutputState = PwmHHoldPwmLReverse;
    stcPwmInit.enMode = PwmThroughMode;
    TIMER4_PWM_Init(TIMER4_UNIT, TIMER4_PWM_CH, &stcPwmInit); /* Initialize timer4 pwm */

    /* Clear && Start CNT */
    TIMER4_CNT_ClearCountVal(TIMER4_UNIT);
    TIMER4_CNT_Start(TIMER4_UNIT);
               
                TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, u8OcoOccrVal/2);
    TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, u8OcoOccrVal/2);
               
                TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, u8OcoOccrVal/4);
    TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, u8OcoOccrVal/4);

使用特权

评论回复
hh电子爱好者| | 2022-1-24 16:24 | 显示全部楼层
Timer4改变不了占空比,用timera就可以,你这里设置的只是相位的偏移,不是占空比的改变。
  /* Set OCO compare value */
    TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, u8OcoOccrVal/2);
    TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, u8OcoOccrVal);

使用特权

评论回复
martinhu| | 2022-1-25 14:40 | 显示全部楼层
hh电子爱好者 发表于 2022-1-24 16:24
Timer4改变不了占空比,用timera就可以,你这里设置的只是相位的偏移,不是占空比的改变。
  /* Set OCO co ...

谁说timer4改变不了占空比的,这个本来就是给电机控制用的,当然,普通PWM的控制也可以。

使用特权

评论回复
martinhu| | 2022-1-25 17:14 | 显示全部楼层
本帖最后由 martinhu 于 2022-1-25 17:58 编辑

这个是锯齿波的OCMR的调制,仅供参考,楼主如果要用,估计得添加修改很多地方

/**
*******************************************************************************
** \brief  Main function of project
**
** \param  None
**
** \retval int32_t return value, if needed
**
******************************************************************************/
int32_t main(void)
{
    stc_timer4_cnt_init_t stcCntInit;
    stc_timer4_oco_init_t stcOcoInit;
    stc_timer4_pwm_init_t stcPwmInit;
    stc_irq_regi_conf_t stcIrqRegiCfg;
    stc_oco_low_ch_compare_mode_t stcLowChCmpMode;
    stc_oco_high_ch_compare_mode_t stcHighChCmpMode;
    uint16_t u8OcoOccrVal = TIMER4_CNT_CYCLE_VAL/2u;
    en_timer4_oco_ch_t enOcoLowCh = (en_timer4_oco_ch_t)(TIMER4_OCO_HIGH_CH + 1);

    /* Clear structures */
    MEM_ZERO_STRUCT(stcCntInit);
    MEM_ZERO_STRUCT(stcOcoInit);
    MEM_ZERO_STRUCT(stcLowChCmpMode);
    MEM_ZERO_STRUCT(stcHighChCmpMode);
    MEM_ZERO_STRUCT(stcPwmInit);
    MEM_ZERO_STRUCT(stcIrqRegiCfg);
   
    BSP_CLK_Init();
   
    /* Initialize LED */
    BSP_LED_Init();
   
    M4_DBGC->MCUSTPCTL_f.TMR41STP = 1;
   
    /* Initialize PWM I/O */
    PORT_SetFunc(TIMER4_PWM_H_PORT, TIMER4_PWM_H_PIN, Func_Tim4, Disable);
    PORT_SetFunc(TIMER4_PWM_L_PORT, TIMER4_PWM_L_PIN, Func_Tim4, Disable);

    M4_PORT->PORRB_f.POR05 = 1;
    M4_PORT->POERB_f.POUTE05 = 1;

    /* Enable peripheral clock */
    PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM41 | PWC_FCG2_PERIPH_TIM42 | PWC_FCG2_PERIPH_TIM43, Enable);

    /* Timer4 CNT : Initialize CNT configuration structure */
    stcCntInit.enBufferCmd = Enable;//Disable;
    stcCntInit.enClk = Timer4CntPclk;
    stcCntInit.enClkDiv = Timer4CntPclkDiv16;  /* CNT clock divide */
    stcCntInit.u16Cycle = TIMER4_CNT_CYCLE_VAL;
    stcCntInit.enCntMode = Timer4CntSawtoothWave;
    stcCntInit.enZeroIntCmd = Enable;//Disable;
    stcCntInit.enPeakIntCmd = Disable;
    stcCntInit.enZeroIntMsk = Timer4CntIntMask0;//Timer4CntIntMask15;//
    stcCntInit.enPeakIntMsk = Timer4CntIntMask0;
    TIMER4_CNT_Init(TIMER4_UNIT, &stcCntInit); /* Initialize CNT */

    /* Timer4 OCO : Initialize OCO configuration structure */
    stcOcoInit.enOccrBufMode = OccrBufTrsfByCntZeroZicZero;//OccrBufDisable;
    stcOcoInit.enOcmrBufMode = OcmrBufDisable;
    stcOcoInit.enPortLevel = OcPortLevelLow;
    stcOcoInit.enOcoIntCmd = Disable;
    TIMER4_OCO_Init(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, &stcOcoInit); /* Initialize OCO high channel */
    TIMER4_OCO_Init(TIMER4_UNIT, enOcoLowCh, &stcOcoInit);         /* Initialize OCO low channel */

    if (!(TIMER4_OCO_HIGH_CH % 2))
    {
        /* ocmr[15:0] = 0x4A4F */        
        
        stcHighChCmpMode.enCntUpCntMatchOpState = OcoOpOutputLow;         //bit[9:8]    //向上计数&匹配
        
        stcHighChCmpMode.enCntPeakNotMatchOpState = OcoOpOutputHold;      //bit[13:12]  //上溢&不匹配
        stcHighChCmpMode.enCntPeakMatchOpState = OcoOpOutputHigh; //OcoOpOutputLow;//        //bit[7:6]    //上溢&匹配
        
        stcHighChCmpMode.enCntDownCntMatchOpState = OcoOpOutputHold;      //bit[5:4]    //向下计数&匹配
        
        stcHighChCmpMode.enCntZeroNotMatchOpState = OcoOpOutputHigh;      //bit[15:14]  //下溢&不匹配
        stcHighChCmpMode.enCntZeroMatchOpState = OcoOpOutputLow;          //bit[11:10]  //下溢&匹配
        
        stcHighChCmpMode.enCntZeroMatchOcfState = OcoOcfSet;              //bit[3]
        stcHighChCmpMode.enCntUpCntMatchOcfState = OcoOcfSet;             //bit[2]
        stcHighChCmpMode.enCntPeakMatchOcfState = OcoOcfSet;              //bit[1]
        stcHighChCmpMode.enCntDownCntMatchOcfState = OcoOcfSet;           //bit[0]

        stcHighChCmpMode.enMatchConditionExtendCmd = Disable;

        TIMER4_OCO_SetHighChCompareMode(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, &stcHighChCmpMode);  /* Set OCO high channel compare mode */
    }

    #if 0
    /*************Timer4 OCO ocmr1[31:0] = 0x4A40 4A4F   Only control by OCMERnL*****************************/
    if (enOcoLowCh % 2)
    {
        /* OCMR[31:0] Ox 0FF0 0FFF    0000 1111 1111 0000   0000 1111 1111 1111 */
        stcLowChCmpMode.enCntZeroLowNotMatchHighMatchLowChOpState = OcoOpOutputHigh;    /* bit[31:30]   */ //计数下溢 & OCCRnL不匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntZeroLowMatchHighMatchLowChOpState = OcoOpOutputLow;        /* bit[27:26]   */ //计数器下溢 & OCCRnL匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntZeroLowNotMatchHighNotMatchLowChOpState = OcoOpOutputHigh; /* bit[15:14]   */ //计数器下溢 & OCCRnL不匹配 & OCCRnH不匹配
        stcLowChCmpMode.enCntZeroLowMatchHighNotMatchLowChOpState = OcoOpOutputLow;     /* bit[11:10]   */ //计数器下溢 & OCCRnL匹配 & OCCRnH不匹配

        stcLowChCmpMode.enCntUpCntLowMatchHighMatchLowChOpState = OcoOpOutputLow;       /* bit[25:24]   */ //向上计数 & OCCRnL匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntUpCntLowNotMatchHighMatchLowChOpState = OcoOpOutputHold;   /* bit[19:18]   */ //向上计数 & OCCRnL不匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntUpCntLowMatchHighNotMatchLowChOpState = OcoOpOutputLow;    /* bit[9:8]     */ //向上计数 & OCCRnL匹配 & OCCRnH不匹配

        stcLowChCmpMode.enCntPeakLowNotMatchHighMatchLowChOpState = OcoOpOutputHold;    /* bit[29:28]   */ //计数器上溢 & OCCRnL不匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntPeakLowMatchHighMatchLowChOpState = OcoOpOutputHigh;       /* bit[23:22]   */ //计数器上溢 & OCCRnL匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntPeakLowNotMatchHighNotMatchLowChOpState = OcoOpOutputHold; /* bit[13:12]   */ //计数器上溢 & OCCRnL不匹配 & OCCRnH不匹配
        stcLowChCmpMode.enCntPeakLowMatchHighNotMatchLowChOpState = OcoOpOutputHigh;    /* bit[7:6]     */ //计数器上溢 & OCCRnL匹配 & OCCRnH不匹配

        stcLowChCmpMode.enCntDownLowMatchHighMatchLowChOpState = OcoOpOutputHold;       /* bit[21:20]   */ //向下计数 & OCCRnL匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntDownLowNotMatchHighMatchLowChOpState = OcoOpOutputHold;    /* bit[17:16]   */ //向下计数 & OCCRnL不匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntDownLowMatchHighNotMatchLowChOpState = OcoOpOutputHold;    /* bit[5:4]     */ //向下计数 & OCCRnL匹配 & OCCRnH不匹配
        
        stcLowChCmpMode.enCntZeroMatchOcfState = OcoOcfSet;    /* bit[3] 1 */
        stcLowChCmpMode.enCntUpCntMatchOcfState = OcoOcfSet;   /* bit[2] 1 */
        stcLowChCmpMode.enCntPeakMatchOcfState = OcoOcfSet;    /* bit[1] 1 */
        stcLowChCmpMode.enCntDownCntMatchOcfState = OcoOcfSet; /* bit[0] 1 */

        TIMER4_OCO_SetLowChCompareMode(TIMER4_UNIT, enOcoLowCh, &stcLowChCmpMode);  /* Set OCO low channel compare mode */
    }
   
    #else
   
    /*************Timer4 OCO ocmr1[31:0] = 0x4484 028F   Control by OCMERnL and OCMERnH*********************/
    if (enOcoLowCh % 2)
    {
        /* OCMR[31:0] Ox 0FF0 0FFF    0000 1111 1111 0000   0000 1111 1111 1111 */
        stcLowChCmpMode.enCntZeroLowNotMatchHighMatchLowChOpState = OcoOpOutputHigh;    /* bit[31:30]   */ //计数下溢 & OCCRnL不匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntZeroLowMatchHighMatchLowChOpState = OcoOpOutputHigh;       /* bit[27:26]   */ //计数器下溢 & OCCRnL匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntZeroLowNotMatchHighNotMatchLowChOpState = OcoOpOutputHold; /* bit[15:14]   */ //计数器下溢 & OCCRnL不匹配 & OCCRnH不匹配
        stcLowChCmpMode.enCntZeroLowMatchHighNotMatchLowChOpState = OcoOpOutputHold;    /* bit[11:10]   */ //计数器下溢 & OCCRnL匹配 & OCCRnH不匹配

        stcLowChCmpMode.enCntUpCntLowMatchHighMatchLowChOpState = OcoOpOutputHold;      /* bit[25:24]   */ //向上计数 & OCCRnL匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntUpCntLowNotMatchHighMatchLowChOpState = OcoOpOutputHigh;   /* bit[19:18]   */ //向上计数 & OCCRnL不匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntUpCntLowMatchHighNotMatchLowChOpState = OcoOpOutputLow;    /* bit[9:8]     */ //向上计数 & OCCRnL匹配 & OCCRnH不匹配

        stcLowChCmpMode.enCntPeakLowNotMatchHighMatchLowChOpState = OcoOpOutputHold;    /* bit[29:28]   */ //计数器上溢 & OCCRnL不匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntPeakLowMatchHighMatchLowChOpState = OcoOpOutputLow;        /* bit[23:22]   */ //计数器上溢 & OCCRnL匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntPeakLowNotMatchHighNotMatchLowChOpState = OcoOpOutputHold; /* bit[13:12]   */ //计数器上溢 & OCCRnL不匹配 & OCCRnH不匹配
        stcLowChCmpMode.enCntPeakLowMatchHighNotMatchLowChOpState = OcoOpOutputLow;     /* bit[7:6]     */ //计数器上溢 & OCCRnL匹配 & OCCRnH不匹配

        stcLowChCmpMode.enCntDownLowMatchHighMatchLowChOpState = OcoOpOutputHold;       /* bit[21:20]   */ //向下计数 & OCCRnL匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntDownLowNotMatchHighMatchLowChOpState = OcoOpOutputHold;    /* bit[17:16]   */ //向下计数 & OCCRnL不匹配 & OCCRnH匹配
        stcLowChCmpMode.enCntDownLowMatchHighNotMatchLowChOpState = OcoOpOutputHold;    /* bit[5:4]     */ //向下计数 & OCCRnL匹配 & OCCRnH不匹配  
        
        stcLowChCmpMode.enCntZeroMatchOcfState = OcoOcfSet;    /* bit[3] 1 */
        stcLowChCmpMode.enCntUpCntMatchOcfState = OcoOcfSet;   /* bit[2] 1 */
        stcLowChCmpMode.enCntPeakMatchOcfState = OcoOcfSet;    /* bit[1] 1 */
        stcLowChCmpMode.enCntDownCntMatchOcfState = OcoOcfSet; /* bit[0] 1 */

        TIMER4_OCO_SetLowChCompareMode(TIMER4_UNIT, enOcoLowCh, &stcLowChCmpMode);  /* Set OCO low channel compare mode */
    }
   
    #endif
   

    /* Set OCO compare value */
    TIMER4_OCO_WriteOccr(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, u8OcoOccrVal);
    TIMER4_OCO_WriteOccr(TIMER4_UNIT, enOcoLowCh, u8OcoOccrVal);
   
//    M4_TMR41->OCSRU_f.OCPH = 1;
//    M4_TMR41->OCSRU_f.OCPL = 1;
   
    //M4_TMR41->OCERU_f.MCECH = 1;

    /* Enable OCO */
    TIMER4_OCO_OutputCompareCmd(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, Enable);
    TIMER4_OCO_OutputCompareCmd(TIMER4_UNIT, enOcoLowCh, Enable);


    /* Timer4 PWM: Initialize PWM configuration structure */
    stcPwmInit.enRtIntMaskCmd = Disable;//Enable;
    stcPwmInit.enClkDiv = PwmPlckDiv1;
    stcPwmInit.enOutputState = PwmHPwmLHold;
    stcPwmInit.enMode = PwmThroughMode;
    TIMER4_PWM_Init(TIMER4_UNIT, TIMER4_PWM_CH, &stcPwmInit); /* Initialize timer4 pwm */
   
   
//    TIMER4_OCO_IrqCmd(TIMER4_UNIT, TIMER4_OCO_HIGH_CH, Enable);
//    /* Set Timer4-CNT IRQ */
//    stcIrqRegiCfg.enIRQn = Int000_IRQn;
//    stcIrqRegiCfg.pfnCallback = &CmpUhMatchIrqCb;
//    stcIrqRegiCfg.enIntSrc = INT_TMR41_GCMUH;
//    enIrqRegistration(&stcIrqRegiCfg);
//    NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
//    NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
//    NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);


    /* Set Timer4-CNT IRQ */
    stcIrqRegiCfg.enIRQn = Int001_IRQn;
    stcIrqRegiCfg.pfnCallback = &CmpZeroMatchIrqCb;
    stcIrqRegiCfg.enIntSrc = INT_TMR41_GUDF;
    enIrqRegistration(&stcIrqRegiCfg);
    NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
    NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
    NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);

    M4_PORT->POSRB_f.POS05 = 1;
   

    /* Clear && Start CNT */
    TIMER4_CNT_ClearCountVal(TIMER4_UNIT);
    TIMER4_CNT_Start(TIMER4_UNIT);

使用特权

评论回复
woai32lala| | 2022-1-26 08:05 | 显示全部楼层
直接修改输出占空比不就好了?

使用特权

评论回复
hilahope| | 2022-2-13 14:08 | 显示全部楼层
这个没有用过,应该跟stm32一样的,能修改。

使用特权

评论回复
sesefadou| | 2022-2-13 17:05 | 显示全部楼层
HC32F460还有这个bug吗      

使用特权

评论回复
myiclife| | 2022-2-13 17:15 | 显示全部楼层
把代码重新初始化呢?   

使用特权

评论回复
touser| | 2022-2-13 17:24 | 显示全部楼层
直接修改寄存器不行吗  

使用特权

评论回复
fengm| | 2022-2-13 17:33 | 显示全部楼层
是初始化的就有问题吗?  

使用特权

评论回复
maudlu| | 2022-2-13 17:42 | 显示全部楼层
可以动态修正吧,尝试关闭定时器,修改,再打开呢

使用特权

评论回复
海滨消消| | 2022-2-15 15:51 | 显示全部楼层
这个是在哪个过程中出现的问题了

使用特权

评论回复
tail066| | 2022-2-18 13:37 | 显示全部楼层
我一般是通过反馈量来改占空比的,
pwm就是脉宽调制,你能输出波形,就能改占空比

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝