Hi Martin,
I have experienced this problem a few times specifically with the FAT. It's down to the compiler not necessarily performing 32-bit maths when shifting.
One way around is to typecast however doing this via a calculation is not really currently possible. We did go to some lengths to try and automatically add a typecast here but I can't remember what the outcome of the investigation was.
Code: Select all
longvar = (long) longvar | (B2 << 16);
longvar = (long) longvar | (B2 << 24);
The (long) is the typecast which tells the compiler to deal in 32-bit when shifting.
Another approach is to use the C unions I added to the TypeDefs CAL files. The v5 and v6 FAT component currently uses these as they are known to work reliably on every target family without having to remember to typecast everything. This is also much more efficient as each bit shift consumes 1 clock cycle whereas the union pops the byte directly into the correct place in the variable without shifting.
MX_Union32 unionvar;
unionvar.AsByte[0] = B0;
unionvar.AsByte[1] = B1;
unionvar.AsByte[2] = B2;
unionvar.AsByte[3] = B3;
longvar = unionvar.AsLong;
I wanted to get these functions into the calculation icon but so far they have not made it in. I can certainly try and bump them up in priority if this is something people would like.
The MX_Union32 union has the values AsLong, AsInt[2] and AsByte[4].
There is also another union for dealing with 16-bit values, The MX_Union16 union has the values AsInt and AsByte[2].