hc32f460 2.2库函数 PWM有BUG tima PWM输出问题

初始化

    stc_timera_base_init_t stcTimeraInit;
    stc_timera_compare_init_t stcTimerCompareInit;
    
    /* configuration structure initialization */
    MEM_ZERO_STRUCT(stcTimeraInit);
    MEM_ZERO_STRUCT(stcTimerCompareInit);

    PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_AOS, Enable);

    
    /* Configuration timera unit 1 base structure */
    stcTimeraInit.enClkDiv = TimeraPclkDiv1;              //TimeraPclkDiv128
    stcTimeraInit.enCntMode = TimeraCountModeSawtoothWave;  //  TimeraCountModeSawtoothWave  锯齿波模式   TimeraCountModeTriangularWave  三角波模式
    stcTimeraInit.enCntDir = TimeraCountDirUp;
    stcTimeraInit.enSyncStartupEn = Disable;
    stcTimeraInit.u16PeriodVal = 500;                //重装载值  TIMERA_COUNT_OVERFLOW
    TIMERA_BaseInit(M4_TMRA1, &stcTimeraInit);

    /* Configuration timera unit 1 compare structure */
    stcTimerCompareInit.u16CompareVal = 0;             //比较输出值  占空比  stcTimeraInit.u16PeriodVal* 5u / 5u
    stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputLow;      //启动计数输出    // TimeraCountStartOutputLow TimeraCountStartOutputHigh 
    stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputLow;        //结束计数输出       TimeraCountStopOutputLow   TimeraCountStopOutputHigh
    stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputLow;  //比较匹配输出     //TimeraCompareMatchOutputLow  TimeraCompareMatchOutputHigh    TimeraCompareMatchOutputKeep  TimeraCompareMatchOutputReverse 匹配反向输出
    stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputHigh;   //Period匹配输出    //TimeraPeriodMatchOutputLow TimeraPeriodMatchOutputHigh TimeraPeriodMatchOutputKeep   TimeraPeriodMatchOutputReverse  
    stcTimerCompareInit.enSpecifyOutput = TimeraSpecifyOutputInvalid;        //指定输出 TimeraSpecifyOutputInvalid  TimeraSpecifyOutputHigh  TimeraSpecifyOutputLow
    stcTimerCompareInit.enCacheEn = Enable;
    stcTimerCompareInit.enTriangularTroughTransEn = Enable;
    stcTimerCompareInit.enTriangularCrestTransEn = Disable;
    stcTimerCompareInit.u16CompareCacheVal = stcTimerCompareInit.u16CompareVal;
     
        /*************/ //PA12 -- TIMA 6  CH1    0
        PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIMA6, Enable);
    PORT_SetFunc(PortA, Pin12, Func_Tima1, Disable);  //TIMA 6  CH1    --- Func_Tima1
        
    TIMERA_BaseInit(M4_TMRA6, &stcTimeraInit);
    TIMERA_CompareInit(M4_TMRA6, TimeraCh1, &stcTimerCompareInit);   
    TIMERA_CompareCmd(M4_TMRA6, TimeraCh1, Enable);
    TIMERA_Cmd(M4_TMRA6,Enable);

占空比控制 以下是自己写的占空比控制函数 ,千万别用库里的  要害死人

#define TIMERA_CALCS_REG_ADDR(reg, chl)          ((uint32_t)(&(reg)) + (chl)*0x4u)

en_result_t TIMERA_SetpareValue(M4_TMRA_TypeDef *TIMERAx, en_timera_channel_t enChannel,
                                   uint16_t u16CompareVals)
{
    __IO stc_tmra_cmpar_field_t *pstcTimeraCompare;
      __IO stc_tmra_cconr_field_t *pstcTimeraCapture;
    __IO stc_tmra_bconr_field_t *pstcTimeraCache;


            /* Configure cache control register */
            if ((TimeraCh1 == enChannel) || (TimeraCh3 == enChannel) ||
                    (TimeraCh5 == enChannel) || (TimeraCh7 == enChannel))
            {
                    pstcTimeraCache = (stc_tmra_bconr_field_t *)TIMERA_CALCS_REG_ADDR(TIMERAx->BCONR1, enChannel);
                    pstcTimeraCache->BSE0 = Disable;
                    pstcTimeraCache->BSE1 = Enable;  
                    pstcTimeraCache->BEN = Enable;
                    /* Configure compare cache value register */
                    pstcTimeraCompare = (stc_tmra_cmpar_field_t *)TIMERA_CALCS_REG_ADDR(TIMERAx->CMPAR1, enChannel + 1);
                    pstcTimeraCompare->CMP = u16CompareVals;  // 
            }

            /* Configure compare value register */
            pstcTimeraCompare = (stc_tmra_cmpar_field_t *)TIMERA_CALCS_REG_ADDR(TIMERAx->CMPAR1, enChannel);
            pstcTimeraCompare->CMP = u16CompareVals;         

            /* Set compare output function */
            pstcTimeraCapture = (stc_tmra_cconr_field_t *)TIMERA_CALCS_REG_ADDR(TIMERAx->CCONR1, enChannel);
            pstcTimeraCapture->CAPMD = 0u;
}

用法如下
TIMERA_SetpareValue(M4_TMRA6, TimeraCh1, 250);

输出频率为 200khz

占空比 = 250/500= 50%

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值