TIM_GetCounter(TIMx)与TIM_GetCapture2(TIMx)的比较与TIM_GetCapture2(TIMx)对标志位TIM_IT_CCx的影响
1.TIM_GetCounter(TIMx) 读取TIMx寄存器CNT中的计数值
/**
* @brief Gets the TIMx Counter value.
* @param TIMx: where x can be 1 to 17 to select the TIM peripheral.
* @retval Counter Register value.
*/
uint16_t TIM_GetCounter(TIM_TypeDef* TIMx)
{
/* Check the parameters */
assert_param(IS_TIM_ALL_PERIPH(TIMx));
/* Get the Counter Register value */
return TIMx->CNT;
}
2.TIM_GetCapture2(TIMx)读取TIMx捕获通道2发生捕获时保存在捕获比较寄存器CCR2中的计数器CNT的值
/**
* @brief Gets the TIMx Input Capture 2 value.
* @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the TIM peripheral.
* @retval Capture Compare 2 Register value.
*/
uint16_t TIM_GetCapture2(TIM_TypeDef* TIMx)
{
/* Check the parameters */
assert_param(IS_TIM_LIST6_PERIPH(TIMx));
/* Get the Capture 2 Register value */
return TIMx->CCR2;
}
总结:
TIM_GetCounter对应寄存器CNT,TIM_GetCapture2对应寄存器CRR2。发生捕获事件时,CRR2寄存器用来存储捕获发生时TIMx_CNT的值。TIM_GetCapture2才是最准确的,完全硬件级别的保存。
注:使用TIM_GetCapture2(TIMx) 读取捕获通道的数值会硬件改变捕获状态标志位。
while(TIM_GetFlagStatus(TIM5,TIM_IT_CC2)!=SET);
printf("TIM_IT_CC2 flag:%d \r\n",TIM_GetFlagStatus(TIM5,TIM_IT_CC2));
TIM_GetCapture2(TIM5);
printf("TIM_IT_CC2 flag:%d \r\n",TIM_GetFlagStatus(TIM5,TIM_IT_CC2));
TIM_GetCapture2(TIM5)对标志位TIM_IT_CC2影响见下图。