2020 Fall: Timer(E230900)單晶片系統設計與應用
Arduino講義:Timer 的使用
ATmega328p 微控制器晶片
ATmega328p是Atmel發展的megaAVR產品線其中一個單晶片產品,也是目前Arduino Uno系列等常使用的8 bits AVR架構微控制器核心。(P代表Picopower,意思是製程更好更省電)
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 →
Paremeter |
Value |
MCU |
8-bit AVR |
Performance |
20 MIPS at 20 MHz |
Flash memory |
32 KB |
SRAM |
2KB |
EEPROM |
1KB |
Clock speed |
16MHz |
External Interrupt |
2 |
Timer |
3 |
Digital I/O |
14 (6 for PWM) |
Analog I/O |
6 |
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 →
更多資訊可以參考ATmega328p的datasheet
ATmega328 微控制器記憶體類型與容量
名稱 |
類型 |
容量大小 |
用途 |
SRAM |
揮發性(volatile),資料會在斷電後消失 |
2048 byte(2KB) |
資料記憶體、暫存程式運作中所需的資料 |
Flash |
非揮發性,資料會在斷電後繼續保存 |
32468bytes(32KB) |
程式記憶體、存放開機啟動程式及使用者自訂的程式碼(開機程式大約佔2KB) |
EEPROM |
非揮發性 |
1024bytes(1KB) |
存放程式的永久性資料 |
暫存器(Register)
I/O Ports
Arduino UNO板所用的晶片ATmega328p有三個 8-bit 的 PORTs :
B: 對應 Arduino 的 digital pin 8 to 13
C: 對應 Arduino 的 analog input pin 0 to 5
D: 對應 Arduino 的 digital pins 0 to 7
- ATmega328p微控制器總共有三組數位輸出入埠,分別為PORTB、PORTC、PORTD。每一組輸出入埠都可作為一般的數位輸入(Digital Input)與數位輸出(Digital Output)使用。
- AVR 晶片每個 port 都受三個暫存器控制,分別是 (x 代表 B, C, D):
- DDRx register(資料方向暫存器)
Data Direction Register,可讀可寫的暫存器,指定輸入/輸出。
- Portx register(輸入/輸出暫存器)
Data Register,透過這類的暫存器設定電壓為高電壓或低電壓。
- PINx register(讀取腳位暫存器)
Port Input Pins(Read Only),用來讀取腳位的輸入訊號。
- 根據datasheet對應Pin 13 的PB5找到所屬之腳位控制暫存器組
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 →
實作LED Blink
- 如下圖所示,在Arduino Uno上Pin 13與ATmega328p的第19腳位相連,並得知其暫存器編號為PB5,且該腳位亦為SPI溝通之SCK腳位。
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 →
* 以下為LED Blink之原始程式碼:
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 →
* 我們可以藉由Arduino UNO與ATmega328p晶片與暫存器對應之關係來修改程式碼,且會像以函式包裝的原始程式碼一樣工作:
- 我們也可以使用 位元遮罩(bit mask) 的方式來描述此段程式碼:(較佳,不會修改其他Bit)
PB5 是 PortB 的 bit 5,所以我們用 (1 << 5) 當作位元遮罩 (bit mask)。
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 →
要特別注意的是,在設定 DDRx 暫存器的時候,1 是代表 OUTPUT,而 0 是代表 INPUT。
中斷服務常式(Interrupt Service Routine,簡稱ISR)
什麼是 Interrupts?
當你在工作的時候,突然電話鈴聲響起,於是你把手邊工作停下來、接電話、講電話,然後回來繼續做剛剛的工作 -- 這就是所謂的中斷 (Interrupt),而電話便是中斷源。
當中斷腳位的訊號發生改變時,將會觸發執行中斷服務常式。ISR是一個函式程式,由微控制器自動觸發執行的函式。
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 →
中斷觸發時機
當擁有外部中斷功能的腳位,其訊號(也就是電壓)改變或處於某訊號時,就會觸發微處理器去執行ISR函式,這個函式我們可以自行定義,再由系統當中斷觸發時自動去執行。
既然觸發的時機是當依據訊號狀態,那麼,就可能會有如下的五種情況:
1.FALLING(當訊號下降時觸發)
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 →
2.RISING(當訊號上升時觸發)
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 →
3.CHANGE(當訊號改變時觸發)
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.LOW(當訊號處在低電位時持續觸發)
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 →
5.HIGH(當訊號處在高電位時持續觸發)
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 →
* 第五種「HIGH」只有Leonardo板子有支援
撰寫ISR程式應注意:
- 程式本體應簡潔
- 中斷處理函式沒有接受參數的輸入,也沒有回傳值
- 在ISR執行期間,所有與時間相關的指令都不會執行,如:millis()、delay()
- 在ISR執行期間的序列阜通訊程式可能會遺失部分資料,如:Serial.print("Hello"),可能只會傳出hell等
- 若ISR程式需要改變某變數值,要在宣告變數前加上"volatile"
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 →
<註>
要使用ISR之前務必要確認:
1.Enable Interrupt
2.對應的Interrupt Mask也有打開(TIMSKx)
volatile 變數宣告
volatile這個變數宣告方式是用於指揮編譯器的指令。
以下面這個程式碼片段為例:

