blinky 2013aug1801 build notes

All 4 tests OK.

int main()
{      
  // blink5Leds(); // blink 5 LEDs using delay loop 
  // echo3Keys();  // echo 3 polling keys by LED          
  // pwmBlinkLedPio08(); // blink 1 LED using PWM for 2 seconds 
  pwmBlink6Led(); // blink 6 LEDs using PWM                     
}



// ***********************************************************************
// Program - FongPwmBlinky
// Description - Testing LPC1114FN28/102 Pwm Blinky
// Author - TL Fong <fongmcu.blogspot.hk> <tlfong01hongkong@google.com>
// Build - 2013.08.18.01
// Date - 2013aug18hkt1144
// Hardware - NXP ARM Cortex M0 LPC1114FN28/102, LPC1114FBD48/302   
//            Somy, DingSung, FongToy LPC1114 Evaluation Board 
// IDE      - MDK-Lite/uVision 4.71, CoLinkEx 1.1, Flash Magic v7.51 
// ***********************************************************************

#include "stdio.h"
#include "lpc11xx.h"
#include "core_cm0.h"
#include "stdint.h"

// *** fongconfig01.h ***

// ***********************************************************************
// ***********************************************************************
// GPIO, timer, peripheral numbering
// ***********************************************************************
// ***********************************************************************

// *** Port and pin numbers ***

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;

// *** Device numbers ***

typedef const int deviceNumber;

deviceNumber Device1 = 1;  
deviceNumber Device2 = 2;
deviceNumber Device3 = 3;
deviceNumber Device4 = 4;
deviceNumber Device5 = 5;
deviceNumber Device6 = 6;

deviceNumber Led1 = Device1;  
deviceNumber Led2 = Device2;
deviceNumber Led3 = Device3;
deviceNumber Led4 = Device4;
deviceNumber Led5 = Device5;

deviceNumber Key1 = Device1;
deviceNumber Key2 = Device2;
deviceNumber Key3 = Device3;
deviceNumber Key4 = Device4;
deviceNumber Key5 = Device5;

deviceNumber Pwm1 = Device1;
deviceNumber Pwm2 = Device2;
deviceNumber Pwm3 = Device3;
deviceNumber Pwm4 = Device4;
deviceNumber Pwm5 = Device5;


// ***********************************************************************
// CMSIS port pin timer 
// ***********************************************************************

typedef const int gpioPortPinNumber;
gpioPortPinNumber Pio08  = 8;
gpioPortPinNumber Pio09  = 9;
gpioPortPinNumber Rpio11 = 11;
gpioPortPinNumber Rpio12 = 12;
gpioPortPinNumber Pio19  = 19;
gpioPortPinNumber Pio110  = 110;

typedef const int timerNumber;
timerNumber Ct16b0 = 0;
timerNumber Ct16b1 = 1;
timerNumber Ct32b0 = 2;
timerNumber Ct32b1 = 3;

typedef const int matchRegisterNumber;
matchRegisterNumber MatchRegister0 = 0;
matchRegisterNumber MatchRegister1 = 1;
matchRegisterNumber MatchRegister2 = 2;

// ***********************************************************************
// CMSIS port pin timer 
// ***********************************************************************

typedef int portPinArray[2]; 

const int MaxBoardDeviceNumber = 5;
const int MaxFongBoardLedNumber = 5;
const int MaxFongBoardKeyNumber = 3;
const int MaxFongBoardPwmNumber = 5;

typedef int portPinArrayIndex;
portPinArrayIndex PortIndex = 0; 
portPinArrayIndex PinIndex  = 1;

// *** Led/Key/Pwm configuration ***

portPinArray FongLedPortPinArray1 = {Port0, Pin5};
portPinArray FongLedPortPinArray2 = {Port0, Pin6};
portPinArray FongLedPortPinArray3 = {Port0, Pin3};
portPinArray FongLedPortPinArray4 = {Port0, Pin4};
portPinArray FongLedPortPinArray5 = {Port0, Pin7};

portPinArray FongKeyPortPinArray1 = {Port1, Pin0};
portPinArray FongKeyPortPinArray2 = {Port1, Pin5};
portPinArray FongKeyPortPinArray3 = {Port1, Pin8};

portPinArray FongPwmPortPinArray1 = {Port0, Pin8};
portPinArray FongPwmPortPinArray2 = {Port0, Pin9};
portPinArray FongPwmPortPinArray3 = {Port1, Pin1};
portPinArray FongPwmPortPinArray4 = {Port1, Pin2};
portPinArray FongPwmPortPinArray5 = {Port1, Pin9};
portPinArray FongPwmPortPinArray6 = {Port1, Pin10};

// *** portPinArrayPointerArray - array of pointers to portPinArray *** 

typedef portPinArray *portPinArrayPointerArray[MaxBoardDeviceNumber];                

portPinArrayPointerArray FongLedList1[MaxFongBoardLedNumber] = 
{
  &FongLedPortPinArray1, &FongLedPortPinArray2, &FongLedPortPinArray3, &FongLedPortPinArray4, &FongLedPortPinArray5,
};

portPinArrayPointerArray FongKeyList1[MaxFongBoardKeyNumber] = 
{
 &FongKeyPortPinArray1, &FongKeyPortPinArray2, &FongKeyPortPinArray3
};
        
portPinArrayPointerArray FongPwmList1[MaxFongBoardPwmNumber] = 
{
  &FongPwmPortPinArray1, 
&FongPwmPortPinArray2, 
&FongPwmPortPinArray3, 
&FongPwmPortPinArray4, 
&FongPwmPortPinArray5, 
&FongPwmPortPinArray6
};
                                                                
// *** Eval board structure ***

typedef struct
{
 portPinArrayPointerArray *LedPortPinArrayPointerArray;
 portPinArrayPointerArray *KeyPortPinArrayPointerArray;
 portPinArrayPointerArray *PwmPortPinArrayPointerArray;
} mcuBoardStruct;

mcuBoardStruct *CreateFongBoardStruct()
{
  mcuBoardStruct *FongBoardStruct;

  FongBoardStruct->LedPortPinArrayPointerArray = FongLedList1;
  FongBoardStruct->KeyPortPinArrayPointerArray = FongKeyList1;
  FongBoardStruct->PwmPortPinArrayPointerArray = FongPwmList1; 

  return FongBoardStruct; 


// *** fongdelay01.h ***

// ***********************************************************************
// ***********************************************************************
// Delay functions and count constants
// ***********************************************************************
// ***********************************************************************

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;
}

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++)
        {
        }
      }
  }
}

// *** Loop constants ***

typedef int repeatCount;
repeatCount OneTime     =  1;
repeatCount TwoTimes    =  2;
repeatCount ThreeTimes  =  3;
repeatCount FourTimes   =  4;
repeatCount FiveTimes   =  5;
repeatCount SixTimes    =  6;
repeatCount EightTimes  =  8;
repeatCount TenTimes    = 10;
repeatCount TwentyTimes = 20;

// # fonggpio01.h #

