LPC1114FBD48/302 breakout board breakout board assembly notes






















Now I am assembling a break out board for the lpc1114fbd48/302 breakout board.

.END

lpc1114fbd48/302
























lpc1114fbd48/302

.END


lpc1114fn28/102 vs lpc1114fbd48/302
























lpc1114fn28/102 vs lpc1114fbd48/302


.END

lpc114fbd48302 learning notes




























lpc114fbd48302

.END


Blink 2 LEDs OK
























Blink 2 LEDs OK.

testBlinkLedPio18();
testBlinkLedPio08();

.END

// ***********************************************************************
// Program - FongBlinky
// Description - Blink 2 LEDs
// Author - TL Fong <fongcomputer.blogspot.hk> <tlfong01hongkong@google.com>
// Build - 2013.07.27.01
// Date - 2013jul27hkt1206
// Hardware - NXP ARM Cortex M0 LPC1114FN28/102, LPC1114F/302  
// Software - MDK-Lite/uVision 4.71, CIMSIS 1.1
// Programming styles - function/object (Ref - MIT OCW 6.01SC Intro EECS1)
// System developing styles - prototyping and incremental testing 
// ***********************************************************************

#include <stdio.h>
#include "lpc11xx.h"

// ***********************************************************************
// Function heirarchy
// ***********************************************************************

//   main()
//     blinky()
//       getLedPortNumber();
//       getGpioStructPointer();
//       getLedPinNumber();
//       setPortPinOutput()
//       setPortPinLow()
//       setPortPinHigh()
//       delayTenthSecond()
//       setPortPinDirectionOutput()
//       setGpioDataRegisterBitLow()
//       setGpioDataRegisterBitHigh()
//         setGpioDirectionRegisterBitHigh()
//         setPortPinValue()

// ***********************************************************************
// Global constants and variables
// ***********************************************************************

// *** GPIO Port and pin number typedef and assignment***

typedef const int portNumber;
portNumber Port0 = 0;
portNumber Port1 = 1;
portNumber Port2 = 2;
portNumber Port3 = 3;
portNumber Port4 = 4;

typedef const int pinNumber;
pinNumber Pin0 = 0;
pinNumber Pin1 = 1;
pinNumber Pin2 = 2;
pinNumber Pin3 = 3;
pinNumber Pin4 = 4;
pinNumber Pin5 = 5;
pinNumber Pin6 = 6;
pinNumber Pin7 = 7;
pinNumber Pin8 = 8;
pinNumber Pin9 = 9;
pinNumber Pin10 = 10;
pinNumber Pin11 = 11;

typedef int directionValue;
directionValue InputDirection = 0;
directionValue OutputDirection = 1;

// *** GPIO control struct pointer array ***

typedef LPC_GPIO_TypeDef *gpioStructPointerArray[4];
gpioStructPointerArray GpioStructPointerArray = {LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3};

// *** Timer control struct pointer array ***

typedef LPC_TMR_TypeDef *timerStructPointerArray[4];
timerStructPointerArray TimerStructPointerArray = {LPC_TMR16B0, LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1};


// *** GPIO pin control register array ***

typedef LPC_IOCON_TypeDef *lpcIoCon;
lpcIoCon LpcIoCon = LPC_IOCON;

void setPinFunction()
{
LPC_IOCON ->PIO1_9 |= (1 << 0); // PIN1_9 = CT16B1_MAT0
}

// *** Timer enable bit array ***

int TimerEnableBitArray[4] = {7, 8, 9, 10}; // SYSAHBCLKCTRL timer enable bits

// *** GPIO pin control register array ***


// *** LED number typedef and assignment ***

typedef const int ledNumber;
ledNumber Led1 = 1;
ledNumber Led2 = 2;
ledNumber Led3 = 3;
ledNumber Led4 = 4;

ledNumber LedPio18 = 5;
ledNumber LedPio08 = 6;

int const LedNumberMax = 6;

typedef int ledPortArrayArrayIndex;

ledPortArrayArrayIndex LedPortIndex = 0;
ledPortArrayArrayIndex LedPinIndex = 1;

