实验目的
主要实现了通过 HAL LPTIM API 利用 LSE 作为计数器时钟来配置和使用 LPTIM 外设生成 PWM 信号,并在低功耗模式下运行。
LPTIM 是一个 16 位的定时器,在降低功耗方面有了最终的发展成果。由于其时钟源的多样性,LPTIM 能够在除待机模式外的所有电源模式下保持运行。比如说,在一些需要持续运行但又对功耗有严格要求的场景中,这一特性就使得它颇具优势。
LPTIM 能够从低功耗模式唤醒系统,并以极低的功耗实现“超时功能”。例如,在物联网设备中,这一功能可以确保高效的电源管理和延长电池寿命。
LPTIM框图
LPTIM_HandleTypeDef hlptim1;
LPTIM句柄
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] LPTIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_LPTIM1_Init(void)
{
/* USER CODE BEGIN LPTIM1_Init 0 */
/* USER CODE END LPTIM1_Init 0 */
LPTIM_OC_ConfigTypeDef sConfig1 = {0};
/* USER CODE BEGIN LPTIM1_Init 1 */
/* USER CODE END LPTIM1_Init 1 */
hlptim1.Instance = LPTIM1;
hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;
hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
hlptim1.Init.Period = PeriodValue;
hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
hlptim1.Init.RepetitionCounter = 0;
if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
{
Error_Handler();
}
sConfig1.Pulse = PulseValue;
sConfig1.OCPolarity = LPTIM_OCPOLARITY_HIGH;
if (HAL_LPTIM_OC_ConfigChannel(&hlptim1, &sConfig1, LPTIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN LPTIM1_Init 2 */
/* USER CODE END LPTIM1_Init 2 */
HAL_LPTIM_MspPostInit(&hlptim1);
}
#define PeriodValue (uint32_t) (100 - 1)
周期
#define PulseValue (uint32_t) (50 - 1)
占空比
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* STM32H5xx 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
*/
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* Configure LED1 and LED3 */
BSP_LED_Init(LED1);
BSP_LED_Init(LED3);
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ICACHE_Init();
MX_LPTIM1_Init();
/* USER CODE BEGIN 2 */
/* USER push-button (External line 13) will be used to wakeup the system from Stop mode */
BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
/* ### Start counting in interrupt mode ############################# */
/*
* Period = 99
* Pulse = 49
* According to LPTIM configuration, the duty cycle will be equal to 50%
*/
if (HAL_LPTIM_PWM_Start(&hlptim1, LPTIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/* ### Enter in Stop mode ########################################### */
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
/* ### Stop counting when leaving Stop mode ########################## */
if (HAL_LPTIM_PWM_Stop(&hlptim1, LPTIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/* Turn LED1 ON */
BSP_LED_On(LED1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
main 函数
- 使用 SystemClock_Config() 函数为 STM32H563ZITx 设备配置系统时钟,使 CPU 运行在 250 MHz
- 使用的 LPTIM 为 LPTIM1
- 低功耗模式为 Stop 模式。
- 计数器时钟为 LSE(32.768 KHz),自动重载值为 99,因此输出频率为 327.680 Hz
- 脉冲值为 49,占空比计算为 50%
- 用户按钮引脚(PC.13)被配置为带有外部中断(外部线路 13)的输入,下降沿触发。当用户按下按钮时,会生成唤醒事件,停止 PWM 信号生 成并使 LED1 点亮。如果 LED3 点亮,则表示发生错误。
|