0

I'm using this code below trying to achieve a intensity transition effect upon turning on a light:

void output2ControlTask(void *argument)
{
  /* USER CODE BEGIN output2ControlTask */
  uint32_t DutyCycle = 0;
  /* Infinite loop */
  for(;;)
  {
    if(flag_EN_OUTPUT2 == ON){
      SteadyBurn_PWM_RTOS(&htim2, TIM_CHANNEL_3, DutyCycle);
      if(DutyCycle < 100){
        DutyCycle++;
      }
    }
    if(flag_EN_OUTPUT2 == OFF){
      SteadyBurn_PWM_RTOS(&htim2, TIM_CHANNEL_3, DutyCycle);
      if(DutyCycle > 0){
        DutyCycle--;
      }
    }
    osDelay(100);
  }
  /* USER CODE END output2ControlTask */
}
void SteadyBurn_PWM_RTOS(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t intensity_percentage){
    __HAL_TIM_SET_COMPARE(htim, Channel, (uint32_t)(COUNTER_PERIOD * (float)(intensity_percentage/100)));
}

Somehow when observed with an oscilloscope the output delayed for like 10 seconds (make sense with osDelay(100)) and then outputted a 100% Duty Cycle PWM, with no transition effect.

I remember using this code without in RTOS environment works fine. I thought about priority issues but when I changed DutyCycle to any int and left out the DutyCycle it outputs the corresponding PWM instantly.

I also debugged the DutyCycle and it was in the correct increment in runtime as well. Is it a HAL problem?

3
  • What does "changed DutyCycle to any int and left out the DutyCycle" mean? Commented Nov 15, 2023 at 22:43
  • Wouldn't it make more sense to change the DutyCycle variable first, then call SteadyBurn_PWM_RTOS()? You do it backwards - you set the current duty cycle, modify the variable, then delay. Commented Nov 15, 2023 at 22:45
  • @pmacfarlane SteadyBurn_PWM_RTOS(&htim2, TIM_CHANNEL_3, 100); will work. without DutyCycle variable involved. I mean do it backwards make sense, but still the HAL function won't take it Commented Nov 16, 2023 at 8:07

1 Answer 1

0

Right, it was a casting problem afterall.

removed a pair of parentheses on the math function and it works.

__HAL_TIM_SET_COMPARE(htim, Channel, (uint32_t)(COUNTER_PERIOD * (float)intensity_percentage/100));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.