blink 2013aug1001 debugging notes

// ***********************************************************************
// Program - FongPwmBlinky
// Description - Testing LPC1114FN28/102 Pwm Blinky
// Author - TL Fong <fongmcu.blogspot.hk> <tlfong01hongkong@google.com>
// Build - 2013.08.10.01
// Date - 2013aug10hkt1049
// Hardware - NXP ARM Cortex M0 LPC1114FN28/102, LPC1114/11U14 FBD48/301/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"

// # fongportpin01.h #
// ***********************************************************************
// GPIO port/pin number definition and declaration
// ***********************************************************************

// *** 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;

typedef const int ledNumber;

ledNumber Led1 = 1;  // caution!!! LED #1 denotes element #0 in port/pin array array !!!
ledNumber Led2 = 2;
ledNumber Led3 = 3;
ledNumber Led4 = 4;
ledNumber Led5 = 5;
ledNumber Led6 = 6;

typedef const int keyNumber;

typedef int keyPortArrayArrayIndex;

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


// fongconfig01.h
// ***********************************************************************
// Fong Board and Xia Board configuration - last update 2013aug03
// ***********************************************************************

// *** Led and key port pin array array ***

typedef int ledPortPinArrayArray[6][2]; // maximum 6 LEDs, each with unique port/pin
typedef int keyPortPinArrayArray[5][2]; // maximum 5 keys, each with unique port/pin

typedef int portPinArrayArrayIndex;

portPinArrayArrayIndex PortIndex = 0; // index of port
portPinArrayArrayIndex PinIndex  = 1; // index of pin

// * Fong Board has 3 LEDs and 2 keys *

int MaxFongLedNumber = 3;
int MaxFongKeyNumber = 2;

ledPortPinArrayArray FongLedPortPinArrayArray = {{Port1, Pin8}, {Port0, Pin2}, {Port0, Pin8}};
keyPortPinArrayArray FongKeyPortPinArrayArray = {{Port1, Pin9}, {Port0, Pin3}};

// * Xia Board has 5 LEDs (1_8, 2_7, 2_8, 2_5, 0_8 (PWM) and 5 keys *

ledPortPinArrayArray XiaLedPortPinArrayArray = {{Port1, Pin8}, {Port2, Pin7}, {Port2, Pin8}, {Port2, Pin5}, {Port0, Pin8}};
int MaxXiaLedNumber = 5;

keyPortPinArrayArray XiaKeyPortPinArrayArray = {{Port0, Pin2},{Port0, Pin3}, {Port3, Pin4}, {Port1, Pin9}, {Port1, Pin4}};
int MaxXiaKeyNumber = 5;


// *** Mpu Board Struct ***

typedef struct
{
ledPortPinArrayArray ledPortPinArrayArray[5][2];
keyPortPinArrayArray keyPortPinArrayArray[5][2];
} mcuBoardStruct;

// * Fong Board Struct *

mcuBoardStruct FongBoardStruct = {{{Port1, Pin8}, {Port0, Pin2}, {Port0, Pin8}},
                                  {{Port1, Pin9}, {Port0, Pin3}}};

mcuBoardStruct *FongBoard = &FongBoardStruct;


// * Xia Board Struct *

mcuBoardStruct XiaBoardStruct = {{{Port1, Pin8},{Port2, Pin7}, {Port2, Pin8}, {Port2, Pin5}, {Port0, Pin8}},
                                 {{Port0, Pin2},{Port0, Pin3}, {Port3, Pin4}, {Port1, Pin9}, {Port1, Pin4}}};

mcuBoardStruct *XiaBoard = &XiaBoardStruct;
 

// # fongdelayloop01.h #
// ***********************************************************************
// Delay functions and count constants
// ***********************************************************************

// *** delay using looping ***

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

// *** delay using Systick ***

static volatile uint32_t TimeTick = 0;

void SysTick_Handler(void)
{
       TimeTick++;
}

typedef int tenthSecond;
/*
tenthSecond OnHalfSecond = 5;
tenthSecond OnOneSecond = 10;

tenthSecond OffHalfSecond = 5;
tenthSecond OffOneSecond = 10;

tenthSecond FourSeconds = 40;
*/

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

// *** Timer constants ***

