2019-02-21 11:03 AM
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 */
Solved! Go to Solution.
2019-02-21 12:59 PM
I don't use Cube so don't understand the code, but basically
JW
2019-02-21 12:59 PM
I don't use Cube so don't understand the code, but basically
JW
2019-02-21 01:06 PM
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?
2019-02-21 01:54 PM
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
2019-02-21 01:58 PM
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.