Home     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Tech-BLOG     RSS     Terms of services     Privacy policy  
 Home     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Tech-News     RSS   

Author Topic: How to read a register if it is zero?  (Read 495 times)

_pike

  • Administrator
  • Full Member
  • *****
  • Posts: 144
    • View Profile
How to read a register if it is zero?
« on: March 16, 2011, 01: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:
Code: [Select]
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
« Last Edit: March 16, 2011, 10:28:11 AM by _pike »
The only difference between men and boys is the price of their toys....

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1331
    • View Profile
Re: How to read a register if it is zero?
« Reply #1 on: March 17, 2011, 10:47:39 AM »
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:

Code: [Select]
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:

Code: [Select]
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 (2-2=0). If not same, then the ZERO flag is 0.




« Last Edit: March 17, 2011, 10:49:57 AM by kam »
And on the eighth day God said, "Okay, Murphy, you're in charge!"

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1331
    • View Profile
Re: How to read a register if it is zero?
« Reply #2 on: March 17, 2011, 11:07:48 AM »
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 (1-0 or 0-1), 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"

Code: [Select]
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:

Code: [Select]
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...
And on the eighth day God said, "Okay, Murphy, you're in charge!"

_pike

  • Administrator
  • Full Member
  • *****
  • Posts: 144
    • View Profile
Re: How to read a register if it is zero?
« Reply #3 on: March 18, 2011, 13:07:43 PM »
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!!!!
« Last Edit: March 18, 2011, 13:09:53 PM by _pike »
The only difference between men and boys is the price of their toys....