本帖最后由 zzd9077 于 2018-5-29 11:40 编辑
问题描述:使用STM32F407的定时器输入捕获功能,在没有使能定时器时,一直进入定时器中断硬件配置:
PA0:ADC功能
PA1:定时器2的PWM功能
PA2:定时器5的通道3的输入捕获功能
PA3:定时器9的通道2的输入捕获功能
软件流程:
定时器2一直在工作,串口收到上位机的测量指令后,使能定时器9,定时器9检测上升沿,检测到上升沿后在定时器9的中断服务函数中使能定时器5,定时器5捕获上升沿后,按照时钟信号频率在中断服务函数中采集PA0上的数据,采集2089次后完成一次完整的采集,关闭定时器5、定时器9,等待上位机下一次的测试指令。在线调试过程中发现,在没有收到上位机指令前,程序就在反复进入定时器9和定时器5的中断服务函数,此时定时器9和定时器5都还没有使能,实在是找不到问题了,特来求助大家。下附上定时器的初始化代码。
void TIM2_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2,ENABLE);
TIM_SetCompare2(TIM2,10);
TIM_Cmd(TIM2,ENABLE);
}
void TIM9_CH2_Cap_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM9_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM9);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM9,&TIM_TimeBaseStructure);
TIM9_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM9_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM9_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM9_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM9_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM9, &TIM9_ICInitStructure);
TIM_ITConfig(TIM9,TIM_IT_CC2,ENABLE);
TIM_ClearITPendingBit(TIM9, TIM_IT_CC2);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// TIM_Cmd(TIM9,ENABLE);
TIM_Cmd(TIM9,DISABLE);
}
void TIM1_BRK_TIM9_IRQHandler(void)
{
if(TIM_GetITStatus(TIM9,TIM_IT_CC2) != RESET)
TIM_Cmd(TIM5,ENSABLE);
TIM_ClearITPendingBit(TIM9, TIM_IT_CC2);
}
void TIM5_CH3_Cap_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM5_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_TIM5);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
TIM_ClearITPendingBit(TIM5, TIM_IT_CC3);
TIM_ITConfig(TIM5,TIM_IT_CC3,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM5,DISABLE);
}
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
if(TIM_GetITStatus(TIM5, TIM_IT_CC3) != RESET)
{
if(ADC_count<5)
{
CCD_Value[ADC_count]=Get_Adc();
ADC_count++;
if(ADC_count==5)
{
ADC_count=0;
TIM_Cmd(TIM5,DISABLE);
TIM_Cmd(TIM9,DISABLE);
// Usart1_Send_buff(CCD_Value,2089);
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC3);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>