You are on page 1of 29

StartingtoProgram

Chapter4
Sections14,10

Dr.IyadJafar

Outline
y Introduction
y ProgramDevelopmentProcess
y ThePIC16F84AInstructionSet
y Examples
y ThePIC16F84AInstructionEncoding
y AssemblerDetails
y SamplePrograms
2

Introduction
y Everycomputercanrecognizeandexecuteagroupof

instructionscalledthe InstructionSet
y Theseinstructionarerepresentedinbinary(machinecode)
y Aprogramisasequenceofinstructionsdrawnfromthe

instructionsetandcombinedtoperformspecificoperation
y Toruntheprogram:
y Itisloadedinbinaryformatinthesystemmemory
y Thecomputerstepsthrougheveryinstructionandexecuteit
y Executioncontinuesunlesssomethingstopsitliketheendof

programoraninterrupt
3

HowtoWritePrograms
y Machinecode
y usethebinaryequivalentoftheinstructions
y Slow,tedious,anderrorprone

00 0111 0001 0101


y Assembly
y Eachinstructionisgivenamnemonic
y AprogramcalledAssembler convertstomachinecode
y Ratherslowandinefficientforlargeandcomplexprograms

addw NUM, w
y Highlevellanguage
y UseEnglishlikecommandstoprogram
y AprogramcalledCompilerconvertstomachinecode
y Easy!!Theprogramcouldbeinefficient!
4

for (i=0; i<10; i++) sum += a[i];

ProgramDevelopmentProcess

ThePIC16SeriesInstructionSet
y ThePIC16SeriesALU

ThePIC16SeriesInstructionSet
y 35 instructions!!!
y Thebinarycodeoftheinstructionitselfiscalledthe

Opcode
y Mostoftheseinstructionoperate/useonvaluescalled
Operands(rangingfromnooperandstotwo)
y Threecategoriesofinstructions
1.
2.
3.

Byteorientedfileregisteroperations
Bitorientedfileregisteroperations
Literalandcontroloperations

y Typeofoperations
1.
2.
3.
4.
7

5.

Arithmetic
Logic
Datamovement
Control
Misc

ThePIC16SeriesInstructionSet
y IntroductiontoPIC16ISA
y Typesofoperands
y A7bitaddressforamemorylocationinRAM

(RegisterFile)denotedbyf
y A3bittospecifyabitlocationwithinanthe8bitdata

denotedbyb
y A1bittodeterminationthedestinationoftheresult

denotedbyd
y A8bitnumberforliteraldataor11bitnumberfor

literaladdressdenotedbyk
8

ThePIC16SeriesInstructionSet
y Examples
y clrw

ClearstheworkingregisterW
clrf f
y Clearsthememorylocationspecifiedbythe7bit
addressf
addwf f,d
y AddsthecontentsoftheworkingregisterWtothe
memorylocationwith7bitaddressinf.theresultis
savedinWifd=0,orinfifd=1
bcf f,b
y Clearsthebitinpositionspecifiedbybinmemory
locationspecifiedby7bitaddressf
addlw k
y AddsthecontentofWtothe8bitvaluespecifiedbyk.
TheresultisstoredbackinW
y

y
9

ThePIC16SeriesInstructionSet
ByteorientedFileRegisterOperations
y Format:

op f, d

y op:operation
y f:numberoffileorregister
y d:destination(0:workingregister,1:fileregister)

y Example:

addwf
PORTA, 0
Addsthecontentsoftheworkingregisterandregister
PORTA,putstheresultintheworkingregister.
10

ThePIC16SeriesInstructionSet
BitorientedFileRegisterOperations
y Format:

op f, b

y op:operation
y f:numberoffileorregister
y b:bitnumber,0through7

y Example:

bsf STATUS, 5
Setsto1Bit5ofregisterSTATUS.
11

ThePIC16SeriesInstructionSet
LiteralandControlOperations
y Format:

op k

y op:operation
y k:literal,an8bitifdataor11bitifaddress

y Examples:

addlw
5
Addstotheworkingregisterthevalue5.
call
9
Callsthesubroutineataddress9.
12

