LPC1764 Project Randomly Working

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by micro9000 on Sat Jul 13 16:00:13 MST 2013
Hey guys, I'm in a bit of a bind as I cannot figure out a problem I have been having for a while. I have a LPC1764 and I am using LPC Expresso 5.2.6 ( before I used 5.2.4 and had the same issues) with LPC-Link 2.

The problem is that when I program the target microcontroller using the "Program Flash" chip icon it sometimes works and other times it doesn't. The project involves a simple blinking LED based off of the systemtick example. However, if I unplug the target or go to it program again, it appears variable when it will work or not. Here is the code I am using:

Here is the code I am using:

Name        : main.c
Author      :
Version     :
Copyright   : Copyright (C)
Description : main definition

#ifdef __USE_CMSIS
#include "LPC17xx.h"

#include <cr_section_macros.h>
#include <NXP/crp.h>

// #define PLL0CFG_Val 0x00090063 ensure this is the case in CMISvp00_17xx under system_LPCxx.c
// see user manual section 4.5 for more information

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

// TODO: insert other include files here

// TODO: insert other definitions and declarations here
// Function to initialize GPIO to access LED2
void led2_init (void)
// Set P0_22 to 00 - GPIO
//LPC_PINCON->PINSEL1= 0x0;//&= (~(3 << 12));
// Set GPIO - P0_22 - to be output
LPC_GPIO0->FIODIR |= (1 << 22);

// Function to turn LED2 on
void led2_on (void)
LPC_GPIO0->FIOSET = (1 << 22);

// Function to turn LED2 off
void led2_off (void)
LPC_GPIO0->FIOCLR = (1 << 22);

void led2_invert (void)
int ledstate;

// Read current state of GPIO P0_0..31, which includes LED2
ledstate = LPC_GPIO0->FIOPIN;
// Turn off LED2 if it is on
// (ANDing to ensure we only affect the LED output)
LPC_GPIO0->FIOCLR = ledstate & (1 << 22);
// Turn on LED2 if it is off
// (ANDing to ensure we only affect the LED output)
LPC_GPIO0->FIOSET = ((~ledstate) & (1 << 22));

volatile uint32_t msTicks; // counter for 1ms SysTicks

// ****************
//  SysTick_Handler - just increment SysTick counter
void SysTick_Handler(void) {

// ****************
// systick_delay - creates a delay of the appropriate number of Systicks (happens every 1 ms)
__INLINE static void systick_delay (uint32_t delayTicks) {
  uint32_t currentTicks;

  currentTicks = msTicks;// read current tick counter
  // Now loop until required number of ticks passes.
  while ((msTicks - currentTicks) < delayTicks);

// ****************
int main(void) {

led2_init();// Setup GPIO for LED2
led2_on();// Turn LED2 on

// Setup SysTick Timer to interrupt at 1 msec intervals
if (SysTick_Config(SystemCoreClock / 1000)) {
    while (1);  // Capture error

// Enter an infinite loop, just incrementing a counter and toggling leds every second
volatile static int i = 0 ;
while(1) {
    systick_delay (500); // wait 2 seconds (2000ms)
    led2_invert();// Toggle state of LED2
return 0 ;

Also, under CMSISv2p00_LPCxx and within system_LPC17xx.c it is still configured for 12MHz crystal.

#define PLL0CFG_Val           0x00050063

I am using the following crystal and capacitors off of digikey:
Crystal selected
Capacitor selected

I looked in the user manual under table 15 of section 4 and I believe it is within spec. I'm not sure if the issue is with the system core clock, hardware, or in fact an issue with the compiler/software drivers.

In sum, the target operates properly only at times. While it is working there is no change and it appears that it can operate indefinitely. However, upon re-programming or disconnecting and reconnecting the USB cable to power the target via the LPC-Link 2, the project stops working (LED does not blink) at times.

I am basing my schematic off of this:
(Note: I am using a 12MHz crystal with 39pF capacitors and I am using SWD (SWDIO, SWDCLK, and RESET) for programming and not JTAG)