/*******************************************************************************
*名 称: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); //更新占空比
}