Shop OBEX P1 Docs P2 Docs Learn Events
Binary Logic — Parallax Forums

Binary Logic

alnajjar1alnajjar1 Posts: 110
edited 2013-07-16 15:05 in BASIC Stamp
Hello,

I am using a 74HC165 to listen to 8 switches. I then use the byte I get from the 165 to turn on LEDs that are connected to pins 0-7 or OutL. The problem is that I want to turn the LEDs only when there is a change from 0 to 1 on the switch and not 1 to 0. I can do this by looking at the change in condition in each bit of the 165 input and turn individual LED on/off. the problem is this is very slow. I need to work with the entire byte.

Here is an example of how I want the logic:

Switches from %01000000 --> %00000101 then OUTL = %00000101

I essentially want to activate the LED "on mouse down" and do nothing on "mouse up" to use a PC comparison.

I have seen some binary operation in Scott Edwards article in Nuts&Volts on Rotary Encoders example where he uses the XOR operator. The problem is that I want to work with the entire byte and not its individual bits since speed is of the essence.

Any idea how I can achieve this?

Many thanks in advance,

Al

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-07-15 21:12
    There are all the necessary bit operators in PBasic. The Manual discusses them in detail. "&" is and, "|" is or, "^" is exclusive or, and "~" is not. You will need two values for the switches, the 8 bits of the "old" setting and the 8 bits of the "new" setting. You want your LED outputs to be: (oldValue == 0) and (newValue == 1). Exclusive or and equiv ("==") are complementary operations, so your LED outputs would be: not (oldValue ^ 0) and not (newValue ^ 1). This is equivalent to: not ((oldValue ^ 0) or (newValue ^ 1)). If you're doing this for 8-bit values, you've got: OUTL = ~((oldValue^%00000000)|(newValue^%11111111)) which simplifies to: OUTL = ~ (oldValue | ~ newValue). Obviously, you have to then set oldValue to newValue and go through another cycle to get a new value. The appropriate LEDs will light for one cycle when the corresponding switch changes from 0 to 1.
  • SapphireSapphire Posts: 496
    edited 2013-07-15 21:32
    Mike,

    Clever, but "!" should be "~" for bitwise not in PBasic.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-07-16 07:59
    OUTL = oldValue ^ newValue & newValue
  • alnajjar1alnajjar1 Posts: 110
    edited 2013-07-16 15:04
    Thanks Mike, always elegant explanations.

    I also tried yesterday (and it worked) inverting the byte from the 165 using the NOT (multiplying by -) then using the AND NOT operator which gives a byte indicating the changes of 0 to 1 on all bits. This was straight from the manual. I always find these logic operators complicated to understand..

    many thanks for the quick response.

    Al
  • alnajjar1alnajjar1 Posts: 110
    edited 2013-07-16 15:05
    This is cool and elegant, thanks Tracy...
Sign In or Register to comment.