stm32f103 pwm波输出,为什么同样的代码(复制的)用不了呢?

2019-07-14 20:04发布



//PA1



#include "pwm_output2.h"


static void TIM5_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

        /* TIM4 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);

  /* GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  /*GPIOB Configuration: TIM4 channel 2 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
}


static void TIM5_Mode_Config(void)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;

        u16 CCR2_Val = 500;        

         
  TIM_TimeBaseStructure.TIM_Period = 999;      
  TIM_TimeBaseStructure.TIM_Prescaler = 0;          
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;       
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;            
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;       
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;          
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  

  TIM_OC2Init(TIM5, &TIM_OCInitStructure);         

  TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM5, ENABLE);                       

  /* TIM5 enable counter */
  TIM_Cmd(TIM5, ENABLE);               
}

void TIM5_PWM_Init(void)
{
        TIM5_GPIO_Config();
        TIM5_Mode_Config();       
}
/****************************/


0条回答

一周热门 更多>