本帖最后由 williamwt 于 2016-12-3 02:23 编辑
之所以选择定时器而不是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
- /**
- ******************************************************************************
- * File Name : main.c
- * Description : Main program body
- ******************************************************************************
- *
- * COPYRIGHT(c) 2016 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 "main.h"
- #include "stm32f4xx_hal.h"
- #include "tim.h"
- #include "gpio.h"
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- __IO uint16_t timer_count=0;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- void Error_Handler(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();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim6);
- HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_SET);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- if(timer_count == 1000)
- {
- timer_count = 0;
- HAL_GPIO_TogglePin(GPIOB, LD2_Pin);
- HAL_GPIO_TogglePin(GPIOB, LD3_Pin);
- }
- }
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 100;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 2;
- RCC_OscInitStruct.PLL.PLLR = 2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler */
- }
- #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 %drn", file, line) */
- /* USER CODE END 6 */
- }
- #endif
- /**
- * @}
- */
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- timer_count++;
- }
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
主要功能在于增加了中断回调函数,每1ms中断一次计数+1,当计数到1000也就是1s时间两个led翻转。
这就是基本定时的用法,下一篇介绍通用定时器产生pwm,用来驱动led产生呼吸灯的效果。
|