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 :wink:
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
Re: bitwise head scratcher? Rather, pulling my hair out.
Code:
if PORTB ^ OutputMap1 then
PORTB = OutputMap1
else
PORTB = 0
endif
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
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