# Understanding Bits from a byte (i2c)

Hi

I currently have a project on the go that talks to a sp5055 via i2c. Link to the datasheet ( http://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CC8QFjAA&url=http%3A%2F%2Ff5ad.free.fr%2FDocs_ATV%2FSP5055.pdf&ei=omynUruGBsaVhQfiloDoAg&usg=AFQjCNGqs1FzRxLmUIjhN1vWaJKO_hYIhg&bvm=bv.57799294,d.ZG4 ) I can write to the device and read from it no problems.

What i am doing it requesting 1 byte from the device after a write to check on something called a phase lock loop

the data sheet says it will return a 8 bit byte, bit 7 of the byte is the phase lock indicator which is 0 for no lock and 1 for a lock.

when i look at the bits of the byte received the bit 6 is the PLL indicator i know this as when i write some bad data to it so it cannot lock the bit becomes a 0.

the code i am using at the moment:

`````` byte rg = Wire.requestFrom(0x61, 1);
if ( rg == 0 ){
lcd.print("No Data");
}
while(Wire.available())
{
lcd.print("MSB:");
for (int d=8; d>0; d--){

}
}
``````

According to the datasheet the MSB is transmitted first.

The bit data i get With PLL Lock 00101100

Without PLL lock 00001100 To achieve this i send a incorrect frequency calculation.

if i only read 7 bits of the byte received everything is ok, i get a logic 1 on bit 6 (Which should be 7 according to the datasheet)

`````` byte rg = Wire.requestFrom(0x61, 1);
if ( rg == 0 ){
lcd.print("No Data");
}
while(Wire.available())
{
lcd.print("MSB:");
for (int d=7; d>0; d--){

}
}
``````

Anyone have any ideas on what im doing wrong.

Thanks

Change the print to:

lcd.print(e, BIN);

leading 0s may be blanked (Serial.print blanks them on the serial monitor) but you can see if your data is getting hosed by the bit shifting stuff or not.

Are A2-A1-A1 coming back correctly? "5 Level ADC data from P6 (see Table 3)"

When "counting" bits, we do it like 1, 2, 3, 4, 5, 6, 7 and 8. But bitRead starts at 0, so you are missing 1 to the position you are reading.

So, bit 7 of the datasheet is bit 6 for bitRead.

Look at the reference, bitRead's LSB is 0, not 1, and MSB is 7 (if it's a byte). So bit 7 when "counting" is bit 6 for bitRead.

Thanks for the replies,

CrossRoads: Are A2-A1-A1 coming back correctly? "5 Level ADC data from P6 (see Table 3)"

As far as i know they are, in my example it shows bit 0 = 0, bit 1= 0 bit 2 = 1 according to the table this = a certain voltage hitting p6, i think!(not 100%) that p6 is connected to a signal meter from another device in circuit.

its no big deal bit 7 being at bit 6 i can still class it as a PLL , i was just trying to understand bits/bytes.

I will keep reading and trying to understand, it just takes 4 years for anything to sink in.