硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3647|回复: 1
收起左侧

[TIMER] STM32H7的低功耗定时器LPTIM,时钟源采用外部脉冲或者其它低功耗时钟

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107628
QQ
发表于 2018-8-25 15:32:15 | 显示全部楼层 |阅读模式
说明:

这里所谓的采用外部脉冲就是这里的说明:
QQ截图20180825151100.png

官方例子LPTIM_PulseCounter

使用LPTIM的好处是系统处于睡眠,停机状态可以正常工作(除了待机模式)。

这个例子实现PD12做时钟源的脉冲输入,PD13做输出的。每采集100个输入,输出1次。

并且设了一个用户按键唤醒停机模式,唤醒后停止PWM输出。


测试代码:

此功能重点还是函数HAL_LPTIM_Init的参数配置学习。

  1. /* Private typedef -----------------------------------------------------------*/
  2. /* Private define ------------------------------------------------------------*/
  3. /* Set the Maximum value of the counter (Auto-Reload) that defines the Period */
  4. #define PeriodValue             (uint32_t) (100 -1)

  5. /* Set the Compare value that defines the duty cycle */
  6. #define PulseValue              (uint32_t) (50 -1)

  7. /* Private macro -------------------------------------------------------------*/
  8. /* Private variables ---------------------------------------------------------*/
  9. /* LPTIM handle declaration */
  10. LPTIM_HandleTypeDef             LptimHandle;

  11. /* Clocks structure declaration */
  12. RCC_PeriphCLKInitTypeDef        RCC_PeriphCLKInitStruct;

  13. /* Private function prototypes -----------------------------------------------*/
  14. static void SystemClock_Config(void);
  15. static void Error_Handler(void);
  16. static void CPU_CACHE_Enable(void);
  17. /* Private functions ---------------------------------------------------------*/

  18. /**
  19.   * @brief  Main program
  20.   * @param  None
  21.   * @retval None
  22.   */
  23. int main(void)
  24. {
  25. /* This sample code shows how to use STM32H7xx LPTIM HAL API to generate a
  26.     PWM at the lowest power consumption, using an external counter clock, in
  27.     Low Power mode (STOP mode).*/

  28.         /* Enable the CPU Cache */
  29.   CPU_CACHE_Enable();
  30.   
  31.   /* STM32H7xx HAL library initialization:
  32.        - Systick timer is configured by default as source of time base, but user
  33.          can eventually implement his proper time base source (a general purpose
  34.          timer for example or other time source), keeping in mind that Time base
  35.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  36.          handled in milliseconds basis.
  37.        - Set NVIC Group Priority to 4
  38.        - Low Level Initialization
  39.      */
  40.   HAL_Init();

  41.   /* Configure the system clock to 400 MHz */
  42.   SystemClock_Config();

  43.   /* Configure LED2 */
  44.   BSP_LED_Init(LED2);
  45.   
  46.   /* User push-button (EXTI_Line15_10) will be used to wakeup the system from STOP mode */
  47.   BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);

  48.   /* ### - 1 - Initialize LPTIM peripheral ################################## */
  49.   /*
  50.    *  Instance        = LPTIM1
  51.    *  Clock Source    = Ultra Low Power source (external input 1)
  52.    *  Clock prescaler = 1 (No division)
  53.    *  Counter Trigger = Software trigger
  54.    *  Output Polarity = High
  55.    *  Update mode     = Immediate (Registers are immediately updated after any
  56.    *                    write access)
  57.    */

  58.   LptimHandle.Instance = LPTIM1;
  59.   
  60.   LptimHandle.Init.Clock.Source    = LPTIM_CLOCKSOURCE_ULPTIM;
  61.   LptimHandle.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;
  62.   LptimHandle.Init.CounterSource   = LPTIM_COUNTERSOURCE_EXTERNAL;
  63.   LptimHandle.Init.Trigger.Source  = LPTIM_TRIGSOURCE_SOFTWARE;
  64.   LptimHandle.Init.OutputPolarity  = LPTIM_OUTPUTPOLARITY_HIGH;
  65.   LptimHandle.Init.UpdateMode      = LPTIM_UPDATE_IMMEDIATE;
  66.   
  67.   /* Initialize LPTIM peripheral according to the passed parameters */
  68.   if (HAL_LPTIM_Init(&LptimHandle) != HAL_OK)
  69.   {
  70.     Error_Handler();
  71.   }

  72.   /* ### - 2 - Start generating the PWM signal ############################## */
  73.   /*
  74.    *  Period = 99
  75.    *  Pulse  = 49
  76.    *  According to this configuration, the duty cycle will be equal to 50% and
  77.    *  the output frequency will be equal to the input frequency divided by 100
  78.    *  since the LPTIM have to count 100 external clock edges each period.
  79.    */
  80.   if (HAL_LPTIM_PWM_Start(&LptimHandle, PeriodValue, PulseValue) != HAL_OK)
  81.   {
  82.     Error_Handler();
  83.   }
  84.   
  85.   /* ### - 3 - Enter in Stop mode ########################################### */
  86.   HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  87.   /* ### - 4 - Stop counting when leaving Stop mode ########################## */
  88.   if (HAL_LPTIM_PWM_Stop(&LptimHandle) != HAL_OK)
  89.   {
  90.     Error_Handler();
  91.   }

  92.   /* Infinite Loop */
  93.   while (1)
  94.   {        
  95.   }
  96. }
复制代码




回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107628
QQ
 楼主| 发表于 2019-1-5 14:52:04 | 显示全部楼层
测试这个超时功能,软件触发或者外部触发都没有问题。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-6-8 21:57 , Processed in 0.157228 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表