你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32L053C8-Discovery开发板LPTIM例程有问题

[复制链接]
厦门浪子 提问时间:2017-6-3 19:33 /
本帖最后由 厦门浪子 于 2017-6-3 19:40 编辑

   在开发板目录:STM32Cube_FW_L0_V1.8.0\Projects\STM32L053C8-Discovery\Examples\LPTIM路径的这个例程:没有做任何修改,但是下载进开发板,程序不能正常进行, LPTIM唤醒定时器没有进入中断,本来进入中断LED灯会亮起来,然后唤醒STOP模式。但是开发板灯没有亮,中断没进去。难道开发板的代码也这么补严谨?

  1. /**
  2.   ******************************************************************************
  3.   * @file    LPTIM/LPTIM_Timeout/Src/main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.8.0
  6.   * @date    25-November-2016
  7.   * @brief   This example describes how to implement a low power timeout to
  8.   *          wake-up the system using the LPTIMER, through the STM32L0xx HAL API.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"

  40. /** @addtogroup STM32L0xx_HAL_Examples
  41.   * @{
  42.   */

  43. /** @addtogroup LPTIM_Timeout
  44.   * @{
  45.   */

  46. /* Private typedef -----------------------------------------------------------*/
  47. /* Private define ------------------------------------------------------------*/
  48. /* Set the Maximum value of the counter (Auto-Reload) that defines the Period */
  49. #define Period               (uint32_t) 65535

  50. /* Set the Timeout value */
  51. #define Timeout              (uint32_t) (32768 - 1)

  52. /* Private macro -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/
  54. /* LPTIM handle declaration */
  55. LPTIM_HandleTypeDef             LptimHandle;

  56. /* Clocks structure declaration */
  57. RCC_PeriphCLKInitTypeDef        RCC_PeriphCLKInitStruct;

  58. /* Private function prototypes -----------------------------------------------*/
  59. static void SystemClock_Config(void);
  60. static void LSE_ClockEnable(void);
  61. static void Error_Handler(void);

  62. /* Private functions ---------------------------------------------------------*/

  63. static void SystemClockConfig_STOP(void)
  64. {
  65.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  66.   RCC_OscInitTypeDef RCC_OscInitStruct;

  67.   /* Enable Power Control clock */
  68.   __HAL_RCC_PWR_CLK_ENABLE();

  69.   /* The voltage scaling allows optimizing the power consumption when the device is
  70.      clocked below the maximum system frequency, to update the voltage scaling value
  71.      regarding system frequency refer to product datasheet.  */
  72.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  73.   /* Get the Oscillators configuration according to the internal RCC registers */
  74.   HAL_RCC_GetOscConfig(&RCC_OscInitStruct);

  75.   /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
  76.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  77.   RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  78.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  79.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  80.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  81.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  82.   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
  83.   RCC_OscInitStruct.HSICalibrationValue = 0x10;
  84.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  85.   {
  86.     Error_Handler();
  87.   }

  88.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  89.      clocks dividers */
  90.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  91.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  92.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  93.   {
  94.     Error_Handler();
  95.   }
  96. }
  97. /**
  98.   * @brief  Main program
  99.   * @param  None
  100.   * @retval None
  101.   */
  102. int main(void)
  103. {
  104. /* This sample code shows how to use STM32L0xx LPTIM HAL API to realize a
  105.     timeout function to wakeup the system from Stop mode.
  106.     To proceed, 4 steps are required:
  107. */

  108.   /* STM32L0xx HAL library initialization:
  109.        - Configure the Flash prefetch, Flash preread and Buffer caches
  110.        - Systick timer is configured by default as source of time base, but user
  111.              can eventually implement his proper time base source (a general purpose
  112.              timer for example or other time source), keeping in mind that Time base
  113.              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  114.              handled in milliseconds basis.
  115.        - Low Level Initialization
  116.      */
  117.   HAL_Init();

  118.   /* Configure the System clock to have a frequency of 2 MHz (Up to 32MHZ possible) */
  119.   SystemClock_Config();

  120.   /* Enable the LSE source */
  121.   LSE_ClockEnable();

  122.   /* Initialize LED2 */

  123.     BSP_LED_Init(LED2);  

  124.   /* ### - 1 - Re-target the LSE to Clock the LPTIM Counter ################# */
  125.   /* Select the LSE clock as LPTIM peripheral clock */
  126.   RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPTIM1;
  127.   RCC_PeriphCLKInitStruct.LptimClockSelection = RCC_LPTIM1CLKSOURCE_LSI;  
  128.   HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);

  129.   /* ### - 2 - Initialize LPTIM peripheral ################################## */
  130.   /*
  131.    *  Instance        = LPTIM1.
  132.    *  Clock Source    = APB or LowPowerOSCillator (in this example LSE is
  133.    *                    already selected from the RCC level).
  134.    *  Counter source  = Internal event.   
  135.    *  Clock prescaler = 1 (No division).
  136.    *  Counter Trigger = Trigger1: PC3 or PB6 (PC3 in this example).
  137.    *  Active Edge     = Rising edge.
  138.    */

  139.   LptimHandle.Instance = LPTIM1;

  140.   LptimHandle.Init.Clock.Source       = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
  141.   LptimHandle.Init.Clock.Prescaler    = LPTIM_PRESCALER_DIV1;  
  142.   LptimHandle.Init.Trigger.Source     = LPTIM_TRIGSOURCE_0;
  143.   LptimHandle.Init.Trigger.ActiveEdge = LPTIM_ACTIVEEDGE_RISING;
  144.   LptimHandle.Init.CounterSource      = LPTIM_COUNTERSOURCE_INTERNAL;

  145.   /* Initialize LPTIM peripheral according to the passed parameters */
  146.   if (HAL_LPTIM_Init(&LptimHandle) != HAL_OK)
  147.   {
  148.     Error_Handler();
  149.   }

  150.   /* ### - 3 - Start the Timeout function in interrupt mode ################# */
  151.   /*
  152.    *  Period = 65535
  153.    *  Pulse  = 32767
  154.    *  According to this configuration (LPTIMER clocked by LSE & compare = 32767,
  155.    *  the Timeout period = (compare + 1)/LSE_Frequency = 1s
  156.    */
  157.   if (HAL_LPTIM_TimeOut_Start_IT(&LptimHandle, Period, Timeout) != HAL_OK)
  158.   {
  159.     Error_Handler();
  160.   }

  161.   /* ### - 4 - Enter in Stop mode ########################################### */
  162.   HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  

  163.   /* Infinite Loop */
  164. //  while (1)
  165. //  {
  166. //
  167. //  }
  168. }

  169. /**
  170.   * @brief  Compare match callback in non blocking mode
  171.   * @param  hlptim : LPTIM handle
  172.   * @retval None
  173.   */
  174. void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
  175. {
  176.   /* Timeout was reached, turn on LED2 */


  177.                    BSP_LED_On(LED2);
  178.   
  179. }


  180. /**
  181.   * @brief  System Clock Configuration
  182.   *         The system Clock is configured as follow :
  183.   *            System Clock source            = MSI
  184.   *            SYSCLK(Hz)                     = 2000000
  185.   *            HCLK(Hz)                       = 2000000
  186.   *            AHB Prescaler                  = 1
  187.   *            APB1 Prescaler                 = 1
  188.   *            APB2 Prescaler                 = 1
  189.   *            Flash Latency(WS)              = 0
  190.   *            Main regulator output voltage  = Scale3 mode
  191.   * @param  None
  192.   * @retval None
  193.   */
  194. static void SystemClock_Config(void)
  195. {
  196.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  197.   RCC_OscInitTypeDef RCC_OscInitStruct;

  198.   /* Enable Power Control clock */
  199.   __HAL_RCC_PWR_CLK_ENABLE();

  200.   /* The voltage scaling allows optimizing the power consumption when the device is
  201.      clocked below the maximum system frequency, to update the voltage scaling value
  202.      regarding system frequency refer to product datasheet.  */
  203.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  204.   /* Enable MSI Oscillator */
  205.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  206.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  207.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  208.   RCC_OscInitStruct.MSICalibrationValue=0x00;
  209.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  210.    if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  211.   {
  212.     /* Initialization Error */
  213.     Error_Handler();
  214.   }


  215.   /* Select MSI as system clock source and configure the HCLK, PCLK1 and PCLK2
  216.      clocks dividers */
  217.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  218.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  219.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  220.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;  
  221.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  222.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  223.   {
  224.     /* Initialization Error */
  225.     Error_Handler();
  226.   }
  227. }

  228. /**
  229.   * @brief  Enable External Low Speed Clock (LSE)
  230.   * @param  None
  231.   * @retval None
  232.   */
  233. static void LSE_ClockEnable(void)
  234. {
  235.   RCC_OscInitTypeDef RCC_OscInitStruct;

  236.   /* Enable LSE clock */
  237.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  238.   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  239.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  240. }

  241. /**
  242.   * @brief  This function is executed in case of error occurrence.
  243.   * @param  None
  244.   * @retval None
  245.   */
  246. static void Error_Handler(void)
  247. {
  248.   /* Infinite loop */
  249.   while(1)
  250.   {
  251.   }
  252. }
  253. #ifdef  USE_FULL_ASSERT

  254. /**
  255.   * @brief  Reports the name of the source file and the source line number
  256.   *         where the assert_param error has occurred.
  257.   * @param  file: pointer to the source file name
  258.   * @param  line: assert_param error line source number
  259.   * @retval None
  260.   */
  261. void assert_failed(uint8_t* file, uint32_t line)
  262. {
  263.   /* User can add his own implementation to report the file name and line number,
  264.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  265.   /* Infinite loop */
  266.   while (1)
  267.   {
  268.   }
  269. }
  270. #endif

  271. /**
  272.   * @}
  273.   */

  274. /**
  275.   * @}
  276.   */

  277. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码





收藏 评论1 发布时间:2017-6-3 19:33

举报

1个回答
ataudio 回答时间:2017-6-27 17:04:09
没试过这个板子。帮楼主顶一下。
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版