在編譯器將原始碼轉換成機械碼的過程中,會先掃描整個程式,並且將原始碼最佳化來節省時間(此過程在記憶體中進行)。一般的程式中,最佳化的程式碼不會出現問題;然而,若是在處理包含中斷事件的程式,就會有無法預期的錯誤。


Timer
- Timer 是一種計時器,可以用來量時間
- 來自石英振盪器脈衝 (pulse) 每一個 clock 會來一次,Timer 的內容會跟著計數遞增。
- ATmega328p內部共有3種timer,分別是:Timer0, Timer1及Timer2
* 如果是 8-bit Timer,那麼可以寫入的最大數值是 255 (16-bit 的話是 65535),假如超過了最大數值,Timer 就會自動 reset 為 0,這種情況稱為溢位 (overflow)。Timer overflow 的時候可以引發中斷,如果啟用了 Timer overflow 中斷,那麼你就必須在程式裏提供 ISR 處理中斷。
Timer暫存器

暫存器 |
簡寫 |
用途 |
Timer Counter Control Register A |
TCCRnA |
決定Timer的運作模式 |
Timer Counter Control Register B |
TCCRnB |
決定Timer的prescale value |
Timer Counter Register |
TCNTn |
儲存Timer目前的累積計數(計數器) |
Output Compare Register A |
OCRnA |
當TCNTn累積計數到OCRnA的計數時,觸發中斷 |
Output Compare Register B |
OCRnB |
當TCNTn累積計數到OCRnB的計數時,觸發中斷 |
Timer Counter Interrupt Mask Register |
TIMSKn |
開啟/結束中斷 |
- ATmega328p中具有三個比對效果的計時計數器,其中各個Timer可以設定的模式不同。若以Timer1為例,共有三種計時模式:
計時模式 |
功能 |
TIMER1_COMPA |
設定計數參數來觸發 |
TIMER1_OVF |
只要overflow就會觸發 |
TIMER1_CAPT |
儲存輸入腳位的觸發瞬間時間 |
 |
|
//把timer的Channel當作input,用來捕捉外部訊號頻率 |
|
- 下圖為500ms計數器的例子,在 CTC(Clear Timer on Compare Match Mode) 模式下,當TCNT1計數器數到一定數值時觸發事件,執行事件為重置為0、反轉LED。

計算500ms 的計數器和除頻數
- 不同的單晶片有不同的MCU時脈,實際上的運用不一定需要這麼高的時脈,這時候便需要進行「除頻」的動作,將MCU的時脈除以除率(prescaler),藉此將Timer的時脈降低。
- Prescaler (預除器) 是一個用來提供 clock 給 Timer 的電路。MCU clock 頻率通常是 1 MHz, 8 MHz, 16 MHz,而 Precaler 的用途則是除頻。
- ATmega328中Timer0與Timer1使用的是相同的一組prescaler(1, 8, 64, 256, 1024),而Timer2則是有較多的prescaler選項(1, 8, 32, 64, 128, 256, 1024)。
- 根據datasheet,找出需要的除頻晶振頻率
- ATmega328p內部晶振頻率為16Mhz,其週期為:1 / 16Mhz(s)
- 500ms = 0.5s,故暫存器應計數0.5/(1/16M) = 8,000,000次
- 而暫存器最大長度僅有16bits(65535),因此需要除頻晶振頻率
- 查詢ATmega328p中timer1可用的除頻範圍及暫存器(TCCR1B)設定:

