主系统时钟HCLK 40M, PCLK0 40M,通过时钟输出测试okay;
但是PWM参考例程"timera_triangular_wave_compare_output"没有PWM输出,也没有进入TimA3定时中断,大家帮看下是什么问题。
#include "hc32_ddl.h"
#include "hal_CLK.h"
#include "hc32f46x_timera.h"
#define LED0_PORT (PortC)
#define LED0_PIN (Pin09)
#define LED0_ON() (PORT_SetBits(LED0_PORT, LED0_PIN))
#define LED0_OFF() (PORT_ResetBits(LED0_PORT, LED0_PIN))
#define LED0_TOGGLE() (PORT_Toggle(LED0_PORT, LED0_PIN))
/* TIMERA unit and clock definition */
#define TIMERA_UNIT3 (M4_TMRA3) // TimerA1--TimerA6,硬件TMRA3
#define TIMERA_UNIT3_CLOCK (PWC_FCG2_PERIPH_TIMA3) // TimerA3时钟
#define TIMERA_UNIT3_OVERFLOW_INT (INT_TMRA3_OVF) // 每次溢出产生中断
/* TIMERA channel 7 Port/Pin definition */
#define TIMERA_UNIT3_CH7 (TimeraCh7)
#define TIMERA_UNIT3_CH7_PORT (PortC)
#define TIMERA_UNIT3_CH7_PIN (Pin04)
#define TIMERA_UNIT3_CH7_FUNC (Func_Tima0)
/* TIMERA channel 8 Port/Pin definition */
#define TIMERA_UNIT3_CH8 (TimeraCh8)
#define TIMERA_UNIT3_CH8_PORT (PortC)
#define TIMERA_UNIT3_CH8_PIN (Pin05)
#define TIMERA_UNIT3_CH8_FUNC (Func_Tima0)
static uint8_t u8ExIntFlag = 0u, u8TmraUnit1Cnt = 0u;
/*******************************************************************************
** \brief Timera unit 3 callback function
**
** \param [in] None
**
** \retval None
**
******************************************************************************/
static void TimeraUnit3_IrqCallback(void)
{
u8TmraUnit1Cnt++;
if (u8TmraUnit1Cnt >= 100u) //1s
{
u8TmraUnit1Cnt = 0u;
LED0_TOGGLE();
}
TIMERA_ClearFlag(TIMERA_UNIT3, 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;
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);
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);
/* Configuration timera unit 3 base structure */
stcTimeraInit.enClkDiv = TimeraPclkDiv128; //PCLK0-PCLK4具体是哪个呢? PCLK1吧,20M/128=156.25Khz
stcTimeraInit.enCntMode = TimeraCountModeTriangularWave;
stcTimeraInit.enCntDir = TimeraCountDirUp;
stcTimeraInit.enSyncStartupEn = Disable;
stcTimeraInit.u16PeriodVal = 781; //freq: 100Hz -> 20M/128/200Hz=781
TIMERA_BaseInit(TIMERA_UNIT3, &stcTimeraInit);
/* Configuration timera unit 3 compare structure */
stcTimerCompareInit.u16CompareVal = stcTimeraInit.u16PeriodVal * 4u / 5u;
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 */
stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputHigh;
stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputHigh;
TIMERA_CompareInit(TIMERA_UNIT3, TIMERA_UNIT3_CH8, &stcTimerCompareInit);
TIMERA_CompareCmd(TIMERA_UNIT3, TIMERA_UNIT3_CH8, Enable);
/* Enable period count interrupt */
TIMERA_IrqCmd(TIMERA_UNIT3, TimeraIrqOverflow, Enable);
/* Interrupt of timera unit 3 */
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 = Enable;
stcTimeraHwConfig.enTrigFallingStartupEn = Disable;
stcTimeraHwConfig.enTrigRisingStartupEn = Disable;
TIMERA_HwStartupConfig(TIMERA_UNIT3, &stcTimeraHwConfig);
// /* Set external Int Ch.4 trigger timera startup */
// stcPortInit.enExInt = Enable;
// PORT_Init(KEY1_PORT, KEY1_PIN, &stcPortInit);
// TIMERA_SetCountTriggerSrc(KEY1_TRIGGER_EVENT);
}
void LED_Init(){
stc_port_init_t stcPortInit;
MEM_ZERO_STRUCT(stcPortInit); /* configure structure initialization */
LED0_OFF();
stcPortInit.enPinMode = Pin_Mode_Out;
PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);
}
void main(){
uint16_t u16TimerPeriod = 0u, u16DutyCycle = 0u;
sysCLK_Init(); //HCLK 40M, PCLK0 40M
LED_Init();
/* Configure Timera */
Timera_Config();
u16DutyCycle = TIMERA_GetCompareValue(TIMERA_UNIT3, TIMERA_UNIT3_CH7);
u16TimerPeriod = TIMERA_GetPeriodValue(TIMERA_UNIT3);
while(1);
}
|