I program C on an embedded system. The processor architecture is 32 bits ( sizeof(int)- 32 bits, sizeof(short)- 16 bits). There is a 32-bit variable, which is a managed memory register ( CTRL_REG), which is indicated as soon as the lower 16 bits are used, and they contain a signed 16-bit integer value (writing to higher bits does not affect). Access to memory should be 32-bit, so I canโt just pounce on a pointer to a couple of bytes, and I canโt accept the statement either. I am concerned that auto type advancement will get confused with what I am storing by expanding the character bit to bit 31 instead of leaving it to bit 15 where I want it. What is the best way to keep something in this place?
Here is my original code, which I'm almost certainly mistaken:
#define CTRL_REG *((volatile unsigned int *)0x4000D008u)
short calibrationValue;
CTRL_REG = -2 * calibrationValue;
Then I tried this, but I think it can still be subjected to integer progress at the destination:
CTRL_REG = (short)(-2 * calibrationValue);
Then finally I thought about it:
CTRL_REG = (unsigned short)(short)(-2 * calibrationValue);
I canโt evaluate these parameters very well, because they all work in my tests, because it calibrationValueturns out to be negative (this is a calibration parameter that is specific to each device, and therefore can be positive on some devices), so after multiplying by -2, I in the end, I keep a positive value, and thus, in fact, I do not encounter the problem that I expect in my tests.
Your help is much appreciated even if it just says "you think too much."
source
share