reading register data bits as a boolean

Situation

An analog front end of an automation controller contains status of outputs in 2 8 bit register as shown

----------System Control 1---------------------
7 6 5 4 3 2 1 0
rsvd rsvd rsvd C1 C2 C3 C4 C5
----------System Control 2---------------------
7 6 5 4 3 2 1 0
rsvd rsvd rsvd C6 C7 C8 C9 C10

I can read the registers in pretty much any form to display the contents or change them but I am struggling to come up with a concept of reading the register to display the contents as a status or boolean of each bit in a human readable format. As you can see the first 3 bits in both registers are reserved and not used so they can be omitted and the 2 registers are independent of each other.

For example:

The output of the registers as queried:

System Control 1 : 00001001
System Control 2 : 00000110

or

System Control 1 : 9
System Control 2 : 6

What I am trying to accomplish:

------------ System Control 1 ----------------------------
C1:False C2:True C3:False C4:False C5:True
------------ System Control 2 -----------------------------
C6:False C7:False C8:True C9:True C10:False

or

S1:
C1:False
C2:True
C3:False
C4:False
C5:True
S2:
C6:False
C7:False
C8:True
C9:True
C10:False

My question is how to I parse the registers in a elegant way that is easier to work with.

Any help?

Use bitRead?

TheMemberFormerlyKnownAsAWOL:
Use bitRead?

Sure but how does that work? This is what I am unclear of, the function and how to use it.

For Example:

int bq769x0::SystemControl()
{
  regSYS_STAT_t sys_stat;
  sys_stat.regByte = readRegister(SYS_STAT);
  Serial.print("System Control: ");
  Serial.print(byte2char(sys_stat.regByte));
 }

Will return

System Control: 00001001

I am trying to break down the bits down to a boolean, yes/no, on/off or 1 or 0… something that I can use for controlling another device when certain conditions are met.

Use bitRead():

  bool  C1, C2, C3, C4, C5;

  // assume System Control 1 value is in the byte variable 'sysCtl1'

  C5 = bitRead(sysCtl1, 0);
  C4 = bitRead(sysCtl1, 1);
  C3 = bitRead(sysCtl1, 2);
  C2 = bitRead(sysCtl1, 3);
  C1 = bitRead(sysCtl1, 4);

or a shift and mask:

  bool  C1, C2, C3, C4, C5;

  // assume System Control 1 value is in the byte variable 'sysCtl1'

  C5 = sysCtl1 & 0x01;
  C4 = sysCtl1>>1 & 0x01;
  C3 = sysCtl1>>2 & 0x01;
  C2 = sysCtl1>>3 & 0x01;
  C1 = sysCtl1>>4 & 0x01;

Have you tried using bitRead?

tedrobe:
Sure but how does that work? This is what I am unclear of, the function and how to use it.

Why don’t you look at the reference?

tedrobe:
For Example:

int bq769x0::SystemControl()

{
 regSYS_STAT_t sys_stat;
 sys_stat.regByte = readRegister(SYS_STAT);
 Serial.print("System Control: ");
 Serial.print(byte2char(sys_stat.regByte));
}




Will return

System Control: 00001001

I am trying to break down the bits down to a boolean, yes/no, on/off or 1 or 0.... something that I can use for controlling another device when certain conditions are met.

That code may print something, but it won't "return" anything as there's no 'return' statement as you promised the compiler there would be in the declaration. In fact, if you you turn up the compiler warnings, you'll see it's unhappy about that.

void printasBool(byte n) {
  for (int i=7; i >= 0; i--) {
    if (bitRead(n, i)) {
      Serial.print("true ");
    } else {
      Serial.print("false ");
    }
  }
  Serial.println();
}

For extra credit, implement:

printasBool(byte n, byte interestingBits, char *labels[8])

Awesome. Thanks guys I think I got it and looks like I am able to stone to birds with one bush with that information.