Skip to main content
GErma.1
Associate III
March 25, 2020
Solved

Micro sec delay with STM32L0

  • March 25, 2020
  • 2 replies
  • 1263 views

Hi

I am using a B-L072Z-LRWAN1  development board and I need to read data from a DTH22 sensor.

I am trying to use TIM6 to get a micro second delay, but it seems I am missing something.

The delay function :

void delay (uint16_t time)

{

/* change your code here for the delay in microseconds */

__HAL_TIM_SET_COUNTER(&htim6, 0);

while ((__HAL_TIM_GET_COUNTER(&htim6))<time);

}

The TIM6 initialization:

static void MX_TIM6_Init(void)

{

 /* USER CODE BEGIN TIM6_Init 0 */

 /* USER CODE END TIM6_Init 0 */

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 /* USER CODE BEGIN TIM6_Init 1 */

 /* USER CODE END TIM6_Init 1 */

 htim6.Instance = TIM6;

 htim6.Init.Prescaler = 50-1;

 htim6.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim6.Init.Period = 0xffff-1;

 if (HAL_TIM_Base_Init(&htim6) != HAL_OK)

 {

  Error_Handler();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)

 {

  Error_Handler();

 }

HAL_TIM_Base_Start(&htim6);

 /* USER CODE BEGIN TIM6_Init 2 */

 /* USER CODE END TIM6_Init 2 */

}

The timer TIM6 is not counting (__HAL_TIM_GET_COUNTER(&htim6) returns alaways 0.

BR

Gilberto

This topic has been closed for replies.
Best answer by Tesla DeLorean

Need to make sure the TIM6 clock is enabled, __HAL_RCC_TIM6_CLK_ENABLE();

htim6.Init.Prescaler = 32-1; // If clocking at 32 MHz

htim6.Init.Period = 0xFFFF; // Count 0 thru 65535

The HAL_TIMEx_MasterConfigSynchronization() stuff shouldn't be necessary

2 replies

Tesla DeLorean
Guru
March 25, 2020

Need to make sure the TIM6 clock is enabled, __HAL_RCC_TIM6_CLK_ENABLE();

htim6.Init.Prescaler = 32-1; // If clocking at 32 MHz

htim6.Init.Period = 0xFFFF; // Count 0 thru 65535

The HAL_TIMEx_MasterConfigSynchronization() stuff shouldn't be necessary

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
GErma.1
GErma.1Author
Associate III
March 25, 2020

Hi

The clock was not enable. It is now working perfectly.

Thanks a lot!

BR

Gilberto