1. Objectives

  • Using GPIO pins as input and output

  • Interfacing with external LEDs, switches, and push-buttons

  • Bit manipulation in C

2. Parts List

  • LPC1769 LPCXpresso board

  • USB A-Type to Mini-B cable

  • Breadboard

  • LEDs

  • 330-Ohm Resistors

  • Jumper wires

3. Background

3.1. Bit Manipulation in C

The core of embedded system programming is setting (or clearing) specific bits in different registers inside the microcontroller. This highlights the importance of bit manipulation as a programming skill.

Most modern architectures are byte-addressable: the smallest unit of data is the byte. Nonetheless, it is possible to operate on individual bits by clever use of bitwise operators.

3.1.1. Bitwise Operators

Bitwise operators apply to each bit of their operands.

Operator Function Examples

&

Bitwise AND

0011 & 0101 = 0001
   3 &    5 =    1

|

Bitwise OR

0011 | 0101 = 0111
   3 |    5 =    7

^

Bitwise XOR

0011 ^ 0101 = 0110
   3 ^    5 =    6

~

Bitwise NOT

~00110101 = 11001010

<<

Shift left

3 << 2 = 12

>>

Shift right

8 >> 2 = 2
Note In C, numbers can be written in decimal, octal, or hexadecimal, but not in binary, e.g. 16 = 020 = 0x10.
Warning Right-shifting in C is implementation-specific. Often, logical shifting is applied to unsigned values, whereas arithmetic shifting is applied to signed values.

3.1.2. Masking

A simple assignment to a 32-bit register or memory location will overwrite all 32 bits. However, manipulating specific bits implies that the remaining bits in the register remain intact. An essential technique to achieve that is bit masking.

A mask is a value that can be used in a binary, i.e. two-operand, bitwise operation to change specific bits of some other value. Masking relies on the following rules of Boolean Algebra:

  • ANDing a bit with a 0 results in a 0. ANDing a bit with a 1 results in the same bit.

  • ORing a bit with a 0 results in the same bit. ORing a bit with a 1 results in a 1.

  • XORing a bit with a 0 results in the same value. XORing a bit with a 1 inverts the bit.

Exercises
  1. What mask and bitwise operation are required to set the third least significant bit (bit 2) to 1 without affecting the other bits in a 32-bit variable x?

  2. What mask and bitwise operation are required to reset bit 10 of a 16-bit variable y to 0?

  3. What mask and bitwise operation are required to toggle bit 20 of a 32-bit variable z?

3.1.3. Creating Masks by Shifting

If you have worked out the exercises above, you would have noticed that spelling out masks can be tedious, verbose, and error-prone. One trick that makes it easier to create masks is to use the shift operations. For example, to create a mask whose bit 10 is 1 and whose other bits are 0, you can use the following C statement:

mask = 1 << 10;
Exercises

Repeat the three exercises above by using shift operations to create the masks.

3.2. Digital Input

A GPIO pins can be configured to act as a general-purpose input pin by setting the corresponding bit in the FIODIR register to 0. A digital input pin is “digital” because it is driven by an external digital device that has only two states (HIGH or LOW); and it is “input” because its state is read by the microcontroller. That implies that some external device/circuit is needed to generate that digital input value (HIGH or LOW).

Examples for simple digital input devices include switches and push-buttons.

Caution A common mistake is to forget about or misuse the FIODIR register.

3.2.1. The FIOPIN Register

In addition to the three registers used in Experiment 1 (FIODIR, FIOSET, and FIOCLR), there are a few additional GPIO-related registers. The one that is particularly essential for reading from a digital input peripheral is FIOPIN.

This register is a R/W register that stores the current state of a port’s pins. In other words, you can write to FIOPIN to set and clear pins of a specific port. You can also read the state of port pins. FIOPEN is essential for the read operation, but since it is a R/W register, it can also be used with output pins. For instance, you can redo Experiment 1 using FIOPIN only instead of FIOSET and FIOCLR.

There is an FIOPIN register for each one of the five I/O ports, and it can be accessed in the same way FIOSET and FIOCLR are. For example, port 1’s FIOPIN register can be accessed using:

LPC_GPIO1->FIOPIN

4. Tasks

4.1. Hardware

  1. Find out which I/O Pins you can use, and choose specific ones.

  2. Connect four LEDs using a proper current-limiting resistor.

  3. Connect two switches using a proper current-limiting resistor.

4.2. Software

Pick an LED and blink it using only one FIOPIN statement and one delay loop!

Tip Use bitwise exclusive-OR.

4.2.2. Implement LED Scrolling

  1. Write a program that makes it look like the light is scrolling through 4 LEDs that are connected externally. The scroll effect can be achieved by turning LEDs ON and OFF sequentially.

  2. Use two switches to control the scrolling. For example, you can use one switch to turn the scrolling ON and OFF, and the second switch to reverse the scroll direction.

5. Grading Sheet

Task Points

Using FIOPIN with XOR

Using FIOPIN with input pins

Discussion