I have been using ST single-chip microcomputers since my work. Although I have a general understanding of other single-chip microcomputers, I have never looked at it seriously. I have just been fine in the past few days. I decided to familiarize myself with PIC single-chip microcomputers. Give it a common reference for the same beginners.

I have been using ST single-chip microcomputers since my work. Although I have a general understanding of other single-chip microcomputers, I have never looked at it seriously. I have just been fine in the past few days. I decided to familiarize myself with PIC single-chip microcomputers. Give it a common reference for the same beginners.

PIC instruction system

There are three levels of PIC 8-bit microcontrollers with corresponding instruction sets. Basic-level PIC series chips have 33 instructions, each with a 12-bit word length; intermediate PIC series chips have 35 instructions, each with a 14-bit word length; advanced PIC series chips have a total of 58 instructions, each instruction is 16-bit word length. Its instructions are downward compatible.

Usage analysis of various assembly language instructions of PIC microcontroller

1. PIC assembly language instruction format

The assembly language instructions of PIC series microcontrollers are the same as the assembly language of MCS-51 series single-chip microcomputers. Each assembly language instruction consists of 4 parts, and its writing format is as follows:

Labeled opcode mnemonic operand 1, operand 2; comment

The instruction format is described as follows: Spaces are used as separators between the 4 parts of the instructions, and the spaces can be 1 or multiple to ensure that the PC can recognize the instructions during cross assembly.

1 The label has the same function as the MCS-51 series single-chip microcomputer, and the label represents the symbolic address of the instruction. When the program is assembled, the specific value of the instruction memory address has been assigned. The symbolic address (namely label) used in assembly language is easy to view and modify, especially to express the instruction transfer address. The label is an optional item in the instruction format, and the label only needs to be assigned when it is quoted by other statements. In the case of no label, one or more spaces must be reserved before the instruction mnemonic before writing the instruction mnemonic. The instruction mnemonic can not occupy the position of the label, otherwise the mnemonic will be mishandled as a label by the assembler.

When writing a label, it is stipulated that the first character must be a letter or a half-width underscore “-“, which can be followed by English and numeric characters, a colon (:) tab table, etc., and can be combined arbitrarily. There are labels that cannot be represented by opcode mnemonics and register codes. The label can also be on its own line.

2 Opcode mnemonic This field is a mandatory option for the instruction. This item can be an instruction mnemonic, or it can be composed of a pseudo-instruction and a macro-command. Its function is to compare the “instruction opcode mnemonic” with the “opcode table” one by one during cross assembly to find out the corresponding One generation of machine code.

3 The operand is composed of the data value of the operand or the data or address value represented by symbols. If there are two operands, use a comma (,) to separate the two operands. When the operand is a constant, the constant can be a binary, octal, decimal, or hexadecimal number. It can also be defined labels, character strings, and ASCⅡ codes. For specific representation, it is stipulated that the binary number is prefixed with the letter “B”, such as B10011100; the octal number is prefixed with the letter “O”, such as O257; the decimal number is prefixed with the letter “D”, such as D122; hexadecimal number Preceded by “H”, such as H2F. Here, the default hexadecimal system of PIC 8-bit microcontroller is hexadecimal. Add Ox before the hexadecimal number. For example, H2F can be written as Ox2F.

The operand of the instruction is also optional.

The PIC series, like the MCS-51 series of 8-bit microcontrollers, has an addressing method, that is, the source or destination of the operand. Because the PIC series microcontroller adopts the reduced instruction set (RISC) structure system, its addressing modes and instructions are both few and simple. Its addressing mode can be divided into four types: immediate addressing, direct addressing, register indirect addressing and bit addressing according to the source of the operand. Therefore, the operands in the instructions of the PIC series microcontrollers often appear in related register symbols. Relevant addressing examples can be found later in this article.

4 Comments are used to explain the program so that people can read the program easily. Use a semicolon (;) to separate the comment from other parts before the beginning of the comment. When the assembler detects a semicolon, the characters after it will not be processed. It is worth noting: when using subroutines, the entry conditions and exit conditions of the procedure should be explained, as well as the functions and effects that the procedure should complete.

2. Clear instructions (4 in total)

1 register clear instruction

Example: CLRW; register W is cleared

Explanation: This instruction is very simple. W is the working register of the PIC microcontroller, which is equivalent to the accumulator A in the MCS-51 series microcontroller. CLR is the abbreviation of English 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 in English. CLR see above description. Note that these two instructions have no operands.

