In XC16 (for the 16-bit dspic33E***MU8xx and MC family) I use union QEICounter and similar unions to convert two 16-bit words (e.g. read from UDP/serial) to 32-bit int32_t or ulong, which is defined like this:
typedef union
{
struct
{
unsigned int loWord; // Counter high word
unsigned int hiWord; // Counter low word
}f; // field access
unsigned int w[2]; // 16 bits access
unsigned long l; // 32 bits access
}qeiCounter;
I noticed that sometimes the compiler apparently doesn’t link the w field writes to the l read with repeated usage. I tried declaring the union variable with volatile, and that apparently does not solve it either.
Any idea how to properly do this? I adopted the union trick from older Microchip libraries (plib,MLA)
I’m still in transition from xc16 to xcdsc, so anything that works on both would be great.