STM32—TIM:基本定时器(定时中断)(标准库)

STM32的定时器分为基本定时器,通用定时器,高级定时器,定时器功能逐渐增加,具体功能如下:

STM32—TIM:基本定时器(定时中断)(标准库)_第1张图片

定时中断功能讲解:
   外设使能:void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)

/**
  * @brief  Enables or disables the Low Speed APB (APB1) peripheral clock.
  * @param  RCC_APB1Periph: specifies the APB1 peripheral to gates its clock.
  *   This parameter can be any combination of the following values:
  *     @arg RCC_APB1Periph_TIM2, RCC_APB1Periph_TIM3, RCC_APB1Periph_TIM4,
  *          RCC_APB1Periph_TIM5, RCC_APB1Periph_TIM6, RCC_APB1Periph_TIM7,
  *          RCC_APB1Periph_WWDG, RCC_APB1Periph_SPI2, RCC_APB1Periph_SPI3,
  *          RCC_APB1Periph_USART2, RCC_APB1Periph_USART3, RCC_APB1Periph_USART4, 
  *          RCC_APB1Periph_USART5, RCC_APB1Periph_I2C1, RCC_APB1Periph_I2C2,
  *          RCC_APB1Periph_USB, RCC_APB1Periph_CAN1, RCC_APB1Periph_BKP,
  *          RCC_APB1Periph_PWR, RCC_APB1Periph_DAC, RCC_APB1Periph_CEC,
  *          RCC_APB1Periph_TIM12, RCC_APB1Periph_TIM13, RCC_APB1Periph_TIM14
  * @param  NewState: new state of the specified peripheral clock.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
{

  ........
}

配置内部时钟:void TIM_InternalClockConfig(TIM_TypeDef* TIMx) (定时器的时钟可选内部和外部)

/**
  * @brief  Configures the TIMx internal Clock
  * @param  TIMx: where x can be  1, 2, 3, 4, 5, 8, 9, 12 or 15
  *         to select the TIM peripheral.
  * @retval None
  */
void TIM_InternalClockConfig(TIM_TypeDef* TIMx)
{
      ............
}

定时器配置初始化:void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)

/**
  * @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 17 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)
{
 ............        
}



//  TIM_TimeBaseInitStruct.TIM_ClockDivision           配置分频器

//  TIM_TimeBaseInitStruct.TIM_CounterMode             配置计数模式

//  TIM_TimeBaseInitStruct.TIM_Period                  配置ARR自动重装器 (0~65535)
      
//  TIM_TimeBaseInitStruct.TIM_Prescaler               配置PSC预分频器   (0~65535)
   
//  TIM_TimeBaseInitStruct.TIM_RepetitionCounter       配置重复寄存器


注意:TIM_TimeBaseInitStruct.TIM_RepetitionCounter  是高级定时器特有参数,如不使用高级定时器 
                                                    可将这参数置为0

时间计算:
     
     CK_CNT_OV = CK_CNT/(ARR+1) = CK_PSC/(PCS+1)/(ARR+1)

解释:
     CK_CNT_OV :  计数频率  
    
     CK_CNT :计数器                            ARR : 自动重装器    
  
     CK_PSC :时钟频率(一般STM32为72MHZ)        PCS : 预分频器
  
举例:如果要定时1s  : 则  CK_CNT_OV 为1HZ      则 72000000/(PCS+1)/(ARR+1) = 1
                     就可以选择                PCS = 7200-1  ARR = 10000-1
   
      如果要定时1ms : 则  CK_CNT_OV 为1000HZ   则 72000000/(PCS+1)/(ARR+1) = 1000
                     就可以选择                PCS = 7200-1  ARR = 10-1

      如果要定时5ms : 则  CK_CNT_OV 为200HZ   则 72000000/(PCS+1)/(ARR+1)  = 200
                     就可以选择                PCS = 7200-1  ARR = 50-1




使能定时器中断:void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState)
 

/**
  * @brief  Enables or disables the specified TIM interrupts.
  * @param  TIMx: where x can be 1 to 17 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 
  *   - TIM6 and TIM7 can only generate an update interrupt.
  *   - TIM9, TIM12 and TIM15 can have only TIM_IT_Update, TIM_IT_CC1,
  *      TIM_IT_CC2 or TIM_IT_Trigger. 
  *   - TIM10, TIM11, TIM13, TIM14, TIM16 and TIM17 can have TIM_IT_Update or TIM_IT_CC1.   
  *   - TIM_IT_Break is used only with TIM1, TIM8 and TIM15. 
  *   - TIM_IT_COM is used only with TIM1, TIM8, TIM15, TIM16 and TIM17.    
  * @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)
{  
 ................
}

配置NVIC: void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
 

/**
  * @brief  Initializes the NVIC peripheral according to the specified
  *         parameters in the NVIC_InitStruct.
  * @param  NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
  *         the configuration information for the specified NVIC peripheral.
  * @retval None
  */
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
{
  .................
}

注意:配置NVIC必须先进行NVIC分组,且NVIC分组一旦设定就不可被更改
  
      NVIC分组函数:
      
      void NVIC_PriorityGroupConfig();

使能定时器:void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState);

/**
  * @brief  Enables or disables the specified TIM peripheral.
  * @param  TIMx: where x can be 1 to 17 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)
{
   ......
}

这样定时器就会开始定时中断

配置中断函数:STM32的中断函数都是封装好了的 
                         在启动文件 
STM32—TIM:基本定时器(定时中断)(标准库)_第2张图片
  

选择定时器的中断函数(以定时器1为例子)

void TIM6_IRQHandler(void)   
{
	if(TIM_GetFlagStatus(TIM6,TIM_FLAG_Update) == SET)
	{
     

     TIM_ClearITPendingBit(TIM6,TIM_FLAG_Update);
    
	}
	
	
}



void TIM_GetFlagStatus()  : 获取TIMx的某个标志位

 此函数的目的是有的中断函数有着多个中断源,需要进行区分。
 
void TIM_ClearITPendingBit()  : 清除TIMx的某个标志位

注意:若按照上面进行TIM的配置,则会在这里出现一个小问题

void TIM_TimeBaseInit();

使用这函数的时候定时器的中断挂起位会被置1(即申请一个更新事件)

此时一旦使能中断就会立马进入一次中断

这显然不是我们想要的


所以在中断使能之前我们需要人为清除这个函数所产生的中断

TIM_ClearITPendingBit(TIM6,TIM_FLAG_Update);

你可能感兴趣的:(STM32(标准库),stm32)