There are three levels of PIC 8-bit microcontrollers, each with a corresponding instruction set. The basic PIC series chip has 33 instructions, each with a 12-bit word length; the intermediate PIC series chip has 35 instructions, each with a 14-bit word length; the advanced PIC series chip has 58 instructions, each with a 16-bit word length. Its instructions are backward compatible.
1. PIC assembly language instruction format
The assembly language instructions of the PIC series microcontrollers are the same as the assembly language of the MCS-51 series microcontrollers. Each assembly language instruction consists of four parts, and its writing format is as follows:
Label opcode mnemonic operand1, operand2; comment
The instruction format is described as follows: The four parts of the instruction are separated by spaces, which can be one or more spaces, to ensure that the PC can recognize the instructions during cross-assembly.
1. Labels have the same function as MCS-51 series microcontrollers. Labels represent the symbolic address of instructions. When the program is assembled, the specific value of the instruction memory address has been assigned. The symbolic address (i.e. label) used in assembly language is convenient for viewing and modification, especially for the representation of instruction transfer addresses. Labels are optional in the instruction format and are only required when referenced by other statements. In the absence of a label, one or more spaces must be reserved before the instruction mnemonic before writing the instruction mnemonic. The instruction mnemonic cannot occupy the position of a label, otherwise the mnemonic will be mishandled as a label by the assembler.
When writing a label, the first character must be a letter or a half-width underscore "—", which can be followed by English and numeric characters, colons (:), and symbols, and can be combined in any way. In addition, labels cannot be represented by opcode mnemonics and register codes. Labels can also occupy a single line.
2. Operation code mnemonic This field is a mandatory item for instructions. It can be an instruction mnemonic, or it can be composed of pseudo instructions and macro commands. Its function is to compare the "instruction operation code mnemonic" with the "operation code table" one by one during cross assembly to find out the corresponding machine code and generate them one by one.
3. Operands consist of the data value of the operand or the data or address value represented by symbols. If there are two operands, the two operands are separated by a comma (,). When the operand is a constant, the constant can be binary, octal, decimal or hexadecimal. It can also be a defined label, string and ASCII code. When it is specifically expressed, it is stipulated that the letter "B" is prefixed before the binary number, such as B10011100; the letter "O" is prefixed before the octal number, such as O257; the letter "D" is prefixed before the decimal number, such as D122; and the letter "H" is prefixed before the hexadecimal number, such as H2F. Here, the default system of the PIC 8-bit microcontroller is hexadecimal. Add Ox before the hexadecimal number, such as H2F can be written as Ox2F.
The operands of the instruction are also optional.
The PIC series, like the MCS-51 series 8-bit microcontrollers, has an addressing method, that is, the source or destination of the operand. Because the PIC series microcontrollers use a reduced instruction set (RISC) architecture, their addressing methods and instructions are few and simple. Its addressing methods can be divided into four types according to the source of the operand: immediate addressing, direct addressing, register indirect addressing, and bit addressing. Therefore, the operands in the instructions of the PIC series microcontrollers often have related register symbols. Relevant addressing examples can be found at the end of this article.
4. Comments are used to explain the program and make it easier for people to read the program. Use a semicolon (;) to separate the comment from other parts. When the assembler detects a semicolon, the characters after it are no longer processed. It is worth noting that when using a subroutine, the entry and exit conditions of the program and the functions and effects that the program should complete should be stated.
2. Clear instruction (4 in total)
1. Register clear instruction
Example: CLRW; register W is cleared
Note: This instruction is very simple, where W is the working register of the PIC microcontroller, equivalent to the accumulator A in the MCS-51 series microcontrollers, and CLR is the abbreviation of the English word Clear.
2. Watchdog timer clear instruction.
Example: CLRWDT; Clear the watchdog timer (if assigned, clear the prescaler at the same time)
Note: WDT is the abbreviation of Watchdog Timer. CLR see the above description. Note that these two instructions have no operands.
3. Register f clear instruction. Instruction format: CLRF f
Example: CLRF TMRO; clear TMRO
Note: In the PIC series 8-bit microcontrollers, the symbol F (or f) is often used to represent various registers and the serial address of F. The value of F varies according to different models of the PIC series, generally Ox00~Ox1F/7F/FF. TMRO stands for timer/counter TMRO, so CLRF clears the register and uses direct addressing to directly give the register TMRO to be accessed.
4. Bit clear instruction. Instruction format BCF f, b
Example: BCF REG1, 2; clear the D2 bit of register REG1
Note: BCF is the abbreviation of Bit Clear F. The F in the instruction format is the same as above; the symbol b represents the bit number (or bit address) of an 8-bit data register F in the PIC chip, so the value of b is 0 to 7 or D0 to D7. REG in the example is the abbreviation of Register. The 2 in the example represents b=2 in the instruction format, that is, D2 of register REG1.
Through the above four clear instruction formats and examples, it can be explained that when learning the instructions of the PIC series 8-bit microcontroller, you should first understand the mnemonic meaning (function) of the instruction, and then its expression method. Beginners do not need to memorize the instructions, the important thing is to understand and practice.
3. Instructions for Bytes, Constants, and Control Operations
1. Transfer immediate data to working register W instruction
Instruction format: MOVLW k; k represents a constant, an immediate value, and a label
Note: MOVLW is the abbreviation of Move Literal to w
Example: MOVL 0x1E; constant 30 sent to W
2. I/O port control register TRIS setting instructions [page]
Instruction format;TRIS f
Description: TRIS f is the abbreviation of Load TRIS Register. Its function is to send the content of working register W to I/O port control register f. When W=0, the corresponding I/O port is set as output; W=1, the I/O port is set as input.
Example: MOVLW 0x00; send 00H to W
TRIS RA; Set PIC RA port as output
MOVLW 0xFF ; send FFH to W
TRIS RB; Set PIC RB port as input
Description: These are some commonly used instructions in PIC assembly language, that is, the statements for setting a certain I/O port (here, RA and RB) as input or output. It can be seen that when reading instructions, one should fully understand the function of the statement format and read it in conjunction with the previous and next instructions.
3. Send the contents of register W to register f (the contents of W remain unchanged) instruction
Instruction format: MOVWF f
Note: MOVWF is the abbreviation of Move W to f
Example: MOVLW 0x0B; send 0BH and then send W
MOVWF 6 ; Send W content to RB port
Explanation: The first instruction 0x0B (constant 11) is sent to the working register W. The second instruction sends the constant 11 in W to register F6. Looking up the table, F6 is port RB, so PORT_B (port B) = 0BH = D11
4. Register f transfer instruction
Instruction format: MOVF f, d
Note: MOVF is the abbreviation of Move f. F represents a register in PIC. The d in the instruction specifies: when d=0, the content of f is sent to W; when d=1, the content of f is sent to the register.
Example: MOVF 6,0; send the contents of port RB to W
MOVWF 8; send the contents of port RB to f8
Explanation: The 6 in the first instruction represents register f=6. Looking up the register table, f=6 is port RB; 0 represents d=0, which means the selected target is register W. The 8 in the second instruction represents register f=8. So the result of the two instructions is to send the content of port RB to f8. As for the content of f8, additional instructions should be added at the beginning of the assembly language, which is omitted here.
5. No-operation instructions
Instruction format: NOP
Note: NOP is the abbreviation of No Operation. NOP has no operand, so it is called no operation. Executing the NOP instruction only increases the program counter PC by 1, so it takes one machine cycle.
Example: MOVLW 0xOF ; send OFH to W
MOVWF PORT_B ;W writes the content to port B
NOP ; No operation
MOVF PORT_B, W ; read operation
Note: These three instructions are an example of continuous operation on port B of the I/O port. The purpose is to ensure that there is a stable time between writing and reading when the content written to port B is to be read out. Therefore, the no operation instruction NOP is added.
6. Unconditional jump instruction
Instruction format: GOTO k
Description: When this instruction is executed, it will be transferred to the specified address (jump). The k in the instruction is often associated with the label in the program.
Example: See instruction No. 9
7. The jump instruction that subtracts 1 from the register content and the result is zero
Instruction format: DECFSZ f, d
Description: DECFSZ is the abbreviation of Decrement f, Skip of not 0. The meanings of the symbols f and d have been explained above. This instruction means to decrement the content of the register by 1 and store it in W (d=0) or f (d=1). If the result of the instruction execution is not zero, the instructions are executed in sequence; if it is zero, the next instruction is skipped and then executed (equivalent to executing a null instruction NOP in sequence). In actual instructions, when d=1, this item is often omitted.
8. Add 1 to the register content, the result is a zero jump instruction
Instruction format: INCFSZ f, d
Note: INCFSZ is the abbreviation of Increment f, Skip of 0. The only difference between this instruction and the previous instruction (7) is the "1", that is, when this instruction is executed, the content of register f is increased by 1. If the result is not zero, the instructions are executed sequentially; if it is zero, the execution skips between instructions. The other logical relationships of executing this instruction are the same as the previous one.
9. Subroutine return instruction
Instruction format: RETLW k
Description: RETLW is the abbreviation of Return Literal to W. This instruction represents the return of a subroutine. Before returning, an 8-bit immediate value is sent to W.
Example: A delay subroutine in a PIC assembly language (abstract):
(1) BELY MOVLW 0xC5; send delay constant 0C5H into W
(2) MOVWF COUNT2; 0C5H is sent to counter 2
(3) CLRF COUNT1; clear counter 1
(4) LOOP INCFSZ COUNT1; counter 1 plus 1 counter 1 plus
1 The result is not zero, jump to loop
(5) GOTO LOOP;
(6) DECTSZ CPUNT2; count 2 minus 1 counter 2 minus 1
If the result is not zero, the loop will be jumped again.
Re-execute instruction 4
(7) GOTO LOOP;
(8) RETLW 0; Return after subroutine execution ends
Note: The comments in the program have explained the function of each instruction. Supplementary explanation: 1. When the result of executing the plus instruction (4) is zero, it will jump to the execution of the instruction (6). 2. When the result of executing the minus instruction (6) is zero, it will jump to the subroutine return of the (8) instruction, and the entire delay instruction will be completed. 3. Counter 1 or 2 represents a register in the PIC, which is determined by the pseudo instruction assignment at the beginning of the program (pseudo instructions will be introduced in detail in the future).
The comments about instructions in this article will be slightly different from those in the previous instructions. The previous instructions were commented on to explain the specific functions of the instructions. This comment method is indeed easy for beginners to accept and understand, but in actual applications, the comments of PIC product assembly language are usually based on what the program is going to do (or the role of the instructions) rather than the direct function of the instructions. In view of the above reasons, the following instruction comments will change the previous comment method and use the role that the program should play as annotated.
10. Register half-byte exchange instruction
Instruction format: SWAPF f, d
Explanation: SWAPF is the abbreviation of Swap f. The meanings of symbols f and d are the same as those mentioned above. The function of this instruction is to exchange the upper 4 bits and lower 4 bits of register f, that is, before the instruction is executed, if the 8-bit state of register f is D7, D6, D5, D4, D3, D2, D1, D0, the 8-bit state after execution becomes D3, D2, D1, D0, D7, D6, D5, D4, and the result is stored in W (d=0) or f (d=1).
Example: Interrupt context protection is an important part of interrupt technology. Since there are no PUSH and POP instructions in the PIC16C×× instruction system, it can only be implemented using other instructions. Because the working register W and the status register STATUS are often used in the main program, the interrupt context protection often needs to protect the registers W and STATUS.
The following is an example program for interrupt protection of PIC16C7× series chips.
MOVWF W_TEMP ; store the contents of W into a temporary register
W_TEMP
SWAPF STATUS, W; swap STATUS and W contents
MOVWF STATUS_TEMP ; store the contents of STATUS into the temporary
... in register STATUS_TEMP
Interrupt Service Routine
…
SWAPF STATUS_TEMP, W; swap STATUS_TEMP with W
Contents
MOVWF STATUS ; STATUS is restored to its original state
SWAPF W_TEMP, F; swap content
SWAPF W_TEMP, W ;W restores to the original state
Note: The comments of each instruction in the above program are basically based on the purpose of the program, and the function of each instruction is almost not mentioned. This is something that beginners should pay special attention to.
11. Subroutine Call
Instruction format: CALL k; k is the immediate address
Note: The implementation methods of subroutine calling are different for different chip models. The common point is that the return address ((PC)+1) is first pushed onto the stack for protection, and then the execution is transferred to the entry address of the called subroutine (similar to the MCS-51 instruction function).
Instruction format mode: HERE CALL DELAY; call delay subroutine
…
DELAY MOVLW 0x80 ; Delay subroutine
RETLW 0
Note: Before the call instruction is executed, PC = address HERE
After the call instruction is executed, PC = address DELAY (label), and stack pointer TOS = HERE + 1 (return address).
Example: See the next instruction for an example
12. Register content inversion instruction
Instruction format: COMF f, d
Note: COMF is the abbreviation of Complement f. When d=1, operation (f)→f; when d=0, operation (f)→w.
Function: Invert the contents of register f and send them to W (d=0) or f itself (d=1).
Example: ORG 0x1FF
GOTO MAIN
ORG 0
DELAY …
MAIN MOVLW 0 ; Main program starts
TRTS 5; Set RA port to output
BCF 5, 0; Set RA port 0 to 0
LOOP CALL DELAY; Flashing delay
COMF 5 ; RA port negation (on-off-
Light... Control)
GOTO LOOP ; loop
…
Note: The above instruction is a part of the PIC16C54 LED light control experiment program. The delay subroutine DELY is not listed, but it does not affect the reading of this instruction. The three instructions at the beginning of the main program in the program have been introduced. The CALL instruction that follows is to call the execution subroutine, and its entry address is the label DELAY. After the subroutine is executed, the LED light on-off...on-off...control instruction of COMF 5 is executed again. The following GOTO LOOP instruction is to achieve the purpose of LED cyclic lighting.
13. Bit-oriented operation instructions (4 in total, one more for PIC advanced products)
In addition to one bit clear instruction, this type of instruction also has an instruction to set register f bit b to 1 and two other bit skip instructions (PIC advanced products have an additional instruction to trigger a conversion of f bit b).
(1) Position 1 instruction. Instruction format BSF f, b
Note: BSF is the abbreviation of Bit Set f. The meanings of F and b are the same as above. The function of this instruction is to set the b bit of register f to 1.
(2) Bit test, jump instruction if it is zero. Instruction format BTFSC f, b
Description: BTFSC is the abbreviation of Bit Test, Skip if Clear. The instruction function is to test the register f bit "b", if it is 0, skip the next instruction; if it is 1, execute sequentially, that is, when f(b)=0, do not execute the current instruction but execute the next instruction (interval jump), that is, replace it with an empty instruction NOP, so this instruction takes 2 instruction cycles.
(3) Bit test, jump instruction. Instruction format BTFSS f, b
Note: BTFSS is the abbreviation of Bit Test, Skip if Set. The logical function of this instruction is opposite to the previous one. If the bit test f(b)=1, the execution will skip, and if f(b)=0, the execution will be sequential.
The PIC 8-bit microcontroller assembly language instructions introduced above are only part of the instructions. In addition, there are also circular left and right shift instructions; W and register f "addition", "and" instructions and instructions to enter sleep mode. Due to the limitation of newspaper layout, I will not introduce them one by one here. I will make additional explanations in the application test of the program in the future.
Knowledge contest questions:
13. Please add comments to the following program. The "5" in the program represents the RA (F5) port; the "6" represents the RB (F6) port.
CLRW ;MOVLW OFFH
TRIS 5 TRIS 6
BCF 5,1 BCF 5.0
BSF 5,0 ... right row to left
Previous article:Solution for automobile reversing obstacle detection system
Next article:On the Problem of Interrupt Context Protection of PIC Microcontroller
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- A set of common codes suitable for small projects
- The universal tool for impedance measurement
- Advice for beginners of DLP4500 light modulation.
- RC parallel circuit bandwidth problem - 3db bandwidth position problem, please help
- EEWORLD University - In-depth technical training on battery management
- 2-way ADC0808 measurement digital tube display assembly program
- CY8CKIT-149 PSoC 4100S Review (1)
- STM32F765@216MHz running NES emulator [code] [video]
- Which expert can help me solve the volume control problem of the digital-analog chip WM8766G?
- Warm congratulations to the Chinese men's football team for drawing with the powerful Philippines