cancel
Showing results for 
Search instead for 
Did you mean: 

Using Low Power Timer for Pulse Counting. When should I receive an interrupt? Does the interrupt only fire when the autoreload register is matched? Or should the interrupt trigger on every count received?

Aaron McNeil
Associate III

I have a pulse input circuit with my counts going both into LPTIM1 input and TIM8 input 1. I am using capture compare to measure the frequency using TIM8. I would also like to use LPTIM1 in conjunction to keep the actual count of my pulses. When I send a pulse, I get an interrupt for my capture compare timer but do not get one on LPTIM1. My counts do seem to be increment using

count =  HAL_LPTIM_ReadCounter(&hlptim1);

However, the counts don't seem to be matching up. It increments maybe 3-4 counts every time I send 1.

Here is my code:

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_USB_OTG_FS_PCD_Init();

 MX_I2C1_Init();

 MX_I2C2_Init();

 MX_I2C3_Init();

 MX_USART2_UART_Init();

 MX_USART3_UART_Init();

 MX_ADC1_Init();

 MX_SPI1_Init();

 MX_TIM8_Init();

 MX_LPTIM1_Init();

 MX_LPTIM2_Init();

 MX_FMC_Init();

 MX_RTC_Init();

 /* USER CODE BEGIN 2 */

  /*##-3- Start the Input Capture in interrupt mode ##########################*/

 if(HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_1) != HAL_OK)

 {

  /* Starting Error */

  Error_Handler();

 }

  

 if (HAL_LPTIM_Counter_Start_IT(&hlptim1, 1000) != HAL_OK)

 {

  Error_Handler();

 }

static void MX_LPTIM1_Init(void)

{

 /* USER CODE BEGIN LPTIM1_Init 0 */

 /* USER CODE END LPTIM1_Init 0 */

 /* USER CODE BEGIN LPTIM1_Init 1 */

 /* USER CODE END LPTIM1_Init 1 */

 hlptim1.Instance = LPTIM1;

 hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_ULPTIM;

 hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;

 hlptim1.Init.UltraLowPowerClock.Polarity = LPTIM_CLOCKPOLARITY_RISING;

 hlptim1.Init.UltraLowPowerClock.SampleTime = LPTIM_CLOCKSAMPLETIME_DIRECTTRANSITION;

 hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;

 hlptim1.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;

 hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;

 hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_EXTERNAL;

 hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;

 hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;

 if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN LPTIM1_Init 2 */

 /* USER CODE END LPTIM1_Init 2 */

1 ACCEPTED SOLUTION

Accepted Solutions

I don't use Cube so don't understand the code, but basically

  • the LPTIM does not have a capture, so you can't really interrupt upon each incoming pulse (theoretically you can if you set autoreload to 1, but that's sort of silly)
  • if your input pulse is not clean enough (e.g. comes from a bouncy button), the LPTIM's asynchronous counter may count quite a couple of transitions while you service only one "conventional" timer interrupt

JW

View solution in original post

4 REPLIES 4

I don't use Cube so don't understand the code, but basically

  • the LPTIM does not have a capture, so you can't really interrupt upon each incoming pulse (theoretically you can if you set autoreload to 1, but that's sort of silly)
  • if your input pulse is not clean enough (e.g. comes from a bouncy button), the LPTIM's asynchronous counter may count quite a couple of transitions while you service only one "conventional" timer interrupt

JW

Aaron McNeil
Associate III

Thank you. I think my wrong count problem was definitely a bouncy button.

One other question I have regarding LPTIM. Why does the counter mode automatically miss the first 5 edges? In a pulse critical application this is not useable. Is there another mode I can put the LPTIM in or something to where it does not miss the first 5 edges?

Five? I know of two (and don't know of any simple workaround, except connecting another pin externally and generating the required "initial" pulses)

https://community.st.com/s/question/0D50X00009XkWkzSAF/lptimer-arr-interrupt

JW

Aaron McNeil
Associate III

According to this example in AN4865 it is 5. And I am seeing exactly 5 from all my tests here. Seems silly for ST to implement this function this way when pulse counting for flow measurement is so critical.

https://www.st.com/content/ccc/resource/technical/document/application_note/group0/bd/16/1d/53/4a/ef/4e/0e/DM00290631/files/DM00290631.pdf/jcr:content/translations/en.DM00290631.pdf