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.
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:
- Connect LPC1768 board via USB (may need driver installation)
- Launch Flash Magic, select device LPC1768
- Click browse and select .bin file containing code
- 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:
JTAG | SWD |
More pins required | Only uses 2 pins |
Older legacy interface | Newer ARM interface |
Higher throughput | Lower 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<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.