打印

那个stm32的pwm输出为什么设置为静态函数啊

[复制链接]
390|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
小蘭|  楼主 | 2018-6-30 10:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[code=c]

[/code]/*
* º¯ÊýÃû£ºTIM3_Mode_Config
* ÃèÊö  £ºÅäÖÃTIM3Êä³öµÄPWMÐźŵÄģʽ£¬ÈçÖÜÆÚ¡¢¼«ÐÔ¡¢Õ¼¿Õ±È
* ÊäÈë  £ºÎÞ
* Êä³ö  £ºÎÞ
* µ÷Óà £ºÄÚ²¿µ÷ÓÃ
*/
static 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%
    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%
  ----------------------------------------------------------------------- */

  /* 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  
}

使用特权

评论回复

相关帖子

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

本版积分规则

451

主题

463

帖子

1

粉丝