typedef int milliSecond;
milliSecond OnQuarterSecond  =  250;
milliSecond OnHalfSecond     =  500;
milliSecond OffQuarterSecond =  250;
milliSecond OffHalfSecond    =  500;
milliSecond OffOneSecond     = 1000;

// *** 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 port/pin set input/output and read/write data 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

// * GPIO port/pin control/data structure pointers array *

// # LPC11xx has 4 control/data structures to handle 4 GPIO ports.
// # The pointers of the 4 structures are stored in a 4 element array.

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

// ***********************************************************************
// 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);
}

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)
 setGpioDirectionRegisterBitInput(gpio_struct_ptr, pinNumber);
else
setGpioDirectionRegisterBitOutput(gpio_struct_ptr, pinNumber);
  }

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


// # fongled01.h
// ***********************************************************************
// LED functions 
// ***********************************************************************

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

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

portNumber getLedPortNumber02(ledNumber ledNumber, mcuBoardStruct *mcuBoardPtr)
{
  int portNumber;
 portNumber = (int)(mcuBoardPtr->ledPortPinArrayArray [ledNumber - 1][PortIndex]);
  return portNumber;
}

pinNumber getLedPinNumber02(ledNumber ledNumber, mcuBoardStruct *mcuBoardPtr)
{
  int ledPinNumber;
  ledPinNumber = (int)(mcuBoardPtr->ledPortPinArrayArray [ledNumber - 1][PinIndex]);
  return ledPinNumber;
}

