I am programming an AVR chip (atmega1284p) with c code, I am using SPI to read in 4 bytes from a chip on my circuit. I would like to take those 4 bytes and return them as an unsigned long.
I can print the bytes to the serial port and I have received logical values, which I can calculate by hand to get the correct value. I then try to do this with programming and I get very peculiar results. Here is the code snippet.
Note: returnBytes is a 4 character array with the correct character values. Also, I am using a bitmask to make sure that the shift doesn't have anything weird going on -- just incase (works the same with and without bitmask).
unsigned long returnLong = (returnBytes&0xFF)<<24; returnLong |= (returnBytes&0xFF)<<16; returnLong |= (returnBytes&0xFF)<<8; returnLong |= (returnBytes&0xFF); return returnLong;
What I get from this is peculiar, in that it should be counting from 4.2bil to 0. It starts at around 4.2 bil, generally counts down and then drops to 30,000 after a couple of counts, then jumps back to 4.2bil after a couple of counts. (Yes the counter will wrap back around once it hits 0).
I am certain that the bytes are correct and my method of printing the unsigned long is correct.
Here is the output of this code (First line is bytes 3 2 1 0, Second line is the value of the unsigned long)
255 254 230 157 Counter Value: 4294960797 255 253 206 25 Counter Value: 4294954521 255 252 182 90 Counter Value: 4294948442 255 251 158 153 Counter Value: 4294942361 255 250 134 159 Counter Value: 4294936223 255 249 110 139 Counter Value: 28299 255 248 87 207 Counter Value: 22479 255 247 65 170 Counter Value: 16810
Hopefully this is enough info.