在本项目中我使用LPTIM1计数器的目的就是对外部引脚输入的脉冲信号进行正确计数。即STM32L476复位启动后,只要在PC0(STM32L476ZGT的26脚)脚上有脉冲信号就能够正确计数。并且计数达到设置的个数后,能够进入中断。现在出现的问题是:出现一个奇怪的问题,只要重新进行过复位,不管是热启动还是冷启动,计数的时候前面总是要丢掉固定的5个脉冲信号。然后,后面不管怎样你只要不重新启动计数就正常。比如,启动后程序第一次运行,我输入了100个脉冲信号,只能够计数95个。后面只要不重新复位,随便什么时候引脚出现脉冲信号,计数一切都正常。芯片用的是STM32L476.之前问过一个ST的技术支持,说这是第一个开始计数的信号识别,是排除干扰的意思,我觉得应该不是这个。下面是我用STM32CubeMX自动生成的代码。麻烦给看看。
/*Includes ------------------------------------------------------------------*/
#include"lptim.h"
#include"gpio.h"
/*USER CODE BEGIN 0 */
/*USER CODE END 0 */
LPTIM_HandleTypeDefhlptim1;
/*LPTIM1 init function */
voidMX_LPTIM1_Init(void)
{
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();
}
}
voidHAL_LPTIM_MspInit(LPTIM_HandleTypeDef* lptimHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(lptimHandle->Instance==LPTIM1)
{
/* USER CODE BEGIN LPTIM1_MspInit 0 */
/* USER CODE END LPTIM1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_LPTIM1_CLK_ENABLE();
/**LPTIM1 GPIO Configuration
PC0 ------> LPTIM1_IN1
*/
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_LPTIM1;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* Peripheral interrupt init */
HAL_NVIC_SetPriority(LPTIM1_IRQn, 0, 0);
LPTIM1->IER = LPTIM_IER_ARRMIE; //¿ªLPTIM1ÖжÏ
HAL_NVIC_EnableIRQ(LPTIM1_IRQn);
/* USER CODE BEGIN LPTIM1_MspInit 1 */
LPTIM1->IER = LPTIM_IER_ARRMIE; //¿ªLPTIM1ÖжÏ
//HAL_LPTIM_Counter_Start_IT(&hlptim1,65535);
HAL_LPTIM_Counter_Start(&hlptim1,65535);
((&hlptim1)->Instance->CR |= LPTIM_CR_CNTSTRT);
//LPTIM1->State = HAL_LPTIM_STATE_READY;
//LPTIM1->CR->CNTSTRT=1;
/* USER CODE END LPTIM1_MspInit 1 */
}
}
voidHAL_LPTIM_MspDeInit(LPTIM_HandleTypeDef* lptimHandle)
{
if(lptimHandle->Instance==LPTIM1)
{
/* USER CODE BEGIN LPTIM1_MspDeInit 0 */
/* USER CODE END LPTIM1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_LPTIM1_CLK_DISABLE();
/**LPTIM1 GPIO Configuration
PC0 ------> LPTIM1_IN1
*/
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0);
/* Peripheral interrupt Deinit*/
HAL_NVIC_DisableIRQ(LPTIM1_IRQn);
}
/* USER CODE BEGIN LPTIM1_MspDeInit 1 */
/* USER CODE END LPTIM1_MspDeInit 1 */
}
/*USER CODE BEGIN 1 */
/*USER CODE END 1 */
/**
* @}
*/
/**
* @}
*/
/************************(C) COPYRIGHT STMicroelectronics *****END OF FILE****/
<LPTIM_PulseCounter.rar.secure>
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>