Lec10 Pic Inst

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

EECE416: Microcomputer Fundamentals and Design

source: www.mwftr.com

PIC Instruction Set and Some Tips for Programming

Dr. Charles J. Kim Howard University


1

F877 Instruction Set


14-Bit Word Byte-Oriented Instruction F: File Register (or RAM) D: Destination D=0: Destination W D=1: Destination File Register Bit-Oriented Instruction B: Bit Field F: Register File where the Bit is located Literal and Control Operation K: 8-bit constant
2

General Form of Instruction

Instruction List

Destination of the result & Machine Code


D=0: Destination D=1: Destination W File Register (Default)

addwf PORTD ; Add content of PORTD to content of the W and store the result back into PORTD addwf PORTD, 0 ; Add content of PORTD to content of the W and store the result into W

Register Addressing Modes


Immediate Addressing
(ex) MOVLW 0x0F

Direct Addressing
Uses 7 bits of 14 bit instruction to identify a register file address 8th and 9th bit comes from RP0 and RP1 bits of STATUS register. Initial condition for RP0 and RP1: RP1=RP0=0 (ex) SSPCON EQU 0x14 STATUS EQU 0x03 SSPSTAT EQU 0x94 BCF STATUS, 0x05 BCF SSPCON, 0x01 BSF STATUS, 0x05 BCF SSPSTAT, 0x02

Direct Addressing - Recap

Indirect Addressing
INDF register
Any instruction using the INDF actually accesses the register pointed to by the File Select Register (FSR).

A 9-bit EA is obtained by concatenating the 8-bit FSR register and the IRP bit(STATUS<7>) Example: Erase the RAM section of 0x20-0x2F
Movlw Movwf Next clrf incf btfss goto 0x20; pointer FSR INDF FSR FSR, 4 next

Direct vs. Indirect Addressing

Instruction Sets description convention

10

addlw

11

addwf

12

andlw

13

andwf

14

Bcf & bsf

15

btfsc

16

Btfss

17

Call

18

CLRF & CLRW

19

COMF & DECF

20

DECFSZ

21

GOTO & INCF

22

INCFSZ

23

IORLW & IORWF

24

MOVLW & MOVF

25

MOVWF & NOP

26

RETFIE & RETLW

27

RETURN

28

RLF & RRF

29

SUBLW & SUBWF

30

SWAPF & XORLW

31

XORWF

32

F877 Instruction Programming Tips


Tips on Instruction Skills and Tricks Frequently Met Situations

(a) Turn on/off an LED


An LED is connected to a pin at one, say, PORTB<0>, and to a ground at the other. Use a bit-oriented file register operation:
bsf PORTB, 0x00 ;to turn on and BSF PORTB, 0 ;same effect bcf PORTB, 0x00 ;to turn off. BCF PORTB, 0 ;same OFF

33

Tips Variable Declaration


(b)Variable Declaration
In C:

byte temp int x


In PIC: Variables must occupy physical RAM space CBLOCK ENDC pair Example:

34

Tips I/O designation


(c) I/O designation to the I/O Port
I/O Ports: PORTA, PORTB, PORTC, PORTD, PORTE Bi-directional Input or Output I/O selection file register
TRISA for PORTA; TRISB for PORTB; TRISC for PORTC TRISD for PORTD; TRISE for PORTE Each pin can be selected as
1: Input 0: Output

35

Tips Reading Sensor Input


(e) Reading sensor (digital) input and decision-making based on the input PIR motion detector & buzzer example PIR output
1: No motion detected 0: Motion detected

Buzzer
1: Buzzer On 0: Buzzer Off

Use BTFSS or BTFSC


39

Sample Code

40

You might also like