利用TIM2_CH2(端口PA1,stm32f407开发板)进行PWM输入捕获时,不断进入TIM2_IRQHandler中断,请各位专家帮忙分析分析
1、TIM2_CH2的有个设置如下:
void TIM2_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM2时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2); //PA1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //GPIOA1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA1
TIM_TimeBaseStruct.TIM_Period=0xFFFFFFFF;
TIM_TimeBaseStruct.TIM_Prescaler=0;
TIM_TimeBaseStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStruct);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_2;//选择TIM2_CH2,选择输入端IC2映射到TI2上
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;//上升沿捕获
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;//映射到TI2上(TIM2映射至IC2--tuhongmao)
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;//在捕获输入上每探测到一个边沿就执行一次捕获
TIM_ICInitStructure.TIM_ICFilter=0;//滤波设置,经历几个周期跳变认定波形稳定
//(采样高电平,只有连续采集到N个电平是高电平才认为是有效的,否则低于N个时认为是无效的N取0x0-0xF)
TIM_PWMIConfig(TIM2,&TIM_ICInitStructure);//以上是输入捕获设置
TIM_SelectInputTrigger(TIM2,TIM_TS_TI2FP2);//选择滤波后的TI2FP2输入作为触发源,触发下面程序的复位
TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_Reset);//从模式控制器被设置为复位模式-选中的触发信号上升沿重新
//初始化计数器并产生一个更新信号(上升沿一到,TIM2->CNT被清零,每次上升沿来到,CNT都会被清零)
//从控制器默认的通道是否为TIM2_CH1 tuhongmao 20191015
TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);//启动定时器的被动触发
TIM_ITConfig(TIM2,TIM_IT_CC2,ENABLE);//捕获中断打开
TIM_ClearFlag(TIM2,TIM_IT_CC2);//清除标志位
TIM_Cmd(TIM2,ENABLE);//使能定时器2
}
2、中断设置与处理函数如下:
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//中断处理程序
void TIM2_IRQHandler(void)
{
static u16 count=0;
if(TIM_GetITStatus(TIM2,TIM_IT_CC2)!= RESET)
{
float IC2Value=0;
float DutyCycle=0;
float Frequency=0;
float pulse=0;
TIM_ClearITPendingBit(TIM2,TIM_IT_CC2);
IC2Value=(float)TIM_GetCapture2(TIM2);//获取CCR2的值
pulse=(float)TIM_GetCapture1(TIM2);//获得CCR1的值
if(IC2Value !=0)
{
DutyCycle=pulse/IC2Value;
Frequency=(float)84000000/IC2Value;
duty=(u32)(DutyCycle*(u32)100);
freq=Frequency/((float)1000);
count++;
if(count>=10000)
{
count=0;
printf("IC2Value is %f \r\n",IC2Value);
printf("pulse is %f \r\n",pulse);
}
}
else
{
duty=0;
freq=0;
}
}
}
3、主程序Main.c如下:
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200);//初始化串口波特率为115200
TIM2_Configuration();
NVIC_Configuration();
while(1)
{
}
}
4、串口打印结果如下:
IC2Value is 2.000000
pulse is 1.000000
IC2Value is 2.000000
pulse is 0.000000
IC2Value is 2.000000
pulse is 0.000000
IC2Value is 2.000000
pulse is 1.000000
IC2Value is 2.000000
pulse is 1.000000
IC2Value is 2.000000
pulse is 1.000000
IC2Value is 2.000000
pulse is 1.000000
IC2Value is 2.000000
...
(每进入10000次中断,再串口打印消息)
其他未做任何设置,端口也没有输入PWM信号,但是不断进入TIM2_IRQHandler中断,请帮忙分析分析。
|