【STM32】STM32学习笔记-定时器定时中断 定时器外部时钟(14)

发布时间:2023年12月24日

00. 目录

01. 定时器中断相关API

1.1 TIM_InternalClockConfig

/**
  * @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)
功能:
	设置 TIMx 内部时钟
参数:
    TIMx:x 可以是 23 或者 4,来选择 TIM 外设    
返回值:

1.2 TIM_TimeBaseInit

/**
  * @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 中指定的参数初始化 TIMx 的时间基数单位
参数:
    TIMx:x 可以是 23 或者 4,来选择 TIM 外设
    TIMTimeBase_InitStruct:指向结构 TIM_TimeBaseInitTypeDef 的指针,包含了TIMx 时间基数单位的配置信息    
返回值:

1.3 TIM_TimeBaseInitTypeDef

/** 
  * @brief  TIM Time Base Init structure definition
  * @note   This structure is used with all TIMx except for TIM6 and TIM7.    
  */

typedef struct
{
  uint16_t TIM_Prescaler;         
  uint16_t TIM_CounterMode;       
  uint16_t TIM_Period;            
  uint16_t TIM_ClockDivision;    
  uint8_t TIM_RepetitionCounter;  
} TIM_TimeBaseInitTypeDef; 

TIM_Period
	TIM_Period 设置了在下一个更新事件装入活动的自动重装载寄存器周期的值。它的取值必须在0x00000xFFFF之间。
TIM_Prescaler
	TIM_Prescaler 设置了用来作为 TIMx 时钟频率除数的预分频值。它的取值必须在0x00000xFFFF之间。
  
/** @defgroup TIM_Counter_Mode 
  * @{
  */
#define TIM_CounterMode_Up                 ((uint16_t)0x0000)
#define TIM_CounterMode_Down               ((uint16_t)0x0010)
#define TIM_CounterMode_CenterAligned1     ((uint16_t)0x0020)
#define TIM_CounterMode_CenterAligned2     ((uint16_t)0x0040)
#define TIM_CounterMode_CenterAligned3     ((uint16_t)0x0060)
    
    
/** @defgroup TIM_Clock_Division_CKD 
  * @{
  */
#define TIM_CKD_DIV1                       ((uint16_t)0x0000)
#define TIM_CKD_DIV2                       ((uint16_t)0x0100)
#define TIM_CKD_DIV4                       ((uint16_t)0x0200)
    
    
TIM_RepetitionCounter 只用在高级定时器中    

1.4 TIM_ClearFlag

/**
  * @brief  Clears the TIMx's pending flags.
  * @param  TIMx: where x can be 1 to 17 to select the TIM peripheral.
  * @param  TIM_FLAG: specifies the flag bit to clear.
  *   This parameter can be any combination of the following values:
  *     @arg TIM_FLAG_Update: TIM update Flag
  *     @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
  *     @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
  *     @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
  *     @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
  *     @arg TIM_FLAG_COM: TIM Commutation Flag
  *     @arg TIM_FLAG_Trigger: TIM Trigger Flag
  *     @arg TIM_FLAG_Break: TIM Break Flag
  *     @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 overcapture Flag
  *     @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 overcapture Flag
  *     @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 overcapture Flag
  *     @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 overcapture Flag
  * @note
  *   - TIM6 and TIM7 can have only one update flag. 
  *   - TIM9, TIM12 and TIM15 can have only TIM_FLAG_Update, TIM_FLAG_CC1,
  *      TIM_FLAG_CC2 or TIM_FLAG_Trigger. 
  *   - TIM10, TIM11, TIM13, TIM14, TIM16 and TIM17 can have TIM_FLAG_Update or TIM_FLAG_CC1.   
  *   - TIM_FLAG_Break is used only with TIM1, TIM8 and TIM15. 
  *   - TIM_FLAG_COM is used only with TIM1, TIM8, TIM15, TIM16 and TIM17.   
  * @retval None
  */
void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG)
功能:
	清除 TIMx 的待处理标志位
参数:
    TIMx:x 可以是 23 或者 4,来选择 TIM 外设
    TIM_FLAG:待清除的 TIM 标志位   
返回值:

1.5 TIM_ITConfig

