I have this code:
void GPIO_InitPortPin(uint8* PortControl, uint8 Pin, uint8 PinDir){
*PortControl &= (~(1U << Pin));
*PortControl |= (PinDir << Pin);
}
If the PortControl
register is 11111111
, the first line should clear the bit of whatever the corresponding Pin
is, but unexpectedly it clears all the register.
My Client Function is: GPIO_InitPortPin(&TRISB, GPIO_PIN_0, GPIO_IN);
GPIO_PIN_0
and GPIO_IN
are macros defined as follows:
#define GPIO_PIN_0 (0)
#define GPIO_PIN_1 (1)
...
#define GPIO_PIN_7 (7)
#define GPIO_OUT (0)
#define GPIO_IN (1)
So, I tried to cast Pin to sint8
: *PortControl &= (~(1U << (sint8) Pin));
and it worked. Also, I tried to hard-code the Pin as 1
and it worked fine. I know the problem is that Pin
is uint8
or specifically to be unsigned
since
*PortControl &= (~(1U << 1));
also worked fine.
My Question is: why the right operand type affects the shifting operation, although I've red otherwise before(That RHS doesn't affect operation)?
Iam using SDCC compiler.
Edit : I've tested the function without casting on gcc and it worked as expected.