1
\$\begingroup\$

According to Instruction Set Description "xorwf myLiteral" should xor contents of "w" with "myLiteral" and if both are same then flag Z in STATUS must be set (not interested where this instruction saves the result but Z flag must be affected if both values were same)

But in my code this is not happening

movf PORTC
xorwf red_on    ;w holds 01000100 and red_on is also 01000100 Z must be one
btfsc STATUS, Z ;but here it's not one
goto Turn_Yellow_On

Full code is following:

    ; PIC16F72 Configuration Bit Settings

; Assembly source line config statements

#include "p16f72.inc"

; CONFIG
; __config 0xFFF7
 __CONFIG _FOSC_RC & _WDTE_ON & _PWRTE_ON & _CP_OFF & _BOREN_ON

STATUS equ 0x03
TRISC equ 0x87
PORTC equ 0x07
INTCON equ 0x0b

RP0 equ 5
RP1 equ 6
Z equ 2

GIE equ 7
INTE equ 4
INTF equ 1

all_off equ 0x00
red_on equ 0x44
yellow_on equ 0x22
green_on equ 0x11 

bank0 macro
    bcf STATUS, RP0
    bcf STATUS, RP1
    endm

bank1 macro
    bsf STATUS, RP0
    bcf STATUS, RP1
    endm

extrn_int_enable macro
    bsf INTCON, GIE
    bsf INTCON, INTE
    endm

PORTC_as_out macro
    bank1
    movlw 0x00
    movwf TRISC
    endm

PORTC_out macro outByte
    bank0
    movlw outByte
    movwf PORTC
    endm

test_and_switch macro testWith, jumpTo
    movf PORTC
    xorwf testWith
    btfsc STATUS, Z
    goto jumpTo
    endm

    org 0x00
Reset_Vector:
    goto Main

    org 0x04
Interrupt_Vector:
    goto Switch_Next

    org 0x05
Main:
    PORTC_as_out
    extrn_int_enable

Loop:
    clrwdt
    goto Loop

    org 0x200
Switch_Next:
    bcf INTCON, INTF

    ;test_and_switch all_off, Turn_Red_On
    movf PORTC      ;I recognized the mistake I should use d = 0 but data
    ;will still be copied in w when program goes to "Turn_Red_On" routine for 
    ;the first time
    xorwf all_off
    btfsc STATUS, Z
    goto Turn_Red_On

    ;test_and_switch red_on, Turn_Yellow_On
    movf PORTC
    xorwf red_on    ;w holds 01000100 and red_on is also 01000100 Z must be one
    btfsc STATUS, Z ;but here it's not one
    goto Turn_Yellow_On

    ;test_and_switch yellow_on, Turn_Green_On
    movf PORTC
    xorwf yellow_on
    btfsc STATUS, Z
    goto Turn_Green_On

    goto Turn_All_Off

Turn_Red_On:
    PORTC_out red_on
    goto Return_From_Int

Turn_Yellow_On:
    PORTC_out yellow_on
    goto Return_From_Int

Turn_Green_On:
    PORTC_out green_on
    goto Return_From_Int

Turn_All_Off:
    PORTC_out all_off
    goto Return_From_Int

Return_From_Int:
    retfie

    end
\$\endgroup\$
2
\$\begingroup\$

Your movf PORTC instruction lacks a destination.

The default is f, PORTC in this case, so you are not putting the number in the w register.

Change it to movf PORTC, w

And you should always state it explicitly to prevent problems. Running a simulation would probably have caught this, and it's worth learning how to do it.

Edit: Also, you are using xorwf (ex-or the w register with a file register), when you should be using xorlw (ex-or a literal with the w register).

xorwf takes the operand to be an address and xor's the w register with the contents of the register at that location (0x44, in the 96 bytes of general purpose registers), which is not what you want.

\$\endgroup\$
  • \$\begingroup\$ destination of the result is not point of interest atm. The undesired result is when w does hold 0x44 and xorwf 0x44 doesn't set Flag Z (it's astonishing to me). If you copy and simulate this code in MPLAB X and use stimulus to interrupt INT pin you'll notice on 2nd interrupt after entering Switch_Next label on about 7th line of code you'll notice the value of w would be 0x44 and "xorwf red_on" (red_on equ 0x44) still won't set Z in STATUS upon it's execution which is strange \$\endgroup\$ – Danish ALI May 2 '18 at 16:01
  • \$\begingroup\$ See edit addition above. \$\endgroup\$ – Spehro Pefhany May 2 '18 at 17:57
  • 1
    \$\begingroup\$ Thanks it was my mistake I didn't recognize xorwf was taking literal as address not as constant value now I replaced it with xorlw and my code is working. Grateful to You! \$\endgroup\$ – Danish ALI May 3 '18 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.