// ***********************************************************************
// ***********************************************************************
// GPIO basic functions 
// ***********************************************************************
// ***********************************************************************

// * typedefs and #defines *

typedef int directionValue;

directionValue InputDirection = 0;
directionValue OutputDirection = 1;

typedef int logicLevelValue;

logicLevelValue LogicLevelLow = 0;
logicLevelValue LogicLevelHigh = 1;

#define LOW 0
#define HIGH 1

// ***********************************************************************
// Name        - GpioStructPointerArray
// Description - Contains the 4 CMSIS port control structures
// ***********************************************************************

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

// ***********************************************************************
// Name   - getGpioStructPointer
// Input  - port number
// Output - GPIO control/data structure pointer
// ***********************************************************************

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

typedef struct
{
LPC_GPIO_TypeDef *GpioStructPointer;
int PinNumber;
} gpioPortStructPinStruct;

gpioPortStructPinStruct *getGpioPortStructPinStructPointer(portNumber portNumber, pinNumber pinNumber)
  {
gpioPortStructPinStruct *gpioPortStructPinStruct;
gpioPortStructPinStruct->GpioStructPointer = getGpioStructPointer(portNumber);
gpioPortStructPinStruct->PinNumber = pinNumber;
return gpioPortStructPinStruct;
  }

// ***********************************************************************
// Name   - setGpioDirectionRegisterBitInput
// Input  - port control structure, pin number
// Output - set pin as input
// ***********************************************************************

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


// ***********************************************************************
// Name   - setGpioDirectionRegisterBitOutput
// Input  - port control structure, pin number
// Output - set pin as output
// ***********************************************************************

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


// ***********************************************************************
// Name   - setGpioDataRegisterBitLow
// Input  - port control structure, pin number
// Output - set pin as output
// ***********************************************************************

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


// ***********************************************************************
// Name   - setGpioDataRegisterBitHigh
// Input  - port control structure, pin number
// Output - set pin as output
// ***********************************************************************

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


// ***********************************************************************
// Name   - setGpioDirectionRegister
// Input  - port control structure, pin number, direction value
// Output - set pin as input or output
// ***********************************************************************

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

// ***********************************************************************
// Name   - setGpioDataRegister
// Input  - port control structure, pin number, data value
// Output - none
// ***********************************************************************

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


// ***********************************************************************
// Function - getGpioDataRegisterBitValue
// Input    - GPIO structure pointer, bit position
// Output   - port pin data bit
// ***********************************************************************

