static void Timera_Config(void)
{
stc_timera_base_init_t stcTimeraInit;
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(PWC_FCG2_PERIPH_TIMA4, Enable);
PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_AOS, Enable);
/* Configuration TIMERA compare pin */
PORT_SetFunc(TIMERA_UNIT1_CH4_PORT, TIMERA_UNIT1_CH4_PIN, TIMERA_UNIT1_CH4_FUNC, Disable);
/* Configuration timera unit 1 base structure */
stcTimeraInit.enClkDiv = TimeraPclkDiv128; //分频
stcTimeraInit.enCntMode = TimeraCountModeTriangularWave; //波形
stcTimeraInit.enCntDir = TimeraCountDirUp;//计数方向
stcTimeraInit.enSyncStartupEn = Disable;//关闭同步
stcTimeraInit.u16PeriodVal = 330u; //freq: 1000Hz //设置周期
TIMERA_BaseInit(M4_TMRA4, &stcTimeraInit); //初始化tima4
/* Configuration timera unit 1 compare structure */
stcTimerCompareInit.u16CompareVal = 87u;//占空比
stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputLow;
stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputLow;
stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputReverse;
stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputKeep;
stcTimerCompareInit.enSpecifyOutput = TimeraSpecifyOutputInvalid;
TIMERA_CompareInit(M4_TMRA4, TIMERA_UNIT1_CH4, &stcTimerCompareInit);//比较使能
TIMERA_CompareCmd(M4_TMRA4, TIMERA_UNIT1_CH4, Enable);//
TIMERA_Cmd(M4_TMRA4,Enable);
}
void Set_comValue(uint16_t Value)
{
float u16TimerPeriod;
u16TimerPeriod = TIMERA_GetPeriodValue(M4_TMRA4);
TIMERA_Cmd(M4_TMRA4,Disable);
if(Value>=255)
{
TIMERA_SpecifyOutputSta(M4_TMRA4,TIMERA_UNIT1_CH4,TimeraSpecifyOutputHigh);
TIMERA_CompareCmd(M4_TMRA4, TIMERA_UNIT1_CH4, Enable);//
}
else if(Value<=0)
{
TIMERA_SpecifyOutputSta(M4_TMRA4,TIMERA_UNIT1_CH4,TimeraSpecifyOutputLow);
TIMERA_CompareCmd(M4_TMRA4, TIMERA_UNIT1_CH4, Enable);//
}
else
{
Value = (u16TimerPeriod/255)*Value;
TIMERA_SetCompareValue(M4_TMRA4, TIMERA_UNIT1_CH4,Value);
TIMERA_CompareCmd(M4_TMRA4, TIMERA_UNIT1_CH4, Enable);//
}
TIMERA_Cmd(M4_TMRA4,Enable);
}
void Linear_variation()
{
for(int i=0;i<255;i++)
{
Ddl_Delay1ms(500ul);
Set_comValue(i);
Ddl_Delay1ms(500ul);
}
TIMERA_Cmd(M4_TMRA4,Enable);
}
/**
*******************************************************************************
** \brief main function for Timera compare function
**
** \param [in] None
**
** \retval int32_t Return value, if needed
**
******************************************************************************/
int32_t main(void)
{
uint16_t u16TimerPeriod = 0u, u16DutyCycle = 0u;
stc_port_init_t stcPortInit;
/* configure structure initialization */
MEM_ZERO_STRUCT(stcPortInit);
/* Configure system clock frequency */
SystemClk_Init();
/* Configure Timera */
Timera_Config();
u16DutyCycle = TIMERA_GetCompareValue(M4_TMRA4, TIMERA_UNIT1_CH4);
u16TimerPeriod = TIMERA_GetPeriodValue(M4_TMRA4);
while (1)
{
Linear_variation();
}
} |