PCB Heaven
Books => Learning the PIC microcontroller @ PCB Heaven  On line book discussion => Topic started by: _pike on March 16, 2011, 00:30:14 AM

Hello!.I would like to ask how can i understand that a register has been changed and is is not zero?
example:
temp1 equ 0x21h ;temp1 = '00000000'
movlw b'00100010'
movwf temp1 ;temp1 = '00100010'
Now how can i compare it, and if all bits are 0 then goto 1 and if any bit has been changed goto 2
Thanks a lot Panagiotis

First, you need to have another register with the "normal value". I usual use the same name of the register, with the extension _old. Example:
temp1 equ 0x21h ;temp1 = '00000000'
temp1_Old equ 0x22h ;temp1 = '00000000'
movlw b'00100010'
movwf temp1 ;temp1 = '00100010'
Here is where temp1 is changed, and you test it to see if is zero or changed:
movf temp1,w ;Load temp1 to W. This will also set ZERO flag if temp1 is zero
btfsc zero ;Test ZERO flag (located in status register)
goto 1 ; If ZERO is set (all bits are 0)then goto 1
;ELSE
subwf temp1_Old,w ;Subtract the content of temp1 (loaded on W) to the content of temp1_Old
btfss zero ; Results (stored in W) are tested.
goto 2 ;If temp1 NOT EQUAL to temp1_Old, the subtraction will NOT be zero. So GOTO 2
This is how you test 2 numbers. Subtraction is the method to see if they are changed. If are same, the ZERO flag is set after the subtraction (22=0). If not same, then the ZERO flag is 0.

Additional COOL info:
How to check if only the 3rd bit is change...
This is a technique that you will certainly need to use one day. Suppose that you have again temp1 and temp1_old, and you want to test if the 3rd bit of temp1 is changed. This is what you do:
movf temp1,w
xorwf temp1_old,w
The XOR instruction has this ability to identify ONLY the changed bits. If both bits are 0 or 1, then the XOR function returns 0. But if bits are different (10 or 01), XOR returns 1. Here is an example:
temp1=00001100
temp1_old=10001000
The XORWF will return
00001100
10001000 XOR

10000100
Now, you can simply check the 3d bit of the XOR result"
movf temp1,w
xorwf temp1_old,w
movwf temp2 ;another temporary register that you store the XOR result
btfsc temp2,3
goto Bit_3_Is_Changed
And here is another way that has the same result but without using this lousy temp2 register:
movf temp1,w
xorwf temp1_old,w
andlw '00001000'
btfss zero
goto Bit_3_Is_Changed
This technique uses the AND logic function, which can isolate a single bit from a number. Check this:
00111110
00001000 AND

00001000
and this:
00110110
00001000 AND

00000000
The result of the AND logic function is affected ONLY by the 3rd bit. So, if the result is 0, this means that bit 3 was zero...

You are AMAZING!!!!!.I'll will try it out this weekend.....also the trick with xorwf instruction will be definetely needed in the future!!! Thank you very much my friend!!!!