typedef int ledPortPinArrayArray[6][2];
ledPortPinArrayArray LedPortPinArrayArray = {{Port1, Pin8}, {Port2, Pin7}, {Port2, Pin8}, {Port2, Pin5}, {Port1, Pin8}, {Port0, Pin8}};

// *** Key number typedef and assignment ***

typedef const int keyNumber;
keyNumber Key1 = 1;
keyNumber Key2 = 2;
keyNumber Key3 = 3;
keyNumber Key4 = 4;
keyNumber Key5 = 5;

int const KeyNumberMax = 5;

typedef int keyPortArrayArrayIndex;

keyPortArrayArrayIndex KeyPortIndex = 0;
keyPortArrayArrayIndex KeyPinIndex = 1;

typedef int keyPortPinArrayArray[5][2];
keyPortPinArrayArray KeyPortPinArrayArray = {{Port0, Pin2},{Port0, Pin3}, {Port3, Pin4}, {Port1, Pin9}, {Port1, Pin4}};

// typedef int tenthSecond;
// tenthSecond OnHalfSecond = 5;
// tenthSecond OffOneSecond = 10;
// tenthSecond FourSeconds = 40;

// *** System timer number typedef and assignment ***

typedef const int timerNumber;
timerNumber Timer16Bit0 = 0;
timerNumber Timer16Bit1 = 1;
timerNumber Timer32Bit0 = 2;
timerNumber Timer32Bit1 = 3;

timerNumber Timer1 = Timer16Bit0;
timerNumber Timer2 = Timer16Bit1;
timerNumber Timer3 = Timer32Bit0;
timerNumber Timer4 = Timer32Bit1;

int const TimerNumberMax = 4;

// *** Logical level defines *** 

#define LOW 0
#define HIGH 1

typedef int logicLevelValue;

logicLevelValue LogicLevelLow = 0;
logicLevelValue LogicLevelHigh = 1;

// *** Time constants typedef ***

typedef int milliSecond;
milliSecond OnHalfSecond = 500;
milliSecond OffOneSecond = 1000;

typedef int repeatCount;
repeatCount FourTimes = 4;
repeatCount TwentyTimes = 20;


// ***********************************************************************
// Application timer Functions
// ***********************************************************************

void delayTenthSecond(int count)
  {
int i, j, k;
for (i = 0; i < count; i++)
    {
for (j = 0; j < 0x4; j++)
 {
        for (k = 0; k < 0x000BB00; k++)
          {
       }
}
     }
}

// ***********************************************************************
// System timer functions 
// ***********************************************************************

static volatile uint32_t TimeTick = 0;

void SysTick_Handler(void)
{
       TimeTick++;
}

void delayMilliSecond(uint32_t mS)
{
  SysTick->LOAD = (((24000)*mS)-1);
  SysTick->VAL = 0;
  SysTick->CTRL |= ((1<<1)|(1<<0));
       
  while(!TimeTick)
  {
  };
  TimeTick = 0;
  SysTick->CTRL =0;
}

int getTimerEnableBit(int timerNumber)
{
int timerEnableBit;

timerEnableBit = TimerEnableBitArray[timerNumber];
return timerEnableBit;
}

void enableTimer(int timerNumber)
{
int timerEnableBit;
timerEnableBit = getTimerEnableBit(timerNumber);
LPC_SYSCON ->SYSAHBCLKCTRL |= (1 <<timerEnableBit);
}

// ***********************************************************************
// Get Led port number and pin number 
// ***********************************************************************

portNumber getLedPortNumber(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
  int portNumber;
  portNumber = ledPortPinArrayArray [ledNumber - 1][LedPortIndex];
  return portNumber;
}

pinNumber getLedPinNumber(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
  int ledPinNumber;
  ledPinNumber = ledPortPinArrayArray [ledNumber - 1][LedPinIndex];
  return ledPinNumber;
}

// ***********************************************************************
// Get Key port number and pin number 
// ***********************************************************************

portNumber getKeyPortNumber(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
  int portNumber;
  portNumber = keyPortPinArrayArray [keyNumber - 1][KeyPortIndex];
  return portNumber;
}

pinNumber getKeyPinNumber(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
  int keyPinNumber;
  keyPinNumber = keyPortPinArrayArray [keyNumber - 1][KeyPinIndex];
  return keyPinNumber;
}

