The following code configures pin 5 from Port C as a digital input with pull-up and filter. The MODE5 bitfield from GPIO_PC_MODELregister must be set to 0b0011, and the correspondent bit in the GPIO_PC_DOUT register must also be set to 1 to determine the pulldirection. A pin can be configured either by using the functions available in the emlib or through a direct register write:
->
|
.DOUT
For EFR32FG14P231F256M32 i have the following combination of C operators. with the descrition shown bellow.I am having trouble matching the description to the code. -> is a pointer. we have pointing ...DOUT
is a constant expression having the binary value P[gpioPortC]
. You could simply replace it with the literal constant value 0x20 or 32, however the purpose of the expression is to create a
bit mask
with bit-5 set.
It is expressed in this manner because:
GPIO->P[gpioPortC ].DOUT = GPIO->P[gpioPortC ].DOUT | (1 << 5); //Input enabled with pull up and filter
GPIO->P[gpioPortC ].MODEL = (GPIO->P[gpioPortC ].MODEL & ~_GPIO_P_MODEL_MODE5_MASK) |
GPIO_P_MODEL_MODE5_INPUTPULLFILTER;
it documents intent without having to write a comment. It is self-documenting code - it says to the reader 1 << 5
when coding it, it is less error prone to let the compiler calculate the constant value of a bit 5 mask.0b100000
So:sets bit 5 of the variable (or in this case register
)
x = x | ( 1 << 5) ;
is a pointer. We have pointing directions on both sides?x
I am not clear what you are asking, but is not a pointer; it is the
x |= (1 <<5) ;
structure dereference
operator; its left-hand operand is a pointer, and its right hand operator is a member of the structure to which the left-hand is a pointerto
- form
->
(which may or may not be a pointer.And before you ask, the second line of your fragment, that has teh
:->
Sets all the bits of that are 1 in to zero. It is the revers of the previous idiom:In your particular example you have a more complex expression:Here you need to understand the concept of
operator precedence. has precedence over
x = x & ~mask ;
, so the x
sub expression is evaluatet first so that the mask
applies to the
x &= ~mask ; // Clear mask bits
x &= mask ; // Set mask pits
member of the referenced structure. Note that
GPIO->P[gpioPortC].DOUT | (1 << 5)
is also an operator, (with higher precedence than . So that in the general form:...->
|
GPIO->P[gpioPortC].DOUT
The << operator means "shift bits left", and it works as follows:| (1 << 5);
shifts the bits in DOUT
a[]
, ->
b
x | y ;
places to the left.to answer your question directly, 1 << 5 moves the bits in 0x01 5 times to the left, resulting in 0x20, which is 32 in decimal notation. This has the effect of multiplying a number by 2 n times (n=5 in your case). The resultant value is ORed with the value in DOUT, and then stored in DOUT itself.
x = GPIO->P[gpioPortC].DOUT
y = (1 << 5)
EDIT:To answer the question OP added in the comments -.
a << b
The operator is shorthand for . The first line in your code instructs the following:Take the value within and OR it with
.
Assign the result to x = x + 5
.
The second line does a pretty similar operation, only instead of preforming a bitwise OR and storing it in the same field, it preforms a bitwise AND of the field with a negated form of some predefined value, and then preforms a bitwise OR of the result with another predefined value, and then stores it within that field.->
It is important to understand that simply using the (*obj).field
operator on an object does nothing except allowing you to access the fields stored within this object (which themselves might be pointers to other locations in memory, or primitive variables).
GPIO->P[gpioPortC].DOUT
对于EFR32FG14P231F256M32,我有以下的C运算符组合。0x10
是一个指针。我们两边都有指向性?GPIO->P[gpioPortC].DOUT
我想是指把 以下成员
细胞。->
但改级