LED Blinking Using LPC1768

Introduction

LPC1768 is a Cortex-M3 based microcontroller. It features a high level of integration and minimal power consumption. The ARM-based microcontrollers also feature advanced debug features as well as support block integration. The operating frequency of LPC1768 would go up to 100 MHz. They are pin compatible to the 100 pins LPC236x ARM7-based microcontroller series. This chapter deals with the firmware details of interfacing an LED with LPC1768 microcontroller device.

Register Configuration

There are five registers in an LPC1768 device that are associated with the GPIO functionality. They are GPIO pins select register, GPIO direction control register, a fast port output set register, fast port output clear register, and fast port pin value register. With the help of these registers, the digital signals are transmitted.

PINSEL is the pin select function register. The GPIO pins have multiple functions; a minimum of one to a maximum of four. The PINSEL register can be configured to choose the required function. Each pin is provided with two bits as there can be up to four functions to choose among. So, at least two PINSEL registers are required to configure the port pins. The first sixteen pin functions of PORT0 can be configured by the 32 bits of PINSEL0 register whereas the other 16 bits are configured using the 32 bits of the PINSEL1 register. The below table shows how to choose the required function for a particular pin using two bits of the PINSEL register.

GPIO Functionality LPC1768
GPIO functionalities

The table below shows each PINSEL register and its corresponding ports.

LPC1768 PIN SELECT registers
PINSELECT registers

FIODIR is the fast GPIO direction control register and it controls the direction of each port pin.

LPC1768 PORT Direction set
FIODIR register

FIOSET is the fast port output set register which controls the state of output pins. While writing 1s produces highs at the corresponding port pins, writing 0s has no particular effects. Reading this register, it is the current contents of the port output register which is being returned, not the physical port value.

LPC1768 PORT SET
FIOSET register

FIOCLR is the fast port output clear register and this register controls the state of output pins. While writing 1s produces lows at the corresponding port pins, writing 0s has no particular effect.

LPC1768 PORT CLEAR
FIOCLR Register

FIOPIN is the fast port pin value register and is used for both reading and writing data to and from the port. Writing to this register helps to place corresponding values in all bits of the particular port pins.
This register is used for both reading and writing data from/to the port.
Output: Writing to this register places corresponding values in all bits of the particular PORT pins.
Input: The current state of digital port pins can be read from this register, regardless of pin direction or alternate function selection (as long as pins are not configured as an input to ADC).

Firmware

  • The following are the definitions that will be used for this program. We will be using one of the 8-bit ports, PORTD of OpenLab™ that contains pins 24 to 31 of PORT 1 of the LPC1768 ARM controller.
#define DATA_DIR       LPC_GPIO1->FIODIR
#define DATA_SET       LPC_GPIO1->FIOSET
#define DATA_CLR       LPC_GPIO1->FIOCLR
#define DATA_PIN         LPC_GPIO1->FIOPIN

#define PIN_D0 24
#define PIN_D1 25
#define PIN_D2 26
#define PIN_D3 27
#define PIN_D4 28
#define PIN_D5 29
#define PIN_D6 30
#define PIN_D7 31
  • The next line sets the PINSEL3 register to 0 which results in selecting the primary function which is the GPIO functionality. PINSEL3 corresponds to Port1[31:16] bits. Since we are going to shift and blink LEDs starting from the PIN1.24 to PIN1.31, we are setting that part of the Port1 to GPIO. This step can be skipped because, by default, these pins will be performing the GPIO functionality. But, one must keep in mind that this step is relevant when an alternate functionality must be selected.
LPC_PINCON→PINSEL3 = 0 ;
  • The next line selects the direction of the PORTD bits of OpenLab™ as output. Since we are giving out values to an LED, port pin direction should be set as output. PIN_D0 is the LSB of PORTD which is PIN1.24. Therefore, we must shift 0xFF, LSB times to the left for this operation to take effect.
DATA_DIR = (0xFF << PIN_D0);
  • We will be assigning the definition PIN_D0 to a random variable ‘i’ and increment the value each time.
   i = PIN_D0;
  • And use it to set and clear the output of the corresponding LEDs in the OpenLab™.
 DATA_SET = (0x01 << i);
 delay_ms(250);
 DATA_CLR = (0x01 << i);
 i++;

Summary

Thus we have learned how to blink 8 LEDs and shift the outputs periodically using the LPC1768 microcontroller. The sample code based on this tutorial is available in the code library section under the ARM. Use the following link: Code Library