硬汉嵌入式论坛

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

[TIMER] STM32H7的低功耗定时器LPTIM,超时功能的实现

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107393
QQ
发表于 2018-8-26 01:15:18 | 显示全部楼层 |阅读模式
说明:

官方例子LPTIM_Timeout

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

这个例子实现这样一个功能,配置LPTIM的溢出时间是1s,采用LSI作为时钟,然后配置芯片工作地低功耗状态。在PG14引脚第1次检查到触发信号,LPTIM就开始工作了,在1s的溢出时间内检测到的触发信号都将将复位计数,定时器重新开始工作。如果1s内没有再接收到触发信号,仅进入溢出中断。

测试代码:

此功能重点还是函数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 Period               (uint32_t) 65535

  5. /* Set the Timeout value */
  6. #define Timeout              (uint32_t) (32768 - 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 LSI_ClockEnable(void);
  16. static void Error_Handler(void);
  17. static void CPU_CACHE_Enable(void);

  18. /* Private functions ---------------------------------------------------------*/

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

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

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

  44.   /* Initialize LED2 */
  45.   BSP_LED_Init(LED2);  
  46.   
  47.   /* Enable the LSI source */
  48.   LSI_ClockEnable();

  49.   /* ### - 1 - Re-target the LSI to Clock the LPTIM Counter ################# */
  50.   /* Select the LSI clock as LPTIM peripheral clock */
  51.   RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPTIM1;
  52.   RCC_PeriphCLKInitStruct.Lptim1ClockSelection = RCC_LPTIM1CLKSOURCE_LSI;
  53.   HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);

  54.   /* ### - 2 - Initialize LPTIM peripheral ################################## */
  55.   /*
  56.    *  Instance        = LPTIM1.
  57.    *  Clock Source    = APB or LowPowerOSCillator
  58.    *  Counter source  = Internal event.   
  59.    *  Clock prescaler = 1 (No division).
  60.    *  Counter Trigger = Trigger1: PG.14
  61.    *  Active Edge     = Rising edge.
  62.    */

  63.   LptimHandle.Instance = LPTIM1;
  64.   
  65.   LptimHandle.Init.Clock.Source       = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
  66.   LptimHandle.Init.Clock.Prescaler    = LPTIM_PRESCALER_DIV1;  
  67.   LptimHandle.Init.Trigger.Source     = LPTIM_TRIGSOURCE_0;
  68.   LptimHandle.Init.Trigger.ActiveEdge = LPTIM_ACTIVEEDGE_RISING;
  69.   LptimHandle.Init.CounterSource      = LPTIM_COUNTERSOURCE_INTERNAL;
  70.   LptimHandle.Init.Input1Source       = LPTIM_INPUT1SOURCE_GPIO;
  71.   LptimHandle.Init.Input2Source       = LPTIM_INPUT2SOURCE_GPIO;
  72.   
  73.   /* Initialize LPTIM peripheral according to the passed parameters */
  74.   if (HAL_LPTIM_Init(&LptimHandle) != HAL_OK)
  75.   {
  76.     Error_Handler();
  77.   }

  78.   /* ### - 3 - Start the Timeout function in interrupt mode ################# */
  79.   /*
  80.    *  Period = 65535
  81.    *  Pulse  = 32767
  82.    *  According to this configuration (LPTIMER clocked by LSI & compare = 32767,
  83.    *  the Timeout period = (compare + 1)/LSI_Frequency = 1s
  84.    */
  85.   if (HAL_LPTIM_TimeOut_Start_IT(&LptimHandle, Period, Timeout) != HAL_OK)
  86.   {
  87.     Error_Handler();
  88.   }
  89.   
  90.   /* ### - 4 - Enter in Stop mode ########################################### */
  91.   HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  92.   /* Infinite Loop */
  93.   while (1)
  94.   {
  95.   }

  96. }

  97. /**
  98.   * @brief  Enable External Low Speed Clock (LSI)
  99.   * @param  None
  100.   * @retval None
  101.   */
  102. static void LSI_ClockEnable(void)
  103. {
  104.   RCC_OscInitTypeDef RCC_OscInitStruct;
  105.   
  106.   /* Enable LSI clock */
  107.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  108.   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  109.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  110.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  111.   {
  112.       Error_Handler();
  113.   }
  114. }
复制代码


回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-29 10:21 , Processed in 0.263810 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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