// ***********************************************************************
// Get GpioStructPointer
// ***********************************************************************

LPC_GPIO_TypeDef *getGpioStructPointer(int portNumber)
{
  LPC_GPIO_TypeDef *gpioStructPointer;
gpioStructPointer =  GpioStructPointerArray[portNumber]; 
return gpioStructPointer;
}

// ***********************************************************************
// Set pin direction and value 
// ***********************************************************************

void setGpioDirectionRegisterBitLow(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR  &= ~(1 << bitNumber);
}

void setGpioDirectionRegisterBitHigh(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR |= (1 << bitNumber);
}

void setGpioDataRegisterBitLow(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA &= ~(1 << bitNumber);
}

void setGpioDataRegisterBitHigh(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA |= (1 << bitNumber);
}

void setPortPinDirection(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, directionValue directionValue)
  {
  if (directionValue == InputDirection)
 setGpioDirectionRegisterBitLow(gpio_struct_ptr, pinNumber);
else
setGpioDirectionRegisterBitHigh(gpio_struct_ptr, pinNumber);
  }

void setPortPinValue(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, logicLevelValue logicLevelValue)
{
  if (logicLevelValue == LogicLevelLow)
setGpioDataRegisterBitLow(gpio_struct_ptr, pinNumber);        
else 
setGpioDataRegisterBitHigh(gpio_struct_ptr, pinNumber);  
}

// ***********************************************************************
// Get pin data value 
// ***********************************************************************

logicLevelValue getGpioDataRegisterBitValue(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
logicLevelValue logicLevelValue;
logicLevelValue = gpio_struct_ptr->DIR  &= ~(1 << bitNumber);
return logicLevelValue;
}

// ***********************************************************************
// Get TimerStructPointer
// ***********************************************************************

LPC_TMR_TypeDef *getTimerStructPointer(int timerNumber)
{
  LPC_TMR_TypeDef *timerStructPointer;
timerStructPointer =  TimerStructPointerArray[timerNumber]; 
return timerStructPointer;
}

// ***********************************************************************
// Timer Initialization
// ***********************************************************************

void initializeAllTimers()
{
enableTimer(Timer1);
enableTimer(Timer2);
enableTimer(Timer3);
enableTimer(Timer4);
}

// ***********************************************************************
// Led Initialization
// ***********************************************************************

void initializeLedPortPin(ledNumber ledNumber)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber(ledNumber, LedPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber(ledNumber, LedPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);
}

void initializeAllLeds()
{
  int ledNumber;

  for (ledNumber = 1; ledNumber < LedNumberMax +1; ledNumber++)
  {
 initializeLedPortPin(ledNumber);
  }
}

// ***********************************************************************
// Key Initialization
// ***********************************************************************

void initializeKeyPortPin(keyNumber keyNumber)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getKeyPortNumber(keyNumber, KeyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getKeyPinNumber(keyNumber, KeyPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);
}

void initializeAllKeys()
{
  int keyNumber;

  for (keyNumber = 1; keyNumber < KeyNumberMax +1; keyNumber++)
  {
 initializeLedPortPin(keyNumber);
  }
}

// ***********************************************************************
// PWM Functions
// ***********************************************************************

void setupPwmPin()
{
LPC_IOCON ->PIO1_9 |= (1 << 0); // PIN1_9 = CT16B1_MAT0
}

// ***********************************************************************
// Initialize all LEDs, keys, and timers
// ***********************************************************************

void initializeSystem()
{
initializeAllLeds();
initializeAllKeys();
initializeAllTimers();
}

// ***********************************************************************
// Blink Led Function
// ***********************************************************************

