情况:我用的是timeA 通道一,由于我现在用了缓存所以我改变了通道二的占空比让他在三角波峰的时候传输,但是在占空比是满的情况下输出的不是高电平。
static void Timera_Config(void)
{
/* 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);
/* 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 = TimeraPclkDiv64;; //分频
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;
stcTimerCompareInit.enCacheEn = Enable;
stcTimerCompareInit.enTriangularTroughTransEn = Enable;
stcTimerCompareInit.enTriangularCrestTransEn = Disable;
stcTimerCompareInit.u16CompareCacheVal = stcTimerCompareInit.u16CompareVal;
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);
if(Value>=255)
{
TIMERA_SpecifyOutputSta(M4_TMRA4,TimeraCh2,TimeraSpecifyOutputHigh);
//TIMERA_SetCompareValue(M4_TMRA4, TimeraCh2,u16TimerPeriod);
}
else if(Value<=0)
{
TIMERA_SetCompareValue(M4_TMRA4, TimeraCh2,0);
//TIMERA_SpecifyOutputSta(M4_TMRA4,TimeraCh2,TimeraSpecifyOutputLow);
}
else
{
Value = (u16TimerPeriod/255)*Value;
TIMERA_SetCompareValue(M4_TMRA4, TimeraCh2,Value);
}
}
void Linear_variation()
{
for(int i=0;i<256;i++)
{
Set_comValue(i);
Ddl_Delay1us(100);
}
}
void Multiple_Changes()
{
uint16_t i=0;
for(i=0;i<300;i=i+10)
{
Ddl_Delay1ms(100);
Set_comValue(i);
Ddl_Delay1ms(100);
Set_comValue(300);
}
}
/**
*******************************************************************************
** \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;
uint16_t getvalue = 0u;
/* configure structure initialization */
MEM_ZERO_STRUCT(stcPortInit);
/* Configure system clock frequency */
SystemClk_Init();
/* Configure Timera */
Timera_Config();
Multiple_Changes(255);
while (1)
{
Multiple_Changes(255);
}
} |