代码:
/*
function :ec风机fg反馈信号输入初始化 PB6/7引脚 无需映射
input :无
output :无
*/
static void ec_motor_fg_input_config(uint16_t psc,uint16_t per)
{
timer_ic_parameter_struct timer_icinitpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(ec1_fg_rcu_clk);
rcu_periph_clock_enable(ec2_fg_rcu_clk);
rcu_periph_clock_enable(ec_time_fg_rcu_clk);
gpio_init(ec1_fg_gpio_port,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,ec1_fg_gpio_pin);
gpio_init(ec2_fg_gpio_port,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,ec2_fg_gpio_pin);
timer_deinit(ec_fg_timer);
timer_initpara.prescaler = psc;
timer_initpara.alignedmode = TIMER_IC_POLARITY_RISING;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = per;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(ec_fg_timer,&timer_initpara);
timer_channel_input_struct_para_init(&timer_icinitpara);
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x04;
timer_input_capture_config(ec_fg_timer,ec1_fg_channel,&timer_icinitpara);
timer_input_capture_config(ec_fg_timer,ec2_fg_channel,&timer_icinitpara);
timer_auto_reload_shadow_enable(ec_fg_timer);
timer_interrupt_flag_clear(ec_fg_timer,ec1_fg_interrupt_flag);
timer_interrupt_flag_clear(ec_fg_timer,ec2_fg_interrupt_flag);
timer_interrupt_enable(ec_fg_timer,ec1_fg_interrupt_ch);
timer_interrupt_enable(ec_fg_timer,ec2_fg_interrupt_ch);
timer_enable(ec_fg_timer);
}
调用:ec_motor_fg_input_config(119,65535); 120MHZ/
宏定义
#define ec_fg_timer TIMER3
#define ec1_fg_channel TIMER_CH_0
#define ec2_fg_channel TIMER_CH_1
#define ec1_fg_interrupt_flag TIMER_INT_FLAG_CH0 /* 定时器中断标志位 */
#define ec2_fg_interrupt_flag TIMER_INT_FLAG_CH1 /* 定时器中断标志位 */
#define ec1_fg_interrupt_ch TIMER_INT_CH0 /* 定时器中断通道 */
#define ec2_fg_interrupt_ch TIMER_INT_CH1 /* 定时器中断通道 */
#define ec1_fg_gpio_port GPIOB /* ec风机1反馈端口 */
#define ec2_fg_gpio_port GPIOB /* ec风机2反馈端口 */
#define ec1_fg_gpio_pin GPIO_PIN_6 /* ec风机1反馈引脚 */
#define ec2_fg_gpio_pin GPIO_PIN_7 /* ec风机2反馈引脚 */
#define ec_time_fg_rcu_clk RCU_TIMER3 /* ec风机2定时器时钟fg */
#define ec1_fg_rcu_clk RCU_GPIOB /* ec风机1反馈时钟 */
#define ec2_fg_rcu_clk RCU_GPIOB /* ec风机2反馈时钟 */
中断处理函数:
void TIMER3_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(ec_fg_timer,ec1_fg_interrupt_flag))
{
timer_interrupt_flag_clear(ec_fg_timer,ec1_fg_interrupt_flag);
if(0 == ec1_ccnumber)
{
/* 第一次中断读取一次数据 */
ec1_fg_read_value1 = timer_channel_capture_value_register_read(ec_fg_timer,ec1_fg_channel);
ec1_ccnumber = 1;
}
else if(1 == ec1_ccnumber)
{
/* 第二次中断读取一次数据 并计算产值和输入频率 */
ec1_fg_read_value2 = timer_channel_capture_value_register_read(ec_fg_timer,ec1_fg_channel);
if(ec1_fg_read_value2 > ec1_fg_read_value1)
{
/* 第二次读取数据大于第一次,说明未越过重装载值 */
ec1_count = ec1_fg_read_value2 - ec1_fg_read_value1;
}
else
{
/* 第二次读取数据小于第一次,说明已越过重装载值 */
ec1_count = (0xFFFFU - ec1_fg_read_value1) + ec1_fg_read_value2;
}
ec1_fre_data = 1000000/ec1_count;
ec1_ccnumber = 0;
}
}
if(SET == timer_interrupt_flag_get(ec_fg_timer,ec2_fg_interrupt_flag))
{
timer_interrupt_flag_clear(ec_fg_timer,ec2_fg_interrupt_flag);
if(0 == ec2_ccnumber)
{
/* 第一次中断读取一次数据 */
ec2_fg_read_value1 = timer_channel_capture_value_register_read(ec_fg_timer,ec2_fg_channel);
ec2_ccnumber = 1;
}
else if(1 == ec2_ccnumber)
{
/* 第二次中断读取一次数据 并计算产值和输入频率 */
ec2_fg_read_value2 = timer_channel_capture_value_register_read(ec_fg_timer,ec2_fg_channel);
if(ec2_fg_read_value2 > ec2_fg_read_value1)
{
/* 第二次读取数据大于第一次,说明未越过重装载值 */
ec2_count = ec2_fg_read_value2 - ec2_fg_read_value1;
}
else
{
/* 第二次读取数据小于第一次,说明已越过重装载值 */
ec2_count = (0xFFFFU - ec2_fg_read_value1) + ec2_fg_read_value2;
}
ec2_fre_data = 1000000/ec2_count;
ec2_ccnumber = 0;
}
}
}
读出来的数据ec1_fre_data/ec2_fre_data数据没有任何规律, 单片机GD32E103RBT6,引脚PB6/7 配置参考官方例程,想请教大佬帮忙解答下 |