一、配置路线
二、问题及反思
配置的时候误以为需要先把【输入捕获配置】了再去配置【从模式】,后面验证了这样配置没办法产生预期的效果。
代码如下:void gtim_timx_cnt_chy_init(uint16_t psc, uint16_t arr)
void gtim_timx_cnt_chy_init(uint16_t psc, uint16_t arr)
{
/* TIM初始化 */
gtim_timx_cnt_chy_handle.Instance = TIM5;
gtim_timx_cnt_chy_handle.Init.Prescaler = psc;
gtim_timx_cnt_chy_handle.Init.Period = arr;
gtim_timx_cnt_chy_handle.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_IC_Init(>im_timx_cnt_chy_handle);
/* GPIO初始化,开启时钟 */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_TIM5_CLK_ENABLE();
GPIO_InitTypeDef gpio_init_struct;
gpio_init_struct.Pin = GPIO_PIN_0; /* LED0引脚 */
gpio_init_struct.Mode = GPIO_MODE_AF_PP; /* 推挽输出 */
gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
gpio_init_struct.Alternate = GPIO_AF2_TIM5;
HAL_GPIO_Init(GPIOA, &gpio_init_struct); /* 初始化LED0引脚 */
// /* 输入捕获配置 */
// TIM_IC_InitTypeDef timx_chy_ic_handle = {0};
// timx_chy_ic_handle.ICPolarity = TIM_ICPOLARITY_RISING;
// timx_chy_ic_handle.ICSelection = TIM_ICSELECTION_DIRECTTI;
// timx_chy_ic_handle.ICPrescaler = TIM_ICPSC_DIV1;
// timx_chy_ic_handle.ICFilter = 0;
// HAL_TIM_IC_ConfigChannel(>im_timx_cnt_chy_handle,&timx_chy_ic_handle, TIM_CHANNEL_1);
// HAL_TIM_IC_Start(>im_timx_cnt_chy_handle, TIM_CHANNEL_1); /* 开启捕获功能 */
/* 从模式初始化 */
TIM_SlaveConfigTypeDef timx_chy_slave_handle = {0};
timx_chy_slave_handle.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
timx_chy_slave_handle.InputTrigger = TIM_TS_TI1FP1;
timx_chy_slave_handle.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;
timx_chy_slave_handle.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
timx_chy_slave_handle.TriggerFilter = 0;
HAL_TIM_SlaveConfigSynchro(>im_timx_cnt_chy_handle,&timx_chy_slave_handle);
/* 使能定时器更新中断,配置定时器中断优先级,开启捕获功能 */
HAL_NVIC_SetPriority(TIM5_IRQn, 1, 3);
HAL_NVIC_EnableIRQ(TIM5_IRQn);
__HAL_TIM_ENABLE_IT(>im_timx_cnt_chy_handle, TIM_IT_UPDATE); /* 使能更新中断 */
HAL_TIM_IC_Start(>im_timx_cnt_chy_handle, TIM_CHANNEL_1); /* 开启捕获功能 */
}
// /* 输入捕获配置 */
// TIM_IC_InitTypeDef timx_chy_ic_handle = {0};
// timx_chy_ic_handle.ICPolarity = TIM_ICPOLARITY_RISING;
// timx_chy_ic_handle.ICSelection = TIM_ICSELECTION_DIRECTTI;
// timx_chy_ic_handle.ICPrescaler = TIM_ICPSC_DIV1;
// timx_chy_ic_handle.ICFilter = 0;
// HAL_TIM_IC_ConfigChannel(>im_timx_cnt_chy_handle,&timx_chy_ic_handle, TIM_CHANNEL_1);
// HAL_TIM_IC_Start(>im_timx_cnt_chy_handle, TIM_CHANNEL_1); /* 开启捕获功能 */
配置方面的代码以及寄存器的理解已经有一定的程度,但是在main.c编写代码逻辑时还是不够清晰,经常犯一些错误。