PIC Code Execution II
http://mango.e-cpe.org
B0
B1
B2
B3
PIC
B4
B5
B6
B7
Memory Mapped I/O (MMIO)
BSF
BCF
06.0
06.0
BSF = Bit Set File, BCF = Bit Clear File
ข้ อดีข้อเสียของ Memory Mapped I/O
MOVWF 06
I/O Operation
MOVWF 21
Mem Operation
ไม่ต้องออกแบบคำสัง่ เฉพำะสำหรับ I/O
ข้ อดีข้อเสียของ Memory Mapped I/O
Memory Mapped
Port-Mapped
With some exceptions
Memory Mapped I/O ใช้ คำสัง่ ชุดเดียวในกำรเข้ ำถึง Memory และอุปกรณ์รอบข้ ำง
ช่วยลดควำมซับซ้ อนของ CPU ทำให้ รำคำถูกลง และ ใช้ งำนได้ ง่ำย
ข้ อดีข้อเสียของ Memory Mapped I/O
128 Bytes
I/O Mapped
240 Bytes
Available RAM
แต่ก็ต้องเสียตำแหน่งใน Memory ไปบำงส่วน
เช่น PIC16F886 จะเสียพื ้นที่ไป 128 Byte จำกทังหมด
้
368 Bytes (~35%)
เพื่อใช้ ทำ Memory Mapped I/O
The 3 Gig RAM Problem
4 GB
3.x GB??
Installed
Usable
32 bit OS
ระบบ 32 บิตควรใช้ งำน RAM ได้ 2^32 = 4GB พอดี แต่ทำไมในควำมเป็ นจริง
กลับใช้ ได้ แค่ 3.x GB
The 3 Gig RAM Problem
Address Space
~1 GB
3 GB
Video Card
BIOS
PCI Bus
Etc.
RAM
(4 GB)
ที่อยูข่ องหน่วยควำมจำส่วนบนถูกเอำไปใช้ สำหรับ I/O
ข้ อดีข้อเสียของ Memory Mapped I/O
MOVWF
06
Slow
I/O Operation
MOVWF
21
Fast
Mem Operation
ถ้ ำ Memory และ I/O ใช้ data bus เดียวกัน อำจทำให้ Memory Access ช้ ำลง
เนื่องจำกต้ องรอคำสัง่ I/O ที่ทำงำนช้ ำกว่ำ
Port Mapped I/O (PMIO)
ใช้ คำสัง่ แยกกันระหว่ำง
Memory Operation กับ Peripheral Operation
ปั จจุบนั CPU ที่ใช้ Port Mapped I/O มักใช้ Memory Mapped I/O
ควบคูก่ นั ไปด้ วย
ข้ อดี/เสียของ Port Mapped I/O
กำรใช้ คำสัง่ เฉพำะทำงทำให้ ประสิทธิภำพโดยรวมดีกว่ำ
แต่ CPU จะมีควำมซับซ้ อนที่สงู ขึ ้นจำกคำสัง่ ที่เพิ่มขึ ้นมำเหล่ำนี ้
กำรใช้ งำนยำกขึ ้นสำหรับผู้พฒ
ั นำ
PIC 16F
Intel x86
35 Instructions
1,000+ Instructions*
* See http://en.wikipedia.org/wiki/X86_instruction_listings
Memory Mapped I/O Case Study
PIC 16F, Tri-state I/O, Memory Organization
Look at the complete ASM code for
output_high(PIN_B0)
....................
00ED: BSF
03.5
00EE: BCF
06.0
00EF: BCF
03.5
00F0: BSF
06.0
output_high(PIN_B0);
Q: Why do we need these commands?
A: Before using a PIN on the PIC, we need to configure it’s direction
Each MCU PIN can be in 3 states (Tri-State)
Output
Mode
Input
Mode
State
Description
High
The pin sources 5V
Low
The pin sinks to GND
High Impedance
The pin is an input
Tri-state PIN configuration
Telling the MCU which mode we want to use an IO pin
0 = Output
1 = Input
So, the code should be like this …
....................
output_high(PIN_B0);
00EE: BCF
86.0 -> Clear bit 0 of TRISB
00F0: BSF
06.0 -> Set bit 0 of PORTB
But why is it like this?
....................
00ED: BSF
03.5
00EE: BCF
06.0
00EF: BCF
03.5
00F0: BSF
06.0
output_high(PIN_B0);
The problem with “BCF 86.0”
0x86 = 1000 0110
The space for the file register address is limited to 7 bits
Status Register (Address 03)
Bit 6,7 ใช้เลือก Register Bank
00 = Bank 0
01 = Bank 1
10 = Bank 2
11 = Bank 3
See section “Register 5-1” in the handout
for details of the Status Register
Bank 0
Bank 1
Bank 2
Bank 3
แบบฝึ กหัด – ทำไฟวิ่งบน PIC Simulator IDE
กำหนดโปรแกรมส่วนต้ นให้ เพื่อ setup ระบบดังนี ้
; set PORTB as output
bsf status,5
bcf status,6
movlw 0
movwf trisb
; disable Analog inputs
bsf status,6
movwf 0x189
; switch to FSR Bank 0
bcf status,5
bcf status,6
bsf portb,0 ; trun on LED on B0
The RLF command
Rotates the bits in a file register through the carry bit
RLF
Example:
MOVLW
MOVWF
RLF
1
0x6 ; RAM value = 0000 00012
0x6,F ; value is now 0000 00102
Status Register (Address 03)
Zero Bit = จะเป็น 1 เมื่อใดก็ตาม
ที่ ALU คานวณค่าออกมาเป็น 0
See section “Register 5-1” in the handout
for details of the Status Register
PIC-C Trick: RAM access
#byte
#bit
b_port = 6
// mem pointer
B0 = b_port.0
b_port = 0xff;
B0 = 1;
// drive port b
// set bit B0 to 1
Methods
Write Machine Code Manually
Write Assembly Code
Use a High-Level Compiler
Writing Machine Code
ENIAC
Benefits of High-Level Compilers
Simple for the programmer
Reduce development time
Allows for the development of larger programs
Easier to port to different hardware systems
Drawbacks of High-Level Compilers
Poor optimization
Non-Optimal Hardware Utilization
Poor
Optimazation
....................
while (1)
....................
Loop:
BSF
03.5
CLRF
06
BCF
03.5
MOVF
21,W
MOVWF 06
GOTO
Loop
output_b(i);
No need to set TRIS bits
every time
Poor Optimization Ex 2
....................
....................
000D: MOVLW 05
000E: BCF
03.5
000F: MOVWF 21
....................
....................
0010: DECF
21,F
....................
0011: MOVF
21,F
0012: BTFSS 03.2
0013: GOTO
010
int i;
i = 5;
do {
i--;
} while (i>0);
DECF already sets the Z bit
Non-Optimal HW Utilization
Blinking LED example
Our code from exercise
BSF
06.0
Loop:
RLF
06
GoTo Loop
Code generated by PIC-C
Loop:
BCF
03.5
MOVF 0x21,W
MOVWF 06
RLF
0x21, F
GOTO Loop
© Copyright 2026 Paperzz