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 กำรใช้งำนยำกขึ ้ สำหรับ1,000+ ผูพั ฒนำ ้ Instructions* 35 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 0x9 ; 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) Carry Bit = จะรับบิตที่เกินออก มาทางซ้ายของ W register 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