pic instruction set and some tips for programming - · pdf file1 eece416: microcomputer...
TRANSCRIPT
![Page 1: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/1.jpg)
1
EECE416: Microcomputer Fundamentals and Design
PIC Instruction Set and Some Tips for Programming
Dr. Charles J. Kim
Howard University
![Page 2: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/2.jpg)
2
F877 Instruction Set14-Bit WordByte-Oriented Instruction
F: File Register (or RAM)D: Destination⌧D=0: Destination W⌧D=1: Destination File Register
Bit-Oriented InstructionB: Bit FieldF: Register File where the Bit is located
Literal and Control OperationK: 8-bit constant
![Page 3: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/3.jpg)
3
General Form of Instruction
![Page 4: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/4.jpg)
4
Instruction List
![Page 5: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/5.jpg)
5
Destination of the result & Machine Code
D=0: Destination WD=1: Destination File Register (Default)
addwf PORTD ; ⌧Add content of PORTD to content of the W and
store the result back into PORTD
addwf PORTD, 0 ;⌧Add content of PORTD to content of the W and
store the result into W
![Page 6: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/6.jpg)
6
Register Addressing ModesImmediate Addressing
⌧(ex) MOVLW 0x0FDirect Addressing
Uses 7 bits of 14 bit instruction to identify a register file address8th and 9th bit comes from RP0 and RP1 bits of STATUS register.Initial condition for RP0 and RP1: RP1=RP0=0(ex) SSPCON EQU 0x14
STATUS EQU 0x03SSPSTAT EQU 0x94BCF STATUS, 0x05BCF SSPCON, 0x01BSF STATUS, 0x05BCF SSPSTAT, 0x02
![Page 7: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/7.jpg)
7
Direct Addressing - Recap
![Page 8: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/8.jpg)
8
Indirect AddressingINDF register
Any instruction using the INDF actually accesses the register pointed to by the File Select Register (FSR).
A 9-bit EA is obtained by concatenating the 8-bit FSRregister and the IRP bit(STATUS<7>)Example: Erase the RAM section of 0x20-0x2F
Movlw 0x20; pointerMovwf FSR
Next clrf INDFincf FSRbtfss FSR, 4goto next
……
![Page 9: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/9.jpg)
9
Direct vs. Indirect Addressing
![Page 10: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/10.jpg)
10
Instruction Sets –description convention
![Page 11: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/11.jpg)
11
addlw
![Page 12: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/12.jpg)
12
addwf
![Page 13: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/13.jpg)
13
andlw
![Page 14: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/14.jpg)
14
andwf
![Page 15: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/15.jpg)
15
Bcf & bsf
![Page 16: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/16.jpg)
16
btfsc
![Page 17: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/17.jpg)
17
Btfss
![Page 18: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/18.jpg)
18
Call
![Page 19: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/19.jpg)
19
CLRF & CLRW
![Page 20: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/20.jpg)
20
COMF & DECF
![Page 21: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/21.jpg)
21
DECFSZ
![Page 22: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/22.jpg)
22
GOTO & INCF
![Page 23: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/23.jpg)
23
INCFSZ
![Page 24: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/24.jpg)
24
IORLW & IORWF
![Page 25: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/25.jpg)
25
MOVLW & MOVF
![Page 26: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/26.jpg)
26
MOVWF & NOP
![Page 27: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/27.jpg)
27
RETFIE & RETLW
![Page 28: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/28.jpg)
28
RETURN
![Page 29: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/29.jpg)
29
RLF & RRF
![Page 30: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/30.jpg)
30
SUBLW & SUBWF
![Page 31: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/31.jpg)
31
SWAPF & XORLW
![Page 32: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/32.jpg)
32
XORWF
![Page 33: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/33.jpg)
33
F877 Instruction – Programming TipsTips on InstructionSkills and TricksFrequently Met Situations
(a) Turn on/off an LEDAn LED is connected to a pin at one, say,
PORTB<0>, and to a ground at the other. Use a bit-oriented file register operation:⌧bsf PORTB, 0x00 ;to turn on and ⌧BSF PORTB, 0 ;same effect⌧bcf PORTB, 0x00 ;to turn off.⌧BCF PORTB, 0 ;same OFF
![Page 34: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/34.jpg)
34
(b)Variable DeclarationIn C:
⌧byte temp
⌧intxIn PIC:⌧Variables must occupy physical RAM space⌧CBLOCK …ENDC pair⌧Example:
Tips – Variable Declaration
![Page 35: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/35.jpg)
35
(c) I/O designation to the I/O PortI/O Ports: PORTA, PORTB, PORTC, PORTD, PORTEBi-directionalInput or OutputI/O selection file register⌧TRISA for PORTA; TRISB for PORTB; TRISC for PORTC⌧TRISD for PORTD; TRISE for PORTE⌧Each pin can be selected as
• 1: Input• 0: Output
Tips – I/O designation
![Page 36: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/36.jpg)
36
(d) Content CheckAssume that an Infrared (IR) receive is connected to a pin of a port and that it reads transmitted IR command sent from an IR remote controller, like our TV or VCR remote.A byte information sent from the remote is just stored to Wregister. Now we want to check the IR command by examining IR command. The IR command, for example, is as follows:
Tips – Content Check
![Page 37: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/37.jpg)
37
andlw 0xFF keeps the content of Wintactandlw 0x00 will clear W, and set the Zandlw 0xFF keeps all the bits of W. Therefore, if the result (by the Z flag) of andlw 0xFF is zero, then we know that the content of W is 0x00 or 00000000b. Further, if the result of andlw 0xFC is zero, then we can see that the content of W is 0x03=00000011b.
Tips – Contents Check (continued)
![Page 38: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/38.jpg)
38
COMreg: a file register where the IR command is initially storedTarget: which button is pressed from a remote controller
Tips – Content Check (Continued)
![Page 39: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/39.jpg)
39
(e) Reading sensor (digital) input and decision-making based on the inputPIR motion detector & buzzer examplePIR output
1: No motion detected0: Motion detected
Buzzer 1: Buzzer On0: Buzzer Off
Use BTFSS or BTFSC
Tips – Reading Sensor Input
![Page 40: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/40.jpg)
40
Sample Code
![Page 41: PIC Instruction Set and Some Tips for Programming - · PDF file1 EECE416: Microcomputer Fundamentals and Design PIC Instruction Set and Some Tips for Programming Dr. Charles J. Kim](https://reader031.vdocument.in/reader031/viewer/2022021816/5a7a1b7f7f8b9a3d058d7ff1/html5/thumbnails/41.jpg)
41
What is this code for?