lecture – 4 pic18 family instruction set 1. outline literal instructions. bit-oriented...

33
Lecture – 4 PIC18 Family Instruction Set 1

Upload: patience-bell

Post on 02-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

1

Lecture – 4

PIC18 Family Instruction Set

Page 2: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

2

Outline

• Literal instructions.

• Bit-oriented instructions.

• Byte-oriented instructions.

• Program control instructions.

Page 3: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

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).

Page 4: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

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.

Page 5: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

5

Literal instructions (3)

Page 6: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

6

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

Page 7: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

7

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.

Page 8: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

8

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).

________________________________________

Page 9: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

9

Example 3

• Suppose a situation requires that the leftmost three bits of the W register to be inverted.

_________________________

Page 10: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

10

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.

Page 11: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

11

Bit-oriented instructions (2)

Page 12: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

12

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.

Page 13: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

13

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).

Page 14: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

14

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.

Page 15: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

15

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.

Page 16: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

16

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.

Page 17: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

17

Rotates (2)

Page 18: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

18

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.

Page 19: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

19

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.

Page 20: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

20

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).

Page 21: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

21

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.

Page 22: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

22

Program control instructions (2)

Page 23: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

23

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.

Page 24: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

24

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.

_______________________________

Page 25: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

25

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.

Page 26: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

26

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.

Page 27: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

27

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.

Page 28: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

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.

Page 29: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

29

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

Page 30: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

30

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

Page 31: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

31

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

Page 32: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

32

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

(d) subtract 1 from access bank location 0x34 in the memory bank specified by BSR

(e) add one to the location 0x34 in the memory bank specified by BSR

Page 33: Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions

33

Questions

6. Develop a sequence of instructions that multiply the contents of the WREG register by ten. You may not use the multiply instruction.

UDATANum RES 1

MOVWF NumBCF STATUS, 0RLCF WREGBCF STATUS, 0RLCF WREGBCF STATUS, 0RLCF WREGADDWF Num, 0ADDWF Num, 0