If you use the 8-bit shift of the MCU, the entire 32-bit variable is a bit of work. In this case, it is better to read 4 bytes of CurrentPosition using pointer arithmetic. Listing:
unsigned char *p = (unsigned char*)&CurrentPosition;
does not change CurrentPosition, but if you try to write p [0], you will change the least significant byte of CurrentPosition. If you want to make a copy, follow these steps:
unsigned char *p = (unsigned char*)&CurrentPosition; unsigned char arr[4]; arr[0] = p[0]; arr[1] = p[1]; arr[2] = p[2]; arr[3] = p[3];
and work with arr. (If you want the high byte to change the order in these assignments).
If you prefer 4 variables, you can obviously do:
unsigned char CP1 = p[0]; unsigned char CP2 = p[1]; unsigned char CP3 = p[2]; unsigned char CP4 = p[3];
Maciej hehl
source share