• STM32L0 HAL库 TIM定时1s


    STM32L0的定制器资源:

    本实验使用TIM6

    HSI频率是16Mhz,则单指令周期是1/16Mhz

    预分频设置为1600,则每跑1600下,定时器加1,相当于定时器加1的时间是1600*(1/16Mhz)=100us

    定义周期为10000,则计数到10000时候,定时器溢出,定时器溢出的时间为10000*100us=1s

    有了这几个基本参数,我们按照如下设置CUBEMX

    HAL的库的调用:

    HAL_TIM_Base_Init这个已经在CubeMX自动生成和调用了,不用再管了。

    HAL_TIM_Base_Start_IT需要自己写代码启动中断模式

    另外计数器溢出回调函数需要自己添加HAL_TIM_PeriodElapsedCallback

    实现1s进入一次回调函数,串口返回一条指令:

    测试代码如下:

    /**
      ******************************************************************************
      * File Name          : main.c
      * Description        : Main program body
      ******************************************************************************
      *
      * COPYRIGHT(c) 2015 STMicroelectronics
      *
      * Redistribution and use in source and binary forms, with or without modification,
      * are permitted provided that the following conditions are met:
      *   1. Redistributions of source code must retain the above copyright notice,
      *      this list of conditions and the following disclaimer.
      *   2. Redistributions in binary form must reproduce the above copyright notice,
      *      this list of conditions and the following disclaimer in the documentation
      *      and/or other materials provided with the distribution.
      *   3. Neither the name of STMicroelectronics nor the names of its contributors
      *      may be used to endorse or promote products derived from this software
      *      without specific prior written permission.
      *
      * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
      * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
      * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
      * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
      * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      *
      ******************************************************************************
      */
    /* Includes ------------------------------------------------------------------*/
    #include "stm32l0xx_hal.h"
    
    /* USER CODE BEGIN Includes */
    
    /* USER CODE END Includes */
    
    /* Private variables ---------------------------------------------------------*/
    TIM_HandleTypeDef htim6;
    
    UART_HandleTypeDef huart2;
    
    /* USER CODE BEGIN PV */
    /* Private variables ---------------------------------------------------------*/
    
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_TIM6_Init(void);
    static void MX_USART2_UART_Init(void);
    
    /* USER CODE BEGIN PFP */
    /* Private function prototypes -----------------------------------------------*/
    
    /* USER CODE END PFP */
    
    /* USER CODE BEGIN 0 */
    
    /* USER CODE END 0 */
    
    int main(void)
    {
    
      /* USER CODE BEGIN 1 */
    
      /* USER CODE END 1 */
    
      /* MCU Configuration----------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      HAL_Init();
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_TIM6_Init();
      MX_USART2_UART_Init();
    
      /* USER CODE BEGIN 2 */
        //启动定时器中断
        if(HAL_TIM_Base_Start_IT(&htim6) != HAL_OK)
      {
        /* Starting Error */
        while(1);
      }
        
        unsigned char txData[]={"helloworld
    "};
        HAL_UART_Transmit(&huart2,txData,sizeof(txData)-1,0xffff);
    
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
      /* USER CODE END WHILE */
    
      /* USER CODE BEGIN 3 */
          // HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
          // HAL_Delay(1000);
          // HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
          // HAL_Delay(1000);
    
        HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
        HAL_Delay(1000);
    
      }
      /* USER CODE END 3 */
    
    }
    
    /** System Clock Configuration
    */
    void SystemClock_Config(void)
    {
    
      RCC_OscInitTypeDef RCC_OscInitStruct;
      RCC_ClkInitTypeDef RCC_ClkInitStruct;
      RCC_PeriphCLKInitTypeDef PeriphClkInit;
    
      __PWR_CLK_ENABLE();
    
      __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    
      RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
      RCC_OscInitStruct.HSIState = RCC_HSI_ON;
      RCC_OscInitStruct.HSICalibrationValue = 16;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
      HAL_RCC_OscConfig(&RCC_OscInitStruct);
    
      RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
      RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
      RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
      RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
      RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
      HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
    
      PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
      PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
      HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
    
      HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
    
      HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
    
    }
    
    /* TIM6 init function */
    void MX_TIM6_Init(void)
    {
    
      TIM_MasterConfigTypeDef sMasterConfig;
    
      htim6.Instance = TIM6;
      htim6.Init.Prescaler = 1600;
      htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
      htim6.Init.Period = 10000;
      HAL_TIM_Base_Init(&htim6);
    
      sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
      sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
      HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig);
    
    }
    
    /* USART2 init function */
    void MX_USART2_UART_Init(void)
    {
    
      huart2.Instance = USART2;
      huart2.Init.BaudRate = 9600;
      huart2.Init.WordLength = UART_WORDLENGTH_8B;
      huart2.Init.StopBits = UART_STOPBITS_1;
      huart2.Init.Parity = UART_PARITY_NONE;
      huart2.Init.Mode = UART_MODE_TX_RX;
      huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
      huart2.Init.OverSampling = UART_OVERSAMPLING_16;
      huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
      huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
      HAL_UART_Init(&huart2);
    
    }
    
    /** Configure pins as 
            * Analog 
            * Input 
            * Output
            * EVENT_OUT
            * EXTI
    */
    void MX_GPIO_Init(void)
    {
    
      GPIO_InitTypeDef GPIO_InitStruct;
    
      /* GPIO Ports Clock Enable */
      __GPIOA_CLK_ENABLE();
    
      /*Configure GPIO pin : PA5 */
      GPIO_InitStruct.Pin = GPIO_PIN_5;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_PULLUP;
      GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    }
    
    /* USER CODE BEGIN 4 */
    
    void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
    {
            if (htim->Instance == htim6.Instance)
            {
                unsigned char txData[]={"in HAL_TIM_PeriodElapsedCallback
    "};
                HAL_UART_Transmit(&huart2,txData,sizeof(txData)-1,0xffff);
            }
    }
    
    /* USER CODE END 4 */
    
    #ifdef USE_FULL_ASSERT
    
    /**
       * @brief Reports the name of the source file and the source line number
       * where the assert_param error has occurred.
       * @param file: pointer to the source file name
       * @param line: assert_param error line source number
       * @retval None
       */
    void assert_failed(uint8_t* file, uint32_t line)
    {
      /* USER CODE BEGIN 6 */
      /* User can add his own implementation to report the file name and line number,
        ex: printf("Wrong parameters value: file %s on line %d
    ", file, line) */
      /* USER CODE END 6 */
    
    }
    
    #endif
    
    /**
      * @}
      */ 
    
    /**
      * @}
    */ 
    
    /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  • 相关阅读:
    python-操作excel之openpyxl
    python之redis
    geetest滑动验证
    vue-cookies
    谷歌浏览器安装vue插件
    axios和vuex
    概率论基础:补充(1)概率的公理化定义与随机变量的概念
    卸载 Anaconda 转用 Miniconda
    傅立叶变换
    SL-主成分分析(PCA)
  • 原文地址:https://www.cnblogs.com/Mysterious/p/4821304.html
Copyright © 2020-2023  润新知