硬汉嵌入式论坛

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

[TIMER] STM32H7的低功耗定时器LPTIM,实现芯片进入停机状态后做脉冲计数

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107393
QQ
发表于 2018-8-24 00:20:31 | 显示全部楼层 |阅读模式
说明:

测试的官方例子:LPTIM_PulseCounter。

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

这个例子选择了LSI作为LPTIM1的时钟源,对外部脉冲计数,计数达到1001个后将触发中断,在中断里面做了个LED闪烁。

特别注意一点,因为使用的LSI时钟,测量的脉冲频率不要超过LSI的时钟速度,大概是32KHz。


测试代码:

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

  1. /* Private typedef -----------------------------------------------------------*/
  2. /* Private define ------------------------------------------------------------*/
  3. /* Private macro -------------------------------------------------------------*/
  4. /* Private variables ---------------------------------------------------------*/
  5. /* LPTIM handle declaration */
  6. LPTIM_HandleTypeDef             LptimHandle;

  7. /* Clocks structure declaration */
  8. RCC_PeriphCLKInitTypeDef        RCC_PeriphCLKInitStruct;

  9. /* Private function prototypes -----------------------------------------------*/
  10. static void SystemClock_Config(void);
  11. static HAL_StatusTypeDef LSI_ClockEnable(void);
  12. static void Error_Handler(void);
  13. static void CPU_CACHE_Enable(void);
  14. /* Private functions ---------------------------------------------------------*/

  15. /**
  16.   * @brief  Main program
  17.   * @param  None
  18.   * @retval None
  19.   */
  20. int main(void)
  21. {
  22.   /* Enable the CPU Cache */
  23.   CPU_CACHE_Enable();
  24.   
  25.   /* STM32H7xx HAL library initialization:
  26.        - Systick timer is configured by default as source of time base, but user
  27.          can eventually implement his proper time base source (a general purpose
  28.          timer for example or other time source), keeping in mind that Time base
  29.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  30.          handled in milliseconds basis.
  31.        - Set NVIC Group Priority to 4
  32.        - Low Level Initialization
  33.      */
  34.   HAL_Init();

  35.   /* Configure the system clock to 400 MHz */
  36.   SystemClock_Config();

  37.   /* Configure LED1 */
  38.   BSP_LED_Init(LED1);

  39.   /* Enable the LSI Clock */
  40.   if (LSI_ClockEnable() != HAL_OK)
  41.   {
  42.     Error_Handler();
  43.   }

  44.   /* ### - 1 - Re-target the LSI to Clock the LPTIM Counter ################# */
  45.   /* Select the LSI clock as LPTIM peripheral clock */
  46.   RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPTIM1;
  47.   RCC_PeriphCLKInitStruct.Lptim1ClockSelection = RCC_LPTIM1CLKSOURCE_LSI;  
  48.   HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
  49.   
  50.   
  51.   /* ### - 2 - Initialize the LPTIM peripheral ############################## */
  52.   /*
  53.    *  Instance        = LPTIM1
  54.    *  Clock Source    = APB or LowPowerOSCillator (in this example LSI is
  55.    *                    already selected from the RCC stage)
  56.    *  Counter source  = External event.
  57.    *  Clock prescaler = 1 (No division)
  58.    *  Counter Trigger = Software trigger
  59.    *  Output Polarity = High
  60.    *  Update mode     = Immediate (Registers are immediately updated after any
  61.    *                    write access)
  62.    */

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

  79.   /* ### - 3 - Start counting in interrupt mode ############################# */
  80.   /*
  81.    *  Period = 1000
  82.    */
  83.   if (HAL_LPTIM_Counter_Start_IT(&LptimHandle, 1000) != HAL_OK)
  84.   {
  85.     Error_Handler();
  86.   }
  87.   
  88.   /* ### - 4 - Enter in Stop mode ########################################### */
  89.   HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

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

  95. /**
  96.   * @brief  Autoreload match callback in non blocking mode
  97.   * @param  hlptim : LPTIM handle
  98.   * @retval None
  99.   */
  100. void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
  101. {
  102.   /* Turn on LED1 */
  103.   BSP_LED_Toggle(LED1);
  104. }

  105. /**
  106.   * @brief  Enable Internal Low Speed Clock (LSI)
  107.   * @param  None
  108.   * @retval Status
  109.   */
  110. static HAL_StatusTypeDef LSI_ClockEnable(void)
  111. {
  112.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  113.   
  114.   /* Enable LSI clock */
  115.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  116.   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  117.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  118.   return (HAL_RCC_OscConfig(&RCC_OscInitStruct));
  119. }

复制代码



回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-29 10:20 , Processed in 0.242009 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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