logicLevelValue getGpioDataRegisterBitValue(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitPosition)
{
 logicLevelValue logicLevelValue;

 int GpioDataRegisterMask = (1 << bitPosition);

 if ((gpio_struct_ptr->DATA  &= GpioDataRegisterMask) == GpioDataRegisterMask) 
  logicLevelValue = HIGH; 
 else
  logicLevelValue = LOW; 

  return logicLevelValue;


// ***********************************************************************
// Name   - get port number for portPinArray
// Input  - port pin array
// Output - portNumber
// ***********************************************************************

portNumber getPortNumber061(portPinArray portPinArray)
{
 int portNumber;
 portNumber = portPinArray[PortIndex];
 return portNumber;
}

// ***********************************************************************
// Name   - get port number from portPinArrayPointerArray
// Input  - portPinPointerArray, deviceNumber
// Output - portNumber
// ***********************************************************************

portNumber getPortNumber06(portPinArrayPointerArray portPinArrayPointerArray, deviceNumber deviceNumber)
{
  int portNumber; 
 portNumber = getPortNumber061(*portPinArrayPointerArray[deviceNumber - 1]);  
 return portNumber;
}

// ***********************************************************************
// Name   - get pin number for portPinArray
// Input  - port pin array
// Output - pinNumber
// ***********************************************************************

pinNumber getPinNumber061(portPinArray portPinArray)
{
 int pinNumber;
 pinNumber = portPinArray[PinIndex];
 return pinNumber;
}

// ***********************************************************************
// Name   - get pin number from portPinArrayPointerArray and index
// Input  - portPinPointerArray, index
// Output - pinNumber
// ***********************************************************************

pinNumber getPinNumber06(portPinArrayPointerArray portPinArrayPointerArray, deviceNumber deviceNumber)
{
  int pinNumber; 
  pinNumber = getPinNumber061(*portPinArrayPointerArray[deviceNumber - 1]);  
  return pinNumber;
}

// *** fonggpio01.h ***

// ***********************************************************************
// ***********************************************************************
// LED functions 
// ***********************************************************************
// ***********************************************************************

// ***********************************************************************
// Name   - initialize LED port pin
// Input  - portPinPointerArray, index
// Output - none
// ***********************************************************************

void initializeLedPortPin06(portPinArrayPointerArray portPinArrayPointerArray, deviceNumber ledNumber)
{
  int portNumber;
  int pinNumber;
  LPC_GPIO_TypeDef *gpioStructPointer;
                                            
  portNumber = getPortNumber06(portPinArrayPointerArray, ledNumber); 
  pinNumber = getPinNumber06(portPinArrayPointerArray, ledNumber); 

  // *** the 3 lines below are not working, don't know why - tlfong01 2013aug17 ***
  // gpioPortStructPinStruct *gpioPortStructPinStructPointer;
// gpioPortStructPinStructPointer = getGpioPortStructPinStructPointer(portNumber, pinNumber);
  // gpioStructPointer = gpioPortStructPinStructPointer -> GpioStructPointer;

  gpioStructPointer = getGpioStructPointer(portNumber);  

  setPortPinDirection(gpioStructPointer, pinNumber, OutputDirection);  
}

// ***********************************************************************
// Function - initialize all LED port pins
// Input    - ledPortPinPointerArray
// Output   - none
// ***********************************************************************

void initializeAllLedPortPin06(portPinArrayPointerArray ledPortPinArrayPointerArray, int maxLedNumber)
{
  int ledNumber;

  for (ledNumber = 1; ledNumber < (maxLedNumber + 1); ledNumber++) // Caution!!! ledNumber starts 1, not 0 !!!
  { 
   initializeLedPortPin06(ledPortPinArrayPointerArray, ledNumber);
  }


// ***********************************************************************
// Blinky functions 
// ***********************************************************************

void blinkLed06(portPinArrayPointerArray portPinArrayPointerArray, deviceNumber ledNumber, int onTime, int offTime, int blinkCount)
{
 int count;
 int portNumber;
 int pinNumber;
 LPC_GPIO_TypeDef *gpio_struct_ptr;
                                         
 portNumber = getPortNumber06(portPinArrayPointerArray, ledNumber); 
 gpio_struct_ptr = getGpioStructPointer(portNumber);  

 pinNumber = getPinNumber06(portPinArrayPointerArray, ledNumber); 

  delayMilliSecond(100); 

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

void testBlinkLed06(portPinArrayPointerArray portPinArrayPointerArray, int maxLedNumber, int onTime, int offTime, int blinkCount)
{
   int i;  
  initializeAllLedPortPin06(portPinArrayPointerArray, maxLedNumber);
     
   for (i = 1; i < maxLedNumber + 1; i++)
  {
    blinkLed06(portPinArrayPointerArray, i, onTime, offTime, blinkCount);
   }
}

void blinkFongLedList1() 
{
  testBlinkLed06(*FongLedList1, 5, 100, 200, 2); 
}

void blink5Leds()
{
blinkFongLedList1();
}


// ***********************************************************************
// Blink Leds using FongBoardStruct - not working !!! 
// ***********************************************************************

void testBlinkLed063() // using mcuBoardStruct, not working !!!
{
  portPinArrayPointerArray *fongLedPortPinArrayPointerArray; 

  mcuBoardStruct *FongBoardStruct;
  FongBoardStruct->LedPortPinArrayPointerArray = FongLedList1;
  FongBoardStruct->KeyPortPinArrayPointerArray = FongKeyList1;
  FongBoardStruct->PwmPortPinArrayPointerArray = FongPwmList1; 

  fongLedPortPinArrayPointerArray = FongBoardStruct->LedPortPinArrayPointerArray;   

  testBlinkLed06(*fongLedPortPinArrayPointerArray, 4, 100, 200, 3);
}


// *** fongkey01.h ***

// ***********************************************************************
// ***********************************************************************
// Key functions 
// ***********************************************************************
// ***********************************************************************

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

// ***********************************************************************
// Name   - initialize key port pin
// Input  - portPinPointerArray, index
// Output - none
// ***********************************************************************

void initializeKeyPortPin06(portPinArrayPointerArray portPinArrayPointerArray, deviceNumber keyNumber)
{
 int portNumber;
 int pinNumber;
 LPC_GPIO_TypeDef *gpio_struct_ptr;
                                            
 portNumber = getPortNumber06(portPinArrayPointerArray, keyNumber); 
 gpio_struct_ptr = getGpioStructPointer(portNumber);  

 pinNumber = getPinNumber06(portPinArrayPointerArray, keyNumber); 

  setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);  
}

// ***********************************************************************
// Function - initialize all key port pins
// Input    - keyPortPinPointerArray
// Output   - none
// ***********************************************************************

void initializeAllKeyPortPin06(portPinArrayPointerArray keyPortPinArrayPointerArray, int maxLedNumber)
{
  int keyNumber;

  for (keyNumber = 1; keyNumber < (maxLedNumber + 1); keyNumber++) // Caution!!! keyNumber starts 1, not 0 !!!
  { 
   initializeLedPortPin06(keyPortPinArrayPointerArray, keyNumber);
  }


// ***********************************************************************
// Function - getKeyStatus
// Input    - keyPortPinArrayPointerArray, keyNumber
// Output   - port pin data bit
// ***********************************************************************

keyStatus getKeyStatus(portPinArrayPointerArray keyPortPinArrayPointerArray, deviceNumber keyNumber)
{
 keyStatus keyStatus;
 int portNumber;
 int pinNumber;
 LPC_GPIO_TypeDef *gpio_struct_ptr;

 portNumber = getPortNumber06(keyPortPinArrayPointerArray, keyNumber); 
 gpio_struct_ptr = getGpioStructPointer(portNumber);   
 pinNumber = getPinNumber06(keyPortPinArrayPointerArray, keyNumber); 
    
 keyStatus = getGpioDataRegisterBitValue(gpio_struct_ptr, pinNumber);

  return keyStatus;
}

// ***********************************************************************
// Function - echo key by LED
// Input    - keyPinPortArrayPointerArray, keyNumber, ledPinPortArrayPointerArray, ledNumber
// Output   - none
// ***********************************************************************

void echoKey(portPinArrayPointerArray keyPortPinArrayPointerArray, deviceNumber keyNumber, portPinArrayPointerArray ledPortPinArrayPointerArray, deviceNumber ledNumber, int echoCount)
{
  int count;
 keyStatus keyStatus;

 for (count = 0; count < echoCount; count++)
 { 
   delayMilliSecond(1000);
  
   keyStatus = HIGH; 
  while (keyStatus == HIGH)
  {
     keyStatus = getKeyStatus(keyPortPinArrayPointerArray, keyNumber);
       
    if (keyStatus == LOW)
     {
        blinkLed06(ledPortPinArrayPointerArray, ledNumber, 100, 200, TwoTimes);
      delayMilliSecond(2000); 
     keyStatus = HIGH;
     break;       
       }
    }
 }
}

void testEchoKey06(deviceNumber keyNumber, deviceNumber ledNumber, repeatCount repeatCount) 
{
 echoKey(*FongKeyList1, keyNumber, *FongLedList1, ledNumber, repeatCount); 
}

void echo3Keys() 
{
 testEchoKey06(Key1, Led1, TwoTimes); 
 testEchoKey06(Key2, Led2, TwoTimes); 
 testEchoKey06(Key3, Led3, TwoTimes); 
}

// *** fongtimer01.h ***

// ***********************************************************************
// ***********************************************************************
// Timer functions
// ***********************************************************************
// ***********************************************************************

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

// ***********************************************************************
// Function - Timer #defines
// Input    - none
// Output   - none
// ***********************************************************************

#define SYSAHBCLKCTRL_GPIO                    ((unsigned int) 0x00000040) // Enables clock for GPIO
#define SYSAHBCLKCTRL_IOCON                   ((unsigned int) 0x00010000) // Enables clock for IO configuration block
#define SYSAHBCLKCTRL_CT16B0                  ((unsigned int) 0x00000080) // Enables clock for 16-bit counter/timer 0
#define SYSAHBCLKCTRL_CT16B1                  ((unsigned int) 0x00000100) // Enables clock for 16-bit counter/timer 1
#define SYSAHBCLKCTRL_CT32B0                  ((unsigned int) 0x00000200) // Enables clock for 32-bit counter/timer 0
#define SYSAHBCLKCTRL_CT32B1                  ((unsigned int) 0x00000400) // Enables clock for 32-bit counter/timer 1

#define TMR_TMR16B0TCR_COUNTERENABLE_ENABLED      ((unsigned int) 0x00000001)
#define TMR_TMR16B1TCR_COUNTERENABLE_ENABLED      ((unsigned int) 0x00000001)
#define TMR_TMR32B0TCR_COUNTERENABLE_ENABLED      ((unsigned int) 0x00000001)
#define TMR_TMR32B1TCR_COUNTERENABLE_ENABLED      ((unsigned int) 0x00000001)

#define TMR_TMR16B0MCR_MR3_RESET_ENABLED          ((unsigned int) 0x00000400)
#define TMR_TMR16B1MCR_MR3_RESET_ENABLED          ((unsigned int) 0x00000400)
#define TMR_TMR32B0MCR_MR3_RESET_ENABLED          ((unsigned int) 0x00000400)
#define TMR_TMR32B1MCR_MR3_RESET_ENABLED          ((unsigned int) 0x00000400)

#define IOCON_PIO0_1_FUNC_MASK                    ((unsigned int) 0x00000007)
#define IOCON_PIO0_1_FUNC_GPIO                    ((unsigned int) 0x00000000)
#define IOCON_PIO0_1_FUNC_CLKOUT                  ((unsigned int) 0x00000001)
#define IOCON_PIO0_1_FUNC_CT32B0_MAT2             ((unsigned int) 0x00000002)

#define IOCON_PIO0_2_FUNC_MASK                    ((unsigned int) 0x00000007)
#define IOCON_PIO0_2_FUNC_GPIO                    ((unsigned int) 0x00000000)
#define IOCON_PIO0_2_FUNC_SSEL                    ((unsigned int) 0x00000001)
#define IOCON_PIO0_2_FUNC_CT16B0_CAP0             ((unsigned int) 0x00000002)

#define IOCON_PIO0_8_FUNC_MASK                    ((unsigned int) 0x00000007)
#define IOCON_PIO0_8_FUNC_GPIO                    ((unsigned int) 0x00000000)
#define IOCON_PIO0_8_FUNC_MISO0                   ((unsigned int) 0x00000001)
#define IOCON_PIO0_8_FUNC_CT16B0_MAT0             ((unsigned int) 0x00000002)

#define IOCON_PIO0_9_FUNC_MASK                    ((unsigned int) 0x00000007)
#define IOCON_PIO0_9_FUNC_GPIO                    ((unsigned int) 0x00000000)
#define IOCON_PIO0_9_FUNC_MOSI0                   ((unsigned int) 0x00000001)
#define IOCON_PIO0_9_FUNC_CT16B0_MAT1             ((unsigned int) 0x00000002)

#define IOCON_PIO1_8_FUNC_MASK                    ((unsigned int) 0x00000007)
#define IOCON_PIO1_8_FUNC_GPIO                    ((unsigned int) 0x00000000)
#define IOCON_PIO1_8_FUNC_CT16B1_CAP0             ((unsigned int) 0x00000001)

#define IOCON_PIO1_9_FUNC_MASK                    ((unsigned int) 0x00000007)
#define IOCON_PIO1_9_FUNC_GPIO                    ((unsigned int) 0x00000000)
#define IOCON_PIO1_9_FUNC_CT16B1_MAT0             ((unsigned int) 0x00000001)

#define IOCON_PIO1_10_FUNC_MASK                   ((unsigned int) 0x00000007)
#define IOCON_PIO1_10_FUNC_GPIO                   ((unsigned int) 0x00000000)
#define IOCON_PIO1_10_FUNC_AD6                    ((unsigned int) 0x00000001)
#define IOCON_PIO1_10_FUNC_CT16B1_MAT1            ((unsigned int) 0x00000002)

#define IOCON_JTAG_TDO_PIO1_1_FUNC_MASK           ((unsigned int) 0x00000007)
#define IOCON_JTAG_TDO_PIO1_1_FUNC_TDO            ((unsigned int) 0x00000000)
#define IOCON_JTAG_TDO_PIO1_1_FUNC_GPIO           ((unsigned int) 0x00000001)
#define IOCON_JTAG_TDO_PIO1_1_FUNC_AD2            ((unsigned int) 0x00000002)
#define IOCON_JTAG_TDO_PIO1_1_FUNC_CT32B1_MAT0    ((unsigned int) 0x00000003)

#define IOCON_JTAG_nTRST_PIO1_2_FUNC_MASK         ((unsigned int) 0x00000007)
#define IOCON_JTAG_nTRST_PIO1_2_FUNC_TRST         ((unsigned int) 0x00000000)
#define IOCON_JTAG_nTRST_PIO1_2_FUNC_GPIO         ((unsigned int) 0x00000001)
#define IOCON_JTAG_nTRST_PIO1_2_FUNC_AD3          ((unsigned int) 0x00000002)
#define IOCON_JTAG_nTRST_PIO1_2_FUNC_CT32B1_MAT1  ((unsigned int) 0x00000003)

#define TMR_TMR16B0EMR_EM0_MASK                   ((unsigned int) 0x00000001) // External match 0
#define TMR_TMR16B0EMR_EM0                        ((unsigned int) 0x00000001)
#define TMR_TMR16B0EMR_EMC0_MASK                  ((unsigned int) 0x00000030)
#define TMR_TMR16B0EMR_EMC0_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR16B0EMR_EMC0_LOW                   ((unsigned int) 0x00000010)
#define TMR_TMR16B0EMR_EMC0_HIGH                  ((unsigned int) 0x00000020)
#define TMR_TMR16B0EMR_EMC0_TOGGLE                ((unsigned int) 0x00000030)
#define TMR_TMR16B0EMR_EM1_MASK                   ((unsigned int) 0x00000002) // External match 1
#define TMR_TMR16B0EMR_EM1                        ((unsigned int) 0x00000002)
#define TMR_TMR16B0EMR_EMC1_MASK                  ((unsigned int) 0x000000C0)
#define TMR_TMR16B0EMR_EMC1_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR16B0EMR_EMC1_LOW                   ((unsigned int) 0x00000040)
#define TMR_TMR16B0EMR_EMC1_HIGH                  ((unsigned int) 0x00000080)
#define TMR_TMR16B0EMR_EMC1_TOGGLE                ((unsigned int) 0x000000C0)
#define TMR_TMR16B0EMR_EM2_MASK                   ((unsigned int) 0x00000004) // External match 2
#define TMR_TMR16B0EMR_EM2                        ((unsigned int) 0x00000004)
#define TMR_TMR16B0EMR_EMC2_MASK                  ((unsigned int) 0x00000300)
#define TMR_TMR16B0EMR_EMC2_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR16B0EMR_EMC2_LOW                   ((unsigned int) 0x00000100)
#define TMR_TMR16B0EMR_EMC2_HIGH                  ((unsigned int) 0x00000200)
#define TMR_TMR16B0EMR_EMC2_TOGGLE                ((unsigned int) 0x00000300)
#define TMR_TMR16B0EMR_EM3_MASK                   ((unsigned int) 0x00000008) // External match 3
#define TMR_TMR16B0EMR_EM3                        ((unsigned int) 0x00000008)
#define TMR_TMR16B0EMR_EMC3_MASK                  ((unsigned int) 0x00000C00)
#define TMR_TMR16B0EMR_EMC3_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR16B0EMR_EMC3_LOW                   ((unsigned int) 0x00000400)
#define TMR_TMR16B0EMR_EMC3_HIGH                  ((unsigned int) 0x00000800)
#define TMR_TMR16B0EMR_EMC3_TOGGLE                ((unsigned int) 0x00000C00)

#define TMR_TMR16B1EMR_EM0_MASK                   ((unsigned int) 0x00000001) // External match 0
#define TMR_TMR16B1EMR_EM0                        ((unsigned int) 0x00000001)
#define TMR_TMR16B1EMR_EMC0_MASK                  ((unsigned int) 0x00000030)
#define TMR_TMR16B1EMR_EMC0_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR16B1EMR_EMC0_LOW                   ((unsigned int) 0x00000010)
#define TMR_TMR16B1EMR_EMC0_HIGH                  ((unsigned int) 0x00000020)
#define TMR_TMR16B1EMR_EMC0_TOGGLE                ((unsigned int) 0x00000030)
#define TMR_TMR16B1EMR_EM1_MASK                   ((unsigned int) 0x00000002) // External match 1
#define TMR_TMR16B1EMR_EM1                        ((unsigned int) 0x00000002)
#define TMR_TMR16B1EMR_EMC1_MASK                  ((unsigned int) 0x000000C0)
#define TMR_TMR16B1EMR_EMC1_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR16B1EMR_EMC1_LOW                   ((unsigned int) 0x00000040)
#define TMR_TMR16B1EMR_EMC1_HIGH                  ((unsigned int) 0x00000080)
#define TMR_TMR16B1EMR_EMC1_TOGGLE                ((unsigned int) 0x000000C0)
#define TMR_TMR16B1EMR_EM2_MASK                   ((unsigned int) 0x00000004) // External match 2
#define TMR_TMR16B1EMR_EM2                        ((unsigned int) 0x00000004)
#define TMR_TMR16B1EMR_EMC2_MASK                  ((unsigned int) 0x00000300)
#define TMR_TMR16B1EMR_EMC2_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR16B1EMR_EMC2_LOW                   ((unsigned int) 0x00000100)
#define TMR_TMR16B1EMR_EMC2_HIGH                  ((unsigned int) 0x00000200)
#define TMR_TMR16B1EMR_EMC2_TOGGLE                ((unsigned int) 0x00000300)
#define TMR_TMR16B1EMR_EM3_MASK                   ((unsigned int) 0x00000008) // External match 3
#define TMR_TMR16B1EMR_EM3                        ((unsigned int) 0x00000008)
#define TMR_TMR16B1EMR_EMC3_MASK                  ((unsigned int) 0x00000C00)
#define TMR_TMR16B1EMR_EMC3_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR16B1EMR_EMC3_LOW                   ((unsigned int) 0x00000400)
#define TMR_TMR16B1EMR_EMC3_HIGH                  ((unsigned int) 0x00000800)
#define TMR_TMR16B1EMR_EMC3_TOGGLE                ((unsigned int) 0x00000C00)

/*  PWM control register */

#define TMR_TMR16B0PWMC_PWM0_MASK                 ((unsigned int) 0x00000001)   
#define TMR_TMR16B0PWMC_PWM0_ENABLED              ((unsigned int) 0x00000001) // PWM mode is enabled for CT16Bn_MAT0
#define TMR_TMR16B0PWMC_PWM0_DISABLED             ((unsigned int) 0x00000000)
#define TMR_TMR16B0PWMC_PWM1_MASK                 ((unsigned int) 0x00000002)   
#define TMR_TMR16B0PWMC_PWM1_ENABLED              ((unsigned int) 0x00000002) // PWM mode is enabled for CT16Bn_MAT1
#define TMR_TMR16B0PWMC_PWM1_DISABLED             ((unsigned int) 0x00000000)
#define TMR_TMR16B0PWMC_PWM2_MASK                 ((unsigned int) 0x00000004)   
#define TMR_TMR16B0PWMC_PWM2_ENABLED              ((unsigned int) 0x00000004) // PWM mode is enabled for CT16Bn_MAT2
#define TMR_TMR16B0PWMC_PWM2_DISABLED             ((unsigned int) 0x00000000)
#define TMR_TMR16B0PWMC_PWM3_MASK                 ((unsigned int) 0x00000008)   
#define TMR_TMR16B0PWMC_PWM3_ENABLED              ((unsigned int) 0x00000008)
#define TMR_TMR16B0PWMC_PWM3_DISABLED             ((unsigned int) 0x00000000)

/*  PWM control register */

#define TMR_TMR16B1PWMC_PWM0_MASK                 ((unsigned int) 0x00000001)   
#define TMR_TMR16B1PWMC_PWM0_ENABLED              ((unsigned int) 0x00000001) // PWM mode is enabled for CT16Bn_MAT0
#define TMR_TMR16B1PWMC_PWM0_DISABLED             ((unsigned int) 0x00000000)
#define TMR_TMR16B1PWMC_PWM1_MASK                 ((unsigned int) 0x00000002)   
#define TMR_TMR16B1PWMC_PWM1_ENABLED              ((unsigned int) 0x00000002) // PWM mode is enabled for CT16Bn_MAT1
#define TMR_TMR16B1PWMC_PWM1_DISABLED             ((unsigned int) 0x00000000)
#define TMR_TMR16B1PWMC_PWM2_MASK                 ((unsigned int) 0x00000004)   
#define TMR_TMR16B1PWMC_PWM2_ENABLED              ((unsigned int) 0x00000004) // PWM mode is enabled for CT16Bn_MAT2
#define TMR_TMR16B1PWMC_PWM2_DISABLED             ((unsigned int) 0x00000000)
#define TMR_TMR16B1PWMC_PWM3_MASK                 ((unsigned int) 0x00000008)   
#define TMR_TMR16B1PWMC_PWM3_ENABLED              ((unsigned int) 0x00000008)
#define TMR_TMR16B1PWMC_PWM3_DISABLED             ((unsigned int) 0x00000000)

/*  External match register */
#define TMR_TMR32B1EMR_EM0_MASK                   ((unsigned int) 0x00000001) // External match 0
#define TMR_TMR32B1EMR_EM0                        ((unsigned int) 0x00000001)
#define TMR_TMR32B1EMR_EMC0_MASK                  ((unsigned int) 0x00000030)
#define TMR_TMR32B1EMR_EMC0_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR32B1EMR_EMC0_LOW                   ((unsigned int) 0x00000010)
#define TMR_TMR32B1EMR_EMC0_HIGH                  ((unsigned int) 0x00000020)
#define TMR_TMR32B1EMR_EMC0_TOGGLE                ((unsigned int) 0x00000030)
#define TMR_TMR32B1EMR_EM1_MASK                   ((unsigned int) 0x00000002) // External match 1
#define TMR_TMR32B1EMR_EM1                        ((unsigned int) 0x00000002)
#define TMR_TMR32B1EMR_EMC1_MASK                  ((unsigned int) 0x000000C0)
#define TMR_TMR32B1EMR_EMC1_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR32B1EMR_EMC1_LOW                   ((unsigned int) 0x00000040)
#define TMR_TMR32B1EMR_EMC1_HIGH                  ((unsigned int) 0x00000080)
#define TMR_TMR32B1EMR_EMC1_TOGGLE                ((unsigned int) 0x000000C0)
#define TMR_TMR32B1EMR_EM2_MASK                   ((unsigned int) 0x00000004) // External match 2
#define TMR_TMR32B1EMR_EM2                        ((unsigned int) 0x00000004)
#define TMR_TMR32B1EMR_EMC2_MASK                  ((unsigned int) 0x00000300)
#define TMR_TMR32B1EMR_EMC2_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR32B1EMR_EMC2_LOW                   ((unsigned int) 0x00000100)
#define TMR_TMR32B1EMR_EMC2_HIGH                  ((unsigned int) 0x00000200)
#define TMR_TMR32B1EMR_EMC2_TOGGLE                ((unsigned int) 0x00000300)
#define TMR_TMR32B1EMR_EM3_MASK                   ((unsigned int) 0x00000008) // External match 3
#define TMR_TMR32B1EMR_EM3                        ((unsigned int) 0x00000008)
#define TMR_TMR32B1EMR_EMC3_MASK                  ((unsigned int) 0x00000C00)
#define TMR_TMR32B1EMR_EMC3_DONOTHING             ((unsigned int) 0x00000000)
#define TMR_TMR32B1EMR_EMC3_LOW                   ((unsigned int) 0x00000400)
#define TMR_TMR32B1EMR_EMC3_HIGH                  ((unsigned int) 0x00000800)
#define TMR_TMR32B1EMR_EMC3_TOGGLE                ((unsigned int) 0x00000C00)

/*  PWM control register */
#define TMR_TMR32B1PWMC_PWM0_MASK                 ((unsigned int) 0x00000001)   
#define TMR_TMR32B1PWMC_PWM0_ENABLED              ((unsigned int) 0x00000001) // PWM mode is enabled for CT32Bn_MAT0
#define TMR_TMR32B1PWMC_PWM0_DISABLED             ((unsigned int) 0x00000000)
#define TMR_TMR32B1PWMC_PWM1_MASK                 ((unsigned int) 0x00000002)   
#define TMR_TMR32B1PWMC_PWM1_ENABLED              ((unsigned int) 0x00000002) // PWM mode is enabled for CT32Bn_MAT1
#define TMR_TMR32B1PWMC_PWM1_DISABLED             ((unsigned int) 0x00000000)
#define TMR_TMR32B1PWMC_PWM2_MASK                 ((unsigned int) 0x00000004)   
#define TMR_TMR32B1PWMC_PWM2_ENABLED              ((unsigned int) 0x00000004) // PWM mode is enabled for CT32Bn_MAT2
#define TMR_TMR32B1PWMC_PWM2_DISABLED             ((unsigned int) 0x00000000)
#define TMR_TMR32B1PWMC_PWM3_MASK                 ((unsigned int) 0x00000008)   
#define TMR_TMR32B1PWMC_PWM3_ENABLED              ((unsigned int) 0x00000008)
#define TMR_TMR32B1PWMC_PWM3_DISABLED             ((unsigned int) 0x00000000)

// ***********************************************************************
// SysCon, IoCon, and Timer pointers
// ***********************************************************************

typedef LPC_SYSCON_TypeDef *lpcSysCon;
lpcSysCon LpcSysCon = LPC_SYSCON;

typedef LPC_IOCON_TypeDef *lpcIoCon;
lpcIoCon LpcIoCon = LPC_IOCON;

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

// *** Timer config and enable functions ***

// portPinArray FongPwmPortPinArray1 = {Port0, Pin1};
// portPinArray FongPwmPortPinArray2 = {Port1, Pin1};
// portPinArray FongPwmPortPinArray3 = {Port1, Pin2};
// portPinArray FongPwmPortPinArray4 = {Port1, Pin4};
// portPinArray FongPwmPortPinArray5 = {Port1, Pin9};


// ***********************************************************************
// ***********************************************************************
// Program     - PWM Blinky
// Description - Blink Somy Board LED PIO0_8 using PWM 
// Build       - 2013.08.10.06
// Date        - 2013aug10hkt1645
// Author      - TL Fong
// Reference   - MIT 6.01SC Intro EE&CS1, <http://ocw.mit.edu/6-01SCS11>
// License     - Creative Commons BY-NC-SA <http://ocw.mit.edu/terms/>
// Hardware    - NXP ARM Cortex M0 LPC1114FBD48/302 (Somy Eval Board)  
// IDE         - MDK-Lite/uVision 4.71, CoLinkEx 1.1, Flash Magic v7.51 
// References  - 
// 1. UM10398 LPC111x/LPC11Cxx User manual R12 2012sep24 Chapter 18
// 2. LPC11xx.h CMSIS Cortex-M0 Core Peripheral Access Layer Header File
//    for NXP LPC11xx/LPC11Cxx V1.10 2010nov24
// Notes       -
// 1. PWM pin configuration 
//      PIO0_8/MISO0/CT16B0_MAT0 = Somy LPC1114 Eval Board P4 Pin 3 
// ***********************************************************************
// ***********************************************************************

// ***********************************************************************
// PWM Blink PIO0_8 without using functions
// ***********************************************************************

void testPwmBlinkLedPio08() // PIO0_8 as CT16B0
{
  // Enable System AHB clock for CT16B0 timer (Table 21 bit 7 CT16B0)
  LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 7); // CT16B0 = 1 enables CT16B0

 // Enable System AHB clock for IOCON block (Table 21 bit 16 IOCON)
 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 16); // IOCON = 1 enable clk for IOCON

  // Set function of PIO0_8 pin (UM10398 Table 79 bits 2:0 FUNC) 
  LPC_IOCON->PIO0_8 &= ~0x7; // FUNC = 0x2 selects function of
  LPC_IOCON->PIO0_8 |=  0x2; //   CT16B0_MAT0 

  // Disable timer (Table 279 TMR16B0CTCR bit0 CEn)
  LPC_TMR16B0->TCR &= ~0x1; // CEn = 0 disables timer ?????

                
  // Set PWM mode for CT16B0_MAT0,3 (Table 293 bit 0 PWMEN0, bit3 PWMEN3)
  LPC_TMR16B0->PWMC |= 0x9; // PWMENO & PWMEN3 = 1 enable MR0, MR3

  // Set External Match Register (Tables 290, 291) // not sure about this
  LPC_TMR16B0->EMR = (0x30 | 0x1); // bits 4, 5 = 11 toggle, bit 0 = 1 
  
  // Resets timer counter on MR3 match (Table 286 bit 10 MR3R)
  LPC_TMR16B0->MCR |= 0x400; // MR3R = 1 resets counter on MR3 match

  // Set prescale 12000 count of AHB clock  = 240 uS (Table 279 TMR16B0PR)
  LPC_TMR16B0->PR = 3000; // (1/(50,000,000/2) x 12000 = 240 uS

  // Set cycle length (Table 279 TMR16B0MR3, Figure 68)
  LPC_TMR16B0->MR3 = 4000; // 240uS x 4000 = 0.96 == 1 second

  // Set duty cycle (Table 279 TMR16B0MR0, Figure 68)
  LPC_TMR16B0->MR0 = 2000; // 240uS x 2000 == 0.5 second (50% duty cycle)

  // Start timer (Table 279 TMR16B0CTCR bit0 CEn)
  LPC_TMR16B0->TCR |= 0x1; // CEn = 1 enables timer 

}

void pwmBlinkLedPio08()
{
  testPwmBlinkLedPio08();

delayTenthSecond(40);

  LPC_TMR16B0->TCR &= ~0x1; // CEn = 0 disables timer 

  delayTenthSecond(80);
}


// ***********************************************************************
// Function - Clock setting and control
// Input    - none
// Output   - none
// ***********************************************************************

void enableAllClocks()
{
  LPC_SYSCON->SYSAHBCLKCTRL |= (SYSAHBCLKCTRL_GPIO);
  LPC_SYSCON->SYSAHBCLKCTRL |= (SYSAHBCLKCTRL_IOCON);
  LPC_SYSCON->SYSAHBCLKCTRL |= (SYSAHBCLKCTRL_CT16B0);
  LPC_SYSCON->SYSAHBCLKCTRL |= (SYSAHBCLKCTRL_CT16B1);
  LPC_SYSCON->SYSAHBCLKCTRL |= (SYSAHBCLKCTRL_CT32B0);
  LPC_SYSCON->SYSAHBCLKCTRL |= (SYSAHBCLKCTRL_CT32B1);
}

void startTimer(timerNumber timerNumber)

  switch (timerNumber)
  {
  case Ct16b0: 
   LPC_TMR16B0->TCR |= TMR_TMR16B0TCR_COUNTERENABLE_ENABLED;
   break;
  case Ct16b1: 
   LPC_TMR16B1->TCR |= TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
   break;
  case Ct32b0: 
   LPC_TMR32B0->TCR |= TMR_TMR32B0TCR_COUNTERENABLE_ENABLED;
   break;
  case Ct32b1: 
   LPC_TMR32B1->TCR |= TMR_TMR32B1TCR_COUNTERENABLE_ENABLED;
   break;
  } 
}

