打印

Six-step PWM signal generation using TIM1

[复制链接]
2370|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wazhzlp|  楼主 | 2013-9-22 22:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 wazhzlp 于 2013-9-22 22:33 编辑

这个例程不知道大家有没有用过?
void main(void)
{
  /* TIM1 Peripheral Configuration */
  TIM1_DeInit();

  /* Time Base configuration */
  TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 4095, 0);

  /* Channel 1, 2 and 3 Configuration in TIMING mode */  

        /* TIM1_Pulse = 2047 */
        TIM1_OC1Init(TIM1_OCMODE_TIMING, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, 2047, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_SET);  

  /* TIM1_Pulse = 1023 */
  TIM1_OC2Init(TIM1_OCMODE_TIMING, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, 1023, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_SET);

  /* TIM1_Pulse = 511 */
  TIM1_OC3Init(TIM1_OCMODE_TIMING, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, 511, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_SET);

  /* Automatic Output enable, Break, dead time and lock configuration*/

        TIM1_BDTRConfig( TIM1_OSSISTATE_ENABLE,  TIM1_LOCKLEVEL_OFF, 1,  TIM1_BREAK_DISABLE,  TIM1_BREAKPOLARITY_LOW,  TIM1_AUTOMATICOUTPUT_ENABLE);

  TIM1_CCPreloadControl(ENABLE);

  TIM1_ITConfig(TIM1_IT_COM, ENABLE);



  /* TIM1 counter enable */
  TIM1_Cmd(ENABLE);

  /* TIM4 Peripheral Configuration */
  /* Time Base configuration */
        TIM4_TimeBaseInit(TIM4_PRESCALER_128, 0xFF);

  /*TIM4 counter enable */
  TIM4_Cmd(ENABLE);

  TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);

  /* Main Output Enable */
  TIM1_CtrlPWMOutputs(ENABLE);
  enableInterrupts();

  while (1);   
}

就程序,有如下问题:
1、下面是TIM4的一个程序,是在180ms  触发一个 COM时间,当一个COM时间被触发之后程序就跳到了TIM的时间中断了,请看问题2
void TIM4_UPD_OVF_IRQHandler(void) interrupt 23
#endif /* _COSMIC_ */
{
        TIM4_ClearITPendingBit(TIM4_IT_UPDATE);

  if (TimeCounter == 11)
  {
                /* Generate TIM1 COM event by software */
                TIM1_GenerateEvent(TIM1_EVENTSOURCE_COM);
                TimeCounter=1;
        /*
                the TIM4 Update Interrupt occurs each 0.064ms ( Tui=(Period * Prescaler)/Fmaster  = (256*128)/2Mhz = 16,384 ms )
                so after 11 TIM4 Update Interrupts,  180ms have been spent  (T= 11 * 16,384  = 180.2 ms)
        */

  }
  else
  {
                TimeCounter++;
  }
}


2、TIM的com事件,还是比较简单,就是进行换相。如下,

void TIM1_CAP_COM_IRQHandler(void) interrupt 12
#endif /* _COSMIC_ */
{
   /* Clear TIM1 COM pending bit */
  TIM1_ClearITPendingBit(TIM1_IT_COM);

  if(step == 1)
  {
    /* Next step: Step 2 Configuration ---------------------------- */        
    /*  Channel3 configuration */
        TIM1_CCxCmd(TIM1_CHANNEL_3, DISABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_3, DISABLE);

        /*  Channel1 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_1, TIM1_FORCEDACTION_ACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_1, ENABLE);

        /*  Channel2 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_2, TIM1_FORCEDACTION_INACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_2, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_2, ENABLE);        
    step++;
  }
  else if(step == 2)
  {
    /* Next step: Step 3 Configuration ---------------------------- */
    /*  Channel2 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_2, TIM1_FORCEDACTION_INACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_2, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_2, ENABLE);

        /*  Channel3 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_3, TIM1_FORCEDACTION_ACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_3, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_3, ENABLE);

        /*  Channel1 configuration */
        TIM1_CCxCmd(TIM1_CHANNEL_1, DISABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_1, DISABLE);
    step++;
  }
  else if(step == 3)
  {
    /* Next step: Step 4 Configuration ---------------------------- */
    /*  Channel3 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_3, TIM1_FORCEDACTION_ACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_3, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_3, ENABLE);

        /*  Channel2 configuration */
        TIM1_CCxCmd(TIM1_CHANNEL_2, DISABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_2, DISABLE);

        /*  Channel1 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_1, TIM1_FORCEDACTION_INACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_1, ENABLE);
    step++;
  }
  else if(step == 4)
  {
    /* Next step: Step 5 Configuration ---------------------------- */
        /*  Channel3 configuration */
        TIM1_CCxCmd(TIM1_CHANNEL_3, DISABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_3, DISABLE);
        
        /*  Channel1 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_1, TIM1_FORCEDACTION_INACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_1, ENABLE);

        /*  Channel2 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_2, TIM1_FORCEDACTION_ACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_2, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_2, ENABLE);   
    step++;
  }
  else if (step == 5)
  {
    /* Next step: Step 6 Configuration ---------------------------- */
        /*  Channel3 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_3, TIM1_FORCEDACTION_INACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_3, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_3, ENABLE);

        /*  Channel1 configuration */
        TIM1_CCxCmd(TIM1_CHANNEL_1, DISABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_1, DISABLE);

        /*  Channel2 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_2, TIM1_FORCEDACTION_ACTIVE);
          TIM1_CCxCmd(TIM1_CHANNEL_2, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_2, ENABLE);
    step++;
  }
  else
  {
    /* Next step: Step 1 Configuration ---------------------------- */
    /*  Channel1 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_1, TIM1_FORCEDACTION_ACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_1, ENABLE);

        /*  Channel3 configuration */
        TIM1_SelectOCxM(TIM1_CHANNEL_3, TIM1_FORCEDACTION_INACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_3, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_3, ENABLE);

        /*  Channel2 configuration */
        TIM1_CCxCmd(TIM1_CHANNEL_2, DISABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_2, DISABLE);
    step = 1;
  }
}


问题在于我不管怎么进行设置,都不可能达到这样的效果。是不是有什么问题,是不是TIM1的初始化设置有什么不对?》,有没有遇到和处理过该问题的兄弟,谢谢!
特别是像这样的库函数:
        TIM1_SelectOCxM(TIM1_CHANNEL_1, TIM1_FORCEDACTION_ACTIVE);
        TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);
        TIM1_CCxNCmd(TIM1_CHANNEL_1, ENABLE);

有没有人用过,这个可行嘛?


沙发
wazhzlp|  楼主 | 2013-10-22 13:51 | 只看该作者
需要对单片机烧互补输出,OK

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

51

帖子

1

粉丝