Board logo

标题: STM32 PWM 占空比可变函数 [打印本页]

作者: yuyang911220    时间: 2014-11-19 21:54     标题: STM32 PWM 占空比可变函数

频率 10K,占空比可以达到1%变化的精度,100K只能是10%的变化精度。
#define PWM_CLK1M_ARR_10K 100-1
void IzADCPWMInit(uint8_t duty)//10%~90%
{
  if(duty>90)
  {
duty = 90;
  }
  if(duty<10)
  {
duty = 10;
  }  
//reset  
  /* TIM4 enable counter */
  TIM_Cmd(TIM4, DISABLE);
   /* TIM4 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM4, DISABLE);  
//end of reset
    /* System Clocks Configuration */
  RCCPWMADCConfiguration();
  /* GPIO Configuration */
  GPIOPWMADCConfiguration();
  /* -----------------------------------------------------------------------
    TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
    clock at 24 MHz the Prescaler is computed as following:
     - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
    SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
    and Connectivity line devices and to 24 MHz for Low-Density Value line and
    Medium-Density Value line devices
    The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
                                                  = 24 MHz / 666 = 36 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%
  ----------------------------------------------------------------------- */
  /* Compute the prescaler value */
  //PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;//24M
  //TIM3 Frequency=1MHz/(9999+1) =100Hz
  PrescalerValue = (uint16_t) (SystemCoreClock / 1000000) - 1; //TIM3 counter clock: 1MHz
  /* Time base configuration */
  //TIM_TimeBaseStructure.TIM_Period = 665;//(ARR)
  //TIM3 Frequency=1MHz/(9999+1) =100Hz
  //TIM_TimeBaseStructure.TIM_Period = 10000-1;  //TIM3 counter clock/TIM3 Frequency
  //TIM3 Frequency=1MHz/(99+1) =10 000Hz
  TIM_TimeBaseStructure.TIM_Period = PWM_CLK1M_ARR_10K;//PWM_CLK1M_ARR_100;  //TIM3 counter clock/TIM3 Frequency  
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  //CCR1_Val =TIM3_ARR*duty/100;
// TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
// TIM_OCInitStructure.TIM_Pulse = (uint16_t)(10000-1)*duty/100;  
  TIM_OCInitStructure.TIM_Pulse = (uint16_t)(PWM_CLK1M_ARR_10K)*duty/100;   
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//  TIM_OC1Init(TIM3, &TIM_OCInitStructure);
//  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
//PB6: TM4 Channel1   
  TIM_OC1Init(TIM4, &TIM_OCInitStructure);
  TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);  
//PB8: TM4 Channel3
  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = (uint16_t)(PWM_CLK1M_ARR_10K)*duty/100;   

  TIM_OC3Init(TIM4, &TIM_OCInitStructure);
  TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0