Go Down

Topic: Bits not shifting (Read 248 times) previous topic - next topic

dicky96

Apr 03, 2013, 03:09 pm Last Edit: Apr 03, 2013, 03:11 pm by dicky96 Reason: 1
After much scratching of the head I connected the logic analyser and figured out what is happening but not what is wrong with this code.....

Code: [Select]
for (byte a=0x10; a<0x12; a++)
   {
   byte d = 0x01;

   I2C_WRITE_BYTE(a, d);  // 1  register 0x10
//    delay(300);
   d<<1;
   I2C_WRITE_BYTE(a, d); // 2
//    delay(300);
   d<<1;
   I2C_WRITE_BYTE(a, d); // 4
//    delay(300);
   d<<1;
   I2C_WRITE_BYTE(a, d); // 8
//    delay(300);
   d<<1;
   I2C_WRITE_BYTE(a, d); // 16
//    delay(300);
   d<<1;
   I2C_WRITE_BYTE(a, d); // 32
//    delay(300);
   d<<1;
   I2C_WRITE_BYTE(a, d); // 64
//    delay(300);
   d<<1;
   I2C_WRITE_BYTE(a, d); // 128
//    delay(300);
   d = 0x00;
   I2C_WRITE_BYTE(a, d); // 00
//    delay(300);
   }


I think it's fairly obvious this code is supposed to write value d to address a, shift d one bit to the left and write the new value to a

However if I take monitor the i2c bus on my logic analyser it continually writes the initial vlaue of d (does not bit shift)

It looks OK to me and the C reference manual says the syntax d<<1; is correct so what is wrong?

(I had to comment out the delays to give my anayser a better chance to capture the data)

Rich

afremont

You have to reassign the value to the variable after shifting.

d = d << 1;
Experience, it's what you get when you were expecting something else.

dicky96

Damn it was as simple as that  :smiley-red:

thank you

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

retrolefty

Are we shifting in the correct direction for the desired divide results?

Lefty

Go Up