下面的代码是我依照网上的资料编写的GD32F303CBT6的输入捕获功能,使用的是PA0,和TIME4_CH0,PA0引脚已经输入1KHZ的50%的信号。
#include "gd32f30x.h"
#include "gd32f303c_start.h"
#include "systick.h"
void timer_config4(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
/* configure systick */
systick_config();
/* enable the LEDs GPIO clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* configure LED1 LED2 GPIO port */
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
/*Configure PA0(TIMER4_CH0) as alternate function*/
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
timer_config4();
/* reset LED1 LED2 GPIO pin */
gpio_bit_reset(GPIOB, GPIO_PIN_1);
while(1){
gpio_bit_set(GPIOB, GPIO_PIN_1);
delay_1ms(1000);
gpio_bit_reset(GPIOB, GPIO_PIN_1);
delay_1ms(1000);
}
}
/**
\brief configure the TIMER peripheral
\param[in] none
\param[out] none
\retval none
*/
void timer_config4(void)
{
/* TIMER1 configuration: generate PWM signals with different duty cycles:
TIMER1CLK = SystemCoreClock / 120 = 1MHz */
timer_ic_parameter_struct timer_icinitpara;
timer_parameter_struct timer_initpara;
/* connect IRC40K clock to the TIMER4_CH3 input for calibration */
//rcu_periph_clock_enable(RCU_AF);
//gpio_pin_remap_config(GPIO_TIMER4CH3_IREMAP, ENABLE);
rcu_periph_clock_enable(RCU_TIMER4);
timer_deinit(TIMER4);
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(TIMER4_IRQn, 1, 1);
/* initialize TIMER init parameter struct */
timer_struct_para_init(&timer_initpara);
/* TIMER4 configuration */
timer_initpara.prescaler = 71;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 0xFFFF;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER4, &timer_initpara);
/* TIMER4 configuration */
/* initialize TIMER channel input parameter struct */
timer_channel_input_struct_para_init(&timer_icinitpara);
/* TIMER4 CH3 input capture configuration */
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_FALLING;
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x0;
timer_input_capture_config(TIMER4, TIMER_CH_0, &timer_icinitpara);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER4);
/* clear channel 3 interrupt bit */
//timer_interrupt_flag_clear(TIMER4, TIMER_INT_FLAG_CH0);
timer_interrupt_flag_clear(TIMER4, TIMER_INT_CH0);
/* channel 3 interrupt enable */
timer_interrupt_enable(TIMER4, TIMER_INT_CH0);
/* TIMER4 counter enable */
timer_enable(TIMER4);
}
中断代码
void TIMER4_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER4, TIMER_INT_CH0)){
/* clear channel 3 interrupt bit */
timer_interrupt_flag_clear(TIMER4, TIMER_INT_CH0);
timer_capture_num++;
if(1 == timer_capture_num){
/* get the input capture value */
timer_capture1 = timer_channel_capture_value_register_read(TIMER4,TIMER_CH_0);
}else if(2 == timer_capture_num){
/* get the input capture value */
timer_capture2 = timer_channel_capture_value_register_read(TIMER4,TIMER_CH_0);
/* end capture, disable TIMER4 and CH3 interrupt */
timer_interrupt_disable(TIMER4, TIMER_INT_CH0);
timer_disable(TIMER4);
}
}
}
该程序运行后就是无法进入中断,请做过的高手指点一下。
|