Course: Computer Architectures
Source lesson: NXP LPC1768 LED and GPIO

1. The 3-step mental model for controlling a pin

Before an LPC1768 pin can drive an LED, software must answer three questions in order, like configuring a smart wall socket:

  1. “What job does this pin do?” PINSEL (e.g., LPC_PINCON->PINSEL4) picks the pin’s hardware role (00 = plain GPIO).
  2. “Does it push signals out or listen in?” FIODIR sets direction (1 = Output for LEDs, 0 = Input for buttons).
  3. “Turn it ON or OFF!” FIOSET pushes 1 (HIGH / LED ON) and FIOCLR pushes 0 (LOW / LED OFF).

2. Register reference for Port 2 LEDs (P2.0–P2.7)

From User Manual Tables 75, 84, 102 and 104 (slides 25–28):

RegisterPlain-English roleBits per pinAccessResetHow software uses it
PINSEL4Pin job selector for P2[15:0]2 bitsR/W0Write 00 to select GPIO (P2.0–P2.7 occupy bits 15:0)
FIODIRPin direction (0 = input, 1 = output)1 bitR/W0Write 1 on a pin’s bit to make it an output
FIOSET”Turn ON” register1 bitR/W0Write 1 to force pin HIGH; writing 0 has no effect
FIOCLR”Turn OFF” register1 bitWO0Write 1 to force pin LOW; writing 0 has no effect
FIOPINLive pin state1 bitR/W0Read current digital HIGH/LOW level (masked by FIOMASK)
FIOMASKPin safety lock1 bitR/W00 = pin unlocked (default); 1 = pin blocked from writes/reads

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).

3. Step 1 — Selecting GPIO mode with PINSEL4 (slides 24–26, 29)

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 (=):

void all_LED_on(void)  { LPC_GPIO2->FIOSET = 0x000000FF; } /* Slide 31 */
void all_LED_off(void) { LPC_GPIO2->FIOCLR = 0x000000FF; } /* Slide 31 */

For single-LED control (num from 0 to 7), funct_led.c defines a lookup mask array (slide 30):

const unsigned long led_mask[] = { 1UL<<0, 1UL<<1, 1UL<<2, 1UL<<3,
                                   1UL<<4, 1UL<<5, 1UL<<6, 1UL<<7 };
 
void LED_On(unsigned int num) {
    LPC_GPIO2->FIOSET |= led_mask[num];
    led_value = LPC_GPIO2->FIOPIN;
}

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

  1. Set specific bit(s) to 1 (keep others): REG |= mask;
  2. Clear specific bit(s) to 0 (keep others): REG &= ~mask;
  3. 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 , 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):

PurposeHex mask (slides)Equivalent << shift formResulting 32-bit mask
Clear P2.0–P2.7 in PINSEL4 (16 bits)PINSEL4 &= 0xFFFF0000;PINSEL4 &= ~((1UL << 16) - 1);
or PINSEL4 &= (0xFFFFUL << 16);
1111...1111 0000...0000
Set P2.0–P2.7 outputs in FIODIR (8 bits)`FIODIR= 0x000000FF;``FIODIR

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 (), 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

  1. 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 () and bit 5 in FIODIR/FIOSET:

    LPC_PINCON->PINSEL4 &= ~(3UL << 10);
    LPC_GPIO2->FIODIR   |=  (1UL << 5);
    LPC_GPIO2->FIOSET    =  (1UL << 5);
  2. 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 ( bits = 4 hex digits), whereas FIODIR allocates 1 bit per pin ( bits = 2 hex digits).

  3. 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.

  4. 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.

  5. 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).