STM32F10x---标准库函数定时器3 输入捕获

/*
函数功能:定时器3通道1 接受外部捕获
函数参数:uint16_t TIM_Period,uint16_t TIM_Prescaler,uint16_t TIM_Pulse
函数返回值:无
函数描述:无
*/
void DingShiQi3_1_Init(uint16_t TIM_Period,uint16_t TIM_Prescaler,uint16_t TIM_Pulse)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_ICInitTypeDef TIM_ICInitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
    
    NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
    NVIC_Init(&NVIC_InitStruct);
    
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD;//这里选择的是上升沿触发 所以下拉输入
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; 
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    
    GPIO_Init(GPIOA,&GPIO_InitStruct);
    GPIO_ResetBits(GPIOA,GPIO_Pin_6);
    
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //1:1  数字滤波器与定时器使用的采样频率之间的分频比例
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_Period = TIM_Period;
    TIM_TimeBaseInitStruct.TIM_Prescaler = TIM_Prescaler;  
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
    
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
    
    TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStruct.TIM_ICFilter = 2;  //滤波器带宽配置  我这里配置2是因为 按下去再回来 中断只触发一次 如果为1 按下去中断一次回来又中断
    TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿触发
    TIM_ICInitStruct.TIM_ICPrescaler = 0;
    TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;//选择ch1通道绑定TI1输入
    
    TIM_ICInit(TIM3,&TIM_ICInitStruct);
    
    TIM_ITConfig(TIM3,TIM_IT_CC1,ENABLE);
    
    TIM_Cmd(TIM3,ENABLE);
}

---------------------------------中断配置-----------------------------

/**
  * @brief  This function handles TIM3_IRQn Handler.
  * @param  None
  * @retval None
  */
void TIM3_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM3,TIM_IT_CC1) == SET)
    {
        printf("tim3 通道1 中断 捕获成功!\r\n");//如果串口没配置好可以 直接用led亮灭来表示 如果需要串口配置请DD我。
        TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
    }

}

------------------主函数里--------------------------

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //总中断优先级配置

猜你喜欢

转载自blog.csdn.net/longjintao1/article/details/124365079