void startAllTimers()

  startTimer(Ct16b0);
  startTimer(Ct16b1);
  startTimer(Ct32b0);
  startTimer(Ct32b1); 
}

void stopTimer(timerNumber timerNumber)

  switch (timerNumber)
  {
  case Ct16b0: 
   LPC_TMR16B0->TCR &= ~TMR_TMR16B0TCR_COUNTERENABLE_ENABLED;
   break;
  case Ct16b1: 
   LPC_TMR16B1->TCR &= ~TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
   break;
  case Ct32b0: 
   LPC_TMR32B0->TCR &= ~TMR_TMR32B0TCR_COUNTERENABLE_ENABLED;
   break;
  case Ct32b1: 
   LPC_TMR32B1->TCR &= ~TMR_TMR32B1TCR_COUNTERENABLE_ENABLED;
   break;
  } 
}

void stopAllTimers()
{
  stopTimer(Ct16b0);
  stopTimer(Ct16b1);
  stopTimer(Ct32b0);
  stopTimer(Ct32b1);
}

// ***********************************************************************
// Function - Timer setting and control
// Input    - none
// Output   - none
// ***********************************************************************

void setGpioPinTimer(gpioPortPinNumber gpioPortPinNumber)
{
  switch (gpioPortPinNumber)
  {
  case Pio08: // PIO0_8 = CT16B0MAT0
    LPC_IOCON->PIO0_8 &= ~IOCON_PIO0_8_FUNC_MASK;
    LPC_IOCON->PIO0_8 |= IOCON_PIO0_8_FUNC_CT16B0_MAT0;
    break;
  case Pio09: // PIO0_9 = CT16B0MAT1
    LPC_IOCON->PIO0_9 &= ~IOCON_PIO0_9_FUNC_MASK;
    LPC_IOCON->PIO0_9 |= IOCON_PIO0_9_FUNC_CT16B0_MAT1;
    break;
  case Pio19: // PIO1_9 = CT16B1MAT0
    LPC_IOCON->PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
    LPC_IOCON->PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;
    break;
 case Pio110: // PIO1_10 = CT16B1MAT1
    LPC_IOCON->PIO1_10 &= ~IOCON_PIO1_10_FUNC_MASK;
    LPC_IOCON->PIO1_10 |= IOCON_PIO1_10_FUNC_CT16B1_MAT1;
    break;
  case Rpio11: // PIO1_1 = CT32B1MAT0
    LPC_IOCON->R_PIO1_1 &= ~IOCON_JTAG_TDO_PIO1_1_FUNC_MASK;
    LPC_IOCON->R_PIO1_1 |= IOCON_JTAG_TDO_PIO1_1_FUNC_CT32B1_MAT0;
    break;
case Rpio12: // PIO1_1 = CT32B1MAT0
    LPC_IOCON->R_PIO1_2 &= ~IOCON_JTAG_nTRST_PIO1_2_FUNC_MASK;
    LPC_IOCON->R_PIO1_2 |= IOCON_JTAG_nTRST_PIO1_2_FUNC_CT32B1_MAT1;
   break;
  }
}