ThePIC16SeriesInstructionSet
ArithmeticInstructions
Mnemonic Operands

13

Description

Cycles

Status
Affected

ADDWF

f,d

AddWandf

C,DC,Z

COMF

f,d

Complementf

DECF

f,d

Decrementf

INCF

f,d

Incrementf

SUBWF

f,d

SubtractWfromf

C,DC,Z

ADDLW

AddliteralandW

C,DC,Z

SUBLW

SubtractWfromliteral

C,DC,Z

d = 0 , result is stored in W
d = 1 , result is stored in F

ThePIC16SeriesInstructionSet
LogicInstructions
Mnemonic Operands

14

Description

Cycles

Status
Affected

ANDWF

f,d

ANDWwithf

IORWF

f,d

InclusiveORWwithf

XORWF

f,d

ExclusiveORWwithf

ANDLW

ANDliteralwithW

IORLW

InclusiveORliteralwithW

XORLW

ExclusiveORliteralwithW

d = 0 , result is stored in W
d = 1 , result is stored in F

ThePIC16SeriesInstructionSet
DataMovementInstructions

Mnemonic Operands

Description

Cycles

Status
Affected
Z

MOVF

f,d

Movef

MOVWF

MoveWtof

SWAPF

f,d

Swapnibblesinf

MOVLW

MoveliteraltoW

d = 0 , result is stored in W
d = 1 , result is stored in F
15

ThePIC16SeriesInstructionSet
ControlInstructions
Mnemonic Operands
DECFSZ
INCFSZ
BTFSC
BTFSS
CALL
GOTO
RETFIE
RETLW
RETURN
16

f,d
f,d
f,b
f,b
k
k

Description

Cycles

Decrementf,Skipif0
Incrementf,Skipif0
BitTestf,SkipifClear
BitTestf,SkipifSet
Callsubroutine
Gotoaddress
Returnfrominterrupt
ReturnwithliteralinW
ReturnfromSubroutine

1(2)
1(2)
1(2)
1(2)
2
2
2
2
2

d = 0 , result is stored in W , d = 1 , result is stored in F

Status
Affected

ThePIC16SeriesInstructionSet
MiscellaneousInstructions
Mnemonic Operands

17

CLRF
CLRW
NOP
RLF

f,d

RRF

f,d

BCF
BSF
CLRWDT
SLEEP

f,b
f,b

Description

Cycles

Clearf
ClearW
NoOperation
RotateLeftfthroughCarry
RotateRightfthrough
Carry
BitClearf
BitSetf
ClearWatchdogTimer
Gointostandbymode

1
1
1
1

Status
Affected
Z
Z
C

1
1
1
1

TO',PD'
TO',PD'

d = 0 , result is stored in W , d = 1 , result is stored in F

ThePIC16SeriesInstructionSet
Examples
Instruction

18

Operation

Flags
Affected

bcf 0x31,3

clearbit3inlocation 0x31

None

bsf 0x04,0

setbit0location0x04

None

bsf STATUS,5

setbit5inSTATUSregistertoselectbank1in
memory

None

bcf STATUS,C

clearthecarrybitinthestatusregister

None

addlw 4

Adds4toworkingregisterW and storetheresult


inbackinW

C,DC,Z

addwf 0x0C, 1

Addthecontentoflocation0x0C toWandstore
theresultin0CH(d=1)

C,DC,Z

sublw 10

SubtractWfrom 10andputtheresultinW

C,DC,Z

subwf 0x3C, 0

SubtractWfromcontents oflocation0x3Cand
storetheresultinW

C,DC,Z

ThePIC16SeriesInstructionSet
Examples
Instruction

19

Operation

Flags
Affected

incf 0x06,0

Incrementlocation0x06by1andstoreresultinW

decf TEMP,1

Decrement locationTEMPby1andstoreinTEMP

compf 0x10,1

Complementthevalueinlocation10Handstorein
0x10

andlw B11110110

ANDliteralvalue 11110110withWandstore
resultinW

andwf 0x33,1

