我用串口给指令让定时器5来发脉冲;有时候当指令发送后,定时器不发脉冲,dug时发现程序执行到打开定时器5后,进不去中断,之后就一直进不去中断,发不了脉冲。
定时器5配置如下:
void TIM5_PWM_Init(u32 arr,u32 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //TIM5时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTF时钟
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM5); //GPIOA3复用为定时5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //GPIOA3
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_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA3
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);//初始化定时5
//初始化TIM5 Channe4 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
TIM_OCInitStructure.TIM_Pulse = arr/2;//arr+1;
TIM_OC4Init(TIM5, &TIM_OCInitStructure); //根据T指定的参数初始化外设
TIM_OC4PreloadConfig(TIM5, TIM_OCPreload_Enable); //使能TIM5在CCR4上的预装载寄存器
TIM5->CNT = 0;
TIM_ARRPreloadConfig(TIM5,ENABLE);//ARPE使能
TIM_ClearFlag(TIM5, TIM_FLAG_Update);
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE); //允许定时器5更新中断
TIM_ITConfig(TIM5,TIM_IT_CC4,DISABLE); //失能定时器5更新中断
TIM_Cmd(TIM5, DISABLE); //不使能TIM5
NVIC_InitStructure.NVIC_IRQChannel=TIM5_IRQn; //定时器5中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
请问有大神知道是咋回事嘛? |