// ***********************************************************************
// Function - PWM registers setting 
// Input    - none
// Output   - none
// ***********************************************************************

void setPwmRegisters01(gpioPortPinNumber gpioPortPinNumber)
{
  switch (gpioPortPinNumber)
  {
  case Pio08: // PIO0_8 = CT16B0MAT0
    LPC_TMR16B0->MCR = (TMR_TMR16B0MCR_MR3_RESET_ENABLED);
    LPC_TMR16B0->EMR = TMR_TMR16B0EMR_EMC0_TOGGLE | TMR_TMR16B0EMR_EM0;
    LPC_TMR16B0->PWMC |= TMR_TMR16B0PWMC_PWM0_ENABLED | TMR_TMR16B0PWMC_PWM3_ENABLED;
    break;
  case Pio09: // PIO0_9 = CT16B0MAT1
    LPC_TMR16B0->MCR = (TMR_TMR16B0MCR_MR3_RESET_ENABLED);
    LPC_TMR16B0->EMR |= TMR_TMR16B0EMR_EMC1_TOGGLE | TMR_TMR16B0EMR_EM1; 
    LPC_TMR16B0->PWMC |= TMR_TMR16B0PWMC_PWM1_ENABLED | TMR_TMR16B0PWMC_PWM3_ENABLED;
    break;
  case Pio19: // PIO1_9 = CT16B1MAT0
    LPC_TMR16B1->MCR = (TMR_TMR16B1MCR_MR3_RESET_ENABLED);
    LPC_TMR16B1->EMR |= TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;
    LPC_TMR16B1->PWMC |= TMR_TMR16B1PWMC_PWM0_ENABLED | TMR_TMR16B1PWMC_PWM3_ENABLED;
    break;
  case Pio110: // PIO1_10 = CT16B1MAT1
    LPC_TMR16B1->MCR = (TMR_TMR16B1MCR_MR3_RESET_ENABLED);
    LPC_TMR16B1->EMR |= TMR_TMR16B1EMR_EMC1_TOGGLE | TMR_TMR16B1EMR_EM1;
    LPC_TMR16B1->PWMC |= TMR_TMR16B1PWMC_PWM1_ENABLED | TMR_TMR16B1PWMC_PWM3_ENABLED;
    break;
  case Rpio11: // PIO1_1 = CT32B1MAT0
    LPC_TMR32B1->MCR = (TMR_TMR32B1MCR_MR3_RESET_ENABLED);
    LPC_TMR32B1->EMR |= TMR_TMR32B1EMR_EMC0_TOGGLE | TMR_TMR32B1EMR_EM0;
    LPC_TMR32B1->PWMC |= TMR_TMR32B1PWMC_PWM0_ENABLED | TMR_TMR32B1PWMC_PWM3_ENABLED;
    break;
  case Rpio12: // PIO1_2 = CT32B1MAT1
    LPC_TMR32B1->MCR = (TMR_TMR32B1MCR_MR3_RESET_ENABLED);
    LPC_TMR32B1->EMR |= TMR_TMR32B1EMR_EMC1_TOGGLE | TMR_TMR32B1EMR_EM1;
    LPC_TMR32B1->PWMC |= TMR_TMR32B1PWMC_PWM1_ENABLED | TMR_TMR32B1PWMC_PWM3_ENABLED;
    break;
  }
}