ANDlocation0x33withWandstoreresultin0x33

iorlw B00001111

InclusiveorWwith00001111

iorwf X1 ,0

Inclusiveor WwithlocationX1andstoreresultin
W

xorlw B01010101

ExclusiveorWwith01010101

xorwf 0x2A ,0

Exclusiveor Wwithlocation0x2Aandstoreresult
inW

ThePIC16SeriesInstructionSet
Examples

Instruction

Flags
Affected

clrw

ClearW

clrf 0x01

Clearlocation0x01

movlw

20

Operation

18

Moveliteralvalue18intoW

NONE

movwf 0x40

Move contentsofWtolocation0x40

NONE

movf 0x21 ,0

Movecontents oflocation0x21toW

movf 0x21,0x33

Incorrectsyntax

movwf 0x1B,1

Incorrectsyntax

swapf T1,1

Swap4bitnibbles oflocationT1

NONE

swapf DATA ,0

MoveDATAtoW, swapnibbles,nochangeonDATA

NONE

rlf TEMP,1

Rotate contentsoflocationTEMPtoleftbyonebit
positionthroughtheCflag

rlf 0x25,0

Copycontents of0x25toWandrotatetoleftby
onebitpositionthroughtheCflag

ThePIC16SeriesInstructionSet
Encoding

21

Check Appendix A
for opecode binary
codes

AssemblerDetails
y Anyassemblerlinemayhaveuptofourdifferentelements

y Wecanspecifyvaluesindifferentbasesinassembler

programs

22

Radix

Example

Decimal

D255

Hexadecimal

H8dor0x8d

Octal

O574

Binary

B01011100

ASCII

GorAG

AssemblerDetails
y Assemblerdirectives
y Theseareassemblerspecificcommandstoaidtheprocessing

ofassemblyprograms

Assembler
directive
list
#include
org
equ

23

Summaryofaction
Implementalistingoption
Includeadditionalsourcefile
Setprogramorigin
Defineanassemblyconstant;this
allowsustoassignavaluetoalabel

cblock andendc

Defineablockofvariables

end

Endprogramblock

ProgramStructure

24

SampleProgram1
y Writeaprogramtoaddthenumbersstoredin

locations31H,45H,and47Handstorethe
resultinlocation22H

25

SampleProgram1

26

; ************************* EQUATES *********************************


STATUS
equ
0x03
; define SFRs
RP0
equ
5
;************************** VECTORS *********************************
org
0x0000
; reset vector
goto
START
org
0x0004
INVEC
goto
INVEC
; interrupt vector
;************************* MAIN PROGRAM *************************
START
bcf
STATUS , RP0 ; select bank 0
movf 0x31 , 0
; put first number in W
addwf 0x45 , 0
; add second number
addwf 0x47 , 0
; add third number
movwf 0x22
; save result in 0x22
DONE
goto
DONE
; endless loop
end

SampleProgram2
y Writeaprogramtoswapthecontentsoflocation

0x33withlocation0x11

27

SampleProgram2

28

; ************************* EQUATES *********************************


STATUS
equ
0x03
; define SFRs
RP0
equ
5
;************************** VECTORS *********************************
org
0x0000
; reset vector
goto
START
org
0x0004
INVEC
goto
INVEC
; interrupt vector
;************************* MAIN PROGRAM *************************
START
bcf
STATUS , RP0
; select bank 0
movf
0x33 , 0
; put first number in W
movwf 0x22
; store the 1st number temporarily
movf
0x11 , 0
; get 2nd number
movwf 0x33
; store 2nd in place of 1st
movf
0x22 , 0
; get 1st number from 0x22
movwf 0x11
; store 1st in place of 2nd
DONE
goto
DONE
; endless loop
end

Summary
y ThePIC16F84Ahas35instructionstoperform

differentcomputationalandcontroloperations
y Programscanbewrittenusingdifferentlevelsof
abstraction
y Usingassemblerssimplifiestheprogram
developmentprocess
y ThereexistmanyIDEtoaidwritingprogramsand
simulatetheirbehaviorbeforeputtingtheminto
hardware

29