微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 02-入门篇(定时器)

02-入门篇(定时器)

时间:10-02 整理:3721RD 点击:

      之所以选择定时器而不是GPIO点灯作为入门篇,就跟背单词换个更有意义的分类一样,如果每次都是A-Z从List A开始,最终也就只记得List A。定时器在单片机的学习中有着极其重要的作用,主要是对机器周期进行计数以产生精确的计时,可实现以下功能:
1. 产生精确的定时时间(定时),用来延时
2. 用于需要需要周期性处理的事件(定时中断)
3. 统计脉冲信号(计数)
4. 产生PWM信号,用于调节LED亮度、电机转速控制、功率控制5. 输入捕获,测量输入信号脉宽和测量PWM的频率和占空比。
一、定时器介绍
     STM32F4xxx系列MCU有高级控制定时器2个,通用定时器10个和基础定时器2个。
     基本定时器主要两个功能,第一就是基本定时功能,生成时间基准;第二就是专门用于驱动数模转换器(DAC)。控制器有两个基本定时器 TIM6 和 TIM7,功能完全一样,但所用资源彼此都完全独立,可以同时使用。
     高级控制定时器和通用定时器在基本定时器的基础上引入了外部引脚,可以输入捕获和输出比较功能。高级控制定时器比通用定时器增加了可编程死区互补输出、重复计数器、带刹车(断路)功能,这些功能都是针对工业电机控制方面。  