// ***********************************************************************
// Function - PWM functions 
// Input    - none
// Output   - none
// ***********************************************************************

void setPwmPrescalePeriod01(timerNumber timerNumber, int prescale, int period)
{
  switch (timerNumber)
  {
  case Ct16b0: 
    LPC_TMR16B0->PR = prescale;   // 12000 = 240 uS
   LPC_TMR16B0->MR3 = period;    // 240uS x 4000 = 0.96 == 1 second
   break;
  case Ct16b1: 
    LPC_TMR16B1->PR = prescale;   // 12000 = 240 uS
    LPC_TMR16B1->MR3 = period;    // 240uS x 4000 = 0.96 == 1 second  
    break;
  case Ct32b1: 
    LPC_TMR32B1->PR = prescale;   // 12000 = 240 uS
    LPC_TMR32B1->MR3 = period;    // 240uS x 4000 = 0.96 == 1 second  
   break;  
  }
}

void setPwmDutyCycle02(timerNumber timerNumber, matchRegisterNumber matchRegisterNumber, int dutyCycle)
{
  switch (timerNumber)
  {
  case Ct16b0: 
switch (matchRegisterNumber)
 {
case MatchRegister0:
        LPC_TMR16B0->MR0 = dutyCycle; // 240uS x 2000 == 0.5 second (50% duty cycle)   
        break;
case MatchRegister1:
        LPC_TMR16B0->MR1 = dutyCycle; // 240uS x 2000 == 0.5 second (50% duty cycle)   
        break;
}
break;
  case Ct16b1: 
switch (matchRegisterNumber)
 {
case MatchRegister0:
        LPC_TMR16B1->MR0 = dutyCycle; // 240uS x 2000 == 0.5 second (50% duty cycle)   
        break;
case MatchRegister1:
        LPC_TMR16B1->MR1 = dutyCycle; // 240uS x 2000 == 0.5 second (50% duty cycle)   
        break;
}
break;
  case Ct32b1: 
switch (matchRegisterNumber)
 {
case MatchRegister0:
        LPC_TMR32B1->MR0 = dutyCycle; // 240uS x 2000 == 0.5 second (50% duty cycle)   
        break;
case MatchRegister1:
        LPC_TMR32B1->MR1 = dutyCycle; // 240uS x 2000 == 0.5 second (50% duty cycle)   
        break;
}
break;
  }
}

