Understanding PIC18 Family Instruction Set and Literal Instructions

Slide Note
Embed
Share

Exploring the operation and significance of literal instructions in the PIC18 family's instruction set, including examples of bitwise operations like AND, IOR, and XOR. Learn how these instructions affect the status register and how they can be used to manipulate data effectively in programming with PIC18 microcontrollers.


Uploaded on Jul 22, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Lecture 4 PIC18 Family Instruction Set 1

  2. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions. 2

  3. Literal instructions (1) A literal is a constant such as a number or ASCII character. Most of the literal addressing instructions operate with the working register or WREG. Most literal instructions use the second byte of the 16-bit instruction to hold the literal data. The first three letters of most of the literal instructions indicate the operation performed by the instruction. The CPU is capable of only a few operations: ADD (addition), AND (logical AND), IOR (inclusive OR), MOV (copy), MUL (multiplication), SUB (subtraction), and XOR (exclusive OR). 3

  4. Literal instructions (2) The last two letters of the opcode indicate something about the instruction. For example, the letter L indicates Literal and the letter W indicates the WREG register. The arithmetic and logic instructions are not complete in the PlC microcontroller. A divide instruction is not implemented in the PlC family, but multiplication is implemented. If division is needed in a system, software must be developed that divides. In general, an arithmetic or logic instruction changes the status register bits. The exception is the multiply instruction, which does not affect any of the status register bits. 4

  5. Literal instructions (3) 5

  6. List of literal instructions 6

  7. Example 1 Find the values in the status register after running the following program: _________________________________ N = 1, OV = 1, C = 0, DC = 1, and Z = 0. The result is negative, the result overflow the W register, there was no carry, there was a half-carry or digit carry, and the result is not zero 7

  8. Literal logic instructions The AND operation is often used to selectively clear a single or multiple bits to zero, because when a zero is ANDed with anything the result is zero. The Inclusive-OR (IOR) operation is often used to selectively set a single or multiple bits to one, because when one is Inclusive-ORed with anything the result a one. The Exclusive-OR (XOR) operation selectively inverts a single bit or multiple bits from zero to one or from one to zero, because when a one is Exclusive-ORed with anything the result is inverted. 8

  9. Example 2 Suppose a situation requires that the rightmost two bits (bits 0 and 1) of WREG must be cleared and that bits 6 and 7 need to be set. ________________________________________ 9

  10. Example 3 Suppose a situation requires that the leftmost three bits of the W register to be inverted. _________________________ 10

  11. Bit-oriented instructions (1) The bit operations set, clear, and toggle test only a single bit, where the AND, IOR, and XOR byte-oriented instructions change a single bit or multiple bits. Many applications for the microcontroller require the use of the bit-oriented instructions to control and test individual bits in a program. This is especially true when interfacing and controlling I/O devices. The a-bit position is needed only in the assembler when a numeric register file address is used in an instruction to specify either the access bank or the bank select register. If the instruction refers to a register by its name or the name of a memory location instead of its numeric address, the a-bit is not used in a program. 11

  12. Bit-oriented instructions (2) 12

  13. Example 4 Describe the process of the following program: ________________________ Uses the conditional bit-oriented instructions to clear bit zero of WREG only if bit 7 is zero. 13

  14. Byte-oriented instructions (1) The byte-oriented instructions allow variable data to be used in a program, whereas the literal instructions allow constant data to be used in a program. The byte-oriented instructions typically use the W register and a location in the register file to perform some operation. Most of these instructions have three operands: The first is the register file location, the second determines the destination, and the third selects the access bank or a register file bank as determined by the bank select register (BSR). 14

  15. Byte-oriented instructions (2) If the second operand (d-bit) is a 0, then the destination is the WREG, and if the second operand is a 1, then the destination is the register file location. The d-bit is needed only to place the result of an operation into WREG; the default is d = 1 If the third operand (a-bit) is a 0, then the access bank is used for the register file location and if it is a 1, then the bank determined by the BSR. The a-bit is required only when referencing a file register by number; if referenced by a label, the a-bit is not used in an instruction. The default is a = 1. 15

  16. ALU in PIC 18 16

  17. Example 5 This example illustrates a short program that adds the 16-bit number in access bank locations 0x10 and 0x11 to the 16-bit number in access bank locations 0x12 and 0x13. Both numbers are stored using the little endian format. The result is stored at access bank locations 0x14 and 0x15. 17

  18. Rotates (1) The PIC18 family instruction set has four, byte-oriented rotate instructions. Bytes are rotated right or left either through the carry or without the carry. One method of multiplying or dividing is by rotating a number. If a number is rotated left, with a zero placed into the rightmost bit, the number is multiplied by a factor of 2. Likewise, if a number is rotated right with a zero placed in the leftmost bit, it is divided by two. For a signed division by two, the sign bit is rotated into the leftmost bit of the result. If a number can be multiplied or divided by two it can be multiplied or divided by any number. 18

  19. Rotates (2) 19

  20. Division If a division by a power of 2 is needed, it is accomplished by shifting a number to the right. A shift right is accomplished by first clearing the carry flag followed by a RRCF instruction. The answer is rounded up if a carry is found after the last rotate right. The bit test and skip if zero instruction is used to skip the INCF (increment) if the carry was zero. 20

  21. Example 6 This example multiplies a test number of 4 in WREG by a factor of 5 and leaves the result of 20 in WREG. 21

  22. Example 7 This example divides a test number of a 100 (decimal) by 8 and the result of 12.5 left in WREG as 13 (a rounded-up 12.5). 22

  23. Instructions that affect the status register 23

  24. COMF instruction This instruction complements the contents of the register and places the result in WREG or register. Example : 24

  25. MOVFF instruction This instruction copies data from one register to another register in data memory. The MOVFF instruction allows the programmer to move data within the total space of the data RAM without going through the WREG register. The MOVFF is a 4-byte (32-bit) instruction. Of the 32 bits, the first 16 bits are set aside for the opcode and the address of the source register and the other 16 bits are used for the opcode and the address of the destination. For both the source and the destination parts of the instruction, 12 bits are used for the register address of the PIC 18. 25

  26. Program control instructions (1) The program control instructions modify the flow of a program through conditional branches and function calls. They also provide some control over the operation of a few internal features of the microcontroller. A GOTO is a 32-bit instruction that branches to any program memory location in the program memory. The BRA (branch always) instruction, which also does an unconditional branch, is a 16-bit instruction that has a limited branch range. A BRA instruction contains an 11-bit number stored with it that is not an address, but a distance. 26

  27. Program control instructions (2) 27

  28. Relative branch This distance or displacement, allows the BRA to jump ahead in a program by up to 1,024 bytes from the address of the next instruction in the program or back by up to -1024 bytes from the next instruction in the program. This type of branch is often called a relative branch because the instruction is moved to some other part of memory and the distance of the branch remains the same. All of the conditional jumps use this displacement form of addressing, but have an even shorter range of between +127 or -128 bytes from the next instruction in a program. The GOTO is often called an absolute branch because the address is fixed or absolute. A branch (BRA) or conditional branch is often called a relative branch. 28

  29. GOTO instruction The GOTO is 4 byte (32-bits) instruction. Of the 32-bits, only 12 bits are set aside for the opcode and the rest (20 bits) are used for the target address of the GOTO. However, the 20-bit address gives the programmer only 1M of the address space. This is solved by making the least-significant bit (LSB) of the GOTO instruction 0. Setting the LSB of the target address to zero will make sure that the target address is an even address, because all the instruction are either 2-byte or 4- byte. this also avoid landing in the middle of an instruction. 29

  30. Conditional jumps Instruction BC BNC BZ BNZ BN BNN BOV BNOV Action Branch if C=1 Branch if C=0 Branch if Z=1 Branch if Z=0 Branch if N=1 Branch if N=0 Branch if OV=1 Branch if OV=0 30

  31. Example 8 This example tests access bank register 0x10 for the value of 4. If it is 4, a 6 is placed into 0x10 and if it is not, a 9 is placed into 0x10. _______________________________ 31

  32. CALL instructions Call instruction is used to call a subroutine. Subroutines are often used to perform tasks that need to be performed frequently. In the PIC18 there are two instructions for call: CALL (long call) and RCALL (relative call) 32

  33. CALL instruction (1) In this 4-byte (32-bit) instruction, the 12 bits are used for the opcode and the other 20 bits, A21-A1, are used for the address of the target subroutine. The lowest bit of the program counter is 0 automatically to ensure it lands on even address. Therefore, CALL can be used to call subroutines located anywhere within 2M address space of the program memory. 33

  34. CALL instruction (2) To make sure that the PIC knows where to come back to after execution of the called subroutine, the microcontroller automatically saves on the stack the address of the instruction immediately below the CALL. When a subroutine is called, control is transferred to theat subroutine, and the processor saves the PC of the next instruction on the stack and begin to fetch instructions from the new location. After finishing execution of the subroutine, the instruction RETURN transfers control back to the caller. Every subroutine needs RETURN as the last instruction. 34

  35. RCALL instruction RCALL is a relative call. RCALL is a 2-byte instruction n contrast to CALL, which is 4- bytes. Because RCALL is a 2-byte instruction, the target address of the subroutine must be within 2K because only 11 bits of the 2 bytes are used for the address. There is no difference between RCALL and CALL in terms of saving the program counter on the stack or the function of the RETURN instruction. 35

  36. Summary Literal instructions allow literal or constant data to be used mainly with the WREG. Literal instructions use opcodes such as MOVLW or ADDLW, where the first three letters are the operation and the last two indicate what is operated upon. (L = literal, W = WREG). The ADDLW instruction adds literal data to WREG. For example, the ADDLW 6 instruction adds a 6 to WREG. Other literal instructions are available for subtraction (SUBLW), include- OR (IORLW), exclusive-OR (XORLW), AND (ANDLW), and multiplication (MULLW). There is also a literal return from a procedure (RETLW) that allows a literal to be returned in WREG. 36

  37. Summary Two of the literal instructions do not use WREG. The MOVLB and the LFSR. The bit-oriented instructions allow individual bits to be cleared (BCF), set (BSF), or toggled (BTG). This allows complete control of the state of any bit anywhere in the data memory, including the special function registers. Other bit-oriented instructions are BTFSC and BTFSS, which test a bit and either skip the next instruction if the bit is cleared (BTFSC) or if it is set (BTFSS). There are more byte-oriented instructions than any other type of instruction. All of them work with byte-sized data. The way that the instruction appears indicates the operation of the instruction. 37

  38. Summary For example, an ADDWF adds a byte from the WREG to a register file location or from the register file location to the WREG. The direction of the operation is selectable. The control instructions allow conditions to be tested and then, based on the outcome, can branch to another part of the program. These instruction often implement an if statement. Program constructs are the building blocks of programming. The program constructs most often used are if-then-else, repeat-unti1, while, and function. 38

  39. Questions 1. Select an option that makes the statement TRUE: - This is a literal instruction: a. ADDLW. b. BSF. c. MOVWF. d. ADDWF. 2. Select an option that makes the statement NOT TRUE: - This is a program control instruction: a. BZ. b. GOTO. c. MOVWF. d. BC.

  40. Questions 3. Explain what each of the following literal instructions accomplish: a. MOVLW 0x6A b. XORLW 4 c. MULLW .10 d. MOVLB 2 ____________________________ (a) moves a 0x6A into WREG (b) inverts bit 2 of WREG, (c) multiplies WREG by 10 decimal (d) selects bank 2 by loading BSR with a 2 40

  41. Questions 4. Write a short sequence of instructions that place a 0x0F into WREG and then clear the right most two bits, set the leftmost bit, and invert bit position 5 using only literal instructions to accomplish the task. _________________ MOVLW ANDLW IORLW XORLW 0x0F ;place 0x0F into W 0xFC ;clear bits 0 and 1 of W 0x80 ;set bit 7 of W 0x20 ;invert bit 5 of W 41

  42. Questions 5. Develop a sequence of instructions using only the bit- oriented and literal instructions that will add a 1 to WREG if the carry status register bit is a 1. ______________________ BTFSC ADDLW STATUS, C 0x01 ;skip next instruction if C = 0 ;add 1 if carry = 1 42

  43. Questions 5. Describe what each of the following instructions accomplish: a. MOVFF WREG,0xl0,0,0 b. ADDWF 0x20, 0 c. ADDWF 0x20, 1, 1 d. DECF 0x34 e. INCF 0x34, 1 __________________________ (a) copies WREG into access bank memory location 0x010, (b) adds WREG to location 0x20 in the memory bank selected by BSR with the result in WREG (c) adds WREG to location 0x20 in the memory bank selected by BSR and store the result in the memory bank location (d) subtract 1 from location 0x34 in the memory bank specified by BSR (e) add one to the location 0x34 in the memory bank specified by BSR 43

  44. Questions 6. Develop a sequence of instructions that multiply the contents of the WREG register by ten. You may not use the multiply instruction. UDATA Num RES 1 MOVWF BCF RLCF BCF RLCF BCF RLCF ADDWF ADDWF Num STATUS, 0 WREG STATUS, 0 WREG STATUS, 0 WREG Num, 0 Num, 0 44

Related


More Related Content