cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L100RCT6 - timer PWM with boolean variable

electronicsbasickid
Associate II

Hello,

I am working with the discovery board STM32L100RC. I am trying to have three different timers (TIM2, TIM3, TIM4) as PWM. I'd like to do it's to have each timer ouput at '1' (3.3v) when a boolean variable is equal to '1' and at '0' (0V) when the same variable is equal to '0' .

Here's what I started to code for each timer :

static void MX_TIM2_Init(void)
{
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};
 
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 1;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 15999;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
 
  HAL_TIM_MspPostInit(&htim2);
}

My question is the following one : how can we ask the timer to change its value according to a variable ?

Thank you in advance for you help,

Jay.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

There's no way to tie a timer output to a variable so that it changes automatically when the variable changes value. You'll need to change the timer output via enabling/disabling or changing duty cycle manually when the variable changes.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

1 REPLY 1
TDK
Guru

There's no way to tie a timer output to a variable so that it changes automatically when the variable changes value. You'll need to change the timer output via enabling/disabling or changing duty cycle manually when the variable changes.

If you feel a post has answered your question, please click "Accept as Solution".