- /**
- * [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 点亮,则表示发生错误。