Crucial width difference: 2 bits/pin vs. 1 bit/pin
Because each pin can take up to 4 multiplexed functions (00, 01, 10, 11), PINSEL registers use 2 bits per pin (so one 32-bit PINSEL register covers 16 pins). All FIO* registers use 1 bit per pin (covering 32 pins per port). For the same 8 LEDs (P2.0–P2.7), PINSEL4 requires 16 bits (4 hex digits) whereas FIODIR/FIOSET/FIOCLR require 8 bits (2 hex digits).
Physical chip pins are multiplexed across peripherals (P2.0 can act as GPIO, PWM1.1, or TXD1). From Table 84 (p. 120, slide 26):
Bits 1:0 control P2.0
Bits 3:2 control P2.1
…
Bits 15:14 control P2.7
Setting a pin’s 2-bit field to 00 selects GPIO. To configure P2.0–P2.7 together without altering P2.8–P2.15 (such as button/interrupt lines P2.10–P2.13), LED_init() uses bitwise AND (&=) with 0s on bits 15:0 and 1s on bits 31:16 (slide 29):
LPC_PINCON->PINSEL4 &= 0xFFFF0000;
anything & 1 stays unchanged (P2.8–P2.15 keep their existing configuration).
anything & 0 becomes 0 (P2.0–P2.7 become 00 = GPIO).
Writing PINSEL4 = 0x00000000; directly would wipe out P2.8–P2.15 and should be avoided.
4. Step 2 — Setting direction with FIODIR (slides 27–29)
From Table 104 (p. 133, slide 28), bit n of FIOxDIR controls pin Px.n: 0 = input (reset default), 1 = output. To set P2.0–P2.7 as outputs without touching P2.8–P2.31, LED_init() uses bitwise OR (|=) with 1s on bits 7:0 (slide 29):
LPC_GPIO2->FIODIR |= 0x000000FF;
anything | 1 becomes 1 (P2.0–P2.7 are set to Output).
anything | 0 stays unchanged (P2.8–P2.31 keep their directions).
5. Step 3 — Turning LEDs ON and OFF (FIOSET, FIOCLR, FIOPIN)
Because FIOSET and FIOCLR ignore 0 bits, writing 1 to a bit only acts on that pin—other pins on Port 2 are unaffected even with plain assignment (=):
Qualification on LED_Off in slide 30 vs. User Manual Table 102
Slide 30 prints LED_Off as:
LPC_GPIO2->FIOCLR &= ~led_mask[num];
Check the hardware specification on slide 27 (Table 102) and all_LED_off() on slide 31: FIOCLR is Write-Only (WO) and writing 1 clears the corresponding output pin to LOW, while writing 0 has no effect. Reading FIOCLR in a read-modify-write (&=) is undefined for a write-only register, and ~led_mask[num] writes 0 to target bit num and 1s to every other bit! The hardware-consistent way to turn off only LED num is:
LPC_GPIO2->FIOCLR = led_mask[num];
6. Bitwise C patterns: hex masks vs. << shift syntax
Both forms compile to the same bit mask, but << shifts make pin positions explicit without mental hex conversion. Using 1UL (unsigned long) avoids signed-shift issues on 32-bit registers.
Three golden C rules
Set specific bit(s) to 1 (keep others):REG |= mask;
Clear specific bit(s) to 0 (keep others):REG &= ~mask;
FIOSET / FIOCLR:REG = mask; (since 0 bits do nothing)
Worked example A: Configure and toggle only P2.3 (LD8)
For pin n = 3:
In PINSEL4 (2 bits/pin): starts at bit 2×3=6, so P2.3 uses bits 7:6. The 2-bit pattern 3UL (11 in binary) shifted by 6 covers bits 7:6.
In FIODIR, FIOSET, FIOCLR (1 bit/pin): P2.3 uses bit 3 (1UL << 3).
LPC_PINCON->PINSEL4 &= ~(3UL << 6); /* Clear bits 7:6 to 00 -> P2.3 is GPIO */LPC_GPIO2->FIODIR |= (1UL << 3); /* Set bit 3 to 1 -> P2.3 is Output */LPC_GPIO2->FIOSET = (1UL << 3); /* Drive bit 3 HIGH -> LD8 ON */LPC_GPIO2->FIOCLR = (1UL << 3); /* Drive bit 3 LOW -> LD8 OFF */
Worked example B: Rewriting PINSEL4 &= 0xFFFF0000; and FIODIR |= 0x000000FF; with <<
A general rule for N consecutive 1 bits starting from bit 0 is (1UL << N) - 1 (for example, 1UL << 16 is 0x00010000, minus 1 gives 0x0000FFFF):
7. Checking LED state in the Keil software debugger (slide 32)
In Keil uVision’s simulator, open Peripherals -> GPIO Fast Interface -> Port 2 to inspect live 32-bit checkboxes (31 down to 0) for FIO2DIR, FIO2MASK, FIO2SET, FIO2CLR, FIO2PIN and Pins. After all_LED_on() executes:
FIO2DIR = 0x000000FF (bits 7:0 checked →P2.0–P2.7 are outputs)
FIO2SET = 0x000000FF (bits 7:0 checked → all 8 LEDs are driven HIGH / ON)
Common confusions
Writing 0 to FIOSET expecting to turn an LED off: Writing 0 to FIOSET is ignored. To drive a pin LOW, write 1 to the corresponding bit in FIOCLR.
Using 1 bit per pin in PINSEL4: Each pin needs 2 bits in PINSEL4, so P2.5 is at bits 11:10 (2×5=10), not bit 5.
Using plain = on PINSEL4 or FIODIR: Direct assignment overwrites unrelated pins in the same register; always mask with &= (clear) or |= (set).
Retrieval questions with answers
Write the C lines using << shifts to configure only P2.5 (LD6) as a GPIO output and turn it ON.
Pin 5 uses bits 11:10 in PINSEL4 (2×5=10) and bit 5 in FIODIR/FIOSET:
Why does PINSEL4 &= 0xFFFF0000; use 4 hex zeros (16 bits) while FIODIR |= 0x000000FF; uses 2 hex Fs (8 bits) for the exact same 8 LEDs? PINSEL4 allocates 2 bits per pin (8×2=16 bits = 4 hex digits), whereas FIODIR allocates 1 bit per pin (8×1=8 bits = 2 hex digits).
Before any change, P2.0 (LD11) and P2.1 (LD10) are ON (FIOPIN bits 1:0 = 11). After running LPC_GPIO2->FIOCLR = 0x01;, which LEDs remain ON?
Bit 0 (P2.0 / LD11) is cleared to 0 (OFF). Bit 1 (P2.1 / LD10) receives a 0 in FIOCLR, which has no effect, so LD10 (P2.1) remains ON.
Why is LPC_GPIO2->FIOSET = 0x000000FF; safe without |=, whereas LPC_GPIO2->FIODIR = 0x000000FF; is risky?
In FIOSET, writing 0 to bits 31:8 does nothing, so P2.8–P2.31 keep their current output levels. In FIODIR, writing 0 to bits 31:8 actively forces those pins to become inputs.
How would you rewrite LPC_GPIO2->FIODIR |= 0x000000FF; using << shift syntax? LPC_GPIO2->FIODIR |= (1UL << 8) - 1; (because 1UL << 8 is 256 = 0x100, and subtracting 1 yields 255 = 0xFF).