void setupDutyCycle(timerNumber timerNumber, matchRegisterNumber matchRegisterNumber, int dutyCycle)
{
 switch (timerNumber)
  {
  case Ct16b0: 
switch (matchRegisterNumber)
 {
case MatchRegister0:
setPwmDutyCycle02(Ct16b0, MatchRegister0, dutyCycle);
case MatchRegister1:
setPwmDutyCycle02(Ct16b0, MatchRegister1, dutyCycle);
        break;
}
break;

case Ct16b1: 
    switch (matchRegisterNumber)
 {
case MatchRegister0:
setPwmDutyCycle02(Ct16b1, MatchRegister0, dutyCycle);
case MatchRegister1:
setPwmDutyCycle02(Ct16b1, MatchRegister1, dutyCycle);
        break;
}
break;

  case Ct32b1: 
    switch (matchRegisterNumber)
 {
case MatchRegister0:
setPwmDutyCycle02(Ct32b1, MatchRegister0, dutyCycle);
case MatchRegister1:
setPwmDutyCycle02(Ct32b1, MatchRegister1, dutyCycle);
        break;
}
break;
  }
}

// ***********************************************************************
// ***********************************************************************
// Function - PWM testing 
// Input    - none
// Output   - none
// ***********************************************************************
// ***********************************************************************

