[原创] 关于STM32F103输入捕获配置过程的问题

wqererty   2024-3-29 12:53 楼主

1、输入捕获边沿的选择

在进行输入捕获的配置时,会用到这个结构体:TIM_ICInitTypeDef,具体如下,

typedef struct
{
  uint16_t TIM_Channel; 
    
  uint16_t TIM_ICPolarity;  
 
  uint16_t TIM_ICSelection; 

  uint16_t TIM_ICPrescaler;  

  uint16_t TIM_ICFilter;          

} TIM_ICInitTypeDef;

这个结构体的第二个参数TIM_ICPolarity似乎有三个可选的配置:

#define  TIM_ICPolarity_Rising             ((uint16_t)0x0000) //上升沿
#define  TIM_ICPolarity_Falling            ((uint16_t)0x0002) //下降沿
#define  TIM_ICPolarity_BothEdge           ((uint16_t)0x000A) //双边沿

但是!!!在这三个宏定义下还有两个带参宏!

#define  TIM_ICPolarity_Rising             ((uint16_t)0x0000)
#define  TIM_ICPolarity_Falling            ((uint16_t)0x0002)
#define  TIM_ICPolarity_BothEdge           ((uint16_t)0x000A)
#define IS_TIM_IC_POLARITY(POLARITY) (((POLARITY) == TIM_ICPolarity_Rising) || \
                                      ((POLARITY) == TIM_ICPolarity_Falling))
#define IS_TIM_IC_POLARITY_LITE(POLARITY) (((POLARITY) == TIM_ICPolarity_Rising) || \
                                           ((POLARITY) == TIM_ICPolarity_Falling)|| \
                                           ((POLARITY) == TIM_ICPolarity_BothEdge)) 

可以看出 IS_TIM_IC_POLARITY(POLARITY) 是不支持双边沿的

而IS_TIM_IC_POLARITY_LITE(POLARITY) 支持双边沿

而ST官方的库函数TIM_ICInit()实现过程中有下面一段代码:

  if((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) ||
     (TIMx == TIM4) ||(TIMx == TIM5))
  {
    assert_param(IS_TIM_IC_POLARITY(TIM_ICInitStruct->TIM_ICPolarity));
  }
  else
  {
    assert_param(IS_TIM_IC_POLARITY_LITE(TIM_ICInitStruct->TIM_ICPolarity));
  }

也就是说如果是定时器1~5,8就执行IS_TIM_IC_POLARITY(POLARITY)

否则就执行 IS_TIM_IC_POLARITY_LITE(POLARITY),我们都知道,STM32F103系列只有定时器1~8,而定时器6,7不支持输入捕获,因此在进行输入捕获的配置时不能将TIM_ICPolarity设置为TIM_ICPolarity_BothEdge。此外,为解决这个问题进行查找资料的过程中,在ST全球论坛上发现了如下回答:

02914f77fa6a44768b0ecda9c5c36b2c.png

原文链接:[bug report] no definition TIM_ICPOLARITY_BOTHE... - STMicroelectronics Community

大概意思就是TIM1/8不支持TIM_ICPolarity_BothEdge

而TIM9/10/11/12/13支持TIM_ICPolarity_BothEdge

但是在我配置过程中将TIM_ICPolarity设置为TIM_ICPolarity_BothEdge,即

	/*定时器4输入捕获配置*/
	TIM4_InPutCapture.TIM_Channel = TIM_Channel_3;
	TIM4_InPutCapture.TIM_ICFilter = 4; 
	TIM4_InPutCapture.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM4_InPutCapture.TIM_ICSelection = TIM_ICSelection_DirectTI;		
	TIM4_InPutCapture.TIM_ICPolarity = TIM_ICPolarity_BothEdge;     //上升沿下降沿都进行捕获
	TIM_ICInit(TIM4,&TIM4_InPutCapture);

在编译过程中也并未出现警告和报错,在执行TIM_ICInit()到下面的代码时

  if((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) ||
     (TIMx == TIM4) ||(TIMx == TIM5))
  {
    assert_param(IS_TIM_IC_POLARITY(TIM_ICInitStruct->TIM_ICPolarity));
  }
  else
  {
    assert_param(IS_TIM_IC_POLARITY_LITE(TIM_ICInitStruct->TIM_ICPolarity));
  }

按道理说应该只会执行

 assert_param(IS_TIM_IC_POLARITY(TIM_ICInitStruct->TIM_ICPolarity));

而 IS_TIM_IC_POLARITY(POLARITY)又不支持 TIM_ICPolarity_BothEdge,编译过程不应该出现报错或者警告吗?希望有人可以解答一下我的问题。

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复