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)的輸出比較中斷標志位,在計數器向上和向下計數時均被設置。 |
具體如下圖所示;