STM32F103系列定时器TIM2 产生多路PWM —占空比可修改

**

STM32F103系列定时器TIM2 产生多路PWM —占空比可修改

**

一、查看手册

手册中标明,当PA0~PA3当做TIM2 PWM输出时需要打开重映射功能GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2, ENABLE);

STM32F103系列定时器TIM2 产生多路PWM —占空比可修改_第1张图片

二、使用步骤

1. TIM2 PWM初始化配置

产生的pwm频率为: Fpwm = 72M / ((arr+1)*(psc+1))(单位:Hz):

/**************************************************************************
函数功能:PWM初始化
入口参数:无
返回  值:无
频率:      Fpwm = 72M / ((arr+1)*(psc+1))(单位:Hz)
占空比:  duty circle = TIM2->CCR1 / arr(单位:%)
**************************************************************************/
void TIM2_PWM_Init(u32 arr,u16 psc)
{	
    uint16_t ChannelPulse = 0; 
    
	 GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);// 
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);                   
    GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2, ENABLE);       //TIM2 Remap CH1->PA15  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3 ; //TIM2_CH1 TIM2_CH2 TIM2_CH3 TIM2_CH4
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
   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); 
    
    ChannelPulse = (uint16_t) (((uint32_t) 50 * (arr+1)) / 100);//占空比30%

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
	TIM_OCInitStructure.TIM_Pulse = ChannelPulse; 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
	TIM_OC1Init(TIM2, &TIM_OCInitStructure);  
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
	TIM_OCInitStructure.TIM_Pulse = ChannelPulse; 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
	TIM_OC2Init(TIM2, &TIM_OCInitStructure);  
	
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
	TIM_OCInitStructure.TIM_Pulse = ChannelPulse; 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
	TIM_OC3Init(TIM2, &TIM_OCInitStructure);  
	
	
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
	TIM_OCInitStructure.TIM_Pulse = ChannelPulse; 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
	TIM_OC4Init(TIM2, &TIM_OCInitStructure);  
 
    TIM_CtrlPWMOutputs(TIM2,ENABLE);		
    TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);   //使能第1路TIM2_CH1 pwm输出
	TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);   //使能第2路TIM2_CH2 pwm输出
	TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);   //使能第3路TIM2_CH3 pwm输出
	TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);  //使能第4路TIM2_CH4 pwm输出

	TIM_ARRPreloadConfig(TIM2, ENABLE); 
	
	TIM_Cmd(TIM2, ENABLE);  

}

## 2.主函数调用


```c
void main(void)
{
   uint16_t duty1, duty2,duty3,duty3;
   uint16_t arr_value;

   arr_value = 7199;
   //初始化占空比设置
   duty1 = (uint16_t) (((uint32_t) 50 * (arr_value+1)) / 100);//TIM2_CH1占空比50%
   duty2 = (uint16_t) (((uint32_t) 30 * (arr_value+1)) / 100);//TIM2_CH2占空比30%
   duty3 = (uint16_t) (((uint32_t) 60 * (arr_value+1)) / 100);//TIM2_CH3占空比60%
   duty4 = (uint16_t) (((uint32_t) 80 * (arr_value+1)) / 100);//TIM2_CH4占空比80%
   TIM2_PWM_Init(7199,4);
   for (;;) {
           //修改占空比设置
            TIM2->CCR1 = duty1 ; 
            TIM2->CCR2 = duty2 ;
            TIM2->CCR3 = duty3 ;
            TIM2->CCR4 = duty4 ;
        }
 }

你可能感兴趣的:(单片机,stm32,嵌入式硬件)