void initializeLedPortPin01(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber01(ledNumber, ledPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber01(ledNumber, ledPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);
}

void initializeAllLeds01(ledPortPinArrayArray ledPortPinArrayArray, int maxLedNumber)
{
  int ledNumber;

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

void initializeAllLeds02(ledPortPinArrayArray ledPortPinArrayArray, int maxLedNumber)
{
  int ledNumber;

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

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

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

portNumber = getLedPortNumber01(ledNumber, ledPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber01(ledNumber, ledPortPinArrayArray);

  delayMilliSecond(500);

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

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

portNumber = getLedPortNumber02(ledNumber, mcuBoardPtr);

// portNumber = 1;

gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber02(ledNumber, mcuBoardPtr);

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

void testFongBoardBlinky01(ledNumber ledNumber, repeatCount repeatCount) 
{
   blinky01(FongLedPortPinArrayArray, ledNumber, OnHalfSecond, OffOneSecond, repeatCount);
}

void testXiaBoardBlinky01(ledNumber ledNumber, repeatCount repeatCount) 
{
   blinky01(XiaLedPortPinArrayArray, ledNumber, OnHalfSecond, OffOneSecond, repeatCount);
}

void testXiaBoardBlinky02(ledNumber ledNumber, repeatCount repeatCount) 
{
   blinky01(XiaLedPortPinArrayArray, ledNumber, OnHalfSecond, OffOneSecond, repeatCount);
}

void testFongBoardBlinky02(ledNumber ledNumber, repeatCount repeatCount) 
{
   blinky02(FongBoard, ledNumber, OnHalfSecond, OffOneSecond, repeatCount);
}


// # fongkey01.h #
// ***********************************************************************
// Key functions 
// ***********************************************************************

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

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

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

void initializeKeyPortPin01(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getKeyPortNumber01(keyNumber, keyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getKeyPinNumber01(keyNumber, keyPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, InputDirection);
}

void initializeAllKeys01(keyPortPinArrayArray keyPortPinArrayArray, int maxKeyNumber)
{
  int keyNumber;

  for (keyNumber = 1; keyNumber < (maxKeyNumber + 1); keyNumber++)
  {
 initializeKeyPortPin01(keyNumber, keyPortPinArrayArray);
  }
}

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;

  // logicLevelValue = LOW; // ########## debug only !!!
// logicLevelValue = HIGH; // ########## debug only !!!

return logicLevelValue;
}

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

portNumber = getKeyPortNumber01(keyNumber, keyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getKeyPinNumber01(keyNumber, keyPortPinArrayArray);
 
keyStatus = getGpioDataRegisterBitValue(gpio_struct_ptr, pinNumber);

return keyStatus;
}


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

for (count = 0; count < echoCount; count++)
{
 delayMilliSecond(1000);

 keyStatus = HIGH;
while (keyStatus == HIGH)
{
  keyStatus = getKeyStatus(keyPortPinArrayArray, keyNumber);
       
if (keyStatus == LOW)
  {
      blinky01(ledPortPinArrayArray, ledNumber, OnQuarterSecond, OffQuarterSecond, TwoTimes);
  delayMilliSecond(2000); 
keyStatus = HIGH;
break;   
       }
    }
}
}

void testFongBoardEchoKey01(keyNumber keyNumber, ledNumber ledNumber, repeatCount repeatCount) 
{
echoKey(FongKeyPortPinArrayArray, keyNumber, FongLedPortPinArrayArray, ledNumber, repeatCount);
}

void testXiaBoardEchoKey01(keyNumber keyNumber, ledNumber ledNumber, repeatCount repeatCount) 
{
echoKey(XiaKeyPortPinArrayArray, keyNumber, XiaLedPortPinArrayArray, ledNumber, repeatCount);
}


// # fongtimer01.h #
// ***********************************************************************
// Timer functions
// ***********************************************************************

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

typedef LPC_IOCON_TypeDef *lpcIoCon;
lpcIoCon LpcIoCon = LPC_IOCON;

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

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

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;

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

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

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

// # fongpwm01.h *
// ***********************************************************************
// PWM Blinky
// ***********************************************************************

// # fonginit01.h #
// ***********************************************************************
// Initialization functions
// ***********************************************************************

void initializeFongBoard01()
{
initializeAllLeds01(FongLedPortPinArrayArray, MaxFongLedNumber);
initializeAllKeys01(FongKeyPortPinArrayArray, MaxFongKeyNumber);
initializeAllTimers01();
}

void initializeXiaBoard01()
{
initializeAllLeds01(XiaLedPortPinArrayArray, MaxXiaLedNumber);
initializeAllKeys01(XiaKeyPortPinArrayArray, MaxXiaKeyNumber);
initializeAllTimers01();
}

/*
void initializeXiaBoard02()
{
initializeAllLeds01(XiaLedPortPinArrayArray, MaxXiaLedNumber);
initializeAllKeys01(XiaKeyPortPinArrayArray, MaxXiaKeyNumber);
initializeAllTimers01();
}
*/

// # fongtest01.h #
// ***********************************************************************
// Test functions
// ***********************************************************************

// *** Fong Board Tests ***

void testFongBoard01()
{
initializeFongBoard01();
testFongBoardBlinky01(Led1, TwoTimes);
testFongBoardBlinky01(Led2, TwoTimes);
testFongBoardEchoKey01(Key1, Led1, TwoTimes); 
testFongBoardEchoKey01(Key1, Led2, TwoTimes); 
}

void testFongBoardDelayBlinky02()
{
initializeFongBoard01();
testFongBoardBlinky01(Led1, OneTime);
testFongBoardBlinky01(Led2, TwoTimes);  
testFongBoardBlinky01(Led3, ThreeTimes);  
}

// *** Xia Board Tests ***

// ***********************************************************************
// Program     - PWM Blinky
// Description - Blink Somy Board LED PIO0_8 using PWM 
// Build       - 2013.08.08.02
// Date        - 2013aug09hkt1259
// Author      - TL Fong (fongmcu.blogspot.hk, tlfong01hongkong@google.com)
// Hardware    - NXP ARM Cortex M0 LPC1114FBD48/302   
// IDE         - MDK-Lite/uVision 4.71, CoLinkEx 1.1, Flash Magic v7.51 
// References  - 
// 1. UM10398 LPC111x/LPC11Cxx User manual R12 2012sep24
// 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 
// ***********************************************************************

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

  // Set function of PIO0_8 pin (UM10398 Table 79 bits 2:0 FUNC) 
  LPC_IOCON->PIO0_8 &= ~0x7; // FUNC = 0x2 selects function
  LPC_IOCON->PIO0_8 |=  0x2; //   CT16B0_MAT0 
                
  // Set PWM mode for CT16B0_MAT0,3 (Table 293 bit 0 PWMEN0, bit3 PWMEN3)
  LPC_TMR16B0->PWMC |= 0x9; // PWMENO, PWMEN3 = 1 enables MR0, MR3

  // Set External Match Register (Tables 290, 291)
  LPC_TMR16B0->EMR = (0x3 | 0x1); // bits 4, 5 = 11 toggle, bit 0 = 1 ?
  
  // Resets timer counter on a 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 = 12000; // (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 

  while (1)
{
// loop forever
}
}

void testXiaBoardPwmBlinky04()
{
  /* Enable the clock for CT16B0 */
  LPC_SYSCON->SYSAHBCLKCTRL |= 0x00000080;

  /* Configure PIO0.8 as Timer1_16 MAT0 Output */
  LPC_IOCON->PIO0_8 &= ~0x00000007;
  LPC_IOCON->PIO0_8 |= 0x00000002;  

  // Set prescale 12000 count of AHB clock  = 240 uS (Table 279 TMR16B0PR)
  LPC_TMR16B0->PR = 12000; // (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)

  /* Configure match control register to reset on MR3 */
  LPC_TMR16B0->MCR =  0x00000400;

  /* External Match Register Settings for PWM */
  LPC_TMR16B0->EMR = 0x00000030 | 0x00000001;

  /* Enable PWM0 and PWM3 */
  LPC_TMR16B0->PWMC = 0x00000001 | 0x00000008;

  /* Enable Timer1 */
  LPC_TMR16B0->TCR = 0x00000001;

  while (1)
{
// loop forever
}
}

void testXiaBoardPwmBlinky05() 
{
 LPC_TMR32B0 -> TCR = 0x0; // disable timer TMR32B0 

 LPC_SYSCON -> SYSAHBCLKCTRL|= (1<<9); // enable clock for timer 

 LPC_TMR32B0->TCR = 0x02;    // reset timer

 LPC_IOCON->PIO0_8 &= ~0x00000007; // set PIO0_8 as MA0
 LPC_IOCON->PIO0_8 |= 0x00000002;

 LPC_TMR32B0->MR0 =500;   // MR0 50% duty cycle

 LPC_TMR32B0->PWMC =0x09; // enable MA0, MA3

 LPC_TMR32B0->PR = 0;     // set prescale to 0

 LPC_TMR32B0->MR3 =1000;   // set period to 20us

 LPC_TMR32B0->MCR = (1 << 10);  // set rest TC on MR3 match

 LPC_TMR32B0->TCR = 0x01;   // enable timer

 while (1)
 {
// loop forever
 }
}

void testXiaBoardPwmBlinky06()
{
  // Enable System AHB clock for CT16B0 timer (Table 21 bit 7 CT16B0)
  LPC_SYSCON->SYSAHBCLKCTRL |= 0x80; // CT16B0 = 1 enables CT16B0

  // Set function of PIO0_8 pin (UM10398 Table 79 bits 2:0 FUNC) 
  LPC_IOCON->PIO0_8 &= ~0x7; // FUNC = 0x2 selects function
  LPC_IOCON->PIO0_8 |=  0x2; //   CT16B0_MAT0 
                
  // Set PWM mode for CT16B0_MAT0,3 (Table 293 bit 0 PWMEN0, bit3 PWMEN3)
  LPC_TMR16B0->PWMC |= 0x9; // PWMENO, PWMEN3 = 1 enables MR0, MR3

  // Set External Match Register (Tables 290, 291)
  LPC_TMR16B0->EMR = 0x31; // bits 4, 5 = 11 toggle, bit 0 = 1 ?
  
  // Resets timer counter on a 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 = 12000; // (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 

  while (1)
{
// loop forever
}
}

// *** microbuilder defines ***

// LPC1114 Code Base - A software library for the Cortex-M3 based LPC1100 family
// http://www.microbuilder.eu/Projects/LPC1114ReferenceDesign/LPC1114CodeBase.aspx
// https://github.com/microbuilder/LPC1114CodeBase
// http://www.microbuilder.eu/Projects/LPC1114ReferenceDesign/CodeBaseDocumentation.aspx

// typedef volatile uint8_t REG8;
// typedef volatile uint16_t REG16;
// typedef volatile uint32_t REG32;
// typedef unsigned char byte_t;

// #define pREG8  (REG8 *)
// #define pREG16 (REG16 *)
// #define pREG32 (REG32 *)

// #define SCB_SYSAHBCLKCTRL                         (*(pREG32 (0x40048080)))    // System AHB clock control
// #define IOCON_PIO1_9                              (*(pREG32 (0x40044038)))
// #define TMR_TMR16B1MR3                            (*(pREG32 (0x40010024)))    // Match register 3
// #define TMR_TMR16B1MR0                            (*(pREG32 (0x40010018)))    // Match register 0
// #define TMR_TMR16B1MCR                            (*(pREG32 (0x40010014)))
// #define TMR_TMR16B1EMR                            (*(pREG32 (0x4001003C)))
// #define TMR_TMR16B1PWMC                           (*(pREG32 (0x40010074)))
// #define TMR_TMR16B1TCR                            (*(pREG32 (0x40010004)))

// #define IOCON_PIO1_8                              (*(pREG32 (0x40044014)))
// #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_8_MODE_MASK                    ((unsigned int) 0x00000018)
// #define IOCON_PIO1_8_MODE_INACTIVE                ((unsigned int) 0x00000000)
// #define IOCON_PIO1_8_MODE_PULLDOWN                ((unsigned int) 0x00000008)
// #define IOCON_PIO1_8_MODE_PULLUP                  ((unsigned int) 0x00000010)
// #define IOCON_PIO1_8_MODE_REPEATER                ((unsigned int) 0x00000018)
// #define IOCON_PIO1_8_HYS_MASK                     ((unsigned int) 0x00000020)
// #define IOCON_PIO1_8_HYS_DISABLE                  ((unsigned int) 0x00000000)
// #define IOCON_PIO1_8_HYS_ENABLE                   ((unsigned int) 0x00000020)

// #define IOCON_PIO1_9                              (*(pREG32 (0x40044038)))
// #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_9_FUNC_MOSI1                   ((unsigned int) 0x00000002) // LPC1100XL
// #define IOCON_PIO1_9_MODE_MASK                    ((unsigned int) 0x00000018)
// #define IOCON_PIO1_9_MODE_INACTIVE                ((unsigned int) 0x00000000)
// #define IOCON_PIO1_9_MODE_PULLDOWN                ((unsigned int) 0x00000008)
// #define IOCON_PIO1_9_MODE_PULLUP                  ((unsigned int) 0x00000010)
// #define IOCON_PIO1_9_MODE_REPEATER                ((unsigned int) 0x00000018)
// #define IOCON_PIO1_9_HYS_MASK                     ((unsigned int) 0x00000020)
// #define IOCON_PIO1_9_HYS_DISABLE                  ((unsigned int) 0x00000000)
// #define IOCON_PIO1_9_HYS_ENABLE                   ((unsigned int) 0x00000020)


#define SCB_SYSAHBCLKCTRL LPC_SYSCON->SYSAHBCLKCTRL  
#define IOCON_PIO1_9   LPC_IOCON->PIO1_9  
#define IOCON_PIO0_8   LPC_IOCON->PIO0_8                               

#define TMR_TMR16B0MR3 LPC_TMR16B0->MR3    
#define TMR_TMR16B0MR0 LPC_TMR16B0->MR0     
#define TMR_TMR16B0MCR  LPC_TMR16B0->MCR   
#define TMR_TMR16B0EMR  LPC_TMR16B0->EMR   
#define TMR_TMR16B0PWMC  LPC_TMR16B0->PWMC  
#define TMR_TMR16B0TCR LPC_TMR16B0->TCR  


#define SCB_SYSAHBCLKCTRL_CT16B1                  ((unsigned int) 0x00000100) // Enables clock for 16-bit counter/timer 1
#define SCB_SYSAHBCLKCTRL_CT16B0                  ((unsigned int) 0x00000080) // Enables clock for 16-bit counter/timer 0
#define IOCON_PIO1_9_FUNC_MASK                    ((unsigned int) 0x00000007)
#define IOCON_PIO1_9_FUNC_CT16B1_MAT0             ((unsigned int) 0x00000001)
#define IOCON_PIO0_8_FUNC_CT16B0_MAT0             ((unsigned int) 0x00000002)
#define IOCON_PIO0_8_FUNC_MASK                    ((unsigned int) 0x00000007)
#define TMR_TMR16B1MCR_MR3_RESET_ENABLED          ((unsigned int) 0x00000400)
#define TMR_TMR16B1EMR_EMC0_TOGGLE                ((unsigned int) 0x00000030)
#define TMR_TMR16B1EMR_EM0                        ((unsigned int) 0x00000001)
#define TMR_TMR16B1PWMC_PWM0_ENABLED              ((unsigned int) 0x00000001) // PWM mode is enabled for CT16Bn_MAT0
#define TMR_TMR16B1PWMC_PWM3_ENABLED              ((unsigned int) 0x00000008)
#define TMR_TMR16B1TCR_COUNTERENABLE_ENABLED      ((unsigned int) 0x00000001)

#define TMR_TMR16B1MCR_MR3_RESET_ENABLED          ((unsigned int) 0x00000400)

#define TIMER16_CCLK_xMS 100000

void testXiaBoardPwmBlinky07()
{
  /* Enable the clock for CT16B1 */
  /* Enable the clock for CT16B0 */
  // SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
  SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B0);

  /* Configure PIO1.9 as Timer1_16 MAT0 Output */
  // IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
  // IOCON_PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;  

  IOCON_PIO0_8 &= ~IOCON_PIO0_8_FUNC_MASK;
  IOCON_PIO0_8 |= IOCON_PIO0_8_FUNC_CT16B0_MAT0; 

  /* Set period (MR3) to 2ms */
  TMR_TMR16B0MR3 = TIMER16_CCLK_xMS * 2;

  /* Set Duty Cycle (MR0) to 50% */
  TMR_TMR16B0MR0 = TIMER16_CCLK_xMS * 1;

  /* Configure match control register to reset on MR3 */
  TMR_TMR16B0MCR = (TMR_TMR16B1MCR_MR3_RESET_ENABLED);

  /* External Match Register Settings for PWM */
  TMR_TMR16B0EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;

  /* Enable PWM0 and PWM3 */
  TMR_TMR16B0PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED | TMR_TMR16B1PWMC_PWM3_ENABLED;

  /* Enable Timer1 */
  TMR_TMR16B0TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;

  while (1)
{
// loop forever
}
}

void testXiaBoardPwmBlinky08()
{
  /* Enable the clock for CT16B0 */  
  LPC_SYSCON->SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B0);

  /* Configure PIO0.8 as Timer1_16 MAT0 Output */
  LPC_IOCON -> PIO0_8 &= ~IOCON_PIO0_8_FUNC_MASK;
  LPC_IOCON -> PIO0_8 |= IOCON_PIO0_8_FUNC_CT16B0_MAT0; 

  /* Set period (MR3) to 2ms */
  LPC_TMR16B0 -> MR3 = TIMER16_CCLK_xMS * 2;

  /* Set Duty Cycle (MR0) to 50% */
  LPC_TMR16B0 -> MR0 = TIMER16_CCLK_xMS * 1;

  /* Configure match control register to reset on MR3 */
  LPC_TMR16B0 -> MCR = (TMR_TMR16B1MCR_MR3_RESET_ENABLED);

  /* External Match Register Settings for PWM */
  LPC_TMR16B0 -> EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;

  /* Enable PWM0 and PWM3 */
  LPC_TMR16B0 -> PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED | TMR_TMR16B1PWMC_PWM3_ENABLED;

  /* Enable Timer1 */
  LPC_TMR16B0 -> TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;

  while (1)
{
// loop forever
}
}


void testXiaBoardDelayBlinky02()
{
testXiaBoardBlinky01(Led1, OneTime);
testXiaBoardBlinky01(Led2, OneTime);  
testXiaBoardBlinky01(Led3, OneTime);  
testXiaBoardBlinky01(Led4, OneTime);
testXiaBoardBlinky01(Led5, OneTime);
}

void testXiaBoardPwmBlinky()
{
initializeXiaBoard01();
testXiaBoardDelayBlinky02(); // delay blink
testXiaBoardPwmBlinky08();   // pwm blink
}

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

int main()
{  
testXiaBoardPwmBlinky();
}

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

// .END 

No comments:

Post a Comment