cancel
Showing results for 
Search instead for 
Did you mean: 

LPTIM -- 50ms debounce measuring 62ms ???

SWenn.1
Senior III

I have a scope and am triggering on the falling edge of a pushbutton. The LED drive signal is being monitored on another scope channel. The LED is to turn on 50ms after the falling edge of the pushbutton but in reality it is 62ms. LPTIM1 is running at LSE with a divider of 32. Can someone explain why I am not seeing 50ms?? The system clock for CPU1 is 64MHz generated from HSE. I currently am not running anything else.

HAL_Delay is used for keeping the LED on for 100ms....this measurement seems to be working just fine.

ISR Code:

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	HAL_LPTIM_OnePulse_Start_IT(&hlptim1, 50, 0);
 
	if (GPIO_Pin == GPIO_PIN_1)
		EXTI->IMR1 &= ~EXTI_IMR1_IM0;
}
 
 
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
	if (hlptim == &hlptim1) //debounce timer
	{
		HAL_LPTIM_OnePulse_Stop_IT(&hlptim1);
		HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, 0); //LED on
 
		if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET)
		{
			EXTI->PR1 |= EXTI_PR1_PIF1; //clear any that came AFTER debounce
			EXTI->IMR1 |= EXTI_IMR1_IM1; //enable / unmask
 
			flag = 1;
		}
	}
}

Main code:

  while (1)
  {
	//SystemClock_Decrease();
	//HAL_SuspendTick();
	//HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);
 
	if (flag != 0)
	{
		flag = 0;
 
		//HAL_PWREx_DisableLowPowerRunMode();
		//SystemClock_Config();
		//HAL_ResumeTick();
 
		HAL_Delay(100);
		HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, 1); //LED off
	}
 
    /* USER CODE END WHILE */
    //MX_APPE_Process();
 
    /* USER CODE BEGIN 3 */
  }

3 REPLIES 3

> EXTI->IMR1 &= ~EXTI_IMR1_IM0;

0?

JW

TDK
Guru

> if (GPIO_Pin == GPIO_PIN_1)

> EXTI->IMR1 &= ~EXTI_IMR1_IM0;

It looks like you're trying to disable the GPIO_PIN_1 interrupt on the first falling edge when you start the timer, but this doesn't do it. It should be EXTI_IMR1_IM1.

The button will likely chatter when you release it as well which, with your current scheme, will be interpreted as another press. As long as it's held for more than 50ms, which buttons typically are.

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

Thank you both for the response as that was definitely incorrect on my part (and I fixed it), however it does NOTHING to change the picture on the scope wrt timing. The edge is still 62ms apart. If I change the 50 (line 3 above ) to 40 I get 52ms. It appears to me that the timer LPTIM1 is being clocked by something other than LSE (btw I tried LSI and get the same thing), or there is some start issue when going into an ISR, or something. My code is VERY short and there should be no reason that it doesn't measure 50ms spot on. Any more ideas?