void setupPwm(int prescale, int period, int dutyCycle)
{
enableAllClocks();

stopAllTimers();

// *** Setup timers CT16B0, CT16B1, CT32B1 ***

setPwmPrescalePeriod01(Ct16b0, prescale, period);
setPwmDutyCycle02(Ct16b0, MatchRegister0, dutyCycle);
setPwmDutyCycle02(Ct16b0, MatchRegister1, dutyCycle);

   setPwmPrescalePeriod01(Ct16b1, prescale, period);
setPwmDutyCycle02(Ct16b1, MatchRegister0, dutyCycle);
   setPwmDutyCycle02(Ct16b1, MatchRegister1, dutyCycle);

   setPwmPrescalePeriod01(Ct32b1, prescale, period);
   setPwmDutyCycle02(Ct32b1, MatchRegister0, dutyCycle);
   setPwmDutyCycle02(Ct32b1, MatchRegister1, dutyCycle);

// *** Setup GPIO pins ***

setGpioPinTimer(Pio08);
   setPwmRegisters01(Pio08);
 
   setGpioPinTimer(Pio09);
   setPwmRegisters01(Pio09);

setGpioPinTimer(Pio19);   
   setPwmRegisters01(Pio19);

   setGpioPinTimer(Pio110);               
   setPwmRegisters01(Pio110);

setGpioPinTimer(Rpio11);               
   setPwmRegisters01(Rpio11);

setGpioPinTimer(Rpio12);               
   setPwmRegisters01(Rpio12);
 
// *** setup duty cylce ***
 
setupDutyCycle(Ct16b0, MatchRegister0, 2000);
  setupDutyCycle(Ct16b0, MatchRegister1, 2000);
  setupDutyCycle(Ct16b1, MatchRegister0, 2000);
  setupDutyCycle(Ct16b1, MatchRegister1, 2000);
  setupDutyCycle(Ct32b1, MatchRegister0, 2000);
  setupDutyCycle(Ct32b1, MatchRegister1, 2000);  
}

void pwmBlink6Led() 
{
setupPwm(12000, 4000, 2000);
startAllTimers();
delayTenthSecond(80);
stopAllTimers();
}

// ***********************************************************************
// ***********************************************************************
// Function - Interrupt testing 
// Input    - none
// Output   - none
// ***********************************************************************
// ***********************************************************************

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

int main()
{      
  // blink5Leds(); // blink 5 LEDs using delay loop
// echo3Keys();  // echo 3 polling keys by LED
  // pwmBlinkLedPio08(); // blink 1 LED using PWM for 2 seconds    
  pwmBlink6Led(); // blink 6 LEDs using PWM
}

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

// .END 

No comments:

Post a Comment