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

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

定时中断功能讲解:
   外设使能: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的中断函数都是封装好了的 
                         在启动文件  <startup_stm32f10x_hd.s> 中

  

选择定时器的中断函数(以定时器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);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用STM32标准库进行stm32高级定时器TIM1的定时中断配置代码示例: ``` // 定时器配置 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 999; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); // 定时中断配置 NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); TIM_Cmd(TIM1, ENABLE); // 中断处理函数 void TIM1_UP_TIM10_IRQHandler(void) { if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM1, TIM_IT_Update); // 定时器中断处理代码 } } ``` 这个示例代码中,我们使用了STM32标准库进行定时器配置定时中断配置。其中,`TIM_TimeBaseStructure`是一个`TIM_TimeBaseInitTypeDef`结构体,用于存储定时器的相关配置信息。`TIM_OCInitStructure`是一个`TIM_OCInitTypeDef`结构体,用于存储定时器输出比较(PWM)的相关配置信息。 在定时中断配置部分,我们使用了标准库提供的函数`NVIC_Init()`来配置定时器中断优先级和使能定时中断。同时,我们也使用了标准库提供的函数`TIM_ITConfig()`来使能定时中断,并使用`TIM_Cmd()`函数启动定时器。 在中断处理函数`TIM1_UP_TIM10_IRQHandler()`中,我们使用了标准库提供的函数`TIM_GetITStatus()`和`TIM_ClearITPendingBit()`来处理定时器中断,并编写定时器中断处理代码。 需要注意的是,这个示例代码中的定时器时钟源为APB2总线,如果需要使用其他时钟源或者其他定时器,请根据具体情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值