PIC18Assembly Language Programming
PIC18Assembly Language Programming
PIC18Assembly Language Programming
Chapter 3
DECFSZ
f = fileReg d = destination
f,d,a
If a is ignored (i.e. DECFSZ COUNT,F) then MPLAB assume a=0; access bank is selected by default.
Example 3-1
Write a program to: a) Clear W b) Add 3 to W ten times and place the result in PORTB. Use DECFSZ to perform the looping.
AGAIN
Example 3-2
Write a program to: a) Clear W b) Add 3 to W ten times
AGAIN
LOP_1 LOP_2
BZ (branch if zero)
Example 3-5
Write a program to determine if fileReg location 0x30 contains the value 0. if so, put 55H in it.
NEXT
Example 3-6
Find the sum of the values 79H, F5H and E2H. Put the sum in fileReg location 5 (low byte) and 6 (high byte).
Unconditional Branch
GOTO (long jump) Can go to any memory location in PIC18 (up to 2MB)
Unconditional Branch
BRA (Branch Always)
over
bra
bra
over @ $
RETURN instruction is used to return to the main program (address is POPped out of the stack)
Example 3-9
Toggle all the bits of PORTB by sending the value 55H and AAH continuously. Use a delay between the toggle process
BACK
EQU ORG MOVLW 0x55 MOVWF PORTB CALL MOVLW MOVWF CALL GOTO ORG MOVLW MOVWF NOP NOP DECF BNZ RETURN END
DELAY AGAIN
MYREG, F AGAIN
CALL vs RCALL
CALL anywhere within 2MB address RCALL within 2kB address Using RCALL could save a number of bytes in Program ROM space.
Example 3-18
MYREG EQU BACK 0x08
ORG DELAY MOVLW MOVWF AGAIN NOP NOP NOP DECF BNZ RETURN END
MYREG, F AGAIN
Delay Calculation
DELAY AGAIN MOVLW MOVWF NOP NOP NOP DECF BNZ RETURN 0xFA MYREG 1 1 1 1 1 1 2/1 2
Loop
MYREG, F AGAIN
True condition
False condition
Total ins. Cycles = 1 + 1 + 249(1+1+1+1+2) + 1(1+1+1+1+1) + 2 = 1503 cycles Time delay = 1503 x 1us = 1503us (assume 4MHz crystal is used)
Example 3-20
Show the mathematical calculation to obtain the value in figure below (based on example 3-20).