# how turn on outside light and turn off the inside one

#### PG1995

##### Active Member
Hi

Do you find the code below correct? I don't understand the code given in the book. What's the purpose of "ORL" and "ANL" instructions? Please help me. Thank you.

Code:
;Assume that bit P2.2 is used to control an outdoor light and bit P2.5 a light inside a building.
;Show how to turn on the outside light and turn off the inside one.

ORG 0H

SETB C
MOV P2.2, C

CLR C
MOV P2.5, C

SJMP $END #### Jon Wilder ##### Active Member You need to gain an understanding of boolean logic functions. Bitwise OR will return a 1 if any of the input bits are a 1 - 1 OR 0 = 1 1 OR 1 = 1 0 OR 1 = 1 If both of the input bits are a zero, the equation will return a 0 - 0 OR 0 = 0 In a bitwise XOR equation (XOR = Exclusively OR), the equation returns a 1 only if input bit 1 OR input bit 2 are a 1- 1 XOR 0 = 1 0 XOR 1 = 1 If both equation bits are a 1 or both equation bits are a 0, the equation returns a 0 - 1 XOR 1 = 0 0 XOR 0 = 0 In a bitwise AND equation, the equation will return a 1 if and ONLY if all of the input bits are a 1 - 1 AND 1 = 1 If any of the input bits are 0, the result will be 0 - 1 AND 0 = 0 0 AND 1 = 0 0 AND 0 = 0 So the instruction ORL states to OR the two operand bits together. ORL C states to OR the carry bit with the second operand bit and place the result in the carry bit - Code:  orl C,P2.2 In this instruction, bit P2.2 is OR'ed with the carry bit. If - Carry Bit = 1 P2.2 = 0 Result is 1 and the carry flag is now set Carry Bit = 0 P2.2 = 0 Result is 0 and the carry flag is now clear Carry Bit = 0 P2.2 = 1 Result is 1 and the carry flag is now set Carry Bit = 1 P2.2 = 1 Result is 1 and the carry flag is now set Code:  anl C,P2.5 If - Carry Bit = 1 P2.5 = 1 Result is 1 and carry flag is now set Carry Bit = 1 P2.5 = 0 Result is 0 and carry flag is now clear Carry Bit = 0 P2.5 = 1 Result is 0 and carry flag is now clear Carry Bit = 0 P2.5 = 0 Result is 0 and carry flag is now clear To summarize, in an OR equation returns a 1 if ANY of the bits in the OR equation are a 1 (result is 1 if either OR bit is 1). An AND equation returns a 1 only if all of the bits in the equation are a 1 (result is 1 if equation bit 1 AND equation bit 2 are a 1). An XOR equation returns a 1 only if bit 1 OR bit 2 is a 1, else it returns a 0. So in the book code, an outdoor light is being activated when an indoor light is deactivated and vice versa. P2.2 controls the outdoor light and P2.5 controls the indoor light. Code:  setb C ;set carry bit orl C,P2.2 ;OR p2.2 with carry bit mov P2.2,C ;turn light on if not already on The first instruction sets the carry bit high. Then the next instruction OR's the carry bit with bit P2.2. If P2.2 = 1 - 1 OR 1 = 1 - carry bit set If P2.2 = 0 - 1 OR 0 = 1 - carry bit set Then the carry bit is moved into P2.2 and P2.2 is set high The next 3 instructions - Code:  clr C ;clear carry bit anl C,P2.5 ;CY = P2.5 AND'ed with CY mov P2.5,C ;turn light off if not already off The first instruction clears the carry bit. The next instruction AND's P2.5 with the carry bit. If P2.5 = 1 - 0 AND 1 = 0 If P2.5 = 0 - 0 AND 0 = 0 Then the carry bit is moved into P2.5 and P2.5 is set low. The above code is example code that ensures that P2.2 is set high when P2.5 is set low if they're not already set to those states. Last edited: #### PG1995 ##### Active Member Thank you very much. But doesn't the code I have written also does the same job without using logic instructions? Please let me know. Thanks. Code: ORG 0H SETB C MOV P2.2, C CLR C MOV P2.5, C SJMP$

END
Regards
PG

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Yes!! It does. The purpose of the boolean algebra is to teach you how to do it using "states" if its off turn it on... if its on leave it on..

This come's into its own when controlling several lights... If you OR a new state with an old state you can leave other lights alone..

Similarly if you AND a new state with the old state you can turn lights off... This will prove very useful soon..

Think of the graphical LCD where there are 8 pixels in a row (lights effectively) OR'ing will turn single pixels on and AND'ing will turn them off, without affecting any others.

Last edited:

#### PG1995

##### Active Member
Thanks a lot, Rogers. I understand it now.

Regards
PG