blinky_2013071602 refactoring notes

// ****************************************************************************
// Program - Blinky
// Description - Blink LED
// Author - TL Fong
// Version - 2013071602
// Date - 2013jul16hkt0944
// 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 logicLevel;
logicLevel LogicLevelLow = 0;
logicLevel 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 tenthSecond;
tenthSecond HalfSecond = 5;
tenthSecond OneSecond = 10;

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

typedef int ledPortPinArrayArray[4][2];
ledPortPinArrayArray Lppaa = {{1, 8},{1, 8}, {1, 8}, {1, 8}};

// ***********************************************************************
// 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 lppaa)
{
  int portNumber;
  portNumber = lppaa [ledNumber][0];
  return portNumber;
}

LPC_GPIO_TypeDef *getGpioStructPointer(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 lppaa)
{
  int ledPinNumber;
  ledPinNumber = lppaa [ledNumber][1];
  return ledPinNumber;
}


// ***********************************************************************
// GPIO Functions (Set pin direction and value) v0.511
// ***********************************************************************

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

void setPortPinValue0513(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, int pinValue)
{
  if (pinValue == 1)
 gpio_struct_ptr->DATA &= ~(1 << pinNumber);        
else
 gpio_struct_ptr->DATA |= (1 << pinNumber);
}

// ***********************************************************************
// GPIO Functions (Set pin direction and value) v.0420
// ***********************************************************************

void setPortPinValue0420(int portNumber, int pinNumber, int pinValue)
{
  if (pinValue == 1)
 LPC_GPIO1->DATA &= ~(1 << pinNumber);        
else
 LPC_GPIO1->DATA |= (1 << pinNumber);
}

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

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

portNumber = getLedPortNumber(ledNumber, Lppaa);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getLedPinNumber(ledNumber, Lppaa);

setPortPinDirection0511(gpio_struct_ptr, pinNumber);
 
for (count = 0; count < blinkCount; count--)
{
  setPortPinValue0513(gpio_struct_ptr, 8, 0);
     delayTenthSecond(onTime);    
setPortPinValue0513(gpio_struct_ptr, 8, 1);
     delayTenthSecond(offTime);
  }
}

void blinky0514(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, int onTime, int offTime, int blinkCount)
{
  int count;

setPortPinDirection0511(gpio_struct_ptr, pinNumber);
 
for (count = 0; count < blinkCount; count--)
{
  setPortPinValue0513(gpio_struct_ptr, 8, 0);
     delayTenthSecond(onTime);    
setPortPinValue0513(gpio_struct_ptr, 8, 1);
     delayTenthSecond(offTime);
  }
}

void blinky0420(int ledNumber, int onTime, int offTime, int blinkCount)
{
  int count;

setPortPinDirection0511(LPC_GPIO1, 8);
 
for (count = 0; count < blinkCount; count--)
{
  setPortPinValue0513(LPC_GPIO1, 8, 0);
     delayTenthSecond(onTime);    
setPortPinValue0513(LPC_GPIO1, 8, 1);
     delayTenthSecond(offTime);
  }
}

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

//void testBlinky()
//{
//  blinky(Port0, Pin8, HalfSecond, OneSecond, TwentyTimes); 
//}  

//void testBlinky01()
//{
//  blinky(Led0, Pin8, HalfSecond, OneSecond, FourTimes); 
//}  

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

int main()
{  
//blinky0420(1, 5, 10, 20); 
// blinky0514(LPC_GPIO1, 5, 10, 20); 
// blinky0514(LPC_GPIO1, Pin8, HalfSecond, OneSecond, TwentyTimes);
blinky0515(Led1, HalfSecond, OneSecond, TwentyTimes);
}

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

No comments:

Post a Comment