void blinky(ledNumber ledNumber, int onTime, int offTime, int blinkCount)
{
  int count;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber(ledNumber, LedPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber(ledNumber, LedPortPinArrayArray);
 
for (count = 0; count < blinkCount; count++)
{
  setPortPinValue(gpio_struct_ptr, pinNumber, LogicLevelLow);
     delayMilliSecond(onTime);    
setPortPinValue(gpio_struct_ptr, pinNumber, LogicLevelHigh);
     delayMilliSecond(offTime);
  }
}

// ***********************************************************************
// Echo key Function !!! not yet tested !!! tlfong01 2013jul2201
// ***********************************************************************

typedef logicLevelValue keyStatus;
keyStatus KeyPressed = LOW;
keyStatus KeyReleased = HIGH;

keyStatus getKeyStatus(keyNumber keyNumber)
{
keyStatus keyStatus;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getKeyPortNumber(keyNumber, KeyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getKeyPinNumber(keyNumber, KeyPortPinArrayArray);
 
keyStatus = getGpioDataRegisterBitValue(gpio_struct_ptr, pinNumber);
  return keyStatus;
}

void echoKey(keyNumber keyNumber, ledNumber ledNumber, int echoCount)
{
  int count;
keyStatus keyStatus;

for (count = 0; count < echoCount; count++)
{
keyStatus = getKeyStatus(keyNumber);
    if (keyStatus == KeyPressed)
{
 keyStatus = KeyReleased;
blinky(ledNumber, OnHalfSecond, OffOneSecond, FourTimes);
}
  }
}

// ***********************************************************************
// Test Functions
// ***********************************************************************

void testBlinkLed()
{
blinky(Led1, OnHalfSecond, OffOneSecond, FourTimes); // Port1, Pin8
blinky(Led2, OnHalfSecond, OffOneSecond, FourTimes); // Port2, Pin7
blinky(Led3, OnHalfSecond, OffOneSecond, FourTimes); // Port2, Pin8
blinky(Led4, OnHalfSecond, OffOneSecond, FourTimes); // Port2, Pin5
}

void testBlinkLedPio18()
{
   blinky(LedPio18, 20000, 400000, 6); 
}

void testBlinkLedPio08()
{
   blinky(LedPio08, 50000, 100000, 4); 
}

void testEchoKey() // not yet tested !!! tlfong01 2013jul2201
{
echoKey(Key4, Led1, FourTimes);
}

// ***********************************************************************
// Main Function
// ***********************************************************************

int main()
{  
initializeSystem();
  // testBlinkLed();
testBlinkLedPio18();
testBlinkLedPio08();
}

// ***********************************************************************
// End of Program
// ***********************************************************************

// .END //

LPC1114fn28 pinout notes
























LPC1114fn28 pinout notes

.END

LPC1114FN28 first program running OK!






























































So I modified the blinky program to blink a LED driven by pin PIO1_8 (LPC1114FN28 pin17).  Then I downloaded the program, set ISP pin from ground to Vcc, then pressed reset to run the program.

blinky(LedPio18, 100, 200, 1000000); // Led driven by Port 1, Pin 8

Everything went smoothly.  The LED blinks and the scope show what is expected.

So far so good.  Time to take a break.

.END

// ***********************************************************************
// Program - FongBlinky
// Description - Blinky
// Author - TL Fong <fongmcu.blogspot.hk> <tlfong01hongkong@google.com>
// Build - 2013.07.25.01
// Date - 2013jul25hkt2126
// Hardware - NXP ARM Cortex M0 LPC1114  
// Software - MDK-Lite/uVision 4.71, CIMSIS 1.1
// Program style - function/object (MIT OCW 6.01SC Intro EECS1)
// Development style - incremental testing 
// ***********************************************************************

#include <stdio.h>
#include "lpc11xx.h"

// ***********************************************************************
// Function heirarchy
// ***********************************************************************

//   main()
//     blinky()
//       getLedPortNumber();
//       getGpioStructPointer();
//       getLedPinNumber();
//       setPortPinOutput()
//       setPortPinLow()
//       setPortPinHigh()
//       delayTenthSecond()
//       setPortPinDirectionOutput()
//       setGpioDataRegisterBitLow()
//       setGpioDataRegisterBitHigh()
//         setGpioDirectionRegisterBitHigh()
//         setPortPinValue()

// ***********************************************************************
// Global constants and variables
// ***********************************************************************

// *** GPIO Port and pin number typedef and assignment***

typedef const int portNumber;
portNumber Port0 = 0;
portNumber Port1 = 1;
portNumber Port2 = 2;
portNumber Port3 = 3;
portNumber Port4 = 4;

typedef const int pinNumber;
pinNumber Pin0 = 0;
pinNumber Pin1 = 1;
pinNumber Pin2 = 2;
pinNumber Pin3 = 3;
pinNumber Pin4 = 4;
pinNumber Pin5 = 5;
pinNumber Pin6 = 6;
pinNumber Pin7 = 7;
pinNumber Pin8 = 8;
pinNumber Pin9 = 9;
pinNumber Pin10 = 10;
pinNumber Pin11 = 11;

typedef int directionValue;
directionValue InputDirection = 0;
directionValue OutputDirection = 1;

// *** GPIO control struct pointer array ***

typedef LPC_GPIO_TypeDef *gpioStructPointerArray[4];
gpioStructPointerArray GpioStructPointerArray = {LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3};

// *** Timer control struct pointer array ***

typedef LPC_TMR_TypeDef *timerStructPointerArray[4];
timerStructPointerArray TimerStructPointerArray = {LPC_TMR16B0, LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1};


// *** GPIO pin control register array ***

typedef LPC_IOCON_TypeDef *lpcIoCon;
lpcIoCon LpcIoCon = LPC_IOCON;

void setPinFunction()
{
LPC_IOCON ->PIO1_9 |= (1 << 0); // PIN1_9 = CT16B1_MAT0
}

// *** Timer enable bit array ***

int TimerEnableBitArray[4] = {7, 8, 9, 10}; // SYSAHBCLKCTRL timer enable bits

// *** GPIO pin control register array ***


// *** LED number typedef and assignment ***

typedef const int ledNumber;
ledNumber Led1 = 1;
ledNumber Led2 = 2;
ledNumber Led3 = 3;
ledNumber Led4 = 4;

ledNumber LedPio18 = 5;

int const LedNumberMax = 4;

typedef int ledPortArrayArrayIndex;

ledPortArrayArrayIndex LedPortIndex = 0;
ledPortArrayArrayIndex LedPinIndex = 1;

typedef int ledPortPinArrayArray[5][2];
ledPortPinArrayArray LedPortPinArrayArray = {{Port1, Pin8},{Port2, Pin7}, {Port2, Pin8}, {Port2, Pin5}, {Port1, Pin8}};

// *** Key number typedef and assignment ***

typedef const int keyNumber;
keyNumber Key1 = 1;
keyNumber Key2 = 2;
keyNumber Key3 = 3;
keyNumber Key4 = 4;
keyNumber Key5 = 5;

int const KeyNumberMax = 5;

typedef int keyPortArrayArrayIndex;

keyPortArrayArrayIndex KeyPortIndex = 0;
keyPortArrayArrayIndex KeyPinIndex = 1;

typedef int keyPortPinArrayArray[5][2];
keyPortPinArrayArray KeyPortPinArrayArray = {{Port0, Pin2},{Port0, Pin3}, {Port3, Pin4}, {Port1, Pin9}, {Port1, Pin4}};

// typedef int tenthSecond;
// tenthSecond OnHalfSecond = 5;
// tenthSecond OffOneSecond = 10;
// tenthSecond FourSeconds = 40;

// *** System timer number typedef and assignment ***

typedef const int timerNumber;
timerNumber Timer16Bit0 = 0;
timerNumber Timer16Bit1 = 1;
timerNumber Timer32Bit0 = 2;
timerNumber Timer32Bit1 = 3;

timerNumber Timer1 = Timer16Bit0;
timerNumber Timer2 = Timer16Bit1;
timerNumber Timer3 = Timer32Bit0;
timerNumber Timer4 = Timer32Bit1;

int const TimerNumberMax = 4;

// *** Logical level defines *** 

#define LOW 0
#define HIGH 1

typedef int logicLevelValue;

logicLevelValue LogicLevelLow = 0;
logicLevelValue LogicLevelHigh = 1;

// *** Time constants typedef ***

typedef int milliSecond;
milliSecond OnHalfSecond = 500;
milliSecond OffOneSecond = 1000;

typedef int repeatCount;
repeatCount FourTimes = 4;
repeatCount TwentyTimes = 20;


// ***********************************************************************
// Application timer Functions
// ***********************************************************************

void delayTenthSecond(int count)
  {
int i, j, k;
for (i = 0; i < count; i++)
    {
for (j = 0; j < 0x4; j++)
 {
        for (k = 0; k < 0x000BB00; k++)
          {
       }
}
     }
}

// ***********************************************************************
// System timer functions 
// ***********************************************************************

static volatile uint32_t TimeTick = 0;

void SysTick_Handler(void)
{
       TimeTick++;
}

void delayMilliSecond(uint32_t mS)
{
  SysTick->LOAD = (((24000)*mS)-1);
  SysTick->VAL = 0;
  SysTick->CTRL |= ((1<<1)|(1<<0));
       
  while(!TimeTick)
  {
  };
  TimeTick = 0;
  SysTick->CTRL =0;
}

int getTimerEnableBit(int timerNumber)
{
int timerEnableBit;

timerEnableBit = TimerEnableBitArray[timerNumber];
return timerEnableBit;
}

void enableTimer(int timerNumber)
{
int timerEnableBit;
timerEnableBit = getTimerEnableBit(timerNumber);
LPC_SYSCON ->SYSAHBCLKCTRL |= (1 <<timerEnableBit);
}

// ***********************************************************************
// Get Led port number and pin number 
// ***********************************************************************

portNumber getLedPortNumber(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
  int portNumber;
  portNumber = ledPortPinArrayArray [ledNumber - 1][LedPortIndex];
  return portNumber;
}

pinNumber getLedPinNumber(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
  int ledPinNumber;
  ledPinNumber = ledPortPinArrayArray [ledNumber - 1][LedPinIndex];
  return ledPinNumber;
}

// ***********************************************************************
// Get Key port number and pin number 
// ***********************************************************************

portNumber getKeyPortNumber(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
  int portNumber;
  portNumber = keyPortPinArrayArray [keyNumber - 1][KeyPortIndex];
  return portNumber;
}

pinNumber getKeyPinNumber(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
  int keyPinNumber;
  keyPinNumber = keyPortPinArrayArray [keyNumber - 1][KeyPinIndex];
  return keyPinNumber;
}

// ***********************************************************************
// Get GpioStructPointer
// ***********************************************************************

LPC_GPIO_TypeDef *getGpioStructPointer(int portNumber)
{
  LPC_GPIO_TypeDef *gpioStructPointer;
gpioStructPointer =  GpioStructPointerArray[portNumber]; 
return gpioStructPointer;
}

// ***********************************************************************
// Set pin direction and value 
// ***********************************************************************

void setGpioDirectionRegisterBitLow(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR  &= ~(1 << bitNumber);
}

void setGpioDirectionRegisterBitHigh(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR |= (1 << bitNumber);
}

void setGpioDataRegisterBitLow(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA &= ~(1 << bitNumber);
}

void setGpioDataRegisterBitHigh(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA |= (1 << bitNumber);
}

void setPortPinDirection(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, directionValue directionValue)
  {
  if (directionValue == InputDirection)
 setGpioDirectionRegisterBitLow(gpio_struct_ptr, pinNumber);
else
setGpioDirectionRegisterBitHigh(gpio_struct_ptr, pinNumber);
  }

void setPortPinValue(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, logicLevelValue logicLevelValue)
{
  if (logicLevelValue == LogicLevelLow)
setGpioDataRegisterBitLow(gpio_struct_ptr, pinNumber);        
else 
setGpioDataRegisterBitHigh(gpio_struct_ptr, pinNumber);  
}

// ***********************************************************************
// Get pin data value 
// ***********************************************************************

logicLevelValue getGpioDataRegisterBitValue(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
logicLevelValue logicLevelValue;
logicLevelValue = gpio_struct_ptr->DIR  &= ~(1 << bitNumber);
return logicLevelValue;
}

// ***********************************************************************
// Get TimerStructPointer
// ***********************************************************************

LPC_TMR_TypeDef *getTimerStructPointer(int timerNumber)
{
  LPC_TMR_TypeDef *timerStructPointer;
timerStructPointer =  TimerStructPointerArray[timerNumber]; 
return timerStructPointer;
}

// ***********************************************************************
// Timer Initialization
// ***********************************************************************

void initializeAllTimers()
{
enableTimer(Timer1);
enableTimer(Timer2);
enableTimer(Timer3);
enableTimer(Timer4);
}

// ***********************************************************************
// Led Initialization
// ***********************************************************************

void initializeLedPortPin(ledNumber ledNumber)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber(ledNumber, LedPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber(ledNumber, LedPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);
}

void initializeAllLeds()
{
  int ledNumber;

  for (ledNumber = 1; ledNumber < LedNumberMax +1; ledNumber++)
  {
 initializeLedPortPin(ledNumber);
  }
}

// ***********************************************************************
// Key Initialization
// ***********************************************************************

void initializeKeyPortPin(keyNumber keyNumber)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getKeyPortNumber(keyNumber, KeyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getKeyPinNumber(keyNumber, KeyPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);
}

void initializeAllKeys()
{
  int keyNumber;

  for (keyNumber = 1; keyNumber < KeyNumberMax +1; keyNumber++)
  {
 initializeLedPortPin(keyNumber);
  }
}

// ***********************************************************************
// PWM Functions
// ***********************************************************************

void setupPwmPin()
{
LPC_IOCON ->PIO1_9 |= (1 << 0); // PIN1_9 = CT16B1_MAT0
}

// ***********************************************************************
// Initialize all LEDs, keys, and timers
// ***********************************************************************

void initializeSystem()
{
initializeAllLeds();
initializeAllKeys();
initializeAllTimers();
}

// ***********************************************************************
// Blink Led Function
// ***********************************************************************

void blinky(ledNumber ledNumber, int onTime, int offTime, int blinkCount)
{
  int count;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber(ledNumber, LedPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber(ledNumber, LedPortPinArrayArray);
 
for (count = 0; count < blinkCount; count++)
{
  setPortPinValue(gpio_struct_ptr, pinNumber, LogicLevelLow);
     delayMilliSecond(onTime);    
setPortPinValue(gpio_struct_ptr, pinNumber, LogicLevelHigh);
     delayMilliSecond(offTime);
  }
}

// ***********************************************************************
// Echo key Function !!! not yet tested !!! tlfong01 2013jul2201
// ***********************************************************************

typedef logicLevelValue keyStatus;
keyStatus KeyPressed = LOW;
keyStatus KeyReleased = HIGH;

keyStatus getKeyStatus(keyNumber keyNumber)
{
keyStatus keyStatus;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getKeyPortNumber(keyNumber, KeyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getKeyPinNumber(keyNumber, KeyPortPinArrayArray);
 
keyStatus = getGpioDataRegisterBitValue(gpio_struct_ptr, pinNumber);
  return keyStatus;
}

void echoKey(keyNumber keyNumber, ledNumber ledNumber, int echoCount)
{
  int count;
keyStatus keyStatus;

for (count = 0; count < echoCount; count++)
{
keyStatus = getKeyStatus(keyNumber);
    if (keyStatus == KeyPressed)
{
 keyStatus = KeyReleased;
blinky(ledNumber, OnHalfSecond, OffOneSecond, FourTimes);
}
  }
}

// ***********************************************************************
// Test Functions
// ***********************************************************************

void testBlinkLed()
{
blinky(Led1, OnHalfSecond, OffOneSecond, FourTimes); // Port1, Pin8
blinky(Led2, OnHalfSecond, OffOneSecond, FourTimes); // Port2, Pin7
blinky(Led3, OnHalfSecond, OffOneSecond, FourTimes); // Port2, Pin8
blinky(Led4, OnHalfSecond, OffOneSecond, FourTimes); // Port2, Pin5
}

void testBlinkLedPio18()
{
   //blinky(LedPio18, OnHalfSecond, OffOneSecond, TwentyTimes); // Led driven by Port 1, Pin 8
blinky(LedPio18, 100, 200, 1000000); // Led driven by Port 1, Pin 8
}

void testEchoKey() // not yet tested !!! tlfong01 2013jul2201
{
echoKey(Key4, Led1, FourTimes);
}

// ***********************************************************************
// Main Function
// ***********************************************************************

int main()
{  
initializeSystem();
  // testBlinkLed();
testBlinkLedPio18();
}

// ***********************************************************************
// End of Program
// ***********************************************************************

// .END //