说明:
测试的官方例子:LPTIM_PulseCounter。
使用LPTIM的好处是系统处于睡眠,停机状态可以正常工作(除了待机模式)。
这个例子选择了LSI作为LPTIM1的时钟源,对外部脉冲计数,计数达到1001个后将触发中断,在中断里面做了个LED闪烁。
特别注意一点,因为使用的LSI时钟,测量的脉冲频率不要超过LSI的时钟速度,大概是32KHz。
测试代码:
此功能重点还是函数HAL_LPTIM_Init的参数配置学习。
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* LPTIM handle declaration */
LPTIM_HandleTypeDef LptimHandle;
/* Clocks structure declaration */
RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct;
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static HAL_StatusTypeDef LSI_ClockEnable(void);
static void Error_Handler(void);
static void CPU_CACHE_Enable(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/* Enable the CPU Cache */
CPU_CACHE_Enable();
/* STM32H7xx HAL library initialization:
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Set NVIC Group Priority to 4
- Low Level Initialization
*/
HAL_Init();
/* Configure the system clock to 400 MHz */
SystemClock_Config();
/* Configure LED1 */
BSP_LED_Init(LED1);
/* Enable the LSI Clock */
if (LSI_ClockEnable() != HAL_OK)
{
Error_Handler();
}
/* ### - 1 - Re-target the LSI to Clock the LPTIM Counter ################# */
/* Select the LSI clock as LPTIM peripheral clock */
RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPTIM1;
RCC_PeriphCLKInitStruct.Lptim1ClockSelection = RCC_LPTIM1CLKSOURCE_LSI;
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
/* ### - 2 - Initialize the LPTIM peripheral ############################## */
/*
* Instance = LPTIM1
* Clock Source = APB or LowPowerOSCillator (in this example LSI is
* already selected from the RCC stage)
* Counter source = External event.
* Clock prescaler = 1 (No division)
* Counter Trigger = Software trigger
* Output Polarity = High
* Update mode = Immediate (Registers are immediately updated after any
* write access)
*/
LptimHandle.Instance = LPTIM1;
LptimHandle.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
LptimHandle.Init.CounterSource = LPTIM_COUNTERSOURCE_EXTERNAL;
LptimHandle.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;
LptimHandle.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
LptimHandle.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;
LptimHandle.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
LptimHandle.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
LptimHandle.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
/* Initialize LPTIM peripheral according to the passed parameters */
if (HAL_LPTIM_Init(&LptimHandle) != HAL_OK)
{
Error_Handler();
}
/* ### - 3 - Start counting in interrupt mode ############################# */
/*
* Period = 1000
*/
if (HAL_LPTIM_Counter_Start_IT(&LptimHandle, 1000) != HAL_OK)
{
Error_Handler();
}
/* ### - 4 - Enter in Stop mode ########################################### */
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
/* Infinite Loop */
while (1)
{
}
}
/**
* @brief Autoreload match callback in non blocking mode
* @param hlptim : LPTIM handle
* @retval None
*/
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
/* Turn on LED1 */
BSP_LED_Toggle(LED1);
}
/**
* @brief Enable Internal Low Speed Clock (LSI)
* @param None
* @retval Status
*/
static HAL_StatusTypeDef LSI_ClockEnable(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
/* Enable LSI clock */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
return (HAL_RCC_OscConfig(&RCC_OscInitStruct));
} |