This post was last edited by Electronic Bad Guy on 2024-6-5 22:04
The u0 series has multiple time timers, but I have never used LPtime. This time I will generate a PWM to try it out.
1. Introduction to LPTIM and PWM
LPTIM (Low-Power Timer) is a low-power timer provided by ST for its STM32 microcontroller series. LPTIM is designed to provide time measurement and generate accurate timing in low-power mode, especially for those occasions with very strict power requirements, such as in stop mode or standby mode.
LPTIM is usually not used directly to generate PWM (Pulse Width Modulation) signals, because PWM signals usually require high-precision timing and fast output changes, which are usually handled by advanced timers or general purpose timers. However, in some cases, LPTIM can be used to generate simple PWM signals, especially when the accuracy requirements are not particularly high and the power consumption requirements are very low. The application scenario of this design is in a small battery-powered blender, where only the speed gear is adjusted.
The basic principle of LPTIM generating PWM is to use its comparison function to control the duty cycle of the output signal through the compare register and the auto-reload register. When the counter value of LPTIM matches the compare register, the output level will change.
The routine gives the calculation method of PWM, which is still the same as PSR\ARR\CCR:
FrequencyOutput = Counter Clock Frequency / (Autoreload + 1)
DutyCycle = 1 - ((PulseValue + 1)/ (Autoreload + 1))
2. Initialization
As shown in the figure, enable lptim 3 and channel 3, and the generated code is as follows:
static void MX_LPTIM3_Init(void)
{
/* USER CODE BEGIN LPTIM3_Init 0 */
/* USER CODE END LPTIM3_Init 0 */
LPTIM_OC_ConfigTypeDef sConfig1 = {0};
/* USER CODE BEGIN LPTIM3_Init 1 */
/* USER CODE END LPTIM3_Init 1 */
hlptim3.Instance = LPTIM3;
hlptim3.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
hlptim3.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;
hlptim3.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
hlptim3.Init.Period = 999;
hlptim3.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
hlptim3.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
hlptim3.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
hlptim3.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
hlptim3.Init.RepetitionCounter = 0;
if (HAL_LPTIM_Init(&hlptim3) != HAL_OK)
{
Error_Handler();
}
sConfig1.Pulse = 499;
sConfig1.OCPolarity = LPTIM_OCPOLARITY_HIGH;
if (HAL_LPTIM_OC_ConfigChannel(&hlptim3, &sConfig1, LPTIM_CHANNEL_3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN LPTIM3_Init 2 */
/* USER CODE END LPTIM3_Init 2 */
HAL_LPTIM_MspPostInit(&hlptim3);
}
3. Test PWM output under low power consumption
Edit the code and add the following to the main function:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_LPTIM_PWM_Start(&hlptim3, LPTIM_CHANNEL_3);
}
/* USER CODE END 3 */
As shown in the figure, a stable PWM signal can be seen in the oscilloscope.
Add STOP2 mode to the code:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(1000);
BSP_LED_Toggle(LED_GREEN);
HAL_LPTIM_PWM_Start(&hlptim3, LPTIM_CHANNEL_3);
HAL_PWREx_EnterSTOP1Mode(PWR_STOPENTRY_WFI);
HAL_Delay(10000);//停止10秒
SystemClock_Config();//重新配置时钟
}
/* USER CODE END 3 */
}
The generated waveform is as follows: