用了
tiM1的1~3通道的PWM互补输出,发现每次调用TIM_SelectOCxM和TIM_CCxCmd后N极总会有一个1个多微秒的有效电平出来。
初始代码:
- nvicInitTypeDef.NVIC_IRQChannel = TIM1_CC_IRQn;//选择定时器1比较中断向量
- nvicInitTypeDef.NVIC_IRQChannelCmd = ENABLE;//使能中断
- nvicInitTypeDef.NVIC_IRQChannelPriority = 1;//设置中断优先级
- NVIC_Init( nvicInitTypeDef);//初始化中断向量
- timeBaseInitTypeDef.TIM_ClockDivision = TIM_CKD_DIV1;//设置定时器分频
- timeBaseInitTypeDef.TIM_CounterMode = TIM_CounterMode_Up;//设置定时器计数方向
- timeBaseInitTypeDef.TIM_Period = 0xffff;//设置计数周期
- timeBaseInitTypeDef.TIM_Prescaler = 0;//设置预分频
- timeBaseInitTypeDef.TIM_RepetitionCounter = 0;//设置重复计数数
- TIM_TimeBaseInit(TIM1, timeBaseInitTypeDef);//根据结构体初始化
- TIM_ARRPreloadConfig(TIM1, ENABLE);
- timeOcInitTypeDef.TIM_OCIdleState = TIM_OCIdleState_Reset;
- timeOcInitTypeDef.TIM_OCMode = TIM_OCMode_Timing;//使用定时模式,输出端口不受影响
- timeOcInitTypeDef.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- timeOcInitTypeDef.TIM_OCNPolarity = TIM_OCNPolarity_High;
- timeOcInitTypeDef.TIM_OutputNState = TIM_OutputNState_Disable;
- timeOcInitTypeDef.TIM_OutputState = TIM_OutputState_Enable;
- timeOcInitTypeDef.TIM_Pulse = 10000 - 1;//选择脉冲数
- TIM_OC4Init(TIM1, timeOcInitTypeDef);//调用初始化函数,初始化通道4
- //PA7-TIM1_CC1+ U-
- //PA8-TIM1_CC1- U+
- //PA9-TIM1_CC2+ V+
- //PB0-TIM1_CC2- V-
- //PA10-TIM1_CC3+ W+
- //PB1-TIM1_CC3- W-
- gpioInitTypeDef.GPIO_Mode = GPIO_Mode_AF;//复用模式,调试的时候用的GPIO_Mode_OUT,浪费了2个小时,一直搞到凌晨1:40,正是个大乌龙!
- gpioInitTypeDef.GPIO_OType = GPIO_OType_PP;
- gpioInitTypeDef.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
- gpioInitTypeDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
- gpioInitTypeDef.GPIO_Speed = GPIO_Speed_Level_3;
- GPIO_Init(GPIOA, gpioInitTypeDef);
- gpioInitTypeDef.GPIO_Mode = GPIO_Mode_AF;//复用模式,调试的时候用的GPIO_Mode_OUT,浪费了2个小时,一直搞到凌晨1:40,正是个大乌龙!
- gpioInitTypeDef.GPIO_OType = GPIO_OType_PP;
- gpioInitTypeDef.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
- gpioInitTypeDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
- gpioInitTypeDef.GPIO_Speed = GPIO_Speed_Level_3;
- GPIO_Init(GPIOB, gpioInitTypeDef);
- //TIM1复用模式要选GPIO_AF_2,不要选GPIO_AF_0,虽然库注释里GPIO_AF_0也有TIM1
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2);//0);//选择GPIOA7为复用0,TIM1
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);//0);//选择GPIOA8为复用0,TIM1
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);//0);//选择GPIOA9为复用0,TIM1
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);//0);//选择GPIOA10为复用0,TIM1
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_2);//0);//选择GPIOA7为复用0,TIM1
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_2);//0);//选择GPIOA8为复用0,TIM1
- timeOcInitTypeDef.TIM_OCIdleState = TIM_OCIdleState_Reset;
- timeOcInitTypeDef.TIM_OCMode = TIM_OCMode_PWM1;//
- timeOcInitTypeDef.TIM_OCNIdleState = TIM_OCIdleState_Set;//Reset;
- timeOcInitTypeDef.TIM_OCNPolarity = TIM_OCNPolarity_High;
- timeOcInitTypeDef.TIM_OutputNState = TIM_OutputNState_Enable;
- timeOcInitTypeDef.TIM_OutputState = TIM_OutputState_Enable;
- timeOcInitTypeDef.TIM_OCPolarity = TIM_OCPolarity_High;
- timeOcInitTypeDef.TIM_Pulse = 10000;//选择脉冲数
- TIM_OC1Init(TIM1, timeOcInitTypeDef);
- TIM_OC2Init(TIM1, timeOcInitTypeDef);
- TIM_OC3Init(TIM1, timeOcInitTypeDef);
- TIM_ITConfig(TIM1, TIM_IT_CC4, ENABLE);//使能第四通道比较中断
- TIM_Cmd(TIM1, ENABLE);//使能定时器1
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
复制代码
在比较中断中轮流使能TIM1的CC1、CC2、CC3的PWM输出。
- /**
- * @brief 定时器1比较中断函数
- * @}
- */
- void TIM1_CC_IRQHandler(void)
- {
- if(SET == TIM_GetITStatus(TIM1, TIM_IT_CC4))
- {
- TIM_ClearITPendingBit(TIM1, TIM_IT_CC4);
- switch(g_u32_pwm_select_cnt)
- {
- case 0:
- {
- TIM_ForcedOC2Config(TIM1, TIM_ForcedAction_InActive);
- TIM_ForcedOC3Config(TIM1, TIM_ForcedAction_InActive);
- TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1);
- TIM_CCxCmd(TIM1, TIM_Channel_1, ENABLE);
- }
- break;
- case 1:
- {
- TIM_ForcedOC1Config(TIM1, TIM_ForcedAction_InActive);
- TIM_ForcedOC3Config(TIM1, TIM_ForcedAction_InActive);
- TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1);
- TIM_CCxCmd(TIM1, TIM_Channel_2, ENABLE);
- }
- break;
- case 2:
- {
- TIM_ForcedOC1Config(TIM1, TIM_ForcedAction_InActive);
- TIM_ForcedOC2Config(TIM1, TIM_ForcedAction_InActive);
- TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1);
- TIM_CCxCmd(TIM1, TIM_Channel_3, ENABLE);
- }
- break;
- case 3:
- {
- TIM_ForcedOC2Config(TIM1, TIM_ForcedAction_InActive);
- TIM_ForcedOC3Config(TIM1, TIM_ForcedAction_InActive);
- TIM_ForcedOC1Config(TIM1, TIM_ForcedAction_InActive);
- //TIM_CtrlPWMOutputs(TIM1, DISABLE);
- }
- break;
- }
- if(g_u32_pwm_select_cnt < 3)
- {
- g_u32_pwm_select_cnt ++;
- }
- else
- {
- g_u32_pwm_select_cnt = 0;
- }
- }
- }