This is another article that introduces the programming of ARM Cortex-M3 LPC1768 microcontroller. Here we will make input and output functions of the GPIO of the LPC1768. For better understanding, we will use a button and the LED. Our idea is to program it so that when you press the button, the LED lights up. The environment setup for ARM cortex M3 development is well discussed in this article.
The LPC 1768 is an ARM Cortex-M3-based microcontroller for embedded application capabilities with low power consumption and a high level of integration. The ARM Cortex M3 is designed in a way to enhance debugging capabilities and a higher level of system integration. It runs at a CPU frequency of 100 MHz and incorporates a 3-stage pipeline and uses a Harvard architecture with separate local instruction and data buses for third-bus peripherals. The ARM Cortex-M3 CPU has an internal prefetch unit to support speculative branches. Peripheral components include 512 KB flash memory, 64 KB data memory, Ethernet MAC, USB OTG, 4 UARTs, 8-channel general-purpose DMA controller, 2 SSP controllers, 10-bit DAC, quadrature encoder interface, SPI interface, 3 bus I2C interface, 2-input plus 2-output I2S bus interface, 4 general-purpose timers, ultra-low power real-time clock (RTC) with separate battery supply, and up to 70 user I/O pins General, 6-output general-purpose PWM. The LPC1768/66/65/64 are pin compatible with the 100-pin LPC236x ARM7 based series of microcontrollers.

Fig. 1: Switch and LED Interface with LPC1768 Prototype
Before going into coding the LED and Switch interface, an introduction to the Registers on the LPC1768 and its Configuration is provided.
Registry configuration (lpc17xx.h):
As LPC1768 is a 32-bit architecture with memory mapped to location 0x2009 C000 to 0x2009 FFFF. The LPC1768 special function registers are defined in lpc17xx.h, which is included at the beginning of the project. There are 5 ports (PORT0 – PORT4). Each PORT will not have 32 physical pins. A structure is defined in the system file LPC_GPIOn(n= 0,1,2,3) contains all the records necessary for GPIO operation. See the lpc17xx.h file for more information about registries.
PINSEL: GPIO Pins Select Register:
The PINSEL register must be configured before using PIN, as almost all pins have a maximum of four alternative functions. The table below explains the function of a specific pin using two bits of the PINSEL register.
|
Value |
Function |
Enumeration |
|
00 |
Primary function (default), typically GPIO port |
PINSEL_FUNC_0 |
|
01 |
First alternative function |
PINSEL_FUNC_1 |
|
10 |
Second alternative function |
PINSEL_FUNC_2 |
|
11 |
Third alternative function |
PINSEL_FUNC_3 |
Fig. 2: PINSEL register bit value for selecting GPIO pins in LPC1768
FIODIR: Fast GPIO direction control registration.
This register individually controls the direction of each port pin.
|
Values |
Direction |
|
0 |
Prohibited |
|
1 |
Exit |
Fig. 3: Bit value of the FIODIR register to control the direction of the ports on the LPC1768
FIOSET: Fast Port Output Set Register.
This register controls the state of the output pins. Writing 1 produces highs on the corresponding port pins. Writing 0s has no effect. Reading this register returns the current contents of the port output register and not the physical port value.
|
Values |
FIOSET |
|
0 |
No effect |
|
1 |
Sets high on the pin |
FIOCLR: Fast Port Output Cleaning Record.
This register controls the state of the output pins. Writing 1 produces minima on the corresponding port pins. Writing 0s has no effect.
|
Values |
FIOCLR |
|
0 |
No effect |
|
1 |
Sets bass on the pin |
Fig. 4: FIOCLR register bit value to control the state of the output pins on the LPC1768
FIOPIN: Fast port pin value register.
This register is used to read and write data to/from the PORT.
Output: Writing to this register places corresponding values on all bits of the specific PORT pins.
Input: The current state of the digital port pins can be read from this register regardless of pin direction or alternative function selection (as long as the pins are not configured as input to ADC).
Note: It is recommended to configure the PORT direction and pin function before using it.
Create a project using Keil uvision4 for LPC1768 microcontroller:
In this section, we will start creating a project in Keil MDK, we have already installed Keil µVision and Co-MDK Plug-in + CoLinkEx Drivers required for the CoLinkEx programming adapter. You can start by downloading the project files and start your hands-on experiment.
Code.rar
Code description:
ARM programming requires good handling of bit manipulation in C language. Here is a small note on introducing bit manipulation for a newbie. C has direct support for bitwise operations that can be used for bit manipulation. In the following examples, n is the index of the bit to be manipulated within the variable bit_fld, which is an unsigned character being used as the bit field. Bit indexing starts at 0, not 1. Bit 0 is the least significant bit.
Define a little
bit_fld = (1 <
Clean up a little
bit_fld &= ~(1 <
Alternate a little
bit_fld ^ = (1 << n)
Test a little
bit_fld & (1 <
The LED is connected to Port 2.0 and the Switch is connected to Port 1.0, below are the code snippets used to make it work.
LPC_GPIO2->FIODIR = 0x000000FF; /* P2.xx set as Outputs */
LPC_GPIO2->FIOCLR = 0x000000FF; /* turns off all LEDs */
LPC_GPIO1->FIODIR = 0x00000000; /* P1.xx defined as inputs */
LPC_GPIO1->FIOCLR = 0x000000FF; /* Decreases */
The lines above are for setting port 2 pins as output and clearing them. The LPC_GPIO2 structure consists of members FIODIR, FIOCLR, and FIOSET.
switchStatus = (LPC_GPIO1->FIOPIN>>0) & 0x01 ; //Read the switch status
if(switchStatus == 1)
{
LPC_GPIO2->FIOPIN = (1<<0);
}
other
{
LPC_GPIO2->FIOPIN = (0<<0);
}
The above if loop is to check the status of the input pin and change the output of the output pin accordingly.
Project source code
###
Circuit diagrams
| Switch and LED interface with LPC1768 | ![]() |
Project Components
- LED
- Resistor
Project video
