打印

pwm波的输出电压总是高一倍怎么回事啊,是不是我的程序写错了

[复制链接]
377|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
pwm波的输出电压总是高一倍怎么回事啊,我自己写了一下pwm的程序,但是测量时电压总是不对,我设置100的占空比理应输出0.32v但是实际数出0.63v其他的也是一样大一倍,下面是我写的程序static void TIM5_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* TIM3 clock enable */
  //PCLK1¾­¹ý2±¶Æµºó×÷ΪTIM2µÄʱÖÓÔ´µÈÓÚ72MHz
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);

  /*GPIOA Configuration: TIM2 channel 1 AND 2 and  3 and 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin =   GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        // ¸´ÓÃÍÆÍìÊä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
static void TIM5_Mode_Config(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  /* 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(TIM5, &TIM_TimeBaseStructure);
}

void TIM5_Mode1_Config(u16 CCR1_Val)
{
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  
  TIM5_Mode_Config();
  
  /* 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(TIM5, &TIM_OCInitStructure);    //ʹÄÜͨµÀ2

  TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable);
   TIM_ARRPreloadConfig(TIM5, ENABLE);       // ʹÄÜTIM3ÖØÔؼĴæÆ÷ARR
    /* TIM3 enable counter */
  TIM_Cmd(TIM5, ENABLE);                   //ʹÄܶ¨Ê±Æ÷3  

}
void TIM5_Mode2_Config(u16 CCR2_Val)
{
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  TIM5_Mode_Config();
  /* PWM1 Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;      //ÅäÖÃΪPWMģʽ1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;    //ÉèÖÃͨµÀ2µÄµçƽÌø±äÖµ£¬Êä³öÁíÍâÒ»¸öÕ¼¿Õ±ÈµÄPWM

  TIM_OC2Init(TIM5, &TIM_OCInitStructure);    //ʹÄÜͨµÀ2

  TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable);
   TIM_ARRPreloadConfig(TIM5, ENABLE);       // ʹÄÜTIM3ÖØÔؼĴæÆ÷ARR
    /* TIM3 enable counter */
  TIM_Cmd(TIM5, ENABLE);                   //ʹÄܶ¨Ê±Æ÷3  
}
void TIM5_Mode3_Config(u16 CCR3_Val)
{
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  TIM5_Mode_Config();
  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;      //ÅäÖÃΪPWMģʽ1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;  //ÉèÖÃͨµÀ3µÄµçƽÌø±äÖµ£¬Êä³öÁíÍâÒ»¸öÕ¼¿Õ±ÈµÄPWM
  TIM_OC3Init(TIM5, &TIM_OCInitStructure);   //ʹÄÜͨµÀ3
  TIM_OC3PreloadConfig(TIM5, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM5, ENABLE);       // ʹÄÜTIM3ÖØÔؼĴæÆ÷ARR
    /* TIM3 enable counter */
  TIM_Cmd(TIM5, ENABLE);                   //ʹÄܶ¨Ê±Æ÷3  
}
void TIM5_Mode4_Config(u16 CCR4_Val)
{
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  TIM5_Mode_Config();
  /* PWM1 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;      //ÅäÖÃΪPWMģʽ1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;  //ÉèÖÃͨµÀ4µÄµçƽÌø±äÖµ£¬Êä³öÁíÍâÒ»¸öÕ¼¿Õ±ÈµÄPWM
  TIM_OC4Init(TIM5, &TIM_OCInitStructure);  //ʹÄÜͨµÀ4
  
  TIM_OC4PreloadConfig(TIM5, TIM_OCPreload_Enable);
  
  TIM_ARRPreloadConfig(TIM5, ENABLE);       // ʹÄÜTIM3ÖØÔؼĴæÆ÷ARR
    /* TIM3 enable counter */
  TIM_Cmd(TIM5, ENABLE);                   //ʹÄܶ¨Ê±Æ÷3  
}

使用特权

评论回复

相关帖子

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

本版积分规则

384

主题

384

帖子

0

粉丝