STM32学习笔记--通用定时器3生成4路PWM

static void GENERAL_TIM_GPIO_Config(void) 
{
  GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
    GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
    RCC_APB2PeriphClockCmd(GENERAL_TIM_CH1_GPIO_CLK, ENABLE);
  GPIO_InitStructure.GPIO_Pin =  GENERAL_TIM_CH1_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GENERAL_TIM_CH1_PORT, &GPIO_InitStructure);
    GPIO_ResetBits(GENERAL_TIM_CH1_PORT,GENERAL_TIM_CH1_PIN);
    RCC_APB2PeriphClockCmd(GENERAL_TIM_CH2_GPIO_CLK, ENABLE);
  GPIO_InitStructure.GPIO_Pin =  GENERAL_TIM_CH2_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GENERAL_TIM_CH2_PORT, &GPIO_InitStructure);
    GPIO_ResetBits(GENERAL_TIM_CH2_PORT,GENERAL_TIM_CH2_PIN);
    RCC_APB2PeriphClockCmd(GENERAL_TIM_CH3_GPIO_CLK, ENABLE);
  GPIO_InitStructure.GPIO_Pin =  GENERAL_TIM_CH3_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GENERAL_TIM_CH3_PORT, &GPIO_InitStructure);
    GPIO_ResetBits(GENERAL_TIM_CH3_PORT,GENERAL_TIM_CH3_PIN);
    RCC_APB2PeriphClockCmd(GENERAL_TIM_CH4_GPIO_CLK, ENABLE);
  GPIO_InitStructure.GPIO_Pin =  GENERAL_TIM_CH4_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GENERAL_TIM_CH4_PORT, &GPIO_InitStructure);
    GPIO_ResetBits(GENERAL_TIM_CH4_PORT,GENERAL_TIM_CH4_PIN);
}
static void GENERAL_TIM_Mode_Config(u16 psc,u16 period)
{
    GENERAL_TIM_APBxClock_FUN(GENERAL_TIM_CLK,ENABLE);
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period=period;    
    TIM_TimeBaseStructure.TIM_Prescaler= psc;    
    TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;        
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;        
    TIM_TimeBaseStructure.TIM_RepetitionCounter=0;    
    TIM_TimeBaseInit(GENERAL_TIM, &TIM_TimeBaseStructure);
    // 这里使用MDK5 勾选C99 所以可以在任意位置定义变量
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;         
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;     
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;         
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OC1Init(GENERAL_TIM, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OC2Init(GENERAL_TIM, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OC3Init(GENERAL_TIM, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OC4Init(GENERAL_TIM, &TIM_OCInitStructure);
    TIM_OC4PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
    TIM_Cmd(GENERAL_TIM, ENABLE);
}
void GENERAL_TIM_Init(u16 psc,u16 period)
{
    GENERAL_TIM_GPIO_Config();
    GENERAL_TIM_Mode_Config(psc,period);        
}

猜你喜欢

转载自blog.csdn.net/tyustli/article/details/84584093