目前用stm32做BLDC驱动,PWM输出碰到问题 1、初始化GPIO口复用为TIM 输出脚后,默认即为高电平输出(实际有效电平为高电平) 2、在PWM输出过程中,关闭某一路通道输出,发现该通道与对应互补通道均输出高电平,应为低电平才对 也尝试改过PWM的输出模式及输出极性,没有效果。还是贴下源码吧 void TIM8_PWM_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStructure; /* TIM8 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); /* Time Base configuration */ // ARR = TIM8CLK / ChopperFreq ChopperFreq:16KHz TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频 PSC TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD - 1; //PWM周期TIM_ARR寄存器 TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); /* Channel 1, 2,3 and 4 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //TIM输出通道初始化 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CH1_PULSE; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; TIM_OC1Init(TIM8, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = CH2_PULSE; TIM_OC2Init(TIM8, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = CH3_PULSE; TIM_OC3Init(TIM8, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM输出通道4初始化,用来触发AD注入采样 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; TIM_OCInitStructure.TIM_Pulse = CH1_PULSE; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC4Init(TIM8,&TIM_OCInitStructure); /* Automatic Output enable, Break, dead time and lock configuration*/ TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //死区刹车初始化 TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; TIM_BDTRInitStructure.TIM_DeadTime = 12; TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable; TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;//TIM_BreakPolarity_Low; TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//TIM_AutomaticOutput_Disable; TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure); TIM_OC1PreloadConfig(TIM8,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道1) TIM_OC2PreloadConfig(TIM8,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道2) TIM_OC3PreloadConfig(TIM8,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道3) //TIM_ITConfig(TIM8, TIM_IT_COM, ENABLE); //使能COM事件中断 TIM_SelectInputTrigger(TIM8, TIM_TS_ITR2); //输入触发源选择TIM4 /* TIM8 counter enable */ TIM_Cmd(TIM8, ENABLE); /* Main Output Enable */ TIM_CtrlPWMOutputs(TIM8, ENABLE); } void TIM8_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* GPIOA and GPIOB clocks enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE); /* GPIOC Configuration: Channel 1, 2 and 3 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure); /* GPIOE Configuration: Channel 1N as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //BKIN GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOA, &GPIO_InitStructure); /* GPIOB Configuration:2N and 3N as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Connect TIM pins to AF */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8); //UH GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8); //VH GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8); //WH GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM8); //UL GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_TIM8); //VL GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_TIM8); //WL GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM8); //BKIN /* Enable the TIM8 Trigger and commutation interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM8_TRG_COM_TIM14_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void MC2_Commutation(uint8_t dir, uint8_t tab) { switch(tab) { case 5: //BA(1L,2H) /* Channel1 configuration */ TIM_SelectOCxM(TIM8, TIM_Channel_1, TIM_OCMode_Active); TIM_CCxCmd(TIM8, TIM_Channel_1, TIM_CCx_Disable); TIM_CCxNCmd(TIM8, TIM_Channel_1, TIM_CCxN_Enable); /* Channel2 configuration */ TIM_SelectOCxM(TIM8, TIM_Channel_2, TIM_OCMode_PWM1); TIM_CCxCmd(TIM8, TIM_Channel_2, TIM_CCx_Enable); TIM_CCxNCmd(TIM8, TIM_Channel_2, TIM_CCxN_Disable); /* Channel3 configuration */ TIM_CCxCmd(TIM8, TIM_Channel_3, TIM_CCx_Disable); TIM_CCxNCmd(TIM8, TIM_Channel_3, TIM_CCxN_Disable); break; case 4: ...... default: break; } } |
回复:STM32 PWM输出问题
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO
| RCC_APB2Periph_GPIOA
| RCC_APB2Periph_GPIOB
| RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD , ENABLE );