Try   HackMD

微算機 Lab8 - Timer

歡迎各位利用這種形式補充以及討論,但請避免貼上完整的程式碼。
IanSung

What is Timer?

  • Timer 簡單的說就是一個會持續不斷的把 Clock 數量計算進入 Timer 中的模組。
  • Timer 一般有兩種稱呼:Timer 或 Counter
    • Counter:輸入的時間未知,僅能得知計數到多少個 Clock,也就是說當某件事情發生(例如某個 bit 從 0 變成 1 的時候就將計數加一)
    • Timer:輸入的 Clock 時間已知(固定時間),可以進一步換算出時間
  • 8-bit MCU 的 Timer 為遞增型,也就是會固定從 0 開始計數
  • 模組可以設定計數到多少 Clock 後要通知 (Interrupt) CPU。

Interrupt & Timer

  • 通知:設中斷旗標 (Interrupt Flag)。當數到設定值時,中斷旗標會被模組設定為 1,對 CPU 發出中斷請求 (Interrupt Request)。
    • 如果此時 GIE 沒開,則必須由程式自行檢查事件是否發生。
    • 如果此時 GIE 有開,就會自動進入 ISR。
  • 舉例:Timer0 的中斷控制在 INTCON register 中控制
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

PIC18F4520 Timer Module

Prescaler (預除器)

  • 16-bit Timer 計數範圍 0~65535。如果需要的計數值超過時,必須透過預除器修正。預除器可以用來擴大計數範圍。
  • 舉例:
    • 如果把預除器設為除 8(1:8),則變成 Clock每 8 個才會有一個進入TMRx,意即 TMRx 數到 1000 時,實際的 Clock 已經產生了 8000 個。
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

Timer0 複習

  • Timer0 可設定為 8-bit 或 16-bit 模式。
  • Clock 來源可以選擇內部或外部來源。
  • 有一個 8-bit Prescaler (預除器)
  • 產生 Overflow 時 FFh to 00h (或 FFFFh to 0000h 也就是計數器滿的時候),即產生中斷。
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Timer1 & Timer3

  • 16-bit 模式的計數器或計時器。
  • 有兩個可讀/寫的 8-bit 暫存器串聯而成(使用 TMR1H:TMR1L)。
  • Clock來源可以選擇外部或內部來源。
  • 預除器有四種選擇: 1:1、1:2、1:4、1:8
  • 當計數器或計時器產生溢位時 FFFFh to 0000h,即產生中斷。
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

每產生4個clock才會收到一次訊號,記得除以4

Registers Associated with TIMER1

  • TMR1L,TMR1H:目前 Timer 數到多少的暫存器,FFFFh to 0000h 發生 overflow 時即產生中斷。
  • PIR1:(TMR1IF) TMR1 Overflow Interrupt Flag bit
  • PIE1:(TMR1IE) TMR1 Overflow Interrupt Enable bit
  • IPR1:(TMR1IP) TMR Overflow Interrupt Priority bit
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

How to get 0.5 second in Timer1

除頻器若選用 1:2 (In internal clock, 1 sec = 250000/2 = 125000)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Postcaler (後除器)

  • 當你用上預除器之後計數範圍還是不夠的時候,就需要後除器。
  • 與預除器效果很像,差別在後除器會在 Timer 溢位的時候不直接觸發 Flag bit,如果是 1:4 的 Postscaler 的話就會溢位 4 次才觸發一次 Flag。

Timer2:

  • 8-bit 模式的計時器,有預除器及後除器之功能。
  • TMR2 會自動加一並與設定的值(PR2)相比;若相等則送出訊號至後除器或產生中斷,並自動將自己清除為零,重新計時。

How to get 0.5 second in Timer2

postscaler 和 prescaler 都選 1:16(In internal clock,1 sec = 62500/16/16 = 244)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →