[TOC] # AVR 指令集 AVR 是一種 8 位元 RISC 微控制器架構,具有高效能和簡單的指令集結構。其指令集設計的目標是使得指令簡單且容易執行,通常每條指令都能在 單一時鐘週期內完成,這使得 AVR 系列微控制器非常快速。 ## The Program and Data Addressing Modes - 在AVR架構下,通用運算暫存器為32個,而 I/O 暫存器的數量通常與每個具體型號的微控制器相關,每個都為8位元。 - **Register Direct** - Single Register Rd ![圖片](https://hackmd.io/_uploads/Hy7B9_Zcyg.png) 操作碼可以直接針對目的暫存器。 - Two Registers, Rd and Rr ![圖片](https://hackmd.io/_uploads/ByXaqdZqkg.png) 操作碼也可以直接操作一個來源暫存器到一個目的暫存器 - **I/O Direct** ![圖片](https://hackmd.io/_uploads/HyxY2OZqJg.png) 操作碼也可以讓暫存器與 I/O 記憶體空間進行直接交互。AVR 微控制器的 I/O 註冊表通常映射到特殊功能寄存器,並且可以像普通暫存器一樣進行讀寫操作。這些 I/O 寄存器通常用來控制外部硬體裝置。 - **Data Direct** ![圖片](https://hackmd.io/_uploads/SkZXJt-cye.png) LDS類型的相關指令可以作為操作碼 針對暫存器載入來自特定內存地址的數據 - **Pointer Register** - **Data Indirect** ![圖片](https://hackmd.io/_uploads/BJKAlK-9kl.png) 可以調用X, Y, Z等指標暫存器 - **Data Indirect with Pre-decrement** ![圖片](https://hackmd.io/_uploads/Sy2GWFb9yg.png) The X,- Y-, or the Z-pointer is decremented before the operation. The operand address is the decremented contentsof the X-, Y-, or the Z-pointer - **Data Indirect with Post-increment** ![圖片](https://hackmd.io/_uploads/HJwQfYZ91g.png) - **Data Indirect with Displacement** ![圖片](https://hackmd.io/_uploads/rkGwMFbckx.png) The operand address is the result of the q displacement contained in the instruction word added to the Y- or, Z-pointer. Rd/Rr specify the destination or source register. 強調指標暫存器可以被靈活的預增加和減少來擴充功能 - **Program Memory Constant Addressing using the LPM, ELPM, and SPM Instructions** 由於程式記憶體的容量通常比資料記憶體大,因此將常數儲存在程式記憶體中可以更有效地利用記憶體空間。 ![圖片](https://hackmd.io/_uploads/HyHD7Fbc1g.png) - LPM (Load Program Memory) ```asm=1 LPM Rd, Z ``` - ELPM (Extended Load Program Memory) ```asm=1 ELPM Rd, Z ``` 當程序內存地址超過 64KB 時,LPM 無法再正常工作,因此使用 ELPM 指令來讀取高位地址的程序內存。 - SPM (Store Program Memory) ```asm=1 SPM Z, Rd ``` - **Direct Program Addressing, JMP and CALL** ![圖片](https://hackmd.io/_uploads/BktnSt-cyg.png) JMP 和 CALL 都是與程式控制流有關的指令,通常用來改變程式執行的順序。這兩條指令分別用於無條件跳轉和函數呼叫,它們在控制程式流程中扮演著重要的角色。 - JMP ```asm=1 start: JMP subroutine ;直接無條件跳轉 subroutine: .... ``` - CALL ```asm=1 start: CALL subroutine ;呼叫 subroutine,並保存返回地址 subroutine: RET ;結束子程式並返回到 CALL 之後的位置 ``` - IJMP ```asm=1 LD Z, address ; Load the address of the subroutine into the Z register ; Use IJMP to jump to the address pointed by Z register IJMP ; Jump to the address contained in Z ``` - ICALL ```asm=1 start: LD Z, subroutine_address ICALL subroutine: RET ;結束子程式並返回到 ICALL 之後的位置 ``` - EIJMP 是 IJMP 的擴展版,主要用於支持超過 64KB 範圍的程式記憶體地址,實現間接跳轉。 - EICALL 是 ICALL 的擴展版,適用於超過 64KB 範圍的函數呼叫,並且會將返回地址壓入堆疊以便後續的 RET 指令使用。 - RJMP and RCALL ![圖片](https://hackmd.io/_uploads/Hyp9qFWcye.png) ## 條件判斷 ![圖片](https://hackmd.io/_uploads/B1vqptb5Je.png) ![圖片](https://hackmd.io/_uploads/HJ8daYZ9Jg.png) ## Instruction Set Summary ### Arithmetic and Logic Instructions ![圖片](https://hackmd.io/_uploads/B1c3yqWq1x.png) ![圖片](https://hackmd.io/_uploads/BkRTJqW91e.png) ### Change of Flow Instructions ![圖片](https://hackmd.io/_uploads/S1xJlqZ5Jg.png) ![圖片](https://hackmd.io/_uploads/BJHlgcZ51x.png) ### Data Transfer Instructions ![圖片](https://hackmd.io/_uploads/r1UWgcZ91g.png) ![圖片](https://hackmd.io/_uploads/S1FGg5-9Jg.png) ![圖片](https://hackmd.io/_uploads/H1C7gcZq1e.png) ### Bit and Bit-Test Instructions ![圖片](https://hackmd.io/_uploads/B134lcbqyl.png) ![圖片](https://hackmd.io/_uploads/H1PHe5b91g.png) ### MCU Control Instructions ![圖片](https://hackmd.io/_uploads/BJnIg9W5kg.png) ## Instruction Description(非必要) ## Appendix A Device Core Overview(非必要) - ATMega168為AVRe+!! ![圖片](https://hackmd.io/_uploads/rJ8bzq-5kl.png) ![圖片](https://hackmd.io/_uploads/HJhLG5W51x.png) ![圖片](https://hackmd.io/_uploads/rJnsMqW5ke.png) # ATMega # LCD ![image](https://hackmd.io/_uploads/rkGQxttqke.png) | 調整 | 50H | 95H |D8H | | -------- |--------|-------- |-------- | | 電壓值(理論)| $1.57\ V$| $2.92\ V$|$4.24\ V$ | | 電壓值(實驗)|$1.502\ V$| $2.79\ V$|$4.04\ V$| | 誤差值 |$-4.3\%$|$-4.45\%$|$-4.72\%$| | 調整 | 50H | 79H |9FH | | -------- |--------|-------- |-------- | | 頻率值(實際)| $12\ KHz$| $8\ KHz$ |$6\ KHz$ | | 猜測的公式 | $12.5 \ KHz$|$8.26 \ KHz$|$6.29\ KHz$| 公式應為 $F=\frac{1000}{Target}\ KHz$ ![image](https://hackmd.io/_uploads/SJf8nhGiyx.png) ![image](https://hackmd.io/_uploads/SJpaThGsJg.png) - denote as 16MIPS ![image](https://hackmd.io/_uploads/H1Vce6zjkx.png) ![image](https://hackmd.io/_uploads/Hk64Zpzsyx.png) ![image](https://hackmd.io/_uploads/SkRDEafokl.png) ![image](https://hackmd.io/_uploads/S1e0XAMjke.png)