[原创]电位器控制led亮度

时间:2014年04月28日

本例中实现了定时器TIM3完全映射到PC6 7 8 9,来输出PWM波控制万利开发板上的LED亮度,用ADC1的0通道上的电位器作为输入控制,ADC采样值一个乘以4直接送到TIM3输出通道1控制PC6上的LED,18000减去另一个乘以4的结果送到TIM3输出通道2控制PC7上的LED.这样当电位器由一端拧到另一端总会有一个LED优亮渐灭,另一个LED由灭渐亮。


/*******************************************************************************
* Function Name  : main
* Description    : Main program
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
  debug();
#endif


  /* System Clocks Configuration */
  RCC_Configuration();


  /* NVIC Configuration */
  NVIC_Configuration();


  /* GPIO Configuration */
  GPIO_Configuration();


  /* -----------------------------------------------------------------------
    TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    TIM3CLK = 36 MHz, Prescaler = 0x0, TIM3 counter clock = 36 MHz
    TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
    TIM3 Frequency = 36 KHz.
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
  ----------------------------------------------------------------------- */


  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 18000;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;


  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);


  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;


  TIM_OC1Init(TIM3, &TIM_OCInitStructure);


  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);


  /* PWM1 Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;


  TIM_OC2Init(TIM3, &TIM_OCInitStructure);


  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);


  TIM_ARRPreloadConfig(TIM3, ENABLE);


  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);


  ADCinit();
  while (1)
  {
        Delay(0x0000FFFF);
        CCR1_Val = ADC_ConvertedValue*4;
        CCR2_Val = 16000 - ADC_ConvertedValue*4;
              /* PWM1 Mode configuration: Channel1 */
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;


        TIM_OC1Init(TIM3, &TIM_OCInitStructure);


        TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);


                /* PWM1 Mode configuration: Channel2 */
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = CCR2_Val;


        TIM_OC2Init(TIM3, &TIM_OCInitStructure);
       
        TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
  }
}


完整的工程欢迎下载[原创]电位器控制led亮度

文章热词:单片机pwm控制led亮度

上一篇:DSP 28335 PWM 死区 AB输出反相设置

下一篇:没有了

延伸阅读:
分享按钮
网友评论
 以下是对 [[原创]电位器控制led亮度] 的评论,总共:0条评论

推荐文章