PIC Instruction Introduction

Publisher:daasddlaLatest update time:2016-10-31 Source: eefocusKeywords:PIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I have been using ST microcontrollers since I started working. Although I have a general understanding of other microcontrollers, I have never taken a serious look at them. I happened to have nothing to do in recent days, so I decided to get familiar with PIC microcontrollers. So I wanted to transfer what I saw on the Internet or in this book to provide a reference for beginners who are similar to me.
PIC's instruction system
PIC 8-bit microcontrollers have three levels, with corresponding instruction sets. The basic PIC series chip has 33 instructions, each of which is 12 bits long; the intermediate PIC series chip has 35 instructions, each of which is 14 bits long; the advanced PIC series chip has 58 instructions, each of which is 16 bits long. Its instructions are backward compatible.

  1. PIC assembly language instruction format The
  assembly language instructions of PIC series microcontrollers are the same as those of MCS-51 series single-chip microcomputers. Each assembly language instruction consists of 4 parts, and its writing format is as follows:
  label opcode mnemonic operand 1, operand 2; comment
  The instruction format is as follows: The 4 parts of the instruction are separated by spaces, which can be 1 or more spaces to ensure that the PC can recognize the instruction during cross-assembly.
  1. The label has the same function as the MCS-51 series single-chip microcomputer. 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 use of symbolic addresses (i.e. labels) 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 they are referenced by other statements. In the absence of labels, one or more spaces must be reserved before the instruction mnemonic before writing the instruction mnemonic. The instruction mnemonic cannot occupy the position of the 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 (:), symbols, etc., and can be combined arbitrarily. In addition, labels cannot be represented by opcode mnemonics and register codes. Labels can also occupy a single line.
  2. The opcode mnemonic field is a required option for instructions. This item can be an instruction mnemonic, or it can be composed of pseudo-instructions and macro commands. Its function is to compare the "instruction opcode mnemonics" with the "opcode table" one by one during cross-assembly, and find out their corresponding machine codes one by one.
  3. The operand consists 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 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 operand item of the instruction is also optional.
  The PIC series, like the MCS-51 series 8-bit microcontroller, 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 mode and instructions are both few and simple. Its addressing mode 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 microcontroller often have relevant register symbols. Relevant addressing examples can be found later in 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 comments from other parts. When the assembler detects a semicolon, the characters after it will no longer be 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.

  II. Clear instructions (4 in total)
  1. Register clear instruction
  Example: CLRW; register W is cleared
  Description: 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 microcontroller, and CLR is the abbreviation of Clear in English.
  2. Watchdog timer clear instruction.
  Example: CLRWDT; watchdog timer clears (if assigned, clear the prescaler at the same time)
  Description: WDT is the abbreviation of Watchdog Timer in English. CLR sees 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
  Description: In the PIC series 8-bit microcontrollers, the symbol F (or f) is often used to represent various registers and the serial addresses of F in the chip. 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
  Description: BCF is the abbreviation of English 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 a certain 8-bit data register F in the PIC chip, so the value of b is 0~7 or D0~D7. In the example, REG is the abbreviation of Register. The 2 in the example represents the b=2 in the instruction format, i.e., the D2 bit 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, one 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.

  III. Instructions for bytes, constants and control operations

  1. Transfer immediate value to working register W instruction
  Instruction format: MOVLW k; k represents constant, immediate value and label
  Description: MOVLW is the abbreviation of Move Literal to w
  Example: MOVL 0x1E; constant 30 is sent to W

  2. I/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 working register W to 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 PIC RB port as input


  Description: These are some commonly used instructions in PIC assembly language, that is, statements that set a certain I/O port (here RA port and RB port) 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 each other.


  3. Instruction to send the content of W register to register f (the content of W remains unchanged) Instruction
  format: MOVWF f
  Description: MOVWF is the abbreviation of Move W to f
  Example: MOVLW 0x0B; send 0BH to W
     MOVWF 6; send the content of W to RB port
  Description: The first instruction 0x0B (constant 11) is sent to the working register W, and the second instruction sends the constant 11 of the content of W to register F6. Looking up the table, F6 is RB port, so PORT_B (B port) = 0BH = D11


  4. Register f transfer instruction
  Instruction format: MOVF f, d
  Description: MOVF is the abbreviation of Move f. F represents a 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; the content of port RB is sent to W
     MOVWF 8; the content of port RB is sent 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, it should be added at the beginning of the assembly language, which is omitted here.

  5. No operation instruction
  Instruction format: NOP
  Explanation: NOP is the abbreviation of No Operation in English. NOP has no operand, so it is called a 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; write W content to port B
     NOP; no operation
     MOVF PORT_B, W; read operation
  Description: These three instructions are an example of continuous operation on port B of 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, so the no operation instruction NOP is added.

  6. Unconditional jump instruction
  Instruction format: GOTO k
  Description: When executing this instruction, the instruction is transferred to the specified address (jump). The k in the instruction is often associated with the label in the program. Example: See the jump instruction of   7. Register
