这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » 求助:STM32的TIM1、2、3、4不能同时初始化

共2条 1/1 1 跳转至

求助:STM32的TIM1、2、3、4不能同时初始化

工程师
2016-08-22 16:36:01     打赏

各位想做个四路PWM频率可调的发生器,可是发现只有TIM3能有PWM 输出。其余三路没有,请高手看看我的使能代码哪里有问题,好长时间不搞了,忘了。


void TIM_GPIO_Config(void) 
{
  GPIO_InitTypeDef GPIO_InitStructure;
  /* 设置TIMCLK 为 72MHZ */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE); 

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD, ENABLE); 

  /*GPIOA Configuration: TIM2、TIM3、TIN1 channel 1 alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_6 | GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    // 复用推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /*GPIOB Configuration: TIM4 channel 1 alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}


void TIM1_Mode_Config(int freq1)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

/* PWM信号电平跳变值 */
  u16 CCR1_Val = 40;        

  /* Time base configuration */  
  TIM_TimeBaseStructure.TIM_Period = freq1;       //当定时器从0计数到一个定时周期 ,此数值即是TIM1_ARR的值。
                                                //This parameter must be a number between 0x0000 and 0xFFFF.  
  TIM_TimeBaseStructure.TIM_Prescaler = 1000;    //设置预分频:1000,即为72MHz/1000=72000  This parameter can be a number between 0x0000 and 0xFFFF 
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2;//TIM_CKD_DIV2 ; //设置时钟分频系数:2分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数模式

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//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(TIM1, &TIM_OCInitStructure); //使能通道1

  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

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

  /* TIM enable counter */
  TIM_Cmd(TIM1, ENABLE);                   //使能定时器1

}

void TIM2_Mode_Config(int freq2)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

/* PWM信号电平跳变值 */
  u16 CCR1_Val = 40;        

/* ----------------------------------------------------------------------- 
    TIM2 Channel1 duty cycle = (TIM2_CCR1/ TIM2_ARR+1)* 100% = 50%
  ----------------------------------------------------------------------- */

  /* Time base configuration */  
  TIM_TimeBaseStructure.TIM_Period = freq2;       //当定时器从0计数到一个定时周期 ,此数值即是TIM2_ARR的值。
                                                //This parameter must be a number between 0x0000 and 0xFFFF.  
  TIM_TimeBaseStructure.TIM_Prescaler = 1000;    //设置预分频:1000,即为72MHz/1000=72000  This parameter can be a number between 0x0000 and 0xFFFF 
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2;//TIM_CKD_DIV2 ; //设置时钟分频系数:2分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数模式

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//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(TIM2, &TIM_OCInitStructure); //使能通道1

  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);

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

  /* TIM enable counter */
  TIM_Cmd(TIM2, ENABLE);                   //使能定时器2

}

void TIM3_Mode_Config(int freq3)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

/* PWM信号电平跳变值 */
  u16 CCR1_Val = 4;        

/* ----------------------------------------------------------------------- 
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR+1)* 100% = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR+1)* 100% = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR+1)* 100% = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR+1)* 100% = 12.5%
  ----------------------------------------------------------------------- */

  /* Time base configuration */  
  TIM_TimeBaseStructure.TIM_Period = freq3;       //当定时器从0计数到一个定时周期 ,此数值即是TIM3_ARR的值。
                                                //This parameter must be a number between 0x0000 and 0xFFFF.  
  TIM_TimeBaseStructure.TIM_Prescaler = 1000;    //设置预分频:1000,即为72MHz/1000=72000  This parameter can be a number between 0x0000 and 0xFFFF 
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2;//TIM_CKD_DIV2 ; //设置时钟分频系数:2分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数模式

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//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);

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

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

}

void TIM4_Mode_Config(int freq4)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

/* PWM信号电平跳变值 */
  u16 CCR1_Val = 4;        
   
/* ----------------------------------------------------------------------- 
    TIM4 Channel1 duty cycle = (TIM4_CCR1/ TIM4_ARR+1)* 100% = 50%
  ----------------------------------------------------------------------- */

  /* Time base configuration */  
  TIM_TimeBaseStructure.TIM_Period = freq4;       //当定时器从0计数到一个定时周期 ,此数值即是TIM4_ARR的值。
                                                //This parameter must be a number between 0x0000 and 0xFFFF.  
  TIM_TimeBaseStructure.TIM_Prescaler = 1000;    //设置预分频:1000,即为72MHz/1000=72000  This parameter can be a number between 0x0000 and 0xFFFF 
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2;//TIM_CKD_DIV2 ; //设置时钟分频系数:2分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数模式

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//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(TIM4, &TIM_OCInitStructure); //使能通道1

  TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

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

  /* TIM enable counter */
  TIM_Cmd(TIM4, ENABLE);                   //使能定时器4

}



高工
2016-11-25 00:43:51     打赏
2楼
超超,我都快忘记这东西了。

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]