0
\$\begingroup\$

I am trying to change the values of Prescaler, Autoreload and Pulse of PWM using Serial communication on a stm32 Nucleo-F091RC. For the most part I am able to do so since the below code works fine:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {

UNUSED(huart);

...

else if (myRxData[0]=='1'){

  int x;
  int var;
  pre2=0;
  aut2=0;
  pul2=0;
  for(x = 1; x < 6; x++)
  {
      var=myRxData[x]-'0';
      pre2=(pre2*10)+var;
  }

  for(x = 6; x < 11; x++)
  {
      var=myRxData[x]-'0';
      aut2=(aut2*10)+var;
  }

  for(x = 11; x < 16; x++)
  {
      var=myRxData[x]-'0';
      pul2=(pul2*10)+var;
  }


  __HAL_TIM_SET_PRESCALER(&htim2,pre2);
  __HAL_TIM_SET_AUTORELOAD(&htim2,aut2);
  __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, pul2);

}

But when I try to change to values of 2 PWM simultaneously like so:

else if (myRxData[0]=='4') {

  int x;
  int var;
  pre14=0;
  aut14=0;
  pul14=0;

  pre3=0;
  aut3=0;
  pul3=0;

  for(x = 1; x < 6; x++)
  {
      var=myRxData[x]-'0';
      pre14=(pre14*10)+var;
      pre3=(pre3*10)+var;
  }

  for(x = 6; x < 11; x++)
  {
      var=myRxData[x]-'0';
      aut14=(aut14*10)+var;
      aut3=(aut3*10)+var;
  }

  for(x = 11; x < 16; x++)
  {
      var=myRxData[x]-'0';
      pul14=(pul14*10)+var;
      pul3=(pul3*10)*var;
  }
  __HAL_TIM_SET_PRESCALER(&htim3,pre3);
  __HAL_TIM_SET_AUTORELOAD(&htim3,aut3);
  __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, pul3);

  __HAL_TIM_SET_PRESCALER(&htim14,pre14);
  __HAL_TIM_SET_AUTORELOAD(&htim14,aut14);
  __HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, pul14);

}

One of the PWM's stops working.

This also happens if I simply try to update one PWM two times using the __HAL_TIM_SET's in the main():

  __HAL_TIM_SET_PRESCALER(&htim3,pre3);
  __HAL_TIM_SET_AUTORELOAD(&htim3,aut3);
  __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, pul3);

   HAL_Delay(1000);      

  __HAL_TIM_SET_PRESCALER(&htim3,pre3);
  __HAL_TIM_SET_AUTORELOAD(&htim3,aut3);
  __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, pul3);

It will update once, but on the second update it will stop working.

What am I doing wrong?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.