UL is for unsigned long type.
Cheers.
( 1UL << 31)
then this line of code say 31 time shift to left an unsigned long?
y use this ? can u explain to me?
It looks like you've extracted that from the following code:
while (!(LPC_ADC->ADGDR & ( 1UL << 31)))
; /* Wait for Conversion end */This is typical C-style bitwise manipulation.
SUMMARY DESCRIPTION:In this device's architecture,
LPC_ADC->ADGDR is at least a 32-bit status/control register. When A/D conversion is complete, the device will set bit 31 (0-indexed; MSB). The while loop prevents the program from moving forward until this bit is set.
STEP-BY-STEP SYNTHESIS AND INTERPRETATION:1.)
1ULIn binary, this looks like:
MSB LSB
31 0
0000 0000 0000 0000 0000 0000 0000 0001 // 1ULThis is interpreted as decimal 1 represented as unsigned long type. Long type, in this case, is clearly mapped to 32-bit representation. A
2's complement (aka signed) integer representation uses the MSB to differentiate positive and negative numbers...the next step will make clear why UL is used.
2.)
1UL << 31In binary, this looks like:
MSB LSB
31 0
1000 0000 0000 0000 0000 0000 0000 0000 // 1UL << 31This is interpreted as 1UL left-shifted 31 times. Arithmetically, left-shifting an unsigned integer is the decimal equivalent of multiply-by-2 for each shift, so the statement is the same as 1 x 2
31 = 2,147,483,64
8, which is precisely 1 unit more than the highest positive integer that can be represented by 32-bit 2's complement, consequently resulting in an overflow condition if signed integer representation (the default option) were used.
This is done for code readability and clarity of intent. The device doesn't really perform the left-shift operation on a predefined constant...that's simply waste. The compiler will inevitably optimize this down to something efficient but a lot less meaningful to a human reader (think "magic numbers").
3.)
LPC_ADC->ADGDR & (1UL << 31)In binary, this looks like:
MSB LSB
31 0
Txxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx // LPC_ADC->ADGDR
1000 0000 0000 0000 0000 0000 0000 0000 // 1UL << 31
---------------------------------------
T000 0000 0000 0000 0000 0000 0000 0000 // LPC_ADC->ADGDR & (1UL << 31)For the ADGDR register,
T represents the target status bit and
x represents bits whose values we don't care about (they can be any state).
& is the bitwise AND operator, which is used to mask off the bits we don't care about, extracting just the target bit in question...(0 & wuteva) always returns 0. On the other hand, (1 & wuteva) returns the value of wuteva.
This means there are only 2 possible states for the expression
LPC_ADC->ADGDR & (1UL << 31):
0000 0000 0000 0000 0000 0000 0000 0000 = 0
1000 0000 0000 0000 0000 0000 0000 0000 = 2,147,483,648 (not 0)4.)
!(LPC_ADC->ADGDR & (1UL << 31))! is the logical NOT operator, which is boolean...in other words, if 0 return FALSE, otherwise return TRUE. Putting this together with...
5.)
while(!(LPC_ADC->ADGDR & (1UL << 31)));...the while loop is TRUE (or not FALSE) and loops indefinitely doing nothing (
; immediate termination) until the target bit T is set.
When the A/D conversion is complete, the device will set the T bit, which will subsequently break out of the loop and proceed to handle the converted data.