一、外设定时器
1. 定义
设置定时时间(定时周期),超时后则执行指定操作的硬件。
2. STM32F407的定时器有以下特征
具有基本的定时功能,也有 PWM 输出(灯光亮度控制、电机的转速)、脉冲捕获功能(红外捕捉)。
2个高级控制定时器、10 个通用定时器 和 2 个基本定时器
高级控制定时器(TIM1 和 TIM8),挂载到APB2
具有 16 位定时器功能,也具有 PWM 输出高级控制功能
通用定时器(TIM2 到 TIM5),挂载到APB1
具有 16 或 32位定时功能,也具有 PWM 输出控制功能
通用定时器(TIM9 到 TIM14),挂载到AP1或APB2
具有 16 位定时功能,也具有 PWM 输出控制功能
基本定时器(TIM6 和 TIM7),挂载到APB1
具有 16 位定时功能。
3. 专业英文
period, /ˈpɪriəd/ ,周期。
注:
1)TIM 是 TIMER 英文的缩写。
2)通用定时器与高级控制定时器
二、定时计算
1.定时器时钟频率由硬件自动设置
STM32F405xx/07xx 和 STM32F415xx/17xx 的定时器时钟频率由硬件自动设置。分为两种情况:
如果 APB 预分频器为 1,定时器时钟频率等于 APB 域(APB1和APB2)的频率。
否则,等于 APB 域(APB1和APB2)的频率的两倍 (×2)。
- 关于APB1与APB2预分频值的说明在system_stm32f4xx.c文件的头部,详细描述如下:
* 5. This file configures the system clock as follows: *============================================================================= *============================================================================= * Supported STM32F40xxx/41xxx devices
*-----------------------------------------------------------------------------
* System Clock source | PLL (HSE)
*-----------------------------------------------------------------------------
* SYSCLK(Hz) | 168000000
*-----------------------------------------------------------------------------
* HCLK(Hz) | 168000000
*-----------------------------------------------------------------------------
* AHB Prescaler | 1
*-----------------------------------------------------------------------------
* APB1 Prescaler | 4
*-----------------------------------------------------------------------------
* APB2 Prescaler | 2
*-----------------------------------------------------------------------------
* HSE Frequency(Hz) | 25000000
- 关于APB1与APB2预分频值设置在system_stm32f4xx.c文件,详细代码如下:
/**
* @brief Configures the System clock source, PLL Multiplier and Divider factors,
* AHB/APBx prescalers and Flash settings
* @Note This function should be called only once the RCC clock configuration
* is reset to the default reset state (done in SystemInit() function).
* @param None
* @retval None
*/
static void SetSysClock(void)
{
............
/* HCLK = SYSCLK / 1*/
RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
#if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx)
/* PCLK2 = HCLK / 2*/
RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
/* PCLK1 = HCLK / 4*/
RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
#endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx */
...........
}
2.定时器3硬件时钟
3.定时500ms
TIM_TimeBaseStructure.TIM_Period = (10000/2)-1; //定时时间的配置,也就是配置重载值,而重载值会传递给计数值 TIM_TimeBaseStructure.TIM_Prescaler = 8400-1; //配置分频值,确定定时器的时钟频率 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数,0->TIM_Period就会触发中断请求 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
疑点:为什么预分频填写参数时要减1?
答:因为参数传递给寄存器时,默认帮忙加1。
拓展1:若定时器3的硬件时钟频率为10000Hz,最大的定时时间?
1000ms Tmax
------- = -------
10000 65536
Tmax = 6553.6ms = 6.5536s
拓展2:
下图STM32F407无时钟分频。
其他ARM平台具有时钟分频(Clock Division),如下图S3C2440 定时器硬件时钟。
三、库函数
1.定时器基本初始化
/**
* @brief Initializes the TIMx Time Base Unit peripheral according to
* the specified parameters in the TIM_TimeBaseInitStruct.
* @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
* @param TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef structure
* that contains the configuration information for the specified TIM peripheral.
* @retval None
*/
void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
2.定时器中断配置
/**
* @brief Enables or disables the specified TIM interrupts.
* @param TIMx: where x can be 1 to 14 to select the TIMx peripheral.
* @param TIM_IT: specifies the TIM interrupts sources to be enabled or disabled.
* This parameter can be any combination of the following values:
* @arg TIM_IT_Update: TIM update Interrupt source
* @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
* @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
* @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
* @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
* @arg TIM_IT_COM: TIM Commutation Interrupt source
* @arg TIM_IT_Trigger: TIM Trigger Interrupt source
* @arg TIM_IT_Break: TIM Break Interrupt source
*
* @note For TIM6 and TIM7 only the parameter TIM_IT_Update can be used
* @note For TIM9 and TIM12 only one of the following parameters can be used: TIM_IT_Update, TIM_IT_CC1, TIM_IT_CC2 or TIM_IT_Trigger.
* @note For TIM10, TIM11, TIM13 and TIM14 only one of the following parameters can
* be used: TIM_IT_Update or TIM_IT_CC1
* @note TIM_IT_COM and TIM_IT_Break can be used only with TIM1 and TIM8
*
* @param NewState: new state of the TIM interrupts.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState)
递增计数模式
在递增计数模式下,计数器从 0 计数到自动重载值(TIMx_ARR 寄存器的内容),然后重新从 0 开始计数并生成计数器上溢事件。每次发生计数器上溢时会生成更新事件
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
3.定时器工作使能
/**
* @brief Enables or disables the specified TIM peripheral.
* @param TIMx: where x can be 1 to 14 to select the TIMx peripheral.
* @param NewState: new state of the TIMx peripheral.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
四、代码思路
- 使能定时器硬件时钟
- 设置定时器分频值
- 确定定时器计数值
- 使能定时器工作
- 使能定时器更新中断触发
- 配置定时器中断优先级
- 编写定时器中断服务函数