330 likes | 524 Views
Microprocessor and Interfacing 261214. PIC Code Execution II. http ://mango.e-cpe.org. B0 B1 B2 B3 B4 B5 B6 B7. PIC. Memory Mapped I/O (MMIO). BSF 06.0 BCF 06.0. BSF = Bit Set File, BCF = Bit Clear File. ข้อดีข้อเสียของ Memory Mapped I/O. ไม่ต้องออกแบบคำสั่งเฉพาะสำหรับ I/O.
E N D
Microprocessor and Interfacing261214 PIC Code Execution II http://mango.e-cpe.org
B0 B1 B2 B3 B4 B5 B6 B7 PIC
Memory Mapped I/O (MMIO) BSF 06.0 BCF 06.0 BSF = Bit Set File, BCF = Bit Clear File
ข้อดีข้อเสียของ Memory Mapped I/O ไม่ต้องออกแบบคำสั่งเฉพาะสำหรับ I/O I/O Operation MOVWF 06 Mem Operation MOVWF 21
ข้อดีข้อเสียของ Memory Mapped I/O Memory Mapped I/O ใช้คำสั่งชุดเดียวในการเข้าถึง Memory และอุปกรณ์รอบข้าง ช่วยลดความซับซ้อนของ CPU ทำให้ราคาถูกลง และ ใช้งานได้ง่าย Memory Mapped Port-Mapped With some exceptions
ข้อดีข้อเสียของ Memory Mapped I/O 128 Bytes I/O Mapped แต่ก็ต้องเสียตำแหน่งใน Memory ไปบางส่วน เช่น PIC16F886 จะเสียพื้นที่ไป 128 Byte จากทั้งหมด 368 Bytes (~35%) เพื่อใช้ทำ Memory Mapped I/O 240 Bytes Available RAM
The 3 Gig RAM Problem ระบบ 32 บิตควรใช้งาน RAM ได้ 2^32 = 4GB พอดี แต่ทำไมในความเป็นจริง กลับใช้ได้แค่ 3.x GB 4 GB 3.x GB?? Installed Usable 32 bitOS
The 3 Gig RAM Problem Address Space ที่อยู่ของหน่วยความจำส่วนบนถูกเอาไปใช้สำหรับ I/O ~1 GB 3 GB Video Card BIOS PCI Bus Etc. RAM (4 GB)
ข้อดีข้อเสียของ Memory Mapped I/O ถ้า Memory และ I/O ใช้ data bus เดียวกัน อาจทำให้ Memory Access ช้าลง เนื่องจากต้องรอคำสั่ง I/O ที่ทำงานช้ากว่า Slow I/O Operation MOVWF 06 MOVWF 21 Fast Mem Operation
Port Mapped I/O (PMIO) • ใช้คำสั่งแยกกันระหว่างMemory Operation กับ Peripheral Operation • ปัจจุบัน CPU ที่ใช้ Port Mapped I/O มักใช้ Memory Mapped I/O ควบคู่กันไปด้วย
ข้อดี/เสียของ Port Mapped I/O • การใช้คำสั่งเฉพาะทางทำให้ประสิทธิภาพโดยรวมดีกว่า • แต่ CPU จะมีความซับซ้อนที่สูงขึ้นจากคำสั่งที่เพิ่มขึ้นมาเหล่านี้ • การใช้งานยากขึ้นสำหรับผู้พัฒนา * See http://en.wikipedia.org/wiki/X86_instruction_listings
Memory Mapped I/O Case StudyPIC 16F, Tri-state I/O, Memory Organization
Look at the complete ASM code foroutput_high(PIN_B0) .................... output_high(PIN_B0); 00ED: BSF 03.5 00EE: BCF 06.0 00EF: BCF 03.5 00F0: BSF 06.0 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
Tri-state PIN configurationTelling 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? .................... output_high(PIN_B0); 00ED: BSF 03.5 00EE: BCF 06.0 00EF: BCF 03.5 00F0: BSF 06.0
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 3 Bank 0 Bank 1 Bank 2
แบบฝึกหัด – ทำไฟวิ่งบน PIC Simulator IDE กำหนดโปรแกรมส่วนต้นให้เพื่อ setup ระบบดังนี้ ; set PORTB as output bsfstatus,5 bcfstatus,6 movlw0 movwftrisb ; disable Analog inputs bsf status,6 movwf0x189 ; switch to FSR Bank 0 bcf status,5 bcf status,6 bsf portb,0 ; trun on LED on B0
The RLF commandRotates the bits in a file register through the carry bit RLF Example: MOVLW 1 MOVWF 0x6 ; RAM value = 0000 00012 RLF 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 b_port = 6 // mem pointer #bit B0 = b_port.0 b_port = 0xff; // drive port b B0 = 1; // 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 • Poor optimization • Non-Optimal Hardware Utilization • 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 Optimazation .................... while (1) .................... output_b(i); Loop: BSF 03.5 CLRF 06 BCF 03.5 MOVF 21,W MOVWF 06 GOTO Loop No need to set TRIS bits every time
Poor Optimization Ex 2 .................... inti; .................... i = 5; 000D: MOVLW 05 000E: BCF 03.5 000F: MOVWF 21 .................... do { .................... i--; 0010: DECF 21,F .................... } while (i>0); 0011: MOVF 21,F 0012: BTFSS 03.2 0013: GOTO 010 DECF already sets the Z bit
Non-Optimal HW UtilizationBlinking 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