340 likes | 511 Views
Lecture – 4 PIC18 Family Instruction Set. Outline . Literal instructions. Bit-oriented instructions. Byte- oriented instructions. Program control instructions. . Literal instructions (1).
E N D
Outline • Literal instructions. • Bit-oriented instructions. • Byte-oriented instructions. • Program control instructions.
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).
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.
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
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.
Example 2 • Suppose a situation requires that the rightmost two bits (bits 0 and 1) of WREG must be cleared (1) and that bits 6 and 7 need to be set (1). ________________________________________
Example 3 • Suppose a situation requires that the leftmost three bits of the W register to be inverted. _________________________
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.
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.
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).
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 BSF. • 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.
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.
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.
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.
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.
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).
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.
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.
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. _______________________________
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.
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.
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.
Questions • Select an option that makes the statement TRUE: - This is a literal instruction: • ADDLW. • BSF. • MOVWF. • ADDWF. • Select an option that makes the statement NOT TRUE: - This is a program control instruction: • BZ. • GOTO. • MOVWF. • BC.
Questions 3.Explain what each of the following literal instructions accomplish: a. MOVLW 0x6A b. XORLW 4 c. MULLW .10 d. MOVLB 2 ____________________________ • moves a 0x6A into WREG • inverts bit 2 of WREG, • multiplies WREG by 10 decimal • selects bank 2 by loading BSR with a 2
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 0x0F ;place 0x0F into W ANDLW 0xFC ;clear bits 0 and 1 of W IORLW 0x80 ;set bit 7 of W XORLW 0x20 ;invert bit 5 of W
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 STATUS, C ;skip next instruction if C = 0 ADDLW 0x01 ;add 1 if carry = 1
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 __________________________ • copies WREG into access bank memory location 0x010, • adds WREG to location 0x20 in the memory bank selected by BSR with the result in WREG • adds WREG to location 0x20 in the memory bank selected by BSR and store the result in the memory bank • subtract 1 from access bank location 0x34 in the memory bank specified by BSR • add one to the location 0x34 in the memory bank specified by BSR
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 Num BCF STATUS, 0 RLCF WREG BCF STATUS, 0 RLCF WREG BCF STATUS, 0 RLCF WREG ADDWF Num, 0 ADDWF Num, 0