二、基本定时器例子——定时中断
      打开STM32CubeMX,新建项目,MCU选择STM32F412ZGTx,进入配置页面。
      第一步,引脚配置RCC设置,开发板只接了32.768k的外部晶振,所以选择低速时钟LSE的晶振/陶瓷振荡器。

     
      第二步,设置led引脚,根据原理图将PB7和PB14,设置为GPIO Output。为了显示的更直观,还可以将PB7的用户标签改为LD2 [Blue],PB14的改为LD3 [Red]。

     
      第三步,激活定时器,选择TIM6,勾选Activated。

   
      第四步,时钟设置,我们可以直接在HCLK设置100MHz,很多的参数CubeMX都帮我们搞定。从数据手册我们可以得知,TIM1,TIM8~11的时钟来自于APB2倍频器,而TIM2~7则是来自于APB1的倍频器。因此我们的基本定时器TIM6的时钟来源是100MHz。

          第五步,配置,点击TIM6,根据数据手册的TIM6框图,PSC分频值为99,自动重载计数器1000,则定时时间为1ms。再进入NVIC中断管理器,勾选TIM6使能。






    OK之后,生成代码。修改main.c

  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2016 STMicroelectronics
  8.   *
  9.   * Redistribution and use in source and binary forms, with or without modification,
  10.   * are permitted provided that the following conditions are met:
  11.   *   1. Redistributions of source code must retain the above copyright notice,
  12.   *      this list of conditions and the following disclaimer.
  13.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.   *      this list of conditions and the following disclaimer in the documentation
  15.   *      and/or other materials provided with the distribution.
  16.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.   *      may be used to endorse or promote products derived from this software
  18.   *      without specific prior written permission.
  19.   *
  20.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.   *
  31.   ******************************************************************************
  32.   */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "main.h"
  35. #include "stm32f4xx_hal.h"
  36. #include "tim.h"
  37. #include "gpio.h"

  38. /* USER CODE BEGIN Includes */

  39. /* USER CODE END Includes */

  40. /* Private variables ---------------------------------------------------------*/
  41. __IO uint16_t timer_count=0;
  42. /* USER CODE BEGIN PV */
  43. /* Private variables ---------------------------------------------------------*/

  44. /* USER CODE END PV */

  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. void Error_Handler(void);

  48. /* USER CODE BEGIN PFP */
  49. /* Private function prototypes -----------------------------------------------*/

  50. /* USER CODE END PFP */

  51. /* USER CODE BEGIN 0 */

  52. /* USER CODE END 0 */

  53. int main(void)
  54. {

  55.   /* USER CODE BEGIN 1 */

  56.   /* USER CODE END 1 */

  57.   /* MCU Configuration----------------------------------------------------------*/

  58.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  59.   HAL_Init();

  60.   /* Configure the system clock */
  61.   SystemClock_Config();

  62.   /* Initialize all configured peripherals */
  63.   MX_GPIO_Init();
  64.   MX_TIM6_Init();

  65.   /* USER CODE BEGIN 2 */
  66.   HAL_TIM_Base_Start_IT(&htim6);
  67.   HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET);
  68.   HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_SET);
  69.   /* USER CODE END 2 */

  70.   /* Infinite loop */
  71.   /* USER CODE BEGIN WHILE */
  72.   while (1)
  73.   {
  74.   /* USER CODE END WHILE */

  75.   /* USER CODE BEGIN 3 */
  76.                 if(timer_count == 1000)
  77.                 {
  78.                         timer_count = 0;
  79.                         HAL_GPIO_TogglePin(GPIOB, LD2_Pin);
  80.                         HAL_GPIO_TogglePin(GPIOB, LD3_Pin);
  81.                 }
  82.   }
  83.   /* USER CODE END 3 */

  84. }

  85. /** System Clock Configuration
  86. */
  87. void SystemClock_Config(void)
  88. {

  89.   RCC_OscInitTypeDef RCC_OscInitStruct;
  90.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  91.     /**Configure the main internal regulator output voltage
  92.     */
  93.   __HAL_RCC_PWR_CLK_ENABLE();

  94.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  95.     /**Initializes the CPU, AHB and APB busses clocks
  96.     */
  97.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  98.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  99.   RCC_OscInitStruct.HSICalibrationValue = 16;
  100.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  101.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  102.   RCC_OscInitStruct.PLL.PLLM = 8;
  103.   RCC_OscInitStruct.PLL.PLLN = 100;
  104.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_div2;
  105.   RCC_OscInitStruct.PLL.PLLQ = 2;
  106.   RCC_OscInitStruct.PLL.PLLR = 2;
  107.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  108.   {
  109.     Error_Handler();
  110.   }

  111.     /**Initializes the CPU, AHB and APB busses clocks
  112.     */
  113.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
  114.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  115.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_div1;
  116.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_div2;
  117.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_div1;

  118.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  119.   {
  120.     Error_Handler();
  121.   }

  122.     /**Configure the Systick interrupt time
  123.     */
  124.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  125.     /**Configure the Systick
  126.     */
  127.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  128.   /* SysTick_IRQn interrupt configuration */
  129.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  130. }

  131. /* USER CODE BEGIN 4 */

  132. /* USER CODE END 4 */

  133. /**
  134.   * @brief  This function is executed in case of error occurrence.
  135.   * @param  None
  136.   * @retval None
  137.   */
  138. void Error_Handler(void)
  139. {
  140.   /* USER CODE BEGIN Error_Handler */
  141.   /* User can add his own implementation to report the HAL error return state */
  142.   while(1)
  143.   {
  144.   }
  145.   /* USER CODE END Error_Handler */
  146. }

  147. #ifdef USE_FULL_ASSERT

  148. /**
  149.    * @brief Reports the name of the source file and the source line number
  150.    * where the assert_param error has occurred.
  151.    * @param file: pointer to the source file name
  152.    * @param line: assert_param error line source number
  153.    * @retval None
  154.    */
  155. void assert_failed(uint8_t* file, uint32_t line)
  156. {
  157.   /* USER CODE BEGIN 6 */
  158.   /* User can add his own implementation to report the file name and line number,
  159.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  160.   /* USER CODE END 6 */

  161. }

  162. #endif

  163. /**
  164.   * @}
  165.   */
  166. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  167. {
  168.   timer_count++;
  169. }

  170. /**
  171.   * @}
  172. */

  173. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

复制代码

主要功能在于增加了中断回调函数,每1ms中断一次计数+1,当计数到1000也就是1s时间两个led翻转。
      这就是基本定时的用法,下一篇介绍通用定时器产生pwm,用来驱动led产生呼吸灯的效果。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top