SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: How to Program the LPC1768 Microcontroller
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

How to Program the LPC1768 Microcontroller

Jeday Schwartz
Last updated: September 6, 2023 7:25 am
Jeday Schwartz 7 Min Read
Share
SHARE

The LPC1768 is an ARM Cortex-M3 based microcontroller manufactured by NXP Semiconductors. With its low power consumption, abundant memory and peripherals, and excellent community support, the LPC1768 is a popular choice for many embedded systems projects.

Contents
Getting Started with LPC1768Understanding LPC1768 ArchitectureMemory OrganizationClocks and PowerInterrupts and ExceptionsProgramming LPC1768 Flash MemoryFlash Magicmbed Online CompilerLPCXpresso IDEDirect Flash ProgrammingProgramming LPC1768 via JTAG/SWDExample Code for Blinking LEDExample: Using the LPC1768 ADCConclusion

Getting Started with LPC1768

To get started programming the LPC1768, you will need:

  • LPC1768 development board – Many options are available like LPCXpresso or mbed boards
  • ARM cross compiler – GCC, ArmCC, IAR EWARM etc.
  • Programming utility – Flash Magic, mbed online compiler, LPCXpresso IDE etc.
  • Code editor – Notepad++, Sublime, VSCode etc.

Additionally, you may need debugging tools like an IDE, JTAG debugger and oscilloscope for more complex projects. Many development boards have built-in debug probes.

Understanding LPC1768 Architecture

The LPC1768 has the following key features:

  • 32-bit ARM Cortex-M3 CPU core running at frequencies up to 100 MHz
  • 512 KB flash memory and 64 KB SRAM
  • Ethernet, USB 2.0 Host/Device/OTG, 2x UARTs, SPI, I2C, I2S, ADC 8/10-bit
  • General purpose DIO, PWM, timers, watchdog, RTC, brownout detect

The CPU core connects to the peripherals and memories through the AHB matrix. The NVIC handles interrupts and exceptions. Several power modes allow optimizing for low power operation.

Memory Organization

The LPC1768 contains 512 KB flash and 64 KB SRAM. The flash is used for storing code and read-only data. SRAM holds the stack, heap and global/static variables. Several memory mapped peripherals are also available.

Clocks and Power

The CPU clock can be sourced from internal RC oscillator or external crystal. PLL generates CPU clock up to 100 MHz from either source. Peripheral clocks can be scaled down from CPU clock. Power modes include active, sleep, deep sleep and power-down.

Interrupts and Exceptions

The Nested Vectored Interrupt Controller (NVIC) provides interrupt management. There are up to 45 interrupt channels with 8 priority levels. Low power modes can wake up on interrupt. The processor also handles exceptions like memory faults.

Programming LPC1768 Flash Memory

The on-chip flash memory needs to be programmed to execute code on LPC1768. Some ways to program it are:

Flash Magic

Flash Magic is a popular utility for programming flash. After installing it:

  1. Connect LPC1768 board via USB (may need driver installation)
  2. Launch Flash Magic, select device LPC1768
  3. Click browse and select .bin file containing code
  4. Click start to erase, blank check, program and verify flash

mbed Online Compiler

The mbed online IDE can directly compile and program flash via USB. Simply import/write project, compile and click download.

LPCXpresso IDE

The LPCXpresso IDE can also directly program flash via its build and debug features. It provides a GNU ARM Eclipse based development environment.

Direct Flash Programming

The LPC1768 flash can also be directly programmed from within user code using the Flash API. This requires allocating flash sectors to data and providing programming algorithms.

Programming LPC1768 via JTAG/SWD

The LPC1768 supports JTAG and Serial Wire Debug (SWD) for debugging and programming. An external JTAG/SWD debugger is required, which connects to a host PC running IDE software. Some options are:

  • LPC-Link2 – Debug probe from NXP
  • ST-Link – Popular and low cost debug probe
  • J-Link – More powerful debug probe

Comparison between JTAG vs SWD:

JTAGSWD
More pins requiredOnly uses 2 pins
Older legacy interfaceNewer ARM interface
Higher throughputLower throughput

The IDE software connects to the debug probe to program flash, set breakpoints, examine variables etc. Some IDE options are:

  • Keil MDK – Popular commercial IDE for ARM
  • IAR EWARM – Also a commercial IDE
  • OpenOCD – Open source IDE and debug interface
  • Eclipse IDEs – Free IDE, needs plugins like GNU ARM Eclipse

Example Code for Blinking LED

A simple example program to blink an LED connected to P1.28 of LPC1768 is given below. It uses busy wait delays and GPIO pin toggle to blink the LED.


#include "LPC17xx.h"

int main(void) 
{
  SystemInit();

  LPC_GPIO1->FIODIR |= (1<<28);    // Set P1.28 as Output

  while(1)
  {
    LPC_GPIO1->FIOPIN ^= (1<<28);    // Toggle LED pin
    for(int i=0; i&lt;200000; i++);    // Delay   
  }

  return 0;
}

The LED will repeatedly turn ON and OFF with a visible delay due to the software delay loop. This demonstrates basic GPIO usage, pin configuration and code execution on the LPC1768.

Example: Using the LPC1768 ADC

The LPC1768 contains an 8/10-bit ADC with multiple channels. Here is example code to read the analog voltage on channel 0 and light up an LED if above a threshold:


#include "LPC17xx.h"

int main(void)
{
  SystemInit();

  LPC_SC->PCONP |= (1<<12);    // Power up ADC

  LPC_PINCON->PINSEL1 |= 0x1<<14;    // Set P0.23 to ADC function

  LPC_ADC->ADCR |= (1<<2)|(1<<8);    // select channel 0, enable ADC

  LPC_ADC->ADCR |= 1;        // Start conversion 

  while(1)
  {
      while(!(LPC_ADC->ADGDR & (1<<31))); // Wait for done

      int result = LPC_ADC->ADGDR >> 6 & 0x3FF;   

      if(result > 600)        // Threshold
        LPC_GPIO1->FIOPIN |= (1<<28); // Turn on LED
      else
        LPC_GPIO1->FIOPIN &= ~(1<<28);    // Turn off LED 
  }

  return 0;
}

This shows usage of the ADC peripheral, analog input pin mapping, triggering conversion and reading result. The analog input voltage controls the LED brightness.

Conclusion

The LPC1768 is a feature-packed ARM Cortex-M3 microcontroller suitable for many embedded applications. With ample flash memory, peripherals and excellent support, it can form the core of designs ranging from simple GPIO based systems to more complex Ethernet and USB powered applications.

An easy way to get started is an LPCXpresso development board and mbed online compiler. More advanced debugging and analysis is possible through JTAG/SWD tools. Many code examples are available forreference from NXP and other sources, along with reference manuals.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article What is the cortex-M0 processor instruction set?
Next Article What is the Difference Between Arm Cortex-M0 and M4?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

What is the Arm Cortex startup sequence?

The Arm Cortex startup sequence refers to the steps that…

7 Min Read

What is arm Cortex-M7?

The ARM Cortex-M7 is a high-performance, energy-efficient 32-bit processor core…

6 Min Read

Is it possible to port the DesignStart Eval design to a different FPGA board?

The short answer is yes, it is possible to port…

8 Min Read

What are the different faults in ARM?

ARM processors, like all microprocessors, are susceptible to faults during…

6 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account