/**
  * @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)
功能:
	使能或者失能指定的 TIM 中断
参数:
    TIMx:x 可以是 23 或者 4,来选择 TIM 外设
    TIM_IT:待使能或者失能的 TIM 中断源
    NewState:TIMx 中断的新状态    
返回值:

1.6 TIM_Cmd

/**
  * @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)
功能:
	使能或者失能 TIMx 外设
参数:
    TIMx:x 可以是 23 或者 4,来选择 TIM 外设
    NewState: 外设 TIMx 的新状态   
返回值:

1.7 中断服务函数

参考程序

void TIM2_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
		Num ++;
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	}
}

1.8 TIM_ETRClockMode2Config

/**
  * @brief  Configures the External clock Mode2
  * @param  TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  * @param  TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  *   This parameter can be one of the following values:
  *     @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  *     @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  *     @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  *     @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  * @param  TIM_ExtTRGPolarity: The external Trigger Polarity.
  *   This parameter can be one of the following values:
  *     @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  *     @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  * @param  ExtTRGFilter: External Trigger Filter.
  *   This parameter must be a value between 0x00 and 0x0F
  * @retval None
  */
void TIM_ETRClockMode2Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler, 
                             uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
功能:
	配置TIMx外部时钟模式2 
参数:
    TIMx:x可以是23或者4,来选择TIM外设
    TIM_ExtTRGPrescaler:外部触发预分频
    TIM_ExtTRGPolarity:外部时钟极性    
    ExtTRGFilter:外部触发滤波器。该参数取值在0x00xF之间。        
返回值:

02. 定时器定时中断接线图

在这里插入图片描述

03. 定时器定时中断示例

timer.h

#ifndef __TIMER_H__
#define __TIMER_H__


#include "stm32f10x.h"                  // Device header


void timer_init(void);


#endif


timer.c

#include "timer.h"


void timer_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	//1. 开启时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	
	//2. 开启定时器2内部时钟
	TIM_InternalClockConfig(TIM2);
	
	//3. 初始化定时器
	TIM_TimeBaseInitStruct.TIM_Period = 10000 - 1;
	TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0; //高级定时器使用
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
	
	//4. 清除中断标志
	TIM_ClearFlag(TIM2, TIM_FLAG_Update);
	
	//5. 使能中断
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	
	//6. 设置NVIC
	NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE ;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStruct);

	//7. 使能定时器
	TIM_Cmd(TIM2, ENABLE);
}

main.c

#include "stm32f10x.h"

#include "delay.h"
#include "oled.h"
#include "timer.h"


int16_t num;

 int main(void)
 {		 
	//初始化
	OLED_Init();

	 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

	 timer_init();
	
	 OLED_ShowString(1, 1, "num:");
	
	while (1)
	{
		OLED_ShowNum(1, 5, num, 5);
	}
	 
	 return 0;
 }

 
 void TIM2_IRQHandler(void)
 {
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
		num ++;
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	}
	 
 }

04. 定时器外部时钟接线图

在这里插入图片描述

05. 定时器外部时钟示例

timer.h

#ifndef __TIMER_H__
#define __TIMER_H__


#include "stm32f10x.h"                  // Device header


void timer_init(void);


#endif


timer.c

#include "timer.h"


void timer_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	//1. 开启时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
		
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	
	//2. 配置TIMx外部时钟模式2 
	TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0xf);
                            
	//3. 初始化定时器
	TIM_TimeBaseInitStruct.TIM_Period = 10 - 1;
	TIM_TimeBaseInitStruct.TIM_Prescaler = 1 - 1;
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0; //高级定时器使用
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
	
	//4. 清除中断标志
	TIM_ClearFlag(TIM2, TIM_FLAG_Update);
	
	//5. 使能中断
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	
	//6. 设置NVIC
	NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE ;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStruct);

	//7. 使能定时器
	TIM_Cmd(TIM2, ENABLE);
}


main.c

#include "stm32f10x.h"

#include "delay.h"
#include "oled.h"
#include "timer.h"


int16_t num;

 int main(void)
 {		 
	//初始化
	OLED_Init();

	 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

	 timer_init();
	
	 OLED_ShowString(1, 1, "num:");
	
	while (1)
	{
		OLED_ShowNum(1, 5, num, 5);
		OLED_ShowNum(2, 5, TIM_GetCounter(TIM2), 5);
	}
	 
	 return 0;
 }

 
 void TIM2_IRQHandler(void)
 {
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
		num ++;
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	}
	 
 }

 

06. 程序下载

09-定时器定时中断.rar

10-定时器外部时钟.rar

07. 附录

参考: 【STM32】江科大STM32学习笔记汇总

文章来源:https://blog.csdn.net/u010249597/article/details/135186842
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。