Pages: [1]   Go Down
Author Topic: Bits not shifting  (Read 199 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: April 03, 2013, 08:11:44 am by dicky96 » Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

d = d << 1;
Logged

Experience, it's what you get when you were expecting something else.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Damn it was as simple as that  smiley-red

thank you
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 289
Posts: 25697
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or simply
Code:
d <<= 1;
Logged

"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.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Lefty
Logged

Pages: [1]   Go Up
Jump to: