Gaurav1234
Gaurav1234

Reputation: 43

how to read the current value of hardware timer in the STM32L1?

I am trying to implement two independent tasks with the hardware timers. I need task 1 to repeat after every 100us and the other to repeat 10us. I generated the code from STM32cubemx. The timer function is as below

static void MX_TIM2_Init(void) {
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};

  TIM_MasterConfigTypeDef sMasterConfig = {0};

  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 100;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 800;
  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();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) {
    Error_Handler();
  }
}

I am further processing with the function as below :

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim-> Instance == TIM2)
    {
        ledBlueBlink();
    }
}

I have a task ledGreenBlink() which is supposed to run after every 10 times task ledBlueBlink() runs. Can anyone please let me know how can I implement this?

Upvotes: 1

Views: 787

Answers (1)

BallisticTomato
BallisticTomato

Reputation: 113

Not sure about the wording, if you're really looking for a task as I do not see a task "ledBlueBlink()".

If you just want to call "ledGreenBlink()" every ten calls of your callback, I would recommend just doing this by adding a counter variable and triggering for every ten times.

static unsigned counter = 0;
if (counter%10)
    ledGreenBlink();

counter++; 

Please be aware that the counter variable needs to be static in order to preserve its value over several calls of the callback. Futhermore it should be an unsigned type in order to safely roll over when it reaches it's maximum value.

In case you really need tasks for something you might want to clarify your code to support the question.

Upvotes: 1

Related Questions