cancel
Showing results for 
Search instead for 
Did you mean: 

Oddities in work of basic timer - 32L476GDISCOVERY

Khetag Goiaev
Associate II

I'm writing library for HC-SR-4 (Ranging Module). To start measurement I have to:

1) send 10 us high signal to TRIGGER and than

2) measure duration of high signal on ECHO

To get 10 us duration I use one of basic timers with enabled interruption.

When I try to send signal TRIGGER for the first time I get strange early interruption. On the picture, you can see measurement performed by logical analyzer after pressing the button (which also sets high level on TRIGGER and starts timer):0690X000006CjEuQAK.png

Next press:0690X000006CjF9QAK.png

I can ignore this first interruption since after it timer works correctly. But to make library work I use two timers instead of one. For now it is not so bad but it is improper approach and I would like to understand why this happens.

Answering Clive:

Timer settings

/* TIM7 init function */
void MX_TIM7_Init(void)
{
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 
  htim7.Instance = TIM7;
  htim7.Init.Prescaler = 79;
  htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim7.Init.Period = 9;
  htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
 
}

 Interruption

/**
  * @brief This function handles TIM7 global interrupt.
  */
void TIM7_IRQHandler(void)
{
  /* USER CODE BEGIN TIM7_IRQn 0 */
 
  /* USER CODE END TIM7_IRQn 0 */
  HAL_TIM_IRQHandler(&htim7);
  /* USER CODE BEGIN TIM7_IRQn 1 */
	HAL_TIM_Base_Stop_IT(&htim7);
	HAL_GPIO_TogglePin(TRIG_PORT, TRIG);
  /* USER CODE END TIM7_IRQn 1 */
}

 Button

void EXTI0_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_IRQn 0 */
 
  /* USER CODE END EXTI0_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  /* USER CODE BEGIN EXTI0_IRQn 1 */
	HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_2);
	htim7.Instance->CNT = 0;
	HAL_TIM_Base_Start_IT(&htim7);
	HAL_GPIO_WritePin(TRIG_PORT, TRIG, 1);
	
  /* USER CODE END EXTI0_IRQn 1 */
}

 Clock configuration

 0690X000006CjGRQA0.png

Thank you,

Khetag

2 REPLIES 2

Perhaps show how the TIM is configured, and where the interrupts are occuring on your plots. Not really clear to me what's what. CH2/TRIGGER presumably the ECHO you want to measure, but what are you seeing on the STM32 side?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Added some more information into post