我用的是TIM2的输入捕获功能 硬件管脚为:PD4 TIM2_CH1
设置代码如下,测试可用 ,你大致参考下,里面注释语句可能不对,你只看没有被注释掉的代码就可以了。
void TIM2_Config(void)
{
// GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_IN_PU_NO_IT);//霍尔信号捕获输入
GPIO_Init(GPIOA,(GPIO_Pin_TypeDef)(GPIO_PIN_1|GPIO_PIN_2),GPIO_MODE_OUT_PP_HIGH_SLOW);//
/* Set the Prescaler value */
TIM2->PSCR = (uint8_t)(TIM2_PRESCALER);
/* Set the Autoreload value */
TIM2->ARRH = (uint8_t)(TIM2_PERIOD >> 8);
TIM2->ARRL = (uint8_t)(TIM2_PERIOD);
/* TI1 Configuration */
/* Disable the Channel 1: Reset the CCE Bit */
TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC1E);
/* Select the Input and set the filter */
TIM2->CCMR1 = (uint8_t)((uint8_t)(TIM2->CCMR1 & (uint8_t)(~(uint8_t)( TIM2_CCMR_CCxS | TIM2_CCMR_ICxF )))
| (uint8_t)(((TIM2_ICSELECTION_DIRECTTI)) | ((uint8_t)( 0x00 << 4))));
/* Select the Polarity */
//TIM2->CCER1 &= (uint8_t)(~TIM2_CCER1_CC1P);//上升沿
TIM2->CCER1 |= TIM2_CCER1_CC1P;//下降沿
/* Set the CCE Bit */
TIM2->CCER1 |= TIM2_CCER1_CC1E;
/* Reset the IC1PSC Bits &Set the IC1PSC value */
TIM2->CCMR1 = (TIM2->CCMR1 & 0xF3)| TIM2_ICPSC_DIV1;
TIM2->CR1 |= 0x01;//使能TIM2
// TIM2->CR1 |= 0x04;// URS 位1:当更新请求使能时,只有计数器溢出才产生更新中断
/* Enable the Interrupt sources */
TIM2->IER |= TIM2_IT_CC1;
TIM2->IER |=TIM2_IT_UPDATE;
}
|