content minus 1   in instruction 9 , the result is zero   Instruction format: DECFSZ f, d   Description: DECFSZ is the abbreviation of Decrement f, Skip of not 0 in English. The meaning of the symbols f and d has been explained above. This instruction means to decrement the contents 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. Register contents plus 1, skip instruction if the result is zero   Instruction format: INCFSZ f, d   Description: INCFSZ is the abbreviation of Increment f, Skip of 0 in English. The only difference between this instruction and the previous instruction (7) is the "1", that is, when executing this instruction, the contents of register f are incremented by 1. If the result is not zero, the instructions are executed in sequence; if it is zero, the instructions are skipped. 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 the subroutine. Before returning, the 8-bit immediate value is sent to W.











Keywords:PIC Reference address:PIC Instruction Introduction

Previous article:PIC16F97+eV1527 decoding source program
Next article:PIC Getting Started 3, SPI Communication and Serial Port Debugging Experiment

Recommended ReadingLatest update time:2024-11-16 12:27

Programming a clock program with preset start time using C language for PIC microcontroller
  (1) Hardware Circuit      Using the 4-digit LED digital tube display circuit, a clock circuit with a 24-hour or 12-hour cycle display can be made, and the clock can be manually preset at the starting time. That is, after the circuit is plugged in, it can be manually adjusted according to the real time at that time,
[Microcontroller]
Programming a clock program with preset start time using C language for PIC microcontroller
PIC microcontroller software development method
1 Use of action flags In the whole control, there are many combined actions. When all action positioning is controlled by photoelectric switches, there are some problems in programming. For example, if the left hand is required to rise to the fault position, the right hand is required to rise to the hand-raising pos
[Microcontroller]
PIC 18f45k80 microcontroller watchdog program
#pragma config WDTPS = 512 // Watchdog reset time 2.048s void main(void) {     while(1)     {         ClrWdt(); // Feed the dog     } }
[Microcontroller]
51. IO port operation of AVR and PIC
The IO port structures of 51 MCU, AVR MCU and PIC MCU are all different, which leads to different IO port operations. The purpose of operating the MCU IO port is to make the MCU pin output logic level and read the logic level of the MCU pin. Let's take a look at the operation methods of 51 MCU, AVR MCU and PIC MCU IO p
[Microcontroller]
PIC16F877 PORTA Notes
PORTA and TRISA Registers PORTA is an 8-bit wide, bidirectional port. Its corresponding data direction register is TRISA (Register 3-2). Setting a TRISA bit (= 1) configures the corresponding PORTA pin as an input (i.e., disables the output driver). Clearing a TRISA bit (= 0) configures the corresponding PORTA pin as
[Microcontroller]
Analysis of various reset instructions of PIC microcontroller
In this article, the reset and other instructions of the PIC microcontroller will be explained to help everyone better master the programming of the PIC microcontroller. 1. Clear instruction 1. Register clear instruction Example: CLRW; register W is cleared Note: This instruction is very simple, where W is the worki
[Microcontroller]
Analysis of various reset instructions of PIC microcontroller
PIC18F2455/2550/4455/4550 Universal Serial Bus USB
    The PIC18FX455/X550 family of devices contains a full-speed and low-speed compatible USB serial interface engine (Serial Interface Engine, SIE), which allows high-speed communication between any USB host and PIC microcontroller. The SIE can be connected directly to the USB using the internal transceiver or through
[Microcontroller]
PIC18F2455/2550/4455/4550 Universal Serial Bus USB
Considerations for crystal selection in PIC microcontroller applications
For a high-reliability system design, the choice of crystal is very important. In the oscillation circuit, the crystal should not be over-driven (easy to oscillate to high harmonics) nor under-driven (difficult to oscillate). Especially in the design of a system with sleep wake-up (often using low voltage for low powe
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号