Found a "binary register reader" but it is reading the bits in reverse (mirror)

I found this binary code reader which I cleaned out and made some minor modifications but it is not perfect.

It works well to read the binary code off a devices registers but it reports the code to the serial monitor in reverse (mirrored)

As an example, I write(or I am just reading one that I know) to the register 0x1C of the MPU6050(0x68) the eight bit binary value 0b11000000
then I use this “Binary Reader” to read it back to me and it reports 00000011 = Binary Value

(I have read other reg that I know the bits values and it always reports the correct bits just in reverse order.)

I tried to change the code to reverse it but with no success.(I am new to coding) Can anyone alter this code to report back “not mirrored” Thanks

Here is the complete code:

#include <Wire.h>


const int MPU_ADDR=0x68;  // Put the address of the actual device here

const int REG_ADDR=0x1C;  // Put the register address to be read here


void setup() {

  Serial.begin(115200);
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(REG_ADDR);
  Wire.endTransmission(false); 
  Wire.requestFrom(MPU_ADDR,1,true);
  uint8_t bin_value = Wire.read();           
  int a = bitRead(bin_value,0);   
  int i;
  for (i=0; i<8;i++)
  {
    int b=bitRead(bin_value,i);
    Serial.print(b);
  }
    Serial.println(" = Binary Value"); 
  }

void loop() {

}

Try:
int b=bitRead(bin_value, 7 - i) ;

Instead of;
int b=bitRead(bin_value,i) ;

Or just use

  uint8_t bin_value = Wire.read();
  Serial.println(bin_value, BIN);

6v6gt:
Try:
int b=bitRead(bin_value, 7 - i) ;

Instead of;
int b=bitRead(bin_value,i) ;

Thank you, it worked the sequence is correct now.

jremington:
Or just use

  uint8_t bin_value = Wire.read();

Serial.println(bin_value, BIN);

Thank you, this one also worked…and with only a few lines of code!!!

Vertical757:
Thank you, this one also worked....and with only a few lines of code!!!

Only possible problem is that it will omit leading zeroes; it depends on your needs if that will indeed be a problem.

Please tell me more! What leading zeros will be omitted?

So far it reports back perfect with all 8 bits present even if is 00000000

void setup()
{
  byte x = 0x01;

  Serial.begin(57600);
  Serial.print(x, BIN);
}

void loop()
{
}

What does it print? 00000001 or 1?

sterretje:

void setup()

{
  byte x = 0x01;

Serial.begin(57600);
  Serial.print(x, BIN);
}

void loop()
{
}



What does it print? 00000001 or 1?

Could not get your code to work but tried something similar such as
uint8_t bin_value = Wire.read();
Serial.println(bin_value, BIN);
and it does indeed omit the leading zeros and writes only 1.

I have decided to go with the following code which I put together with the help of a lot folks and afterwards made some modifications of my own. It writes all 8 bits and in perfect order 100% of the time.
There are a lot of comments so an absolute beginner can use it and understand the code.
You can both write and read the binary value in one shot.(you can confirm the binary you just wrote)
Let me know how it works for you.

#include <Wire.h>

const int MPU_ADDR=0x68;           // Set the device address (0x68 is only an example)
const int REG_ADDR=0x1C;           // Set the register address to read or write from (0x1C is only an example)

const int BINARY=0b00011000;        // If you are writing, binary goes here, (0b00011000 is only an example)

   // You only need to alter these 3 variables above and comment or uncomment
   // the Wire.write(BINARY); below *****  to write and/or read your binary value.

void setup() {

  Serial.begin(115200);             // Set the baud rate, if using 3.3v 8Mhz use 38400 or less (works great even with 9600)
  
  Wire.beginTransmission(MPU_ADDR); // This is not necessary all the time but this is included
  Wire.write(0x6B);                 // because I noticed that the depending on what operations the
  Wire.write(0x00);                 // MPU-6050 just finished, it might go into sleep mode and then you wonder why the
  Wire.endTransmission(true);       // binary did not register. This ensures it always wakes up before the operation.
  
  Wire.beginTransmission(MPU_ADDR); // These next 4 lines write the binary.
  Wire.write(REG_ADDR);
  //Wire.write(BINARY);               // ***** Comment this normally (// in front) to only read the binary
  Wire.endTransmission(true);       // so you don't forget and write something "unwanted" to the register.
                                    // As long as // is in front of Wire.write(BINARY); this is a safe READ ONLY tool *****

  Wire.beginTransmission(MPU_ADDR); // These next 10 lines reads all 8 bits of the binary from the above selected register.
  Wire.write(REG_ADDR);
  Wire.endTransmission(false); 
  Wire.requestFrom(MPU_ADDR,1,true);
  uint8_t bin_value = Wire.read();           
  int a = bitRead(bin_value,0);   
  int i;
  for (i=0; i<8;i++)
  {
    int b=bitRead(bin_value,7-i);
    Serial.print(b);               // These next 4 lines print the binary to the serial console.
  }
    Serial.println(" = Binary Value"); 
  }

void loop() {                     // These next 3 line do absolutely nothing to contribute to the program but are necessary
                                  // for the code to be complete and compile successfully.
}