原子哥,我想把TIM2_CH3设置成PWM 输出,可是现在的代码是这样的,但是不能够输出,我想问一下是什么情况。

2019-07-21 01:49发布

//??×?????????
void TIM2_PWM_Init(u16 arr , u16 psc )
{
GPIO_InitTypeDef GPIO_InitStructure ;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure ;
TIM_OCInitTypeDef TIM_OCInitStructure ;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE );


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;

GPIO_Init(GPIOA , &GPIO_InitStructure);

// ??????TIM2 ur
TIM_TimeBaseStructure.TIM_Period = arr ;
TIM_TimeBaseStructure.TIM_Prescaler = psc ;
TIM_TimeBaseStructure.TIM_ClockDivision = 0 ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit (TIM2 , &TIM_TimeBaseStructure );

//??????TIM2 Channel2 PWM ????
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High ;
TIM_OC2Init(TIM2 , &TIM_OCInitStructure );

TIM_OC2PreloadConfig (TIM2 ,TIM_OCPreload_Enable );
TIM_Cmd (TIM2 , ENABLE );

}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
14条回答
水滴滴水
2019-07-22 11:55
回复【10楼】水滴滴水:
---------------------------------
TIM_OC3Init(TIM2 , &TIM_OCInitStructure );

TIM_OC3PreloadConfig (TIM2 ,TIM_OCPreload_Enable );
Initializes the TIMx Channel3 according to the specified
  *         parameters in the TIM_OCInitStruct.
这句话的意思是将TIMxChannel3初始化,我现在使用的是PA2/ TIM2_CH3,故需要将TIM_OC3Init使能,如果想将PA3/TIM2_CH4使能的话,需要将 TIM_OC4Init(TIM2 , &TIM_OCInitStructure );

TIM_OC4PreloadConfig (TIM2 ,TIM_OCPreload_Enable );
TIM_Cmd (TIM2 , ENABLE ); 
使能才可以,可以看到官方库给的标准Initializes the TIMx Channel4 according to the specified
  *         parameters in the TIM_OCInitStruct..故使能它,并且在main函数中设置TIM_SetCompare4(TIM2, pwm_value);这句话的含义是
* @brief  Sets the TIMx Capture Compare4 Register value
  * @param  TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  * @param  Compare4: specifies the Capture Compare4 register new value.
  * @retval None
理解为TIMx_CH4的比较值即可。

一周热门 更多>