关于STM32F072产生不了PWM

2019-07-20 23:19发布

本帖最后由 qq4056 于 2019-6-27 19:18 编辑
  1. void TIM2_PWM_Init() //产生pwm
  2. {
  3.         GPIO_InitTypeDef GPIO_InitStructure;
  4.         TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  5.         TIM_OCInitTypeDef  TIM_OCInitStructure;

  6.         float TimerPeriod = 0;
  7.         uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
  8.         
  9.         /* GPIOA Clocks enable */
  10.         RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
  11.         
  12.         /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */
  13.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  14.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  15.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  16.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  17.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  18.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  19.         
  20.         GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_2);

  21.         
  22.         /* TIM1 clock enable */
  23.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
  24.         
  25.         /* Time Base configuration */
  26.         TIM_TimeBaseStructure.TIM_Prescaler = 48-1;//预分频值
  27.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  28.         TIM_TimeBaseStructure.TIM_Period = 500000;  //自动重装载值
  29.         TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  30.         TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  31.         
  32.         TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  33.         
  34.         /* Channel 1, 2, 3 and 4 Configuration in PWM mode */
  35.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  36.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  37.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  38.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//设置输出极性
  39.         TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  40.         TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  41.         TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
  42.         
  43.         TIM_OCInitStructure.TIM_Pulse = 100000;  //占空比时间
  44.         TIM_OC3Init(TIM2, &TIM_OCInitStructure);

  45.         
  46.         /* TIM1 counter enable */
  47.         TIM_Cmd(TIM2, ENABLE);
  48.         
  49.         /* TIM1 Main Output Enable */
  50.         TIM_CtrlPWMOutputs(TIM2, ENABLE);

  51. }
复制代码我用PA2,定时器TIM2,通道TIM2_CH3,可是也是产生不了pwm.是在是不知道哪出问题
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。