STM32 TIM1高級定時器配置快速入門


layout: post
tags: [STM32]
comments: true

重點內容

不管是基於標準庫還是直接操作寄存器,因爲TIM定時器的功能比較多,這裏單純只從定時器的角度進行學習,這裏需要重點關注的地方應該有以下幾點:

  • 定時器時鐘頻率的計算;
  • 計數器計數的模式,以及一般模式會有哪些應用場景;
    • 向上計數
    • 向下計數
    • 中央對齊模式:該模式下需要關注觸發中斷幾種的方式
      • 向上溢出中斷;
      • 向下溢出中斷;
      • 向上和向下都產生溢出中斷;

時基單元

TIM1定時器是一個16位計數器以及相關的自動裝載寄存器,比較寄存器,預分頻器,可以實現向上,向下,或者向上再向下的計數模式;
基本的時基單元分爲:

  • 計數器寄存器(TIMx_CNT)
  • 預分頻器寄存器 (TIMx_PSC)
  • 自動裝載寄存器 (TIMx_ARR)
  • 重複次數寄存器 (TIMx_RCR)

可以參考STM32參考手冊查看相關的寄存器配置說明
下面是TIM功能的整體框圖,功能很多,提取了一下計數器的寄存器相應關係;

在這裏插入圖片描述

標準庫中,時基單元封裝到結構體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;         /*!< Specifies the prescaler value used to                                       divide the TIM clock.
                                       This parameter can be a number between 0x0000 and 0xFFFF */

  uint16_t TIM_CounterMode;       /*!< Specifies the counter mode.
                                       This parameter can be a value of @ref TIM_Counter_Mode */

  uint16_t TIM_Period;            /*!< Specifies the period value to be loaded into the active
                                       Auto-Reload Register at the next update event.
                                       This parameter must be a number between 0x0000 and 0xFFFF.  */ 

  uint16_t TIM_ClockDivision;     /*!< Specifies the clock division.
                                      This parameter can be a value of @ref TIM_Clock_Division_CKD */

  uint8_t TIM_RepetitionCounter;  /*!< Specifies the repetition counter value. Each time the RCR downcounter
                                       reaches zero, an update event is generated and counting restarts
                                       from the RCR value (N).
                                       This means in PWM mode that (N+1) corresponds to:
                                          - the number of PWM periods in edge-aligned mode
                                          - the number of half PWM period in center-aligned mode
                                       This parameter must be a number between 0x00 and 0xFF. 
                                       @note This parameter is valid only for TIM1 and TIM8. */
} TIM_TimeBaseInitTypeDef;  

基於標準庫的時鐘頻率計算:

TIMclock = SYSclock * 1/TIM_Prescaler * TIM_Period


	/* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
	TimerPeriod = (SystemCoreClock / 17570 ) - 1;
	/* Time Base configuration */
	TIM_TimeBaseStructure.TIM_Prescaler = TIM_PSCReloadMode_Update;
	//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
	TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

計數模式

  • 向上計數模式
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up
  • 向下計數模式
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down
  • 中央對齊模式
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1

中央對齊模式可以配置TIMx_CR1寄存器的CMS[1:0]位,從而有三種模式可以選擇:

CMS[1:0] 選擇中央對齊模式 (Center-aligned mode selection)
00 邊沿對齊模式。計數器依據方向位(DIR)向上或向下計數。
01 中央對齊模式1。計數器交替地向上和向下計數。產生下溢中斷,配置爲輸出的通道(TIMx_CCMRx寄存器中CCxS=00)的輸出比較中斷標誌位,只在計數器向下計數時被設置。
10 中央對齊模式2。計數器交替地向上和向下計數。產生上溢中斷,配置爲輸出的通道(TIMx_CCMRx寄存器中CCxS=00)的輸出比較中斷標誌位,只在計數器向上計數時被設置。
11 中央對齊模式3。計數器交替地向上和向下計數。產生下溢和上溢中斷,配置爲輸出的通道(TIMx_CCMRx寄存器中CCxS=00)的輸出比較中斷標誌位,在計數器向上和向下計數時均被設置。

具體如下圖所示;
在這裏插入圖片描述

發佈了95 篇原創文章 · 獲贊 74 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章