30
Lecture 3 EN 318 1 EN 318

Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

Lecture 3

EN 318

1

EN 318

Page 2: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

2

Page 3: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

Status Register

• The STATUS register contains

– Arithmetic status of the ALU

– RESET status

– Bank select bit for data memory.

• As with any register, the STATUS register can

3

• As with any register, the STATUS register can

be the destination for any instruction.

Page 4: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

4

Page 5: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

Status Register

• Z, DC, C, TO and PD bits are not writable.

– These bits are set or cleared according to device logic.

• For example, CLRF STATUS will clear the upper-three bits and set the Z bit. This leaves the

5

upper-three bits and set the Z bit. This leaves the STATUS register as 000u u1uu (where u = unchanged).

• Only the BCF, BSF, SWAPF and MOVWF instructions should be used to alter the STATUS register because these instructions do not affect any status bit.

Page 6: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

PIC Instruction Set

• The instruction set for the PIC16F84

consists of 35 single word instructions

• These are grouped into three major

categories

6

categories

– Byte-oriented

– Bit-oriented

– Literal and control

Page 7: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

7

Page 8: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

List and Equates

• Before writing your program, you need to describe the

type of device that the program is to be burned to

List p=16F84

• Equate

– Similar to variable declaration in other programming

8

languages.

– Labels are assigned to addresses. Later, whenever that label is

referred to in the program, the compiler looks up its address.

• E.g. Assigns ‘portB’ to the file register located at 0x06.

Port B is always located at this file register.

portB equ 0x06

Page 9: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

Sample Program

LIST P=16F84

PORTB equ 0x06

TRISB equ 0x06

STATUS equ 0x03

RP0 equ 0x05

org 0

movlw 0x0FD

9

movlw 0x0FD

movwf PORTB

bsf STATUS, RP0

bcf TRISB 0x080, 1

bcf TRISB 0x080, 2

movlw 0x07F

Page 10: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

• Each instruction is a 14-bit word divided into:

• An OPCODE which specifies the instruction type

• One or more operands which further specify the operation of the

instruction.

10

Page 11: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

Field Description

11

Page 12: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

CLRF

12

Page 13: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

CLRW

13

Page 14: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

ADDLW

14

Page 15: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

ADDWF

15

Page 16: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

MOVLW

16

Page 17: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

MOVF

17

Page 18: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

MOVWF

18

Page 19: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

Exercise

• Write a program to clear W and Port B, then add

0x0A to 0x0B and send it out thru Port B.

19

Page 20: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

CALL

20

Page 21: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

GOTO

21

Page 22: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

Example for CALL and GOTOlist p=16F84

goto Main

; Subroutine

Delay

movlw 0xff

movwf 0x0c

L1 nop

decfsz 0x0c,1

goto L1

22

goto L1

return

; Main program

Main

movlw 0x0

tris porta

tris portb

movlw 0xff

movwf portb

call Delay

Page 23: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

Exercise

• Write a subroutine to clear PortB and send data

in W to PortB

• Write a program to add 0xA to 0xB and send it to

PortB (use the subroutine)

23

Page 24: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

INCF

24

Page 25: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

DECF

25

Page 26: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

INCFSZ

26

Page 27: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

DECFSZ

27

Page 28: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

NOP

28

Page 29: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

Exercise

• Write a subroutine to delay for 1 msec, given that

your input clock is 4 MHz

29

Page 30: Lecture 3 EN 318 - KDU University Collegekcn.kdupg.edu.my/webpage/course/NU-BEEE/EN318... · • Z, DC, C, TO and PD bits are not writable. –These bits are set or cleared according

• Loops & Timing analysis

• Subroutine

30