使用STM32F103 TIM1 输出PWM波不互补,channel 1和channel 1N的波形一样,不知道什么原因。
程序
void PWM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/////////////////IO Config//////////////////////////////////////////
//4.<PA8--PA10> PWM(TIM1_CH1 C+,TIM1_CH2 B+,TIM1_CH3 A+) ->BLMOTOR
GPIO_InitStructure.GPIO_Pin = PWM_CP_PIN|PWM_BP_PIN|PWM_AP_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//10.<PB13--PB15> PWM(TIM1_CH1N C-,TIM1_CH2N B-,TIM1_CH3N A-) ->BLMOTOR
GPIO_InitStructure.GPIO_Pin = PWM_CN_PIN|PWM_BN_PIN|PWM_AN_PIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//7.<PB0,PB1,PB12> BC,AC,AB (EXINT) ->BLMOTOR 检测电机当前状态
/////////////////Timer1 Config//////////////////////////////////////////
/* Time Base configuration */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_DeInit(TIM1); //重设缺省值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频(时钟分频)72M/(0+1)=72M
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period = 9000-1; //装载值 72M/9000 = 8k(频率)
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //不分割
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Channel 1 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 3000-1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_Low;//TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;//TIM_OCNIdleState_Set;
//配置CCR1
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
} |