打印
[技术问答]

HC32F460 PWM输出异常

[复制链接]
3956|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 茂xiang 于 2021-10-2 21:25 编辑

使用PC4 PC5 PB0 PB1 4路TimerA的PWM,设置不同周期,发现设置相互影响,也不知道哪个设置的是对的,感觉PWM有些问题。PCLK1-20Mhz


// TIMERA unit and clock definition,硬件PB0 PB1 PC4 PC5 4个引脚连接对应TimerA3
#define TIMERA_UNIT3                    (M4_TMRA3)                                // TimerA1--TimerA6,硬件TMRA3
#define TIMERA_UNIT3_CLOCK              (PWC_FCG2_PERIPH_TIMA3)        // TimerA3时钟
#define TIMERA_UNIT3_OVERFLOW_INT       (INT_TMRA3_OVF)                        // 每次溢出产生中断


// TIMERA channel 7 Port/Pin definition
#define TIMERA_UNIT3_CH7                (TimeraCh7)
#define TIMERA_UNIT3_CH7_PORT           (PortC)
#define TIMERA_UNIT3_CH7_PIN            (Pin04)
#define TIMERA_UNIT3_CH7_FUNC           (Func_Tima1)                        // 知识点-Func5对应TIMA1

// TIMERA channel 8 Port/Pin definition
#define TIMERA_UNIT3_CH8                (TimeraCh8)
#define TIMERA_UNIT3_CH8_PORT           (PortC)
#define TIMERA_UNIT3_CH8_PIN            (Pin05)
#define TIMERA_UNIT3_CH8_FUNC           (Func_Tima1)                        // 知识点-Func5对应TIMA1

// TIMERA channel 3 Port/Pin definition
#define TIMERA_UNIT3_CH3                (TimeraCh3)
#define TIMERA_UNIT3_CH3_PORT           (PortB)
#define TIMERA_UNIT3_CH3_PIN            (Pin00)
#define TIMERA_UNIT3_CH3_FUNC           (Func_Tima1)                        // 知识点-Func5对应TIMA1

// TIMERA channel 4 Port/Pin definition
#define TIMERA_UNIT3_CH4                (TimeraCh4)
#define TIMERA_UNIT3_CH4_PORT           (PortB)
#define TIMERA_UNIT3_CH4_PIN            (Pin01)
#define TIMERA_UNIT3_CH4_FUNC           (Func_Tima1)                        // 知识点-Func5对应TIMA1


#define PWM_FREQ_20K_CNT_250                        250                                                //20K频率,对应TimerA计数周期
/*******************************************************************************
*名  称:TimeraUnit3_IrqCallback
*功  能:PWM周期到达回调中断函数
*说  明:
******************************************************************************/
void TimeraUnit3_IrqCallback(){
    u16TmraUnit1Cnt++;
    if(u16TmraUnit1Cnt >= 20000u){      //PWM 1s测试,20K频率
        u16TmraUnit1Cnt = 0u;
        LED_Y_TOGGLE;
    }
    TIMERA_ClearFlag(TIMERA_UNIT3, TimeraFlagOverflow);
}

