微算機 Chapter 5 PIC18F Architecture and Addressing Modes
Basic Features
- PIC18F 是使用 Harvard architecture ( program 和 data memory 是用不同的 memory space 及 兩個的 bus 是獨立的),所以 PIC18F 可以同時 access program 和 data 。
- PIC18F 使用 flash memory 去存 program memory ;而 RAM 去存 data memory 。
- PIC18F 的 F 表示:這個晶片包含 flash memory 。
- Special Function Registers ( SFRs ),是 CPU 的 registers 和 registers of the peripheral module (外圍模塊的 register {google 翻譯})。
- Typical CPU register :
- Stack Pointer
- STATUS register
- Program Counter
- Typical peripheral registers :
- I/O ports
- Timer register
- ADC register
- CCP register
- 最大的 Program Counter ( PC ) 是 21-bit wide 。所以 PIC18F 就可以最大有 2 MB( )的 program memory space 。
- 最大的 Data memeory address 是 12-bit wide 。所以地址最多只有 4 KB( )的大小。
每一個不同家族的 PIC18F 可能會有不同的 program memory space 和 data memory space 。
-
CCP module ( Capture / Compare / PWM ( pulse width modulation ) ): PIC18F 可以使用 capture module 去計算傳入訊號的週期;用 compare module 去產生週期波或 time delay 。 ( PWM 那邊我看不懂 QQ )
-
10-bit A/D converter :可以把類比的訊號轉換成 10-bit binary 。
-
所有的 PIC18F 都使用 pipelining 去執行指令。 PIC18F 使用 main oscillators ( OSC1 )去同步內部的操作。
-
一個 machine cycle 包含4個 OSC1 的 pulse :
- program counter += 2 會在 Q1 時執行,並該指到的 instruction 在 Q4 時被 fetch ,同時, previous instruction 會在 Q1 ~ Q4 被執行。
- instruction exection :包含3階段, fetch, decode, exection 。 fetch 之後會被存入 Instruction Register ( IR )然後被 decode 再 exection 。
- 目前的 instruction exection 會和下一個 instruction 的 fetch 同時在一個 mechine cycle 中執行 ( two-stage pipeline )。
4.
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 →
PIC18F Register Architecture
SFRs 有包含 BSR ( Band Select Register )
WREG ( Woking register )
- WREG is 8-bit wide.
- 主要做大部份的數值運算及邏輯運算的實現。
SP ( Stack Pointer )
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 →
- 在 PIC18F 裡面被稱為 STKPTR 。
- 有 31 個 stack register。
- 有 8-bit 寬。
- push 時 +1 , pop 時 -1 。
- Bit 5 is not implemented and is read as 0.
- bit 7 是判斷 stack overflow , 用超過31個 resgister 要PUSH的時候會設成1。
- 這裡的 stack 有 underflow 用太多且要POP的時候也是會設成1,
PC ( Program Counter )
- 最大能到 21-bit wide 。
- 通常只向下一個要執行的 instruction 。
- 因為有 21-bit wide 所以可以最多有 2 MB 的 program memory 。
- PC 包含3個 8-bit registers :
- PCL ( PC Low byte): PC 的 0~7 。
- PCLATH ( PC latch High byte ): PC 的 8~15 。
- PCLATU ( PC latch upper 5 bits ): PC 的 16~20 (存在 PCLATU 的低5位)。
Table Pointer
- PIC18F 使用 21-bit Table Pointer register 當作互相複製 program memory 和 data memory 之間的橋樑。
BSR ( Band Select Register )
- BSR is 8-bit wide.
- 低4位為 bank address, range from 0~F.
- 高4位是0。
FSR ( File Select Register)
- 由3個 16-bit registers 所組成( FSR0, FSR1, FSR2 。
- 每個 FSR 的 upper 4 bits 皆為 0 。
- 而 low 12 bits 用來儲存 data memory address 。
- 簡單來說這些 registers 可以理解為 memory pointers 。
- 每一個 FSRx 可被分為 FSRxH 和 FSRxL 。( high byte and low byte )
- 藉由操作 FSR 來對那12 bit 的記憶體位置操作。
PRODH / PRODL
- PRODH and PRODL registers is 8-bit wide.
- PIC18F 內建 8-bit * 8-bit unsigned multiplication instrucction ,這兩個 registers 就是用來儲存運算完的結果的。
SR ( Status 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 →
- SR is 8-bit wide.
- 每一個 bit 代表著不同的 flag :
- C ( Carry Flag ):當加法進位時和減法借位時, carry flag 被設為 1 。
- DC ( Digit Carry Flag ):當加法進位由 low 4 bits 到 high 4 bits 時;減法借位由 high 4 bits 借給 low 4 bits 時, digit carry flag 被設為 1 。
- Z ( Zero Flag ):當任何 instruction 的結果為 0 時, zero flag 被設為 1 。
- OV ( Overflow Flag ):當數值運算 overflow 時, overflow flag 被設為 1 ( )。
- N ( Nagetive flag ) 如果最前面是1 ( 即負數 ) 那 N 設為 1 。 反之
- Bits 5 through 7 are not implement.
Carry 表示進位,當運算前的位數比運算後的位數還小的時候, carry flag 才會被設為 1 。
PIC18F Memory Organization
- PIC18F 使用3種 memories ,分別為 flash memory, SRAM and EEPROM :
- Flash memory is used to store program. ( use as program memory )
- 剩下兩個是用來儲存 data ,但 SRAM 是 volatile read/write memory 而 EEPROM 是 nonvolatile memory 。
- 因為 EEPROM 的讀取速度比其他記憶體快,所以通常用來儲存重要的資料,而且多為 read-mostly memory 。
- EEPROM 是被劃分在 Special Function Registers ( SFR ) 裡面。
- Data 可以以 immediate data 或 address of data 來被 instruction 所使用。
- MOVLW 2 :這個 2 是 immediate data 。
- MOVFF 0x20, WREG :這個會透過 0x20 這個地址去取用裡面的值 , 0x20 就是 address data 。
- PIC18F 的 data memory 是用 static RAM 。每個 register 有 12-bit wide address ,所以共有 4096 bytes of data memory ,而這些 memory space 又被劃分成 16個 bank ,每個包含 256 bytes 。
- Program Counter ( PC )用來儲存要被 fetch and execute 的 instruction 的 address ,最大是 21-bit wide ( range from 0x000000 to 0x1FFFFF )。
- PIC18F 的 data memory 也被稱為 file register 或 data register 。
- data memory 被劃分成 16 banks ,每個 bank 有 256 bytes ,而 data memroy address 的前 4 個 bits 代表 bank number ,而後 4 個 bits 代表 BSR ,中間 4 個 bits 是 0 。
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 →
- The programmer can access a special bank called the Access Bank.
- Access Bank 就 Bank 0 啦。
- 使用 Access Bank 的話就不用透過 BSR ( 12-bit wide address ),這樣執行的速度可以比較快。
- Access RAM Low 包含 GPRs ( General Purpose Registers ); Access RAM High 包含 SFRs 。
- 確切位置可以參考上圖:
- GPRs 在 Bank 0 的 Low 。
- SFRs 在 Bank 15 的 High 。
- 把 memory 切成 16 份的這種技術叫做 RAM banking scheme 。
- 每個 address 可以看成 8-bit low-order address and a 4-bit Bank Pointer 或 full 12-bit address 。
- 如果要切換 Bank ,可以用 MOVLB K ( K is a 8-bit number ), K 的 low 4-bit 會被讀取成 bank number 。
- 如果忘記切換 Bank 的話可能會很危險,所以才會有 Access bank 存在。
PIC18F Addressing Modes
- 有下面幾種:
-
Literal or Immediate Addressing Mode
- operand 是 literal 或 constant data 。
- constant data 不能直接放進去 file registers ( 基本上就只有 WREG 和 BSR 可以)。
- 例如:
MOVLW 0x2A
。
-
Inherent or Implied Addressing Mode
- 這種模式 instructions 不需要 operands ,也被稱為 no-operand instructions 。
- 例如:
DAW
(有 implicit 的使用到 WREG )
-
Direct or Absolute Addressing Mode
- 簡單來說就是由一個 register 搬資料到另一個 register 。
- 例如:
MOVWF 0x50
。
-
Indirect Addressing Mode
- 如果使用到 pointer 去 assess data memory 就會被稱為這種模式。
- 像是使用到 FSR0, FSR1 and FSR2 ( file select register )這種 register 。
- 雖然 FSRx 有 16-bit wide 但只會用到後 12-bit 。
- FSRx 與 INDFx 有關。
- 舉例:
LFSR 0, 0x0010
,這是用來設定 FSR0 裡面的值為 0x0010 。而如果接下來執行 MOVWF INDF0
,會把 WREG 裡面的內容,存到 0x0010 的地址裡面。
- FSR 還有 INC DEC (加在地址上)這幾種東西,記得還有 POST 和 PRE 的差別,去做實驗你就會知道ㄌ。
注意!
- 沒有
PREDECx
。
- 跟 ppt 不一樣,我執行
CLRF POSTINC0
啥都沒有被清掉。
- 對於
PLUSW2
,不論是 WREG 或 FSR2 都不會被改變。
-
Relative Addressing Mode
- 任何有關 branch 的東西屬於這個模式。
- 公式 ( PC + 2 + d * 2)
- 舉例:
BRA 0x50
而在 BRA 這行地址是 0x40 ,在執行到這行時的 PC 為 0x42 ,這時要 branch 到 0x50 此時的 PC 會變成 ( 0x40 + 2 + 0x50 * 2) = 0x4C。
- 阿上面有關 STATUS register 的東西,自己看。
-
Bit Addressing Mode
- 對於單個 bit 的操作都是這種模式。
ex : BCF 0x10 , 3
會清掉 0x10 的第三個 bit。