本帖最后由 uccms 于 2021-6-4 09:07 编辑
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM2_ICInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // 时钟使能
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // 速度为50MHz
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
GPIO_PinAFConfig(GPIOB, GPIO_Pin_6, GPIO_AF_4); // TIM2_CH1
TIM_TimeBaseInitStruct.TIM_Period = arr; // 设置自动重装载值
TIM_TimeBaseInitStruct.TIM_Prescaler = psc; // 设置定时器预分频值
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // 设置时钟分频
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; // 计数模式
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0; // 重复计数器的值
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct); // 初始化定时器
//输入捕获参数初始化
TIM2_ICInitStruct.TIM_Channel = TIM_Channel_1; //选择输入端 IC1映射到TI1上
TIM2_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Falling; // 边沿捕获
TIM2_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI; // 映射到TI1上
TIM2_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1; // 配置输入分频
TIM2_ICInitStruct.TIM_ICFilter = 0x00; //配置输入滤波器
TIM_ICInit(TIM2, &TIM2_ICInitStruct);
//TIM_SetCounter(TIM2, 0);
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn; // 中断源
NVIC_InitStruct.NVIC_IRQChannelPriority = 0; // 优先级
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; // 中断通道使能
NVIC_Init(&NVIC_InitStruct);
TIM_ClearFlag(TIM2, TIM_FLAG_Update | TIM_FLAG_CC1); // 清除中断标志位
TIM_ITConfig(TIM2, TIM_IT_Update | TIM_IT_CC1 ,ENABLE); // 使能定时器中断
TIM_Cmd(TIM2, ENABLE); //使能TIMx外设
定时器溢出中断正常,但是捕获中断一直没反应。不知哪里问题,帮忙看看,谢谢。 |