Go Down

Topic: Getting strange warning with 2 of 3 identical right shift? (Read 472 times) previous topic - next topic

RicThot

Feb 06, 2013, 07:13 pm Last Edit: Feb 06, 2013, 07:49 pm by RicThot Reason: 1
Hi guys,

I'm a pretty confused newbie right now. Not sure what happened with my code but it was working flawlessly until (???) happened - then I started getting weird results on the serial monitor and noticed 2 warnings when my code compile, which I didn't notice before, I'm wondering if that could explain the mess.

My code is pretty simple, it reads/erase/write bytes from/to an EEPROM chip (Winbond W25Q16BV).

I have 3 'address' variables, declared and initilialized as follow:
Code: [Select]

void loop()
{
unsigned long read_address;
unsigned long erase_address;
unsigned long write_address;

...some code...

erase_address = 0;
erase_eeprom(erase_address);
write_address = 0;
write_eeprom(write_address);
read_address = 0;
read_eeprom(read_address);

...more code...
}

void erase_eeprom(unsigned long address)
{
  ...
  spi((byte)address>>16);  //MSB
  spi((byte)address>>8);
  spi((byte)address);        //LSB
  ...
}
void write_eeprom(unsigned long address)
{
  ...
  spi((byte)address>>16);  //MSB
  spi((byte)address>>8);
  spi((byte)address);        //LSB
  ...
}
void read_eeprom(unsigned long address)
{
  ...
  spi((byte)address>>16);  //MSB
  spi((byte)address>>8);
  spi((byte)address);        //LSB
  ...
}


All 3 functions handles the unsigned long address pretty much the same way, but I'm getting right shift warnings on the erase and write function and I can't figure out why:
Code: [Select]

W25Q16BV_readv2.ino: In function 'void sector_erase(long unsigned int)':
W25Q16BV_readv2.ino:174: warning: right shift count >= width of type
W25Q16BV_readv2.ino: In function 'void write_eeprom(long unsigned int, byte)':
W25Q16BV_readv2.ino:186: warning: right shift count >= width of type


As far as I know, an unsigned long is 4bytes/32bits... why would I get this warning when right shifting 16 bits ?
And why am I getting the warning only on those 2, but not the read_eeprom function?

AWOL

A byte is 8 bits wide.
What do you think happens to it, if you shift it right sixteen bits?
"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.

RicThot

#2
Feb 06, 2013, 07:35 pm Last Edit: Feb 06, 2013, 07:41 pm by RicThot Reason: 1
Yeah... I get it now - I guess I really misunderstood using a typecast and shifting at the same time, thanks for clearing that up. I must have added this at some point and can't remember.

I'm still confused why I'm not getting the same warning on the read_eeprom() function though...



EDIT....
Actually, I just figured out where I failed... it was supposed to be:
Code: [Select]

spi((byte)(address>>16));
...


AWOL

Is that really your code?
Why have you put a semicolon at the end of the function definitions?
"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.

RicThot

Not really the code, typed in by hand in my post... beginner mistake, I don't usually put semi-colon at the end of my function declaration ;)

I made some edits to avoid further confusion... thanks.

Go Up