参考例程timera_triangular_wave_compare_output修改的输出波形不是方波。哪位大佬能帮我看看哪里有问题。
#include "hc32_ddl.h"
/* TIMERA unit and clock definition */
#define TIMERA_UNIT1 (M4_TMRA1)
#define TIMERA_UNIT1_CLOCK (PWC_FCG2_PERIPH_TIMA1)
#define TIMERA_UNIT1_OVERFLOW_INT (INT_TMRA1_OVF)
/* TIMERA channel 1 Port/Pin definition */
#define TIMERA_UNIT1_CH1 (TimeraCh1)
#define TIMERA_UNIT1_CH1_PORT (PortA)
#define TIMERA_UNIT1_CH1_PIN (Pin14)
#define TIMERA_UNIT1_CH1_FUNC (Func_Tima0)
/* TIMERA channel 3 Port/Pin definition */
#define TIMERA_UNIT1_CH3 (TimeraCh3)
#define TIMERA_UNIT1_CH3_PORT (PortA)
#define TIMERA_UNIT1_CH3_PIN (Pin05)
#define TIMERA_UNIT1_CH3_FUNC (Func_Tima0)
static uint8_t u8TmraUnit1Cnt = 0u;
uint16_t u16TimerPeriod = 0u, u16DutyCycle = 0u;
/**
*******************************************************************************
** \brief Timera unit 1 callback function
**
** \param [in] None
**
** \retval None
**
******************************************************************************/
static void TimeraUnit1_IrqCallback(void)
{
u8TmraUnit1Cnt++;
if (u8TmraUnit1Cnt >= 100u) //1s
{
u8TmraUnit1Cnt = 0u;
PORT_Toggle(PortH, Pin02);//LED
}
TIMERA_ClearFlag(TIMERA_UNIT1, TimeraFlagOverflow);
}
/**
*******************************************************************************
** \brief Configure Timera peripheral function
**
** \param [in] None
**
** \retval None
**
******************************************************************************/
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;
/* configuration structure initialization */
MEM_ZERO_STRUCT(stcTimeraInit);
MEM_ZERO_STRUCT(stcIrqRegiConf);
MEM_ZERO_STRUCT(stcTimerCompareInit);
MEM_ZERO_STRUCT(stcTimeraHwConfig);
/* Configuration peripheral clock */
PWC_Fcg2PeriphClockCmd(TIMERA_UNIT1_CLOCK, Enable);
PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_AOS, Enable);
/* Configuration TIMERA compare pin */
PORT_SetFunc(TIMERA_UNIT1_CH1_PORT, TIMERA_UNIT1_CH1_PIN, TIMERA_UNIT1_CH1_FUNC, Disable);
PORT_SetFunc(TIMERA_UNIT1_CH3_PORT, TIMERA_UNIT1_CH3_PIN, TIMERA_UNIT1_CH3_FUNC, Disable);
/* Configuration timera unit 1 base structure */
stcTimeraInit.enClkDiv = TimeraPclkDiv128; //100MHz 128分频
//stcTimeraInit.enCntMode = TimeraCountModeSawtoothWave;
stcTimeraInit.enCntMode = TimeraCountModeTriangularWave;
stcTimeraInit.enCntDir = TimeraCountDirUp;
stcTimeraInit.enSyncStartupEn = Disable;
stcTimeraInit.u16PeriodVal = 1000u;
TIMERA_BaseInit(TIMERA_UNIT1, &stcTimeraInit);
/* Configuration timera unit 1 compare structure */
stcTimerCompareInit.u16CompareVal = stcTimeraInit.u16PeriodVal * 4u / 5u;
stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputLow;
stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputLow;
stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputHigh;
stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputLow;
stcTimerCompareInit.enSpecifyOutput = TimeraSpecifyOutputInvalid;
stcTimerCompareInit.enCacheEn = Enable;
stcTimerCompareInit.enTriangularTroughTransEn = Enable;
stcTimerCompareInit.enTriangularCrestTransEn = Disable;
stcTimerCompareInit.u16CompareCacheVal = stcTimeraInit.u16PeriodVal * 4u / 5u;
/* Configure Channel 1 */
TIMERA_CompareInit(TIMERA_UNIT1, TIMERA_UNIT1_CH1, &stcTimerCompareInit);
TIMERA_CompareCmd(TIMERA_UNIT1, TIMERA_UNIT1_CH1, Enable);
/* Configure channel 3 */
stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputHigh;
stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputHigh;
stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputLow;
stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputHigh;
TIMERA_CompareInit(TIMERA_UNIT1, TIMERA_UNIT1_CH3, &stcTimerCompareInit);
TIMERA_CompareCmd(TIMERA_UNIT1, TIMERA_UNIT1_CH3, Enable);
/* Enable period count interrupt */
TIMERA_IrqCmd(TIMERA_UNIT1, TimeraIrqOverflow, Enable);
/* Interrupt of timera unit 1 */
stcIrqRegiConf.enIntSrc = TIMERA_UNIT1_OVERFLOW_INT;
stcIrqRegiConf.enIRQn = Int006_IRQn;
stcIrqRegiConf.pfnCallback = &TimeraUnit1_IrqCallback;
enIrqRegistration(&stcIrqRegiConf);
NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);
NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
TIMERA_Cmd(TIMERA_UNIT1,Enable);
TIMERA_SetCompareValue(TIMERA_UNIT1, TIMERA_UNIT1_CH1, stcTimeraInit.u16PeriodVal * 4u / 5u);
TIMERA_SetCompareValue(TIMERA_UNIT1, TIMERA_UNIT1_CH3, stcTimeraInit.u16PeriodVal * 4u / 5u);
}
/**
*******************************************************************************
** \brief main function for Timera compare function
**
** \param [in] None
**
** \retval int32_t Return value, if needed
**
******************************************************************************/
int32_t main(void)
{
stc_port_init_t stcPortInit;
MEM_ZERO_STRUCT(stcPortInit);
stcPortInit.enPinMode = Pin_Mode_Out;
stcPortInit.enExInt = Enable;
stcPortInit.enPullUp = Enable;
PORT_Init(PortH, Pin02,&stcPortInit);//Led
/* BSP initialization */
BSP_CLK_Init();
/* Configure Timera */
Timera_Config();
while (1)
{
;
}
}
/*******************************************************************************
* EOF (not truncated)
******************************************************************************/
|