Hi,
First of all, there's an error in my previous example. The shift left operator is of course << and nothing else, sorry about that. The "edit window" is closed so I can't go back and fix it.
With your code as an example, try this:Lets go thru the two first lines:Code:MyLoop: Temp = PortB & %11110001 PortB = Temp | (X << 1) X = X + 1 IF X > 7 THEN X = 0 PAUSE 1000 Goto MyLoop
Temp = PortB & %11110001 - This reads the state of PortB into a variable called Temp and ANDS it with the value 14. What's happening there is that Temp will be whatever PortB is except for bits 1-3 which will be cleared to 0.
PortB = Temp | (X << 1) - This takes the value of Temp and ORs with the value of X shifted one "step" to the left. Remember that Temp is a copy of what PortB was except bits 1-3 which are now 0. So if X is 3 (%00000011) PortB will be %xxxx011x where x is whatever was there before because X is shifted one "step" before ORing it with Temp.
When doing a bitwise AND on two numbers each bit must be '1' in BOTH numbers for the bit to be '1' in the "result. So when doing a bitwise and with a value where certain bits are '0' those bits will be '0' in the result, example.
%11111111 & %00000000 = %00000000
%11111111 & %11110000 = %11110000
%11001000 & %00001111 = %00001000
When doung a biteise OR on two numbers it's enough that a bit in either number is '1' for that bit to be '1' in the result, example:
%11111111 | %00000000 = %11111111
%00000000 | %00001111 = %00001111
%11110000 | %00001111 = %11110000
So in the above code we read in PortB, clear bits 1-3 and put the value of X in those 3 bits.
/Henrik.




Bookmarks