使用STM32F030F4P6捕获外部脉冲一直没有成功
这是我的程序,希望大神帮帮忙
void TIM3_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* TIM3的NVIC中断配置 */
NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
// PA6 TIM3_CH1 PA7 TIM3_CH2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2);
TIM_BaseInitStructure.TIM_Period = 0xffff;
TIM_BaseInitStructure.TIM_Prescaler = 400;
TIM_BaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_BaseInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //上升沿
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; //全部捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_ITConfig(TIM3,TIM_IT_CC1|TIM_IT_CC2,ENABLE);
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);//清除更新中断|TIM_IT_Update
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);//清除捕获中断
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);//清除捕获中断
TIM_Cmd(TIM3 ,ENABLE);
}
void TIM3_IRQHandler(void)
{
if(TIM3->SR & 0x02) //通道一捕获
{
TIM3_CH1_temp[0] = TIM3->CCR1 ;
}
}
一周热门 更多>