blink_2013071605 refactoring notes

// ****************************************************************************
// Program - Blinky
// Description - Blink LED
// Author - TL Fong
// Version - 2013071605
// Date - 2013jul16hkt1122
// Hardware - Somy ARM Cortex M0 LPC1114/301 Learning Board 
// Software - MDK-Lite 4.71.0.0, uVision 4.71.2.0
// Method - Muddling through + Incremental testing
// Somy Configuration -
//   LED1 - P1.8
// Function structure
//   main()
//     blinky()
//       setPortPinOutput()
//       setPortPinLow()
//       setPortPinHigh()
//       delayTenthSecond()
// ****************************************************************************

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

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

typedef int logicLevelValue;
logicLevelValue LogicLevelLow = 0;
logicLevelValue LogicLevelHigh = 1;

typedef const int portNumber;
portNumber Port0 = 0;
portNumber Port1 = 1;
portNumber Port2 = 2;
portNumber Port3 = 3;
portNumber Port4 = 4;
portNumber Port5 = 5;
portNumber Port6 = 6;
portNumber Port7 = 7;

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 ledNumber;
ledNumber Led0 = 0;
ledNumber Led1 = 1;

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

typedef int tenthSecond;
tenthSecond OnHalfSecond = 5;
tenthSecond OffOneSecond = 10;

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

typedef int ledPortPinArrayArray[4][2];
ledPortPinArrayArray LedPortPinArrayArray = {{Port2, Pin3},{Port2, Pin3}, {Port1, Pin8}, {Port1, Pin8}};

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

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

// ***********************************************************************
// GPIO Functions (GPIO control structure, port, pin)
// ***********************************************************************

portNumber getLedPortNumber(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
  int portNumber;
  portNumber = ledPortPinArrayArray [ledNumber][PortIndex];
  return portNumber;
}

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


LPC_GPIO_TypeDef *getGpioStructPointer01(int portNumber)
{
  LPC_GPIO_TypeDef *gpioStructPointer;

  switch(portNumber)
    {
    case 0:
      gpioStructPointer = LPC_GPIO0;
break;
    case 1:
      gpioStructPointer = LPC_GPIO1;
break;    
default:
 break;
 }
return gpioStructPointer;
}

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

// ***********************************************************************
// GPIO Functions (Set pin direction and value) 
// ***********************************************************************

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 setPortPinDirectionOutput(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber)
  {
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);  
}

// ***********************************************************************
// LED Functions
// ***********************************************************************

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

setPortPinDirectionOutput(gpio_struct_ptr, pinNumber);
 
for (count = 0; count < blinkCount; count--)
{
  setPortPinValue(gpio_struct_ptr, pinNumber, LogicLevelLow);
     delayTenthSecond(onTime);    
setPortPinValue(gpio_struct_ptr, pinNumber, LogicLevelHigh);
     delayTenthSecond(offTime);
  }
}

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

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

int main()
{  
blinky(Led1, OnHalfSecond, OffOneSecond, TwentyTimes);
}

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

No comments:

Post a Comment