3 Register f clear instruction. Command format: CLRF f

Example: CLRF TMRO; clear TMRO

Note: In the PIC series of 8-bit microcontrollers, the commonly used symbol F (or f) represents the serial number address of various registers and F on the chip. The value of F varies with 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 English Bit Clear F. The F in the instruction format is the same as the above description; 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. In the example, REG is the abbreviation of Register. The 2 in the example represents b=2 in the instruction format, that is, the D2 bit of the 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 microcontrollers, you should first understand the mnemonic meaning (function) of the instruction, and then there is its expression. Beginners do not need to memorize instructions by rote, it is important to understand and practice.

3. Instructions for byte, constant and control operation

1 Send the immediate data to the working register W command

Instruction format: MOVLW k; k represents constants, immediate numbers and labels

Description: MOVLW is the abbreviation of Move Literal to w

Example: MOVL 0x1E; constant 30 to send W

2I/O port control register TRIS setting instruction

Instruction format; TRIS f

Description; TRIS f is the abbreviation of Load TRIS Register. Its function is to send the content of the working register W to the I/O port control register f. When W=0, set the corresponding I/O port as output; W=1, set the I/O port 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 the PIC RB port as input

Description: These are several instructions commonly used in PIC assembly language, that is, a statement to set a certain I/O port (here, RA port and RB port) as input or output. It can be seen that when reading the instructions, one should fully understand the function of the sentence format, and the other should be read in conjunction with each other before and after.

3W register content send register f (W content remains unchanged) instruction

Command format: MOVWF f

Note: MOVWF is the abbreviation of Move W to f

Example: MOVLW 0x0B; send 0BH to send W

MOVWF 6; Send W content to RB port

Explanation: The first instruction 0x0B (constant 11) is sent to the working register W, and the second instruction is to send the W content constant 11 to the register F6. The look-up table F6 is the RB port, so PORT_B (B port)=0BH=D11

4 register f transfer instruction

Instruction format: MOVF f, d

Note: MOVF is the abbreviation of Move f. F represents a certain register in PIC. The d in the instruction stipulates: 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; RB port content to send W

MOVWF 8; RB port content to send f8

Explanation: The 6 in the first instruction represents register f=6, and the register table f=6 is RB port; 0 represents d=0, which means that the selected target is register W. The 8 in the second instruction represents the register f=8. So the result of the two instructions is to send the content of the RB port 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 in English. NOP has no operands, so it is called a no-op. Executing the NOP instruction only increases the program counter PC by 1, so it takes up one machine cycle.

Example: MOVLW 0xOF; send OFH to W

MOVWF PORT_B; W content is written into port B

NOP; no operation

MOVF PORT_B, W; read operation

Note: The three instructions are an example of continuous operation on the B port 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 the B port is to be read out, so it is added No operation instruction NOP.

6 Unconditional jump instructions

Command format: GOTO k

Note: When this instruction is executed, the instruction will be transferred to the specified address (jump). The k in the instruction is often associated with the label in the program.

Example: See the 9th instruction

7 The jump instruction in which the contents of the register is subtracted by 1, and the result is zero

Instruction format: DECFSZ f, d

Note: DECFSZ is the abbreviation of Decrement f, Skip of not 0 in English. The meanings of the symbols f and d have been explained above. This instruction means that the contents of the register minus 1 and store it in W (d=0) or f (d=1). If the instruction execution result minus 1 is not zero, the instructions will be executed in sequence; when it is zero, the next instruction will be skipped and then executed (equivalent to execute a null instruction NOP in sequence). In the actual instruction, when d=1, this item Often omitted.

Add 1 to the contents of the 8 register, and the result is a jump instruction between zeros

Instruction format: INCFSZ f, d

Note: INCFSZ is the abbreviation of Increment f and Skip of 0 in English. The difference between this instruction and the previous (7) instruction is only “1”, that is, when this instruction is executed, the content of register f is incremented by 1, and if the result is not zero, the instructions will be executed sequentially; if it is zero, the instructions will skip execution. The other logical relationships for executing this instruction are the same as the previous one.

9 subroutine return instruction

Command format: RETLW k

Note: RETLW is the abbreviation of Return Literal to W. This instruction represents the return of the subroutine, and the 8-bit immediate data is sent to W before returning.

The Links:   2MBI150U4B-120 2SC0435T2A0-17