cancel
Showing results for 
Search instead for 
Did you mean: 

Controlling Rgb led using Timers in stm32

rubenles
Associate III

Hi, i am using Timers to control the intensity of a RGB LED. I need help with a code because I don't know why i have to do this thing.

I have these lines:

TIM1->CCR1 = Intensity; For Red Color

TIM1->CCR2 = Intensity; For green color

TIM1->CCR4 = Intensity; For blue color

 

but it doesn't work while calling 

(&htim1, TIM_CHANNEL_4);

But if i do this:

// Set alternate output for CH2, disabling main output (no need of it because already disabled, but just in case)

CLEAR_BIT(TIM1->CCER, 1 << 4);

SET_BIT(TIM1->CCER, 1 << 6);

// Set alternate output for CH1, disabling main output (no need of last thing but whatever)

CLEAR_BIT(TIM1->CCER, 1 << 0);

SET_BIT(TIM1->CCER, 1 << 2);

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);

 

It starts working. This last code is not mine, this is why i am asking this. Why i have to use clear bit, and do this shifts? I have no idea with this. 

 

Thank you so much.

1 ACCEPTED SOLUTION

Accepted Solutions

Hello,

 

Based on GPIO configuration, it seems you are using CH1N, CH2N and CH4 channels of TIM1. Therefore, CH1 and CH2 didn't need to be enabled. However, CCER1 << 4, CCER, 1 << 6 are used to disable CH2 channel and enable CH2N which normally not required.

You should have the following startup sequence:

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);

Is working well on my side.

Best Regards,

Gwénolé

 

View solution in original post

5 REPLIES 5
GwenoleB
ST Employee

Hello @rubenles,


TIM1->CCR1 = Intensity; For Red Color

TIM1->CCR2 = Intensity; For green color

TIM1->CCR4 = Intensity; For blue color


I guess those lines are used during TIMER configuration. It allows to select the pulse width for each channel.

Could you share your configuration function? Could you also that channels 1 & 2 are already setup before starting channel 4?

Best Regards,

Gwénolé

rubenles
Associate III
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(timHandle->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspPostInit 0 */

  /* USER CODE END TIM1_MspPostInit 0 */
    __HAL_RCC_GPIOE_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**TIM1 GPIO Configuration
    PE8     ------> TIM1_CH1N
    PE10     ------> TIM1_CH2N
    PA11     ------> TIM1_CH4
    */
    GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

My doubt is why to do CCER1 << 4, CCER, 1 << 6, and those lines.., the configuration i think it's ok.

Hello,

 

Based on GPIO configuration, it seems you are using CH1N, CH2N and CH4 channels of TIM1. Therefore, CH1 and CH2 didn't need to be enabled. However, CCER1 << 4, CCER, 1 << 6 are used to disable CH2 channel and enable CH2N which normally not required.

You should have the following startup sequence:

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);

Is working well on my side.

Best Regards,

Gwénolé

 

I tried to do what you said deleting my shift lines and it works but not perfectly... 

 

When i use TIM1->CCR2 = 500; it switches on the green color that is right, when i use TIM1->CCR4 = 500; blue color switches on, but, when i use  TIM1->CCR1 = 500; it has to switch on to red color but the result is an strange green.

Using the code i sent before, using CCR1 shows red color, and now it hasn't. What it could be?

 

Thank you so much, i think i am near the final solution!!

@GwenoleB Fixed, i have other line of CCR2 in other place of the code, i was crazy looking for the error. The thing you tell me works perfectly!! Thank you so much