void TIM3_Config(void) //TIM3输出PWM控制俯仰电机 { /* TIM Configuration */ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* GPIOC Configuration: CH1 (PC6)*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //PC6口使用复用功能模式 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; //上拉 GPIO_Init(GPIOC, &GPIO_InitStructure); /* Connect TIM3 pins to AF2 */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3); //将PC6映射至TIM3 /* Compute the prescaler value */ TIM3_PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 500000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 9999; TIM_TimeBaseStructure.TIM_Prescaler = TIM3_PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = TIM3_CCR1_Val; //定义占空比 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable); //TIM3_CCR1_Val值预装载 TIM_ARRPreloadConfig(TIM3, ENABLE); //TIM_ClearFlag(TIM3, TIM_FLAG_Update); TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE); //上电时不打开中断,待标定完成开始扫描时再打开计数 } void TIM3_NVIC_Config(void) //定义TIM3中断 { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); } 这是中断函数 void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3,TIM_IT_CC1)!=RESET) { switch(direction) { case 0 : pulse_counter=pulse_counter+1; //位置加1,逆时针旋转 if(pulse_counter>=N) //此时俯仰角为45°,需要切换方向 { TIM_Cmd(TIM3, DISABLE); //俯仰电机停止 GPIO_ToggleBits(GPIOE, GPIO_Pin_3); //俯仰电机准备切换方向 //Systickdelay(5); TIM_Cmd(TIM3, ENABLE); //启动俯仰电机 direction= 1; } TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); //清除中断标志位 case 1 : pulse_counter=pulse_counter-1; //顺时针旋转 TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); //清除中断标志位 } } } 其中的TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);是放在主程序里面打开的,为什么进入不了中断,请大师们指教。我原意是想每一个周期产生一次中断。 |