Variable Color Generation with PWM signals and RGB led.
Hello everybody.
I tried to develop a simple application, using a RGB led to produce variable colors in a continous way along the time and I still not have succeeded completely. Let me explain what is happening:
1) I used STM32CubeMX 5.3.0 to configure PWM3 (Channel2), PWM4 (Channel 1) and PWM1 (Channel 1N) to control PWM signals respectively in PC7 (Red), PB6 (Blue) and PA7 (Green). These GPIOs are fixed and linked to the RGB led terminals mounted in a commercial shield. I´m using STM32 Nucleo for F1 Board (STM32F103RB MCU).
2) I configured STM32CubeMX clock tree with PB1 and PB2 Timer Clocks with 0.0625 MHz (62500 KHz). My intention is to generate a PWM 500 Hz frequency with resolution value equal to 125.
3) My initial configuration of the PWM channels in STM32CubeMX was:
TIM4 (Channel1 = PWM Generation CH1; Clock Source: Internal Clock; Prescaler = 0; Counter Period = 125; Pulse = 62);
TIM3 (Channel2 = PWM Generation CH2; Clock Source: Internal Clock; Prescaler = 0; Counter Period = 125; Pulse = 62);
TIM1 (Channel1 = PWM Generation CH1N; Clock Source: Internal Clock; Prescaler = 0; Counter Period = 125; Pulse = 62);
4) I generated the Code to SW4STM. I included the code below in the infinite loop:
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE END WHILE */
// setPWM(htim1, TIM_CHANNEL_1, 125, 1);
for(int i=0; i<125; i++){
setPWM(htim3, TIM_CHANNEL_2, 125, i+=2); // Sinal R
for(int j=0; j<125; j++){
setPWM(htim4, TIM_CHANNEL_1, 125, j+=2); // Sinal B
for(int k=0; k<125; k++){
setPWM(htim1, TIM_CHANNEL_1, 125, k+=2); // Sinal G
}
}
}
/* USER CODE BEGIN 3 */
}
5) My setPWM function is described below:
/* USER CODE BEGIN 4 */
void setPWM(TIM_HandleTypeDef timer, uint32_t channel, uint16_t period, uint16_t pulse)
{
HAL_TIM_PWM_Stop(&timer, channel); // stop generation of pwm
TIM_OC_InitTypeDef sConfigOC;
timer.Init.Period = period; // set the period duration
HAL_TIM_PWM_Init(&timer); // reinititialise with new period value
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = pulse; // set the pulse duration
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&timer, &sConfigOC, channel);
HAL_TIM_PWM_Start(&timer, channel); // start pwm generation
}
/* USER CODE END 4 */
6) I tested the code and what is happening?
TIM3 (Signal Red) and TIM4 (signal Blue) varies color along the time as expected.
TIM1 (Signal Green) remains off all the time.
Fact: I´ve already test and I verified that Green Led is working properly.
What I suspect after that?
My TIM1 is not configured properly and/or I´m not using TIM1 in a correct manner, or both.
I appreciate if you could provide me a clue or point me where I can solve my question.
If you need the complete code to have a better analysis, ask me that I can post on this thread question.
Best wishes.
Ricardo