質問

I have learned about shift left and shift right registers in bitwise operators. But i am confused with below code. RX_DR shifted ? what does that mean ? this is just random example i just need to know meaning of it.

nrf24_configRegister(STATUS,(1<<RX_DR)|(1<<TX_DS)|(1<<MAX_RT));

Please help.

役に立ちましたか?

解決

It looks like these constants are the bit numbers of various flags in a register. Looking at nRF24L01.h leads me to believe their values are:

#define RX_DR  6
#define TX_DS  5
#define MAX_RT 4

This corresponds to a bit layout like:

                TX_DS
                v 
(bit #) 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
            ^       ^
(flag)      RX_DR   MAX_RT

Here I've labeled a few of the bits in an 8-bit register. Bit 6 is the RX_DR flag, bit 5 is the TX_DS flag, and bit 4 is the MAX_RT flag.

(1<<RX_DR)|(1<<TX_DS)|(1<<MAX_RT)

This bit of code constructs a register value where those three bits are set and the rest are unset. How does it do that? Consider 1<<TX_DS. This takes the value 1 (binary 00000001) and shifts it left 5 places, yielding 32 (binary 00100000).

That value is bitwise ORed (|) with the other two flags. ORing numbers together combines their 1 bits, yielding a value where all of the 1 bits from each operand are set.

1<<RX_DR  == 1<<6 == 01000000 in binary
1<<TX_DS  == 1<<5 == 00100000 in binary
1<<MAX_RT == 1<<4 == 00010000 in binary

ORing 1<<RX_DR and 1<<TX_DS and 1<<MAX_RT together gives the value 112, or in binary:

01110000

See how that binary value has bits 6, 5, and 4 set?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top