- 此處選擇256除頻16MHz時脈,得到8,000,000/256 = 31250,代表暫存器可以在256個時脈之後計數一次
- 最後我們可以通過使用中斷模式來使用500ms定時器做特定的工作
- 其餘Timer 計時的使用方式以此類推
範例
LAB 1 運用Timer1
- 實作目的: 學習使用Timer1 暫存器 及 I/O暫存器操作。
- 內容要求:使用Timer1暫存器來改變LED閃爍頻率,禁止使用delay()函數,取而代之請使用ISR()函數來呼叫對應之中斷向量。請利用如下所示之範例及其提示(TODO:)進行修改:
LAB 2 計時器
使用Timer1暫存器設計方法,完成能夠顯示00(hr):00(min):00(sec):00(10ms) 之簡易計時器在LCD螢幕上。
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 →
LAB 3 超聲波測距
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 →
Bonus1: 利用Timer.h
TIMER函式庫
運用timer函式庫,實作LAB3:超聲波測距
函式庫寫法請參考:Arduino Timer Library
Arduino 睡眠模式與看門狗計時器
- Arduino和我們的電腦手機一樣具有睡眠、休眠或是待機的模式。
- 在睡眠模式下系統完全停止運作,僅保留基本偵測功能
- 睡眠模式是開發人員節省電力的一種方法,ATmega328p微控器具有六種睡眠模式如下表,所用的指令須先下載Enerlib。消耗電流指的是ATmega328控制器本身,而非整個Arduino板。

- Arduino內部除了MCU之外,還有記憶體、ADC、序列通訊等模組。在越省電的模式下,在運行的模組就越少。
- 以POWER-DOWN來說,僅剩下外部中斷及看門狗計時器有在運作。
看門狗計時器(Watchdog Timer, WDT)
- 看門狗計時器是微控器內部的「當機」監控器,若微控器當掉了,它會自動重新啟動微控器。
- 看門狗是一種專為系統復位而設計的方法。
- 運作原理:在看門狗內部有一個計時器,微處理機必須每隔一段時間就像看門狗發出訊號,來重設計時器的值。倘若看門狗遲遲沒收到微處理機的訊號,計時器仍會持續倒數,直至計數器變成0時,就會認定微處理機已經當機,此時會重新啟動。
- WDT內部有一個除頻器,我們可以設定WDT的值,最長可至8秒,最短則為16ms。
* WDT內部有三種操作模式
- 中斷- 當WDT超時時,將調用WDT_vect中斷向量。可用於喚醒微睡眠模式,包括最低功耗睡眠模式(SLEEP_MODE_PWR_DOWN),其他定時器不可用。
- 系統復位-將發生看門狗超時復位,即微控制器將重新啟動。
- 中斷及系統復位- 首先將調用WDT_vect中斷向量,完成後將發生看門狗超時復位。
範例程式
- 啟動時,每隔0.5秒點、滅三次位於第13腳的LED。
- LED閃爍完畢後,進入“Power-down(斷電)”睡眠模式。
- 當中斷0(第2腳)的訊號改變時,喚醒Arduino,再次閃爍LED三次,接著再進入睡眠模式。
- PS: 請先把Arduino的數位腳2接高電位(5V或3.3V)
Bonus2:睡眠喚醒
- 使用WDT的中斷模式,以切換LED的方式,每隔4秒將Arduino從睡眠狀態喚醒,在這邊會使用到power.h及sleep.h裡的函式。
課後習題
- Arduino上的ATmega328p所採用的是AVR架構;之後課程用到的STM32則是使用ARM架構,比較兩種架構的差別。
- 分別描述在ATmega328p晶片上的PWM模式:Normal Mode, CTC Mode, Fast PWM Mode,及 Phase Correct PWM Mode。(Hint:參考datasheet 17.4)
Datasheet中有些暫存器命名方式可以透過所提供的硬體方塊圖來了解其操作原理,當不了解該暫存器的文字說明時,可以利用這些方塊圖來嘗試理解該暫存器運作原理。
