配置过程:(以TIM3为例,其CH1-CH4为:PA6、PA7、PB0、PB1)

1)开启TIM3时钟,配置4个IO口为复用推挽输出。

2)设置TIM3的ARR和PSC来控制PWM的周期。

3)设置TIM3的CH1-CH4的PWM模式及通道方向,使能TIM3的CH1-CH4输出。

4)使能TIM3。


程序如下:

/*
 * 函数名:TIM3_GPIO_Config
 * 描述  :配置TIM3复用输出PWM时用到的I/O
 * 输入  :无
 * 输出  :无
 * 调用  :内部调用
 */
void TIM3_GPIO_Config(void) 
{
  GPIO_InitTypeDef GPIO_InitStructure;

  //PCLK1经过2倍频后作为TIM3的时钟源等于72MHz
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);  //开启TIM3时钟


  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); 

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    // 复用推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}


/*
 * 函数名:TIM3_Mode_Config
 * 描述  :配置TIM3输出的PWM信号的模式,如周期、极性、占空比
 * 输入  :无
 * 输出  :无
 * 调用  :内部调用
 */
void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;


/* PWM信号电平跳变值 */
u16 CCR1_Val = 500;        
u16 CCR2_Val = 375;
u16 CCR3_Val = 250;
u16 CCR4_Val = 125;


/* -----------------------------------------------------------------------
    TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    TIM3CLK = 72 MHz, Prescaler = 0x0, TIM3 counter clock = 72 MHz
    TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
    TIM3 Frequency = 72 KHz.
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%       500/(999+1)
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
  ----------------------------------------------------------------------- */  //TIM3_ARR=(TIM_Period +1)
                                                                                            //TIM3_CCR1=TIM_Pulse 

  /* Time base configuration */  //具体定时时间设置在后面 
  TIM_TimeBaseStructure.TIM_Period = 999;       //当定时器从0计数到999,即为1000次,为一个定时周期
  TIM_TimeBaseStructure.TIM_Prescaler = 0;    //设置预分频:不预分频,即为72MHz
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;//设置时钟分频系数:不分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数模式
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);


  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;   //配置为PWM模式1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;   //设置跳变值,当计数器计数到这个值时,电平发生跳变
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //当定时器计数值小于CCR1_Val时为高电平
  TIM_OC1Init(TIM3, &TIM_OCInitStructure); //使能通道1
  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);  


  /* PWM1 Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;  //设置通道2的电平跳变值,输出另外一个占空比的PWM
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);  //使能通道2
  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);


  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //设置通道3的电平跳变值,输出另外一个占空比的PWM
  TIM_OC3Init(TIM3, &TIM_OCInitStructure); //使能通道3
  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);


  /* PWM1 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //设置通道4的电平跳变值,输出另外一个占空比的PWM
  TIM_OC4Init(TIM3, &TIM_OCInitStructure); //使能通道4
  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能TIM3重载寄存器ARR

  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);                   //使能定时器3
}


/*
 * 函数名:TIM3_PWM_Init
 * 描述  :TIM3 输出PWM信号初始化,只要调用这个函数
 *         TIM3的四个通道就会有PWM信号输出
 * 输入  :无
 * 输出  :无
 * 调用  :外部调用
 */
void TIM3_PWM_Init(void)
{
TIM3_GPIO_Config();
TIM3_Mode_Config();
}


1)定时时间计算:(以定时1S为例)



((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+7199)/72M)*(1+9999)=1秒 */



 定时器的基本设置:



TIM_TimeBaseStructure.TIM_Prescaler = 7199;//时钟预分频数   例如 :时钟频率=72/(时钟预分频+1)  



TIM_TimeBaseStructure.TIM_Period = 9999; // 自动重装载寄存器周期的值



TIM_TimeBaseStructure.TIM_CounterMode =  TIM1_CounterMode_Up; //定时器模式  向上计数 



TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时间分割值  
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//初始化定时器2



TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);  //打开中断   溢出中断  



TIM_Cmd(TIM2, ENABLE);//打开定时器




或者:
TIM_TimeBaseStructure.TIM_Prescaler = 35999;//分频35999      72M/



(35999+1)/2=1Hz  1秒中断溢出一次
 TIM_TimeBaseStructure.TIM_Period = 2000; //计数值2000 




((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+35999)/72M)*(1+2000)=1秒 */  







TIM_OCMode )配置:



在向下计数时,当TIMx_CNT>TIMx_CCRn时,通道n输出为无效电平,否则为有效电平;PWM2模式与PWM1模式相反。其中有效电平是指TIM_OCPolarity 的值,上面例子中有效电平为高电平。









TIM_OCxPreloadConfig(TIM3, TIM_OCPreload_Enable);配置了各通道的比较寄存器TIM_CRR预装载使能;使用TIM_ARRPreloadConfig()把重载寄存器TIMx_ARR使能,最后用TIM_Cmd()使能定时器TIM3,定时器外设就开始工作了。