初始化
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%