(STM32F4) SysTick理解使用

關於Cortex System Timer (Systick) 網上隨便google就可以找到許多相關範例。

他就是ARM提供的一個24-bit的下數(count-down)計時器我看大部分應用都是提供給delay使用,在RTOS也有一些應用.

簡單提及一下 Systick Register

1 typedef struct
2 {
3   __IO uint32_t CTRL;                    /*!< Offset: 0x000 (R/W)  SysTick Control and Status Register */
4   __IO uint32_t LOAD;                    /*!< Offset: 0x004 (R/W)  SysTick Reload Value Register       */
5   __IO uint32_t VAL;                     /*!< Offset: 0x008 (R/W)  SysTick Current Value Register      */
6   __I  uint32_t CALIB;                   /*!< Offset: 0x00C (R/ )  SysTick Calibration Register        */
7 } SysTick_Type;

Systick CTRL register

1. COUNT FLAG[16] : Return 1 if timer counted to 0.

2. CLK SOURCE [2] : select the clock soruce, 0 : AHB / 8, 1 : AHB.

3. TICK INT [1] : Counting down to zero either assert or not the Systick exception request.

4. ENABLE [0] : Enbles the counter.

Programming manual 的 note 敘述可以利用軟體的方式去讀取 COUNT FLAG來判斷counter是否曾經到0.

Systick RELOAD register

主要是提供 24bit 的counter reload value.

Systick VAL register

當Systick timer 開始計數 VAL register就可以觀察目前計數到的數值

貼心的官方當然有提供相對應的配置function

 static __INLINE uint32_t SysTick_Config(uint32_t ticks) 

選 system clock 為主所以在配置 reload register 會影響中斷發生的時間

 SysTick_Config(SystemCoreClock / 1000) 

以這行程式碼來說 systick timer counter 1 次需要 1/168MHz的時間

Reload = 168M / 1000 = 168K , interrupt 時間 = 168K / 168M = 1ms發生一次.

官方的範例提供了 2 個function主要負責計算delay,Delay time 則是透過 Systick 配置決定的

 1 /**
 2   * @brief  Inserts a delay time.
 3   * @param  nTime: specifies the delay time length, in milliseconds.
 4   * @retval None
 5   */
 6 void Delay(__IO uint32_t nTime)
 7 { 
 8   TimingDelay = nTime;
 9 
10   while(TimingDelay != 0);
11 }
12 
13 /**
14   * @brief  Decrements the TimingDelay variable.
15   * @param  None
16   * @retval None
17   */
18 void TimingDelay_Decrement(void)
19 {
20   if (TimingDelay != 0x00)
21   { 
22     TimingDelay--;
23   }
24 }
25 
26 /**
27   * @brief  This function handles SysTick Handler.
28   * @param  None
29   * @retval None
30   */
31 void SysTick_Handler(void)
32 {
33   TimingDelay_Decrement();
34 }

 

转载于:https://www.cnblogs.com/ollie-lin/p/10099955.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值