bitwise head scratcher? Rather, pulling my hair out.


+ Reply to Thread
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108

    Default bitwise head scratcher? Rather, pulling my hair out.

    Hello all,

    Very long time since my last post. I hope everyone is well, considering! Well here goes...
    I've got a setup (18F2420) that controls 8 outputs via an ESP8266 as well as 8 inputs that can also control the outputs.
    The individual inputs can be mapped to any number of outputs.
    Let's say hardware "IN1" controls outputs 1,2 and 3 (OutputMap1 = %00000111)
    AND let's assume that PORTB is currently %00000111 or %00000000.
    Here's the snip of code...
    Code:
        if IN1 = 0 then
            if PORTB & OutputMap1 = OutputMap1 then     ' The selected outputs are ON
                PORTB = PORTB ^ OutputMap1              ' Turn the selected outputs OFF
            else
                PORTB = PORTB | OutputMap1
            endif
        endif
    The above works great until Outputs 1, 2 or 3 are toggled through the ESP8266.
    Let's say that Output 2 was toggled so, PORTB now equals %00000101.
    Now, when the code above is run, my outputs would now be %00000010.
    When run yet again, my outputs are %00000101.

    Basically the goal is, for example:
    If the OutputMap1 is %00000111 and PORTB is %00000001 then PORTB should be %00000111.
    If the OutputMap1 is %00000111 and PORTB is %00000010 then PORTB should be %00000111.
    If the OutputMap1 is %00000111 and PORTB is %00000011 then PORTB should be %00000111.
    If the OutputMap1 is %00000111 and PORTB is %00000100 then PORTB should be %00000111.
    If the OutputMap1 is %00000111 and PORTB is %00000101 then PORTB should be %00000111.
    and finally...
    If the OutputMap1 is %00000111 and PORTB is %00000111 then PORTB should be %00000000.
    *Pay no mind to the binary count above. I just wanted to provide more than 1 example

    Anyhow, I can't figure out what I'm doing wrong?
    Hopefully one of you fine folks can point me in the right direction.

    Thanks all.
    Chris

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,607

    Post Re: bitwise head scratcher? Rather, pulling my hair out.

    Code:
    if  PORTB ^ OutputMap1 then
        PORTB = OutputMap1
    else
        PORTB = 0
    endif
    This is more entertaining than Free to Air TV

  3. #3
    Join Date
    Apr 2014
    Location
    Northeast
    Posts
    313

    Default Re: bitwise head scratcher? Rather, pulling my hair out.

    Along those same lines:
    Code:
    IF (OutputMap1 = 7) & (PORTB = 7) THEN
      PORTB = 0
    ELSE
      PORTB = 7
    ENDIF

  4. #4
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108

    Thumbs up Re: bitwise head scratcher? Rather, pulling my hair out.

    Thanks for your replies guys!

    Don't I feel like an idiot! Here was my problem...

    From this:
    if PORTB & OutputMap1 = OutputMap1 then

    To this:
    if (PORTB & OutputMap1) = OutputMap1 then

    Works perfectly.

    If for nothing else, thank you guys for proding me to look at my code closer.

    Take care and be safe,
    Chris

Similar Threads

  1. Replies: 3
    Last Post: - 14th June 2012, 17:01
  2. My Head Hurts with Tables, Lookup, EXT etc etc
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th June 2012, 07:21
  3. Oh Dear. My head is spinning. HPWM RGB LEDs What to choose?
    By jimseng in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 19th March 2012, 17:03
  4. 3AM hair pulling again
    By muddy0409 in forum General
    Replies: 2
    Last Post: - 14th March 2009, 07:59
  5. Pulling port high
    By the_antique in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 14th November 2005, 18:18

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts