Controlling outputs by setting bits

If I want to control all outputs except bit 3, neither turn on or off what do I need to send?

DDRA = B11111111; // set PORTA (digital 7~0) to outputs
PORTA = 0b11111111; //Set all output high which is off (0b is binary representation of 255)

What I send:
byte byteVal = (values[0]); // (values[0]) sent from XOJO program via RS-485
PORTA = ~ byteVal; //Invert

Your question is not really clear.
You probably need a mask as a filter.

byte mask = 0b111111011;
port = mask & controlsignal;

Hi, @cliffcoulter
Google;

direct port manipulation arduino

This will help;

Tom.... :smiley: :+1: :coffee: :australia:

You're right about a bit mask. I actually haven't tried this but I believe this my be your suggestion.

/******* Set Outputs *********/
      if (values[2] == 2)
      {
        byte byteVal = (values[0]);// Change to byte representation
        if (digitalRead(zoneSwitch) != CLOSED) // No zone valve closed
        {
          byteVal = byteVal &= 0b11110111; // Ignore Floor pump
          PORTA = ~ byteVal; //Negate or invert bits. 1 sent to turn on output is inverted to 0 to ground output and turn on relay
          KeepAlive.start(0, 0, keepAliveTime, 0); // Keep alive signal from XOJO (10 Min)
        offLine = false;
        
        }
        else
        {
          PORTA = ~ byteVal; //Negate or invert bits. 1 sent to turn on output is inverted to 0 to ground output and turn on relay
        }

Thanks for this material. This helped.

Should be:

Or:

byteVal=byteVal & 0b11110111;

You stated you wanted to mask bit 3. Now you are masking bit 4... (if you start numbering at 1).
Suggestion: rename byteVal to something that makes sense. What is behind the ports? Something like pumpState would do...

That turns off bit 3.

  port = (newByte & mask) | (port & ~mask);

places the bits of newByte where the mask is 1 into the port and leaves the other (mask is 0) bits of port unchanged.

a7

1 Like

Yes, is see if the bit was set it would be changed using &. It seems weird to be able to use PORT in the second half of your equation when that is what your outcome is. I hope I’m seeing that correctly.

In more lines of code

   byte myCopy = PORTD;         // get the output register

   myCopy &= ~mask;             // set the masked bit(s) to 0

   byte myNewValue = byteVal;   // copy the new value

   myNewValue &= mask;          // set the unmasked bit(s) to 0

   PORTD = myCopy | myNewValue; // combine. No conflicting bits

Where there are 1s in the mask, the bits come from byteVal.

Where there are 0s in the mask, we preserve the PORTD bits.

If mask is 0b11110111, then ~mask is 0b00001000.

In this case you could combine them with addition, as there will never be any carrying as there will not be 1s in the same bit position. But use the '|' logical or operator.

Sry if this was all clear. Sun very hot just now.

a7

I'll need to give this one a try. The breakdown is great &= was what I needed to understand your example. Keep cool!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.