SysTick With Blue Board LPC1768-H and Keil MDK

Hello all,

Some time ago, I purchased the Blue Board LPC1768-H, and struggled with using Eclipse + Yagarto followed by Eclipse + CodeSourcery G++ Lite. Now I’ve finally decided to port to the trial version of Keil MDK.

I tested it with a simple program that used a for loop to count to 100000 as a delay to blink the LED. It works! So the next step I wanted was to get SysTick going so I can get proper delays. Here’s the code I wrote:

#include "LPC17xx.h"

void delay(uint32_t a);
extern "C" void SysTick_Handler(void);

volatile uint32_t millis = 0;

int main(void) {
	//Blinky with SysTick
	SysTick->CTRL |= 1 << 0; //Enable SysTick
	SysTick->CTRL |= 1 << 1; //Enable SysTick Interrupts
	SysTick->CTRL |= 1 << 2; //Select CPU Clock
	SysTick->LOAD = (100000 - 1); //100MHz clock, 1ms = 100000 counts

	LPC_SC->PCONP |= (1 << 15); //Power up GPIO
	LPC_GPIO1->FIODIR |= 1 << 29; //Puts P1.29 into output mode.
	while(1) {
		LPC_GPIO1->FIOPIN ^= 1 << 29; //Toggle LED
		delay(500);
		/*for(int i=0;i<100000;i++);
		LPC_GPIO1->FIOSET = 1 << 29;
		for(int i=0;i<100000;i++);
		LPC_GPIO1->FIOCLR = 1 << 29;*/		
	}
}

void delay(uint32_t a) {
	uint32_t start_time = millis;
	while(millis - start_time < a);
}

extern "C" void SysTick_Handler(void) {
	millis++;
}

It doesn’t work. Not even when I modify the while(1) loop to:

	while(1) {
		LPC_GPIO1->FIOPIN ^= 1 << 29; //Toggle LED
		delay(500);
		/*for(int i=0;i<100000;i++);
		LPC_GPIO1->FIOSET = 1 << 29;
		for(int i=0;i<100000;i++);
		LPC_GPIO1->FIOCLR = 1 << 29;*/		
	}

I found the problem to be caused by the line:

SysTick->CTRL |= 1 << 2; //Select CPU Clock

When I comment it out, the program is able to reach the while(1) loop where the for() loops make the LED blink.

It looks all good to me, but simply doesn’t work! Does anyone have any ideas? I also downloaded the CMSIS drivers library (which has a SysTick driver) but I still can’t figure out how to actually include the drivers in Keil :oops:

By the way, I solved the problem. The CMSIS drivers I downloaded from NXP had consistency problems with declaring SystemCoreClock. Fixed. And works. Working on UART0 now (doesn’t work).