391 views|2 replies

126

Posts

1

Resources
The OP
 

【NUCLEO-U083RC】7.LPTIME generates PWM [Copy link]

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:

U083RC_LPTIME.zip (8.07 MB, downloads: 0)
This post is from stm32/stm8

Latest reply

The waveform looks good. In actual applications, there are not many situations where PWM accuracy is required to be so high.   Details Published on 2024-6-6 09:08
Personal signature

没用比没有强

 

6045

Posts

6

Resources
2
 

The waveform looks good. In actual applications, there are not many situations where PWM accuracy is required to be so high.

This post is from stm32/stm8

Comments

Yes, if the accuracy requirement is high, it is better to use an advanced timer   Details Published on 2024-6-6 14:49
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 

126

Posts

1

Resources
3
 
Qintianqintian0303 posted on 2024-6-6 09:08 The waveform looks good. In actual applications, there are not many situations where the PWM precision is required to be so high

Yes, if the accuracy requirement is high, it is better to use an advanced timer

This post is from stm32/stm8
 
Personal signature

没用比没有强

 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list