cancel
Showing results for 
Search instead for 
Did you mean: 

How to setup input capture mode for Maxbotix LV-MaxSonar using Pulse Width (Pin 2)

ELith
Associate

Hi I'm new to stm32Cube and have been trying for days to read these ultra sonic sensors. Here is the data sheet to the one i am using:

https://www.maxbotix.com/documents/LV-MaxSonar-EZ_Datasheet.pdf

if someone could please attach a picture of the stm32cube setup in order to read the Pulse Width (Pin 2) from the sensor i want the internal timer to start on the rising edge and stop/capture on the falling edge of the signal here is the setup i'm currently trying :

0690X000008A0ChQAK.jpg

Any help would be much appreciated I'm very new to using timers and tried reading the timers cookbook but after 12 setup attempts i get an output that changes but i cant seem to figure out if i'm doing it right

this is my flag:

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
 
	if (htim->Instance == TIM2) {
		if (i == 0) {
			cap1 = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
			i = 1;
		} else if (i == 1) {
			cap2 = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
			i = 0;
		}
 
		__HAL_TIM_SET_COUNTER(&htim2, 0);
	}
}

and this is my while:

HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  count = __HAL_TIM_GET_COUNTER(&htim2);
  }
  /* USER CODE END 3 */
}

 hers a snapshot of of output the values change i'm just no sure if it's right:

0690X000008A0CrQAK.jpg

2 REPLIES 2

hi,

prescaler value is too high.

suggest to put as prescaler value Tim2Freq/1000000 to have one microsecond resolution and to put at Auto Reload Register 0xffffffff ( the maximum value.)

there is no need to zeroize timer inside callback .

Save the two last captured values (as you already have done) and subtract the value cached by rise callback from the value catched by fall callback. (eg. cap2-cap1) The result will be in microseconds an you can convert it to inches by divide this by 147 (as writen in datasheet of the sonar)

ELith
Associate

Thank you vangelis i solved it thanks to that i changed my code it to have a 2 channels (1 direct on indirect) one for rising one for falling capture. The key to getting a nice looking output was as you said changing the prescaler to 72.