// Program - Blinky
// Description - Blink LED
// Author - TL Fong <fongcm0.blogspot.hk> <tlfong01hongkong@google.com>
// Build - 2013.07.17.04
// Date - 2013jul17hkt1055
// Hardware - NXP ARM Cortex M0 LPC1114
// Software - Keil MDK-Lite 4.71.0.0, uVision 4.71.2.0
// Reference - MIT OCW 601SC Intro EE&CS1, <http://ocw.mit.edu/6-01SCS11>
// License - Creative Commons <http://ocw.mit.edu/terms/#cc>
// ***********************************************************************
#include <stdio.h>
#include "lpc11xx.h"
// ***********************************************************************
// Function heirarchy
// ***********************************************************************
// main()
// blinky()
// getLedPortNumber();
// getGpioStructPointer();
// getLedPinNumber();
// setPortPinOutput()
// setPortPinLow()
// setPortPinHigh()
// delayTenthSecond()
// setPortPinDirectionOutput()
// setGpioDataRegisterBitLow()
// setGpioDataRegisterBitHigh()
// setGpioDirectionRegisterBitHigh()
// setPortPinValue()
// ***********************************************************************
// Gpio Pin Assignment
// ***********************************************************************
// Port Pin Function
// 0 1
// 0 2 Key 1
// 0 3 Key 2
// 0 4
// 0 5
// 0 6
// 0 7
// 0 8
// 0 9
// 0 10
// 0 11
// 1 1
// 1 2
// 1 3
// 1 4 Key 5
// 1 5
// 1 6
// 1 7
// 1 8 Led 1
// 1 9 Key 4
// 1 10
// 1 11
// 2 1
// 2 2
// 2 3
// 2 4
// 2 5 Led 4
// 2 6
// 2 7 Led 2
// 2 8 Led 3
// 2 9
// 2 10 Key 1
// 2 11
// 3 1
// 3 2
// 3 3
// 3 4 Key 3
// 3 5
// 3 6
// 3 7
// 3 8
// 3 9
// 3 10
// 3 11
// ***********************************************************************
// Global constants and variables
// ***********************************************************************
#define LOW 0
#define HIGH 1
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;
ledNumber Led2 = 2;
ledNumber Led3 = 3;
ledNumber Led4 = 4;
int const LedNumberMax = 4;
typedef const int keyNumber;
keyNumber Key1 = 1;
keyNumber Key2 = 2;
keyNumber Key3 = 3;
keyNumber Key4 = 4;
keyNumber Key5 = 5;
int const KeyNumberMax = 5;
typedef int ledPortArrayArrayIndex;
ledPortArrayArrayIndex PortIndex = 0;
ledPortArrayArrayIndex PinIndex = 1;
typedef int tenthSecond;
tenthSecond OnHalfSecond = 5;
tenthSecond OffOneSecond = 10;
tenthSecond FourSeconds = 40;
typedef int repeatCount;
repeatCount FourTimes = 4;
repeatCount TwentyTimes = 20;
typedef int ledPortPinArrayArray[4][2];
ledPortPinArrayArray LedPortPinArrayArray = {{Port1, Pin8},{Port2, Pin7}, {Port2, Pin8}, {Port2, Pin5}};
typedef int keyPortPinArrayArray[5][2];
keyPortPinArrayArray KeyPortPinArrayArray = {{Port0, Pin2},{Port0, Pin3}, {Port3, Pin4}, {Port1, Pin9}, {Port1, Pin4}};
typedef LPC_GPIO_TypeDef *gpioStructPointerArray[4];
gpioStructPointerArray GpioStructPointerArray = {LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3};
typedef int logicLevelValue;
logicLevelValue LogicLevelLow = 0;
logicLevelValue LogicLevelHigh = 1;
typedef int directionValue;
directionValue InputDirection = 0;
directionValue OutputDirection = 1;
// ***********************************************************************
// 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++)
{
}
}
}
}
// ***********************************************************************
// Get Led port number and pin number
// ***********************************************************************
portNumber getLedPortNumber(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
int portNumber;
portNumber = ledPortPinArrayArray [ledNumber - 1][PortIndex];
return portNumber;
}
pinNumber getLedPinNumber(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
int ledPinNumber;
ledPinNumber = ledPortPinArrayArray [ledNumber - 1][PinIndex];
return ledPinNumber;
}
// ***********************************************************************
// Get Key port number and pin number
// ***********************************************************************
portNumber getKeyPortNumber(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
int portNumber;
portNumber = keyPortPinArrayArray [keyNumber - 1][PortIndex];
return portNumber;
}
pinNumber getKeyPinNumber(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
int keyPinNumber;
keyPinNumber = keyPortPinArrayArray [keyNumber - 1][PinIndex];
return keyPinNumber;
}
// ***********************************************************************
// Get GpioStructPointer
// ***********************************************************************
LPC_GPIO_TypeDef *getGpioStructPointer(int portNumber)
{
LPC_GPIO_TypeDef *gpioStructPointer;
gpioStructPointer = GpioStructPointerArray[portNumber];
return gpioStructPointer;
}
// ***********************************************************************
// Set pin direction and value
// ***********************************************************************
void setGpioDirectionRegisterBitLow(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR &= ~(1 << bitNumber);
}
void setGpioDirectionRegisterBitHigh(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR |= (1 << bitNumber);
}
void setGpioDataRegisterBitLow(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA &= ~(1 << bitNumber);
}
void setGpioDataRegisterBitHigh(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA |= (1 << bitNumber);
}
void setPortPinDirection(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, directionValue directionValue)
{
if (directionValue == InputDirection)
setGpioDirectionRegisterBitLow(gpio_struct_ptr, pinNumber);
else
setGpioDirectionRegisterBitHigh(gpio_struct_ptr, pinNumber);
}
void setPortPinValue(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, logicLevelValue logicLevelValue)
{
if (logicLevelValue == LogicLevelLow)
setGpioDataRegisterBitLow(gpio_struct_ptr, pinNumber);
else
setGpioDataRegisterBitHigh(gpio_struct_ptr, pinNumber);
}
// ***********************************************************************
// Get pin data value
// ***********************************************************************
logicLevelValue getGpioDataRegisterBitValue(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
logicLevelValue logicLevelValue;
logicLevelValue = gpio_struct_ptr->DIR &= ~(1 << bitNumber);
return logicLevelValue;
}
// ***********************************************************************
// Led Initialization
// ***********************************************************************
void initializeLedPortPin(ledNumber ledNumber)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;
portNumber = getLedPortNumber(ledNumber, LedPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getLedPinNumber(ledNumber, LedPortPinArrayArray);
setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);
}
void initializeAllLedPortPins()
{
int ledNumber;
for (ledNumber = 1; ledNumber < LedNumberMax +1; ledNumber++)
{
initializeLedPortPin(ledNumber);
}
}
// ***********************************************************************
// Key Initialization
// ***********************************************************************
void initializeKeyPortPin(keyNumber keyNumber)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;
portNumber = getKeyPortNumber(keyNumber, KeyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getKeyPinNumber(keyNumber, KeyPortPinArrayArray);
setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);
}
void initializeAllKeyPortPins()
{
int keyNumber;
for (keyNumber = 1; keyNumber < KeyNumberMax +1; keyNumber++)
{
initializeLedPortPin(keyNumber);
}
}
// ***********************************************************************
// Initialize all Led and Keys
// ***********************************************************************
void initializeLedKey()
{
initializeAllLedPortPins();
initializeAllKeyPortPins();
}
// ***********************************************************************
// Blink Led Function
// ***********************************************************************
void blinky(ledNumber ledNumber, int onTime, int offTime, int blinkCount)
{
int count;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;
portNumber = getLedPortNumber(ledNumber, LedPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getLedPinNumber(ledNumber, LedPortPinArrayArray);
for (count = 0; count < blinkCount; count++)
{
setPortPinValue(gpio_struct_ptr, pinNumber, LogicLevelLow);
delayTenthSecond(onTime);
setPortPinValue(gpio_struct_ptr, pinNumber, LogicLevelHigh);
delayTenthSecond(offTime);
}
}
// ***********************************************************************
// Echo key Function
// ***********************************************************************
typedef logicLevelValue keyStatus;
keyStatus KeyPressed = LOW;
keyStatus KeyReleased = HIGH;
keyStatus getKeyStatus(keyNumber keyNumber)
{
keyStatus keyStatus;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;
portNumber = getKeyPortNumber(keyNumber, KeyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getKeyPinNumber(keyNumber, KeyPortPinArrayArray);
keyStatus = getGpioDataRegisterBitValue(gpio_struct_ptr, pinNumber);
return keyStatus;
}
void echoKey(keyNumber keyNumber, ledNumber ledNumber, int echoCount)
{
int count;
keyStatus keyStatus;
for (count = 0; count < echoCount; count++)
{
keyStatus = getKeyStatus(keyNumber);
if (keyStatus == KeyPressed)
{
keyStatus = KeyReleased;
blinky(ledNumber, OnHalfSecond, OffOneSecond, FourTimes);
}
}
}
// ***********************************************************************
// Test Functions
// ***********************************************************************
void testBlinkLed()
{
blinky(Led1, OnHalfSecond, OffOneSecond, FourTimes);
// blinky(Led2, OnHalfSecond, OffOneSecond, FourTimes);
// blinky(Led3, OnHalfSecond, OffOneSecond, FourTimes);
// blinky(Led4, OnHalfSecond, OffOneSecond, FourTimes);
}
void testEchoKey()
{
echoKey(Key4, Led1, FourTimes);
}
// ***********************************************************************
// Main Function
// ***********************************************************************
int main()
{
initializeLedKey();
testBlinkLed();
testEchoKey();
}
// ***********************************************************************
// End of Program
// ***********************************************************************
No comments:
Post a Comment