Understanding how the "Zero", "Carry" and "Digit Carry" flags work in different applications is important to understanding how to program the PICmicro MCU. This application demonstrates how the registers change with different instructions.
In the application, note the use of the:
ClearFlags Macro ; Clear the Processor Status Flags
movlw 0x0F8
andwf STATUS, w
movwf STATUS
endm
macro which clears the three arithmetic status flags instead of the macro:ClearFlags Macro ; Invalid Clear of the Processor Status Flags
movlw 0x0F8
andwf STATUS, f
endm
The second Macro will not work in the PICmicro because the three STATUS register flags, "Zero", "Carry" and "Digit Carry" cannot be updated by an arithmetic/bitwise instruction. The only instructions which can update these flags are:- movwf STATUS
- bcf STATUS, Z|C|DC
- bsf STATUS, Z|C|DC
The second macro was used for this application in the first edition of this book in error.
The source code is listed below, or it can be accessed from the CD-ROM by clicking Here.
title "Status - Showing How the Status Flags Work"
;
; This Program plays around with the Execution Status Flags to
; show how they are changed by different operations.
;
; The Flags are:
; STATUS Bit 0 = "C" - The "Carry" Flag
; STATUS Bit 1 = "DC" - The "Digit Carry" Flag
; STATUS Bit 2 = "Z" - The "Zero" Flag
;
; Myke Predko
; 96.05.13
;
; Updated:
; 99.11.01 - Corrected the "Clear Flags" Macro
; - Updated with Additional Comments to Explain how the
; Instructions Execute
; - Updated with "Rotate" Instructions to Show how the
; Carry Flag is used with them
; - "nops" added after Status Flags Updates to allow
; user to observe STATUS Flag States outside of
; "ClearFlags" Macro
;
; Hardware Notes:
; Simulated 16F84
; No I/O
; Use "status.pjt" along with "status.wat"
;
LIST P=16F84, R=DEC
INCLUDE "p16f84.inc"
; Macros
ClearFlags Macro ; Clear the Processor Status Flags
movlw 0x0F8
andwf STATUS, w
movwf STATUS
endm
; Registers
CBLOCK 0x020
i, j ; Registers to Operate on
ENDC
__CONFIG _CP_OFF & _WDT_OFF & _RC_OSC
PAGE
; Mainline of Status
org 0
movlw H'80' ; Set "i" to 0x080
movwf i
movlw 8
movwf j
; Clear the Status Flags before doing any operations
ClearFlags
movf i, w ; Zero Flag not Set (w != 0)
nop
xorwf i, w ; Zero Flag Set After Instruction
nop ; ((i ^ i) == 0)
movf i, w ; Add to Show Carry and Zero being Set
addlw 1 ; Add one - No Flags Set (w = 0x081)
nop
addwf i, w ; ((0x081 + 0x080) == 0x0101)
nop ; 0x0101 => Carry Set
; 0x0101 => w = 1/Zero Reset
ClearFlags
rlf i, w ; Load "Carry" with the Bit 7 of "i" and
; Shift i to the left by 1 (w == 0x000)
rlf j, w ; Shift j to the left by 1 (w == 0x010)
nop ; with carry inserted as the LSB (w == 0x011)
ClearFlags
movf j, w ; Now, Show the Digit Carry Flag Being
addwf j, w ; Set ((0x008 + 0x008) == 0x010)
nop
ClearFlags
movf i, w ; Now, Set all three Status Bits
subwf i, w ; 0x080 - 0x080 = 0x080 + (0x080 ^ 0x0FF) + 1
movwf i ; = 0x080 + 0x07F + 1
nop ; = 0x0100
; 0x0100 => Carry Set
; 0x0F + 1 => Digit Carry Set
; 0x0100 => Zero Result in "w"/Zero Set
ClearFlags
movf i, f ; Now, just show the zero flag set
nop ; ( i == 0)
Finished ; Finished, Just Loop Around Forever
goto $
end
No comments:
Post a Comment