关于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.是在是不知道哪出问题
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
6条回答
qq4056
2019-07-21 18:10
nashui_sx 发表于 2019-6-28 21:27
A2是串口2 是否外设检查占用看看

没有被占用,我换成PB0,定时器3,也不行。
  1. void TIM3_PWM_Init() //产生pwm
  2. {
  3.         GPIO_InitTypeDef GPIO_InitStructure;
  4.         TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  5.         TIM_OCInitTypeDef  TIM_OCInitStructure;
  6.        
  7.         /* GPIOB Clocks enable */
  8.         RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
  9.        
  10.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  11.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  12.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  14.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  15.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  16.        
  17.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_1);

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

  47. }
复制代码

一周热门 更多>