/*******************************************************************************
*名    称:Timera_Config
*功    能:TimerA初始化,PWM初始化
*说    明:
*******************************************************************************/
void Timera_Config(){
    stc_timera_base_init_t stcTimeraInit;                                        // TimerA初始化结构体
    stc_timera_compare_init_t stcTimerCompareInit;                        //
    stc_irq_regi_conf_t stcIrqRegiConf;                                                // 溢出中断配置结构体
    stc_timera_hw_startup_config_t stcTimeraHwConfig;                // 硬件触发,后期删除
    stc_port_init_t stcPortInit;

    // configuration structure initialization
    MEM_ZERO_STRUCT(stcTimeraInit);
    MEM_ZERO_STRUCT(stcIrqRegiConf);
    MEM_ZERO_STRUCT(stcTimerCompareInit);
    MEM_ZERO_STRUCT(stcTimeraHwConfig);
    MEM_ZERO_STRUCT(stcPortInit);

    // Configuration peripheral clock
    PWC_Fcg2PeriphClockCmd(TIMERA_UNIT3_CLOCK, Enable);                // 使能TIMERA3时钟
        PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_AOS, Enable);        // 功能未知

    // Configuration TIMERA compare pin
        PORT_SetFunc(TIMERA_UNIT3_CH7_PORT, TIMERA_UNIT3_CH7_PIN, TIMERA_UNIT3_CH7_FUNC, Disable);
    PORT_SetFunc(TIMERA_UNIT3_CH8_PORT, TIMERA_UNIT3_CH8_PIN, TIMERA_UNIT3_CH8_FUNC, Disable);
        PORT_SetFunc(TIMERA_UNIT3_CH3_PORT, TIMERA_UNIT3_CH3_PIN, TIMERA_UNIT3_CH3_FUNC, Disable);
        PORT_SetFunc(TIMERA_UNIT3_CH4_PORT, TIMERA_UNIT3_CH4_PIN, TIMERA_UNIT3_CH4_FUNC, Disable);
       
    // Configuration timera unit 3 base structure
    stcTimeraInit.enClkDiv = TimeraPclkDiv2;                                //PCLK1,20M/2 = 10MHhz
    stcTimeraInit.enCntMode = TimeraCountModeTriangularWave;// 三角波向上计数
    stcTimeraInit.enCntDir = TimeraCountDirUp;                                // 向上计数
    stcTimeraInit.enSyncStartupEn = Disable;                                // 6个单元的TimerA,可以实现软件/硬件同步启动。单元2~6可以选择与单元1实现同步启动
    stcTimeraInit.u16PeriodVal = PWM_FREQ_20K_CNT_250;      //freq: 20KPWM周期 -> 20M/2/20K=500 (但周期一定要设置为250,慢一半,原因未知)
    TIMERA_BaseInit(TIMERA_UNIT3, &stcTimeraInit);

    // Configuration timera unit 3 compare structure
    //stcTimerCompareInit.u16CompareVal = stcTimeraInit.u16PeriodVal * 4u / 5u;
        stcTimerCompareInit.u16CompareVal = 100;
        stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputLow;
    stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputLow;
    stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputReverse;        // 建议该参数,修改后高占空比测试无效
    stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputKeep;                // 建议该参数,修改后高占空比测试无效
    stcTimerCompareInit.enSpecifyOutput = TimeraSpecifyOutputInvalid;
    stcTimerCompareInit.enCacheEn = Enable;
    stcTimerCompareInit.enTriangularTroughTransEn = Enable;
    stcTimerCompareInit.enTriangularCrestTransEn = Disable;
    stcTimerCompareInit.u16CompareCacheVal = stcTimerCompareInit.u16CompareVal;
    // Configure Channel 7
    TIMERA_CompareInit(TIMERA_UNIT3, TIMERA_UNIT3_CH7, &stcTimerCompareInit);
    TIMERA_CompareCmd(TIMERA_UNIT3, TIMERA_UNIT3_CH7, Enable);
    // Configure channel 8
    TIMERA_CompareInit(TIMERA_UNIT3, TIMERA_UNIT3_CH8, &stcTimerCompareInit);
    TIMERA_CompareCmd(TIMERA_UNIT3, TIMERA_UNIT3_CH8, Enable);
        // Configure channel 3
    TIMERA_CompareInit(TIMERA_UNIT3, TIMERA_UNIT3_CH3, &stcTimerCompareInit);
    TIMERA_CompareCmd(TIMERA_UNIT3, TIMERA_UNIT3_CH3, Enable);
        // Configure channel 4
    TIMERA_CompareInit(TIMERA_UNIT3, TIMERA_UNIT3_CH4, &stcTimerCompareInit);
    TIMERA_CompareCmd(TIMERA_UNIT3, TIMERA_UNIT3_CH4, Enable);
       
    // Enable period count interrupt
    TIMERA_IrqCmd(TIMERA_UNIT3, TimeraIrqOverflow, Enable);
    stcIrqRegiConf.enIntSrc = TIMERA_UNIT3_OVERFLOW_INT;
    stcIrqRegiConf.enIRQn = Int006_IRQn;
    stcIrqRegiConf.pfnCallback = &TimeraUnit3_IrqCallback;
    enIrqRegistration(&stcIrqRegiConf);
    NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);
    NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_15);
    NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);

    // Configure timera unit 3 startup
    stcTimeraHwConfig.enSpecifyEventStartupEn = Disable;
    stcTimeraHwConfig.enTrigFallingStartupEn = Disable;
    stcTimeraHwConfig.enTrigRisingStartupEn = Disable;
    TIMERA_HwStartupConfig(TIMERA_UNIT3, &stcTimeraHwConfig);

        TIMERA_Cmd(TIMERA_UNIT3, Enable);
       
        //设置不同PWM,测试输出占空比、频率
        TIMERA_SetCacheValue(TIMERA_UNIT3, TIMERA_UNIT3_CH7, 200);        //更新占空比
        TIMERA_SetCacheValue(TIMERA_UNIT3, TIMERA_UNIT3_CH8, 150);        //更新占空比
        TIMERA_SetCacheValue(TIMERA_UNIT3, TIMERA_UNIT3_CH4, 100);        //更新占空比
        TIMERA_SetCacheValue(TIMERA_UNIT3, TIMERA_UNIT3_CH3, 50);        //更新占空比
}


PWM硬件连接.png (58.06 KB )

PWM硬件连接.png

实际输出.png (225.98 KB )

实际输出.png

使用特权

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

本版积分规则

6

主题

13

帖子

1

粉丝