Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Appendix A (LED on while pressing the switch)

#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically


__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

int main(void) {

unsigned int buttons_value;

//Initialization
//The LEDs are set as output pins (port 2 pin 0 to pin 7)
// Switch buttons are set as input pins (port 2 pin 8 to pin 11 and port 3
pin 0 to pin 3)
LPC_GPIO2->DIR = 0x0ff;
LPC_GPIO3->DIR = 0;

//Initializing the status of the LEDs to off mode with logic 1


LPC_GPIO2->DATA = 0xff;

//Enabling hysteresis and pull up resistors for the input parameters


LPC_IOCON->PIO2_8 = 0xf0;
LPC_IOCON->PIO2_9 = 0xf0;
LPC_IOCON->PIO2_10 = 0xf0;
LPC_IOCON->PIO2_11 = 0xf0;
LPC_IOCON->PIO3_0 = 0xf0;
LPC_IOCON->PIO3_1 = 0xf0;
LPC_IOCON->PIO3_2 = 0xf0;
LPC_IOCON->PIO3_3 = 0xf0;

// Check the status of push buttons by using the polling method


// Logic 0 when the button is pressed
while(1) {

// Moving the values of the first four switches to the first four binary digits of
buttons_value
buttons_value = LPC_GPIO2->DATA>>8;

// Moving the values of the last four switches to the last four binary digits of
buttons_value
buttons_value |= LPC_GPIO3->DATA<<4;

LPC_GPIO2->DATA = buttons_value;
}
return 0 ;
}

1
Appendix B (LEDs flashing for half second)

#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically


__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

void TIMER16_0_IRQHandler(void)
{
//Check for 'Match Register 3' Interrupt flag in Interrupt Register
(IR)
if ((LPC_TMR16B0->IR & 0x08) == 0x08) {
// The interrupt is generated by Match Register 3.
// Writing a logic one to the bit 3 in IR will reset
// the interrupt for Match Register 3.
LPC_TMR16B0->IR=0x08;

// Apply Ex-OR bitwise operator to toggle LED


LPC_GPIO2->DATA ^= 0xff;
}
}

int main(void) {
//Initialisation of IO port 2
//Set the lowest eight bits of GPIO 2 as output
LPC_GPIO2->DIR = 0xff;
//Set the lowest eight bit to logic HIGH which turn off LED0-LED7
LPC_GPIO2->DATA = 0xff;

// Initialisation of 16-bit counter/timer 0.


// Enables clock for CT16B0 16-bit counter/timer 0.
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);

// Pre-scaler settings - System Clock is 72MHz


// Pre-scaled timer rate = 72000000/(7199+1) = 10000 count/sec
// The 16-bit Pre-scale Register specifies the maximum value for the Pre-scale
Counter.
LPC_TMR16B0->PR = 7199;

// Configure Match Control Register(MCR) to enable Match Register(MR)


// 3 to:
LPC_TMR16B0->MCR = 0x600;
// Load Match Register MR3 for achieving the count rate =
//10000/(4999+1) = 2 count/sec, i.e. 500ms per count
LPC_TMR16B0->MR3 = 4999;

// Enable Interrupt for CT16B0 Timer


NVIC_EnableIRQ(TIMER_16_0_IRQn);

2
// Enable timer for counting by setting value 1 to TCR
LPC_TMR16B0->TCR = 0x01;

while(1) {
}
return 0 ;
}

3
Appendix C (Generate PWM with a period of 16 x 0.5 second)

#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically


__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

unsigned int timetick; //Count the number of interrupt


unsigned int key; //Count the number of LED to turn on

void TIMER16_0_IRQHandler(void)
{
//Check for 'Match Register 3' Interrupt flag in Interrupt Register (IR)
if ((LPC_TMR16B0->IR & 0x08) == 0x08)
{
//The interrupt is generated by Match Register 3.
//Writing a logic one to the bit 3 in IR will reset the interrupt for
Match Register 3.
LPC_TMR16B0->IR=0x08;
if (timetick < 15) {
timetick++;
}
else {
timetick = 0;
}
if (timertick < key){
LPC_GPIO2 -> DATA = 0xfe;
//Turn on the LED light
}
else {
LPC_GPIO2 -> DATA = 0xff;
//Turn off the LED light
}
// Apply XOR bitwise operator to toggle LED
// LPC_GPIO2->DATA ^= 0xff;
}
}

int main(void) {
//Function as digital input with hysteresis and pull up resistors enabled
LPC_IOCON->PIO2_8 = 0xf0;
LPC_IOCON->PIO2_9 = 0xf0;
LPC_IOCON->PIO2_10 = 0xf0;
LPC_IOCON->PIO2_11 = 0xf0;

//Initialisation of IO port 2
//Set the lowest eight bits of GPIO2 as output
LPC_GPIO2 -> DIR = 0xff;

4
//Set the lowest eight bits to logic HIGH which turn off LED0 - LED7
LPC_GPIO2 -> DATA = 0xff;

//Initialisation of 16-bit counter/timer 0


//Enables clock for CT16B0 16-bit counter/timer 0
LPC_SYSCON -> SYSAHBCLKCTRL |= (1 << 7);

//Pre-scaler settings - system clock is 72MHz


//Pre-scaled timer rate = 10000 counter/sec
LPC_TMR16B0 -> PR = 7199;

//Configure Match Control Register (MCR) to enable Match Register (MR) 3


LPC_TMR16B0 -> MCR = 0x600;

//Load Match REgister MR3 for achieving the counter rate = 2 counter / sec
LPC_TMR16B0 -> MR3 = 4999;

//Enable Interrupt for CT16B0 Timer


NVIC_EnableIRQ(TIMER_16_0_IRQn);

//Enable timer for counting by setting value 1 to TCR


LPC_TMR16B0->TCR = 0x01;

//Read the switches continuously


//To let the LEDs be turned on particularly period while (1)
{
key=((LPC_GPIO2 -> DATA) >> 8) ^ 0x0f;
}
return 0 ;
}

5
Appendix D (Broken line following)

adc.c

#include "common.h"
/*
unsigned char ADC_BUSY;
void ADC_IRQHandler()
{
unsigned int temp;
temp=LPC_ADC->STAT;
ADC_BUSY=0;
}
*/

void adc_init(void)
{
//Power-down configuration register
LPC_SYSCON->PDRUNCFG &= ~(1<<4);
//IOCON registers
//LPC_IOCON->ARM_SWDIO_PIO1_3 = 0x42; /* SHOULD NOT USE THIS PIN UNLESS SERIAL
WIRE DEBUG IS NOT NECESSARY */
LPC_IOCON->PIO1_4 = 0x41;
LPC_IOCON->PIO1_11 = 0x41;
//Clock
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<13);
//A/D Control Register
LPC_ADC->CR = 0X0000f00;
//A/D Interrupt Enable Register
LPC_ADC->INTEN = 0x00;
}

unsigned int adc10b(unsigned char ch)


{
unsigned int temp;
LPC_ADC->CR&=~(0x000000ff);
LPC_ADC->CR|=((1<<24)|(1<<ch));
//temp=0x80000000;
temp=0;
while ((temp&0x80000000)==0)
{
switch (ch)
{
case 5: temp=LPC_ADC->DR5;break;
case 7: temp=LPC_ADC->DR7;break;
}
}
LPC_ADC->CR&=~(1<<24);
if (temp&0x40000000)
temp = 1024;
else
temp = ((temp>>6)&0x3ff);
return temp;
}
6
void report_adc()
{
outdec16b(adc10b(5));
outdec16b(adc10b(7));
}

adc.h

#ifndef ADC_H_
#define ADC_H_

void adc_init(void);
unsigned int adc10b(unsigned char ch);

void report_adc(void);

#endif /* ADC_H_ */

cart_crtl.c

#include "common.h"

int l_duty, r_duty;


int error_index, pidout;
int error[10], sum_error, diff_error;

void motor1_power(int value)


{
if (value>=0)
{
if (value>100)
value=100;
LPC_TMR16B0->MR0 = value;
LPC_TMR16B0->MR1 = 0;
l_duty=value;
}
else
{
value*=-1;
if (value>100)
value=100;
LPC_TMR16B0->MR0 = 0;
LPC_TMR16B0->MR1 = value;
l_duty=-value;
}
}

void motor2_power(int value)


{
7
if (value>=0)
{
if (value>100)
value=100;
LPC_TMR16B1->MR0 = value;
LPC_TMR16B1->MR1 = 0;
r_duty=value;
}
else
{
value*=-1;
if (value>100)
value=100;
LPC_TMR16B1->MR0 = 0;
LPC_TMR16B1->MR1 = value;
r_duty=-value;
}
}

void set_base_duty(int value)


{
if ((value>0)&&(value<100))
base_duty=value;
}

void mod_base_duty(int value)


{
int temp;
temp=base_duty;
switch (value)
{
case -3: temp-=20;break;
case -2: temp-=10;break;
case -1: temp-=5;break;
case 1: temp+=5;break;
case 2: temp+=10;break;
case 3: temp+=20;break;
}
//if ((temp>0)&&(temp<100))
if (temp<0) temp=0;
if (temp>100) temp=100;
base_duty=temp;
}

void move_fw()
{
l_duty=base_duty;
r_duty=base_duty;
motor1_power(l_duty);
motor2_power(r_duty);
}

void move_bw()
{

8
l_duty=-base_duty;
r_duty=-base_duty;
motor1_power(l_duty);
motor2_power(r_duty);
}

void move_cw()
{
l_duty=base_duty;
r_duty=-base_duty;
motor1_power(l_duty);
motor2_power(r_duty);
}

void move_ccw()
{
l_duty=-base_duty;
r_duty=base_duty;
motor1_power(l_duty);
motor2_power(r_duty);
}

void stop()
{
l_duty=0;
r_duty=0;
motor1_power(l_duty);
motor2_power(r_duty);
}

void pid_line_tracking(int setpt)


{
/*
* centroid > 0 if line at left
* centroid = 0 if line at middle
* centroid < 0 if line at right
*/
int kp,ki,kd;

kp=500;
ki=20;
kd=20;

sum_error-=error[error_index];
error[error_index]=setpt-centroid;
sum_error+=error[error_index];
diff_error=centroid-pre_centroid;

pidout= kp*error[error_index] + ki*sum_error + kd* diff_error;

l_duty=base_duty+pidout;
r_duty=base_duty-pidout;
motor1_power(l_duty);
motor2_power(r_duty);

9
if (error_index<9)
error_index++;
else
error_index=0;
}

void report_cart_status()
{
outdec8b(mode);
outdec8b(base_duty);
outdec8b(l_duty);
outdec8b(r_duty);
outdec16b(pidout);
}

cart_ctrl.h

#ifndef CART_CTRL_H_
#define CART_CTRL_H_

unsigned char mode;


int base_duty;

void motor1_power(int value);


void motor2_power(int value);

void set_base_duty(int value);


void mod_base_duty(int value);

void move_fw();
void move_bw();
void move_cw();
void move_ccw();
void stop();

void pid_line_tracking(int setpt);

void report_cart_status(void);
#endif /* CART_CTRL_H_ */

common.c

#include "common.h"

void io_init(void)
{
//Temp
//LPC_IOCON->JTAG_TDI_PIO0_11=0xf1;
10
//LPC_SYSCON->SYSAHBCLKCTRL |= 0x10040;
LPC_SYSCON->SYSAHBCLKCTRL |= ((1<<6)|(1<<16));
LPC_GPIO0->DIR = 0xb82;
LPC_GPIO1->DIR = 0x606;
LPC_GPIO2->DIR = 0;
LPC_GPIO3->DIR = 0;

LPC_GPIO0->MASKED_ACCESS[0xfff] = 0x802;
LPC_GPIO1->MASKED_ACCESS[0xfff] = 0;
LPC_GPIO2->MASKED_ACCESS[0xfff] = 0;
LPC_GPIO3->MASKED_ACCESS[0xfff] = 0;
}

void set_test_led(unsigned char value)


{
if (value)
LPC_GPIO0->MASKED_ACCESS[0xfff] |= 0x80;
//LPC_GPIO0->MASKED_ACCESS[0xfff] = 0x08;
else
LPC_GPIO0->MASKED_ACCESS[0xfff] &= ~(0x80);
//LPC_GPIO0->MASKED_ACCESS[0xfff] = 0x00;
}

void toggle_test_led(void)
{
LPC_GPIO0->MASKED_ACCESS[0xfff] ^= 0x80;
}

void report()
{
if (report_en)
{
//report_adc();
report_sensors();
spout('|');
report_keys();
spout('|');
report_cart_status();
spout('\r');
spout('\n');
}
}

common.h

#ifndef COMMON_H_
#define COMMON_H_

#include "LPC13xx.h"
#include "timers.h"
#include "uart.h"
11
#include "adc.h"
#include "cart_ctrl.h"
#include "sensors.h"
#include "keys.h"

unsigned char report_en, report_flag;

void io_init(void);
void set_test_led(unsigned char value);
void toggle_test_led(void);
void report(void);

#endif /* COMMON_H_ */

cr_startup_lpc13
//*****************************************************************************
// +--+
// | ++----+
// +-++ |
// | |
// +-+--+ |
// | +--+--+
// +----+ Copyright (c) 2009-11 Code Red Technologies Ltd.
//
// Microcontroller Startup code for use with Red Suite
//
// Version : 110427
//
// Software License Agreement
//
// The software is owned by Code Red Technologies and/or its suppliers, and is
// protected under applicable copyright laws. All rights are reserved. Any
// use in violation of the foregoing restrictions may subject the user to criminal
// sanctions under applicable laws, as well as to civil liability for the breach
// of the terms and conditions of this license.
//
// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// USE OF THIS SOFTWARE FOR COMMERCIAL DEVELOPMENT AND/OR EDUCATION IS SUBJECT
// TO A CURRENT END USER LICENSE AGREEMENT (COMMERCIAL OR EDUCATIONAL) WITH
// CODE RED TECHNOLOGIES LTD.
//
//*****************************************************************************
#if defined (__cplusplus)
#ifdef __REDLIB__
#error Redlib does not support C++
#else
//*****************************************************************************
//
// The entry point for the C++ library startup
//
//*****************************************************************************
extern "C" {

12
extern void __libc_init_array(void);
}
#endif
#endif

#define WEAK __attribute__ ((weak))


#define ALIAS(f) __attribute__ ((weak, alias (#f)))

// Code Red - if CMSIS is being used, then SystemInit() routine


// will be called by startup code rather than in application's main()
#if defined (__USE_CMSIS)
#include "LPC13xx.h"
#endif

//*****************************************************************************
#if defined (__cplusplus)
extern "C" {
#endif

//*****************************************************************************
//
// Forward declaration of the default handlers. These are aliased.
// When the application defines a handler (with the same name), this will
// automatically take precedence over these weak definitions
//
//*****************************************************************************
void ResetISR(void);
WEAK void NMI_Handler(void);
WEAK void HardFault_Handler(void);
WEAK void MemManage_Handler(void);
WEAK void BusFault_Handler(void);
WEAK void UsageFault_Handler(void);
WEAK void SVCall_Handler(void);
WEAK void DebugMon_Handler(void);
WEAK void PendSV_Handler(void);
WEAK void SysTick_Handler(void);
WEAK void IntDefaultHandler(void);
//*****************************************************************************
//
// Forward declaration of the specific IRQ handlers. These are aliased
// to the IntDefaultHandler, which is a 'forever' loop. When the application
// defines a handler (with the same name), this will automatically take
// precedence over these weak definitions
//
//*****************************************************************************

void I2C_IRQHandler(void) ALIAS(IntDefaultHandler);


void TIMER16_0_IRQHandler(void) ALIAS(IntDefaultHandler);
void TIMER16_1_IRQHandler(void) ALIAS(IntDefaultHandler);
void TIMER32_0_IRQHandler(void) ALIAS(IntDefaultHandler);
void TIMER32_1_IRQHandler(void) ALIAS(IntDefaultHandler);
void SSP_IRQHandler(void) ALIAS(IntDefaultHandler);
void UART_IRQHandler(void) ALIAS(IntDefaultHandler);
void USB_IRQHandler(void) ALIAS(IntDefaultHandler);

13
void USB_FIQHandler(void) ALIAS(IntDefaultHandler);
void ADC_IRQHandler(void) ALIAS(IntDefaultHandler);
void WDT_IRQHandler(void) ALIAS(IntDefaultHandler);
void BOD_IRQHandler(void) ALIAS(IntDefaultHandler);
void FMC_IRQHandler(void) ALIAS(IntDefaultHandler);
void PIOINT3_IRQHandler(void) ALIAS(IntDefaultHandler);
void PIOINT2_IRQHandler(void) ALIAS(IntDefaultHandler);
void PIOINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
void PIOINT0_IRQHandler(void) ALIAS(IntDefaultHandler);
void WAKEUP_IRQHandler(void) ALIAS(IntDefaultHandler);
void SSP1_IRQHandler(void) ALIAS(IntDefaultHandler);

//*****************************************************************************
//
// The entry point for the application.
// __main() is the entry point for Redlib based applications
// main() is the entry point for Newlib based applications
//
//*****************************************************************************
#if defined (__REDLIB__)
extern void __main(void);
#endif
extern int main(void);
//*****************************************************************************
//
// External declaration for the pointer to the stack top from the Linker Script
//
//*****************************************************************************
extern void _vStackTop(void);

//*****************************************************************************
#if defined (__cplusplus)
} // extern "C"
#endif
//*****************************************************************************
//
// The vector table. Note that the proper constructs must be placed on this to
// ensure that it ends up at physical address 0x0000.0000.
//
//*****************************************************************************
extern void (* const g_pfnVectors[])(void);
__attribute__ ((section(".isr_vector")))
void (* const g_pfnVectors[])(void) = {
// Core Level - CM3
&_vStackTop, // The initial stack pointer
ResetISR, // The reset handler
NMI_Handler, // The NMI handler
HardFault_Handler, // The hard fault handler
MemManage_Handler, // The MPU fault handler
BusFault_Handler, // The bus fault handler
UsageFault_Handler, // The usage fault handler
0, // Reserved
0, // Reserved
0, // Reserved

14
0, // Reserved
SVCall_Handler, // SVCall handler
DebugMon_Handler, // Debug monitor handler
0, // Reserved
PendSV_Handler, // The PendSV handler
SysTick_Handler, // The SysTick handler

// Wakeup sources (40 ea.) for the I/O pins:


// PIO0 (0:11)
// PIO1 (0:11)
// PIO2 (0:11)
// PIO3 (0:3)
WAKEUP_IRQHandler, // PIO0_0 Wakeup
WAKEUP_IRQHandler, // PIO0_1 Wakeup
WAKEUP_IRQHandler, // PIO0_2 Wakeup
WAKEUP_IRQHandler, // PIO0_3 Wakeup
WAKEUP_IRQHandler, // PIO0_4 Wakeup
WAKEUP_IRQHandler, // PIO0_5 Wakeup
WAKEUP_IRQHandler, // PIO0_6 Wakeup
WAKEUP_IRQHandler, // PIO0_7 Wakeup
WAKEUP_IRQHandler, // PIO0_8 Wakeup
WAKEUP_IRQHandler, // PIO0_9 Wakeup
WAKEUP_IRQHandler, // PIO0_10 Wakeup
WAKEUP_IRQHandler, // PIO0_11 Wakeup

WAKEUP_IRQHandler, // PIO1_0 Wakeup


WAKEUP_IRQHandler, // PIO1_1 Wakeup
WAKEUP_IRQHandler, // PIO1_2 Wakeup
WAKEUP_IRQHandler, // PIO1_3 Wakeup
WAKEUP_IRQHandler, // PIO1_4 Wakeup
WAKEUP_IRQHandler, // PIO1_5 Wakeup
WAKEUP_IRQHandler, // PIO1_6 Wakeup
WAKEUP_IRQHandler, // PIO1_7 Wakeup
WAKEUP_IRQHandler, // PIO1_8 Wakeup
WAKEUP_IRQHandler, // PIO1_9 Wakeup
WAKEUP_IRQHandler, // PIO1_10 Wakeup
WAKEUP_IRQHandler, // PIO1_11 Wakeup

WAKEUP_IRQHandler, // PIO2_0 Wakeup


WAKEUP_IRQHandler, // PIO2_1 Wakeup
WAKEUP_IRQHandler, // PIO2_2 Wakeup
WAKEUP_IRQHandler, // PIO2_3 Wakeup
WAKEUP_IRQHandler, // PIO2_4 Wakeup
WAKEUP_IRQHandler, // PIO2_5 Wakeup
WAKEUP_IRQHandler, // PIO2_6 Wakeup
WAKEUP_IRQHandler, // PIO2_7 Wakeup
WAKEUP_IRQHandler, // PIO2_8 Wakeup
WAKEUP_IRQHandler, // PIO2_9 Wakeup
WAKEUP_IRQHandler, // PIO2_10 Wakeup
WAKEUP_IRQHandler, // PIO2_11 Wakeup

WAKEUP_IRQHandler, // PIO3_0 Wakeup


WAKEUP_IRQHandler, // PIO3_1 Wakeup

15
WAKEUP_IRQHandler, // PIO3_2 Wakeup
WAKEUP_IRQHandler, // PIO3_3 Wakeup

I2C_IRQHandler, // I2C0
TIMER16_0_IRQHandler, // CT16B0 (16-bit Timer 0)
TIMER16_1_IRQHandler, // CT16B1 (16-bit Timer 1)
TIMER32_0_IRQHandler, // CT32B0 (32-bit Timer 0)
TIMER32_1_IRQHandler, // CT32B1 (32-bit Timer 1)
SSP_IRQHandler, // SSP0
UART_IRQHandler, // UART0

USB_IRQHandler, // USB IRQ


USB_FIQHandler, // USB FIQ

ADC_IRQHandler, // ADC (A/D Converter)


WDT_IRQHandler, // WDT (Watchdog Timer)
BOD_IRQHandler, // BOD (Brownout Detect)
FMC_IRQHandler, // Flash (IP2111 Flash Memory Controller)
PIOINT3_IRQHandler, // PIO INT3
PIOINT2_IRQHandler, // PIO INT2
PIOINT1_IRQHandler, // PIO INT1
PIOINT0_IRQHandler, // PIO INT0
SSP1_IRQHandler, // SSP1

};

//*****************************************************************************
// Functions to carry out the initialization of RW and BSS data sections. These
// are written as separate functions rather than being inlined within the
// ResetISR() function in order to cope with MCUs with multiple banks of
// memory.
//*****************************************************************************
__attribute__ ((section(".after_vectors")))
void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
unsigned int *pulDest = (unsigned int*) start;
unsigned int *pulSrc = (unsigned int*) romstart;
unsigned int loop;
for (loop = 0; loop < len; loop = loop + 4)
*pulDest++ = *pulSrc++;
}

__attribute__ ((section(".after_vectors")))
void bss_init(unsigned int start, unsigned int len) {
unsigned int *pulDest = (unsigned int*) start;
unsigned int loop;
for (loop = 0; loop < len; loop = loop + 4)
*pulDest++ = 0;
}

#ifndef USE_OLD_STYLE_DATA_BSS_INIT
//*****************************************************************************
// The following symbols are constructs generated by the linker, indicating
// the location of various points in the "Global Section Table". This table is
// created by the linker via the Code Red managed linker script mechanism. It

16
// contains the load address, execution address and length of each RW data
// section and the execution and length of each BSS (zero initialized) section.
//*****************************************************************************
extern unsigned int __data_section_table;
extern unsigned int __data_section_table_end;
extern unsigned int __bss_section_table;
extern unsigned int __bss_section_table_end;
#else
//*****************************************************************************
// The following symbols are constructs generated by the linker, indicating
// the load address, execution address and length of the RW data section and
// the execution and length of the BSS (zero initialized) section.
// Note that these symbols are not normally used by the managed linker script
// mechanism in Red Suite/LPCXpresso 3.6 (Windows) and LPCXpresso 3.8 (Linux).
// They are provide here simply so this startup code can be used with earlier
// versions of Red Suite which do not support the more advanced managed linker
// script mechanism introduced in the above version. To enable their use,
// define "USE_OLD_STYLE_DATA_BSS_INIT".
//*****************************************************************************
extern unsigned int _etext;
extern unsigned int _data;
extern unsigned int _edata;
extern unsigned int _bss;
extern unsigned int _ebss;
#endif

//*****************************************************************************
// Reset entry point for your code.
// Sets up a simple runtime environment and initializes the C/C++
// library.
//*****************************************************************************
__attribute__ ((section(".after_vectors")))
void
ResetISR(void) {

#ifndef USE_OLD_STYLE_DATA_BSS_INIT
//
// Copy the data sections from flash to SRAM.
//
unsigned int LoadAddr, ExeAddr, SectionLen;
unsigned int *SectionTableAddr;

// Load base address of Global Section Table


SectionTableAddr = &__data_section_table;

// Copy the data sections from flash to SRAM.


while (SectionTableAddr < &__data_section_table_end) {
LoadAddr = *SectionTableAddr++;
ExeAddr = *SectionTableAddr++;
SectionLen = *SectionTableAddr++;
data_init(LoadAddr, ExeAddr, SectionLen);
}
// At this point, SectionTableAddr = &__bss_section_table;
// Zero fill the bss segment

17
while (SectionTableAddr < &__bss_section_table_end) {
ExeAddr = *SectionTableAddr++;
SectionLen = *SectionTableAddr++;
bss_init(ExeAddr, SectionLen);
}
#else
// Use Old Style Data and BSS section initialization.
// This will only initialize a single RAM bank.
unsigned int * LoadAddr, *ExeAddr, *EndAddr, SectionLen;

// Copy the data segment from flash to SRAM.


LoadAddr = &_etext;
ExeAddr = &_data;
EndAddr = &_edata;
SectionLen = (void*)EndAddr - (void*)ExeAddr;
data_init((unsigned int)LoadAddr, (unsigned int)ExeAddr, SectionLen);
// Zero fill the bss segment
ExeAddr = &_bss;
EndAddr = &_ebss;
SectionLen = (void*)EndAddr - (void*)ExeAddr;
bss_init ((unsigned int)ExeAddr, SectionLen);
#endif

#ifdef __USE_CMSIS
SystemInit();
#endif

#if defined (__cplusplus)


//
// Call C++ library initialisation
//
__libc_init_array();
#endif

#if defined (__REDLIB__)


// Call the Redlib library, which in turn calls main()
__main() ;
#else
main();
#endif
//
// main() shouldn't return, but if it does, we'll just enter an
infinite loop
//
while (1) {
;
}
}

//*****************************************************************************
// Default exception handlers. Override the ones here by defining your own
// handler routines in your application code.
//*****************************************************************************
__attribute__ ((section(".after_vectors")))

18
void NMI_Handler(void) {
while (1) {
}
}
__attribute__ ((section(".after_vectors")))
void HardFault_Handler(void) {
while (1) {
}
}
__attribute__ ((section(".after_vectors")))
void MemManage_Handler(void) {
while (1) {
}
}
__attribute__ ((section(".after_vectors")))
void BusFault_Handler(void) {
while (1) {
}
}
__attribute__ ((section(".after_vectors")))
void UsageFault_Handler(void) {
while (1) {
}
}
__attribute__ ((section(".after_vectors")))
void SVCall_Handler(void) {
while (1) {
}
}
__attribute__ ((section(".after_vectors")))
void DebugMon_Handler(void) {
while (1) {
}
}
__attribute__ ((section(".after_vectors")))
void PendSV_Handler(void) {
while (1) {
}
}
__attribute__ ((section(".after_vectors")))
void SysTick_Handler(void) {
while (1) {
}
}

//*****************************************************************************
//
// Processor ends up here if an unexpected interrupt occurs or a handler
// is not present in the application code.
//
//*****************************************************************************
__attribute__ ((section(".after_vectors")))
void IntDefaultHandler(void) {
//

19
// Go into an infinite loop.
//
while (1) {
}
}

keys.c

#include "common.h"

unsigned char keys, filtered_keys, pre_filtered_keys;


unsigned int key_cnt[8];

void keys_init(void)
{
LPC_IOCON->PIO2_8 = 0x70;
LPC_IOCON->PIO2_9 = 0x70;
LPC_IOCON->PIO2_10 = 0x70;
LPC_IOCON->PIO2_11 = 0x70;
LPC_IOCON->PIO3_0 = 0x70;
LPC_IOCON->PIO3_1 = 0x70;
LPC_IOCON->PIO3_2 = 0x70;
LPC_IOCON->PIO3_3 = 0x70;
}

void keys_filter(void)
{
unsigned char i;

keys=((LPC_GPIO2->MASKED_ACCESS[0xf00])>>8);
keys|=((LPC_GPIO3->MASKED_ACCESS[0x0f])<<4);
pre_filtered_keys=filtered_keys;
for (i=0;i<8;i++)
{
if ((keys&(1<<i))==0)
{
if (key_cnt[i]<100)
key_cnt[i]++;
else
filtered_keys&=~(1<<i);
}
else
{
key_cnt[i]=0;
filtered_keys|=(1<<i);
}
}
keys_handler();
}

void keys_handler(void)
{
20
if ((pre_filtered_keys!=filtered_keys)&&(pre_filtered_keys==0xff))
{
switch (filtered_keys)
{
case 0xfe : mode=1;break;
case 0xfd : mode=2;break;
case 0xfb : mode=3;break;
case 0xf7 : mode=4;break;
case 0xef : mode=5;break;
case 0xdf : mode=6;break;
case 0xbf : mod_base_duty(2);break;
case 0x7f : mod_base_duty(-2);break;
}
}
}

void report_keys(void)
{
out_byte_in_bits(filtered_keys);
}

keys.h

#ifndef KEYS_H_
#define KEYS_H_

void keys_init(void);
void keys_filter(void);
void keys_handler(void);

void report_keys(void);

#endif /* KEYS_H_ */

main.c

#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif
*/

#include "common.h"
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically


// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

21
#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif

#include <cr_section_macros.h>
#define PERIOD 200
#define P0_FW 0xa02
#define P1_FW 0x400
#define P0_BW 0x902
#define P1_BW 0x200
#define P0_STOP 0x802
#define P1_STOP 0x0
#define P0_RIGHT 0xa02
#define P1_RIGHT 0x200
#define P0_LEFT 0x902
#define P1_LEFT 0x400

unsigned char sensors,timertick;


unsigned char pulse=190;

void TIMER16_0_IRQHandler(void)
{
unsigned char temp;
temp = (LPC_TMR16B1->IR)&0x1f;
if (temp&0x01) INT_CT16B1_MR0();
if (temp&0x02) INT_CT16B1_MR1();
if (temp&0x04) INT_CT16B1_MR2();
if (temp&0x08) INT_CT16B1_MR3();
if (temp&0x10) INT_CT16B1_CR0();
//Check for 'Match Register 3' Interrupt flag in Interrupt Register (IR)
// see Table 255 Interrupt Register in page 272 in UM10375
if ((LPC_TMR16B0->IR & 0x08) == 0x08)
{
//The interrupt is generated by Match Register 3.
//Writing a logic one to the bit 3 in IR will reset the interrupt for
Match Register 3.
LPC_TMR16B0->IR=0x08;

// Set the period of the PWM


if (timertick < PERIOD)
timertick++;
else
timertick=0;
//The motors will move during the On duration and
// stop during the Off duration in PWM
if (timertick < pulse)
{
// When SEN4 (LED3) detected a black line, it move forward.
//Otherwise, it stops

if (((sensors&0b00001111)!=0)&&((sensors&0b11110000)==0)){
LPC_GPIO0->DATA = P0_RIGHT;
LPC_GPIO1->DATA = P1_RIGHT;
}else if (((sensors&0b10000001)==0)&&((sensors&0b01111110)!=0)){

22
LPC_GPIO0->DATA = P0_FW;
LPC_GPIO1->DATA = P1_FW;
} else if (((sensors&0b11110000)!=0)&&((sensors&0b00001111)==0)){
LPC_GPIO0->DATA = P0_LEFT;
LPC_GPIO1->DATA = P1_LEFT;
}else if
(((sensors&0b11111000)==0b11111000)||((sensors&0b11111100)==0b11111100)||((sensors&
0b11111110)==0b11111110)){
LPC_GPIO0->DATA = P0_LEFT;
LPC_GPIO1->DATA = P1_LEFT;
} else if
(((sensors&0b00011111)==0b00011111)||((sensors&0b00111111)==0b00111111)||((sensors&
0b01111111)==0b01111111)){
LPC_GPIO0->DATA = P0_RIGHT;
LPC_GPIO1->DATA = P1_RIGHT;
} else if((sensors&0b11111111)==0) {
if (timertick<60){
LPC_GPIO0->DATA = P0_FW;
LPC_GPIO1->DATA = P1_FW;
}else if(timertick<4000){
LPC_GPIO0->DATA = P0_RIGHT;
LPC_GPIO1->DATA = P1_STOP;}
}
} else{
LPC_GPIO0->DATA = P0_STOP;
LPC_GPIO1->DATA = P1_STOP;}
}
}

int main()
{

LPC_GPIO2->DIR = 0x0;
LPC_GPIO0->DIR = 0xb02;
LPC_GPIO1->DIR = 0x600;

LPC_IOCON->PIO2_0 = 0xf0;

LPC_IOCON->PIO2_1 = 0xf0;
LPC_IOCON->PIO2_2 = 0xf0;
LPC_IOCON->PIO2_3 = 0xf0;
LPC_IOCON->PIO2_4 = 0xf0;
LPC_IOCON->PIO2_5 = 0xf0;
LPC_IOCON->PIO2_6 = 0xf0;
LPC_IOCON->PIO2_7 = 0xf0;
LPC_IOCON->PIO2_8 = 0xf0;

LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
LPC_TMR16B0->PR = 7199;
LPC_TMR16B0->MCR = 0x600;
LPC_TMR16B0->MR3 = 9;
NVIC_EnableIRQ(TIMER_16_0_IRQn);

23
LPC_TMR16B0->TCR = 0x01;

while(1)
{
sensors = (LPC_GPIO2->DATA) & 255;

if (((LPC_GPIO2->DATA)& 0x100) == 0)
{
pulse=0;
}

}
return 0;
}

sensors.c

#include "common.h"

#define THRESHOLD 200


#define NUM_SENSORS 3

unsigned char sensors,filtered_sensors;


unsigned int sensors_cnt[NUM_SENSORS];
unsigned int blind_cnt;

void sensor_init(void)
{
LPC_IOCON->PIO2_0 = 0x70;
LPC_IOCON->PIO2_1 = 0x70;
LPC_IOCON->PIO2_2 = 0x70;
LPC_IOCON->PIO2_3 = 0x70;
LPC_IOCON->PIO2_4 = 0x70;
LPC_IOCON->PIO2_5 = 0x70;
LPC_IOCON->PIO2_6 = 0x70;
LPC_IOCON->PIO2_7 = 0x70;
}

void sensor_filter(void)
{
int i, temp1, temp2;

sensors=LPC_GPIO2->MASKED_ACCESS[0x07];

if (BLACKLINE_MODE)
sensors^=0x07;

temp1=0;
temp2=0;
for (i=0;i<NUM_SENSORS;i++)
{
if ((sensors&(1<<i))==0)
24
{
if (sensors_cnt[i]<THRESHOLD)
sensors_cnt[i]++;
else
{
temp1++;
filtered_sensors|=(1<<i);
temp2+=(i+1);
}
}
else
{
if (sensors_cnt[i]>0)
sensors_cnt[i]--;
else
filtered_sensors&=~(1<<i);
}
}

pre_centroid=centroid;
activesensors=temp1;
if (temp1)
{
blind_cnt=0;
blind_flag=0;
centroid=(((temp2<<1)/temp1)-4);
}
else
{
if (blind_cnt<1000)
blind_cnt++;
else
{
centroid=0;
blind_flag=1;
}
}
}

void report_sensors()
{
out_byte_in_bits(filtered_sensors);
outdec8b(activesensors);
outdec8b(centroid);
}

sensors.h

#ifndef SENSORS_H_
#define SENSORS_H_

25
#define BLACKLINE_MODE 1

int pre_centroid, centroid, activesensors;


unsigned char blind_flag;

void sensor_init(void);
void sensor_filter(void);
void report_sensors(void);

#endif /* SENSORS_H_ */

timers.c

#include "common.h"
unsigned int timertick1;

void TIMER16_0_IRQHandler(void)
{
unsigned char temp;
temp = (LPC_TMR16B0->IR)&0x1f;
if (temp&0x01) INT_CT16B0_MR0();
if (temp&0x02) INT_CT16B0_MR1();
if (temp&0x04) INT_CT16B0_MR2();
if (temp&0x08) INT_CT16B0_MR3();
if (temp&0x10) INT_CT16B0_CR0();
}

void TIMER16_1_IRQHandler(void)
{
unsigned char temp;
temp = (LPC_TMR16B1->IR)&0x1f;
if (temp&0x01) INT_CT16B1_MR0();
if (temp&0x02) INT_CT16B1_MR1();
if (temp&0x04) INT_CT16B1_MR2();
if (temp&0x08) INT_CT16B1_MR3();
if (temp&0x10) INT_CT16B1_CR0();
}

void TIMER32_0_IRQHandler(void)
{
unsigned char temp;
temp = (LPC_TMR32B0->IR)&0x1f;
if (temp&0x01) INT_CT32B0_MR0();
if (temp&0x02) INT_CT32B0_MR1();
if (temp&0x04) INT_CT32B0_MR2();
if (temp&0x08) INT_CT32B0_MR3();
if (temp&0x10) INT_CT32B0_CR0();
}

void TIMER32_1_IRQHandler(void)
{
26
unsigned char temp;
temp = (LPC_TMR32B0->IR)&0x1f;
if (temp&0x01) INT_CT32B1_MR0();
if (temp&0x02) INT_CT32B1_MR1();
if (temp&0x04) INT_CT32B1_MR2();
if (temp&0x08) INT_CT32B1_MR3();
if (temp&0x10) INT_CT32B1_CR0();
}

void timer_init(void)
{
if (EN_CT16B0) CT16B0_init();
if (EN_CT16B1) CT16B1_init();
if (EN_CT32B0) CT32B0_init();
if (EN_CT32B1) CT32B1_init();
}

void CT16B0_init(void)
{
//IOCON registers
LPC_IOCON->PIO0_2 = 0x72;
LPC_IOCON->PIO0_8 = 0x72;
LPC_IOCON->PIO0_9 = 0x72;
//Clock
//LPC_SYSCON->SYSAHBCLKCTRL |= 0x80;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
//Prescalar settings
LPC_TMR16B0->PR = 7199;
//Match control settings
LPC_TMR16B0->MCR = 0x600;
//Match Register 3
LPC_TMR16B0->MR3 = 100;
//Capture control settings
LPC_TMR16B0->CCR = 0x07;
//External Match Registers
LPC_TMR16B0->EMR = 0x03;
//Count Control Register
LPC_TMR16B0->CTCR = 0;
//PWM Control Register
LPC_TMR16B0->PWMC = 0x03;
//Enable Interrupt
NVIC_EnableIRQ(TIMER_16_0_IRQn);
//Enable timer
LPC_TMR16B0->TCR = 0x01;
}

void CT16B1_init(void)
{
//IOCON registers
LPC_IOCON->PIO1_8 = 0x71;
LPC_IOCON->PIO1_9 = 0x71;
LPC_IOCON->PIO1_10 = 0xf2;
//Clock
//LPC_SYSCON->SYSAHBCLKCTRL |= 0x100;

27
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
//Prescalar settings
LPC_TMR16B1->PR = 7199;
//Match control settings
LPC_TMR16B1->MCR = 0x400;
//Match Register 3
LPC_TMR16B1->MR3 = 99;
//Capture control settings
LPC_TMR16B1->CCR = 0x07;
//External Match Registers
LPC_TMR16B1->EMR = 0x03;
//Count Control Register
LPC_TMR16B1->CTCR = 0;
//PWM Control Register
LPC_TMR16B1->PWMC = 0x03;
//Enable Interrupt
NVIC_EnableIRQ(TIMER_16_1_IRQn);
//Enable timer
LPC_TMR16B1->TCR = 0x01;
}

void CT32B0_init(void)
{
//IOCON registers
LPC_IOCON->PIO0_1 = 0x72;
LPC_IOCON->JTAG_TDI_PIO0_11 = 0xf3;
LPC_IOCON->PIO1_5 = 0x72;
//Clock
//LPC_SYSCON->SYSAHBCLKCTRL |= 0x200;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9);
//Prescalar settings
LPC_TMR32B0->PR = 0;
//Match control settings
LPC_TMR32B0->MCR = 0x400;
//Match Register 3
LPC_TMR32B0->MR3 = 0xffff;
//Capture control settings
LPC_TMR32B0->CCR = 0x07;
//External Match Registers
LPC_TMR32B0->EMR = 0x03;
//Count Control Register
LPC_TMR32B0->CTCR = 0;
//PWM Control Register
LPC_TMR32B0->PWMC = 0x03;
//Enable Interrupt
NVIC_EnableIRQ(TIMER_32_0_IRQn);
//Enable timer
LPC_TMR32B0->TCR = 0x01;
}

void CT32B1_init(void)
{
//IOCON registers
LPC_IOCON->JTAG_TMS_PIO1_0 = 0xf3;

28
LPC_IOCON->JTAG_TDO_PIO1_1 = 0xf3;
LPC_IOCON->JTAG_nTRST_PIO1_2 = 0xf3;
//Clock
//LPC_SYSCON->SYSAHBCLKCTRL |= 0x400;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10);
//Prescalar settings
LPC_TMR32B1->PR = 0;
//Match control settings
LPC_TMR32B1->MCR = 0x400;
//Match Register 3
LPC_TMR32B1->MR3 = 0xffff;
//Capture control settings
LPC_TMR32B1->CCR = 0x07;
//External Match Registers
LPC_TMR32B1->EMR = 0x03;
//Count Control Register
LPC_TMR32B1->CTCR = 0;
//PWM Control Register
LPC_TMR32B1->PWMC = 0x03;
//Enable Interrupt
NVIC_EnableIRQ(TIMER_32_1_IRQn);
//Enable timer
LPC_TMR32B1->TCR = 0x01;
}

void INT_CT16B0_MR0(void)
{
LPC_TMR16B0->IR=0x01;
}

void INT_CT16B0_MR1(void)
{
LPC_TMR16B0->IR=0x02;
}

void INT_CT16B0_MR2(void)
{
LPC_TMR16B0->IR=0x04;
}

void INT_CT16B0_MR3(void)
{
//toggle_test_led();
LPC_TMR16B0->IR=0x08;
if (timertick1<50)
timertick1++;
else
{
timertick1=0;
report_flag=1;
}

29
void INT_CT16B0_CR0(void)
{
LPC_TMR16B0->IR=0x10;
}

void INT_CT16B1_MR0(void)
{
LPC_TMR16B1->IR=0x01;
}

void INT_CT16B1_MR1(void)
{
LPC_TMR16B1->IR=0x02;
}

void INT_CT16B1_MR2(void)
{
LPC_TMR16B1->IR=0x04;
}

void INT_CT16B1_MR3(void)
{
LPC_TMR16B1->IR=0x08;
}

void INT_CT16B1_CR0(void)
{
LPC_TMR16B1->IR=0x10;
}

void INT_CT32B0_MR0(void)
{
LPC_TMR32B0->IR=0x01;
}

void INT_CT32B0_MR1(void)
{
LPC_TMR32B0->IR=0x02;
}

void INT_CT32B0_MR2(void)
{
LPC_TMR32B0->IR=0x04;
}

void INT_CT32B0_MR3(void)
{
LPC_TMR32B0->IR=0x08;
}

void INT_CT32B0_CR0(void)
{
LPC_TMR32B0->IR=0x10;

30
}

void INT_CT32B1_MR0(void)
{
LPC_TMR32B1->IR=0x01;
}

void INT_CT32B1_MR1(void)
{
LPC_TMR32B1->IR=0x02;
}

void INT_CT32B1_MR2(void)
{
LPC_TMR32B1->IR=0x04;
}

void INT_CT32B1_MR3(void)
{
LPC_TMR32B1->IR=0x08;
}

void INT_CT32B1_CR0(void)
{
LPC_TMR32B1->IR=0x10;
}

timers.h

#ifndef TIMERS_H_
#define TIMERS_H_

#define EN_CT16B0 1
#define EN_CT16B1 1
#define EN_CT32B0 0
#define EN_CT32B1 0

void timer_init(void);

void CT16B0_init(void);
void CT16B1_init(void);
void CT32B0_init(void);
void CT32B1_init(void);

void INT_CT16B0_MR0(void);
void INT_CT16B0_MR1(void);
void INT_CT16B0_MR2(void);
void INT_CT16B0_MR3(void);
void INT_CT16B0_CR0(void);

void INT_CT16B1_MR0(void);
31
void INT_CT16B1_MR1(void);
void INT_CT16B1_MR2(void);
void INT_CT16B1_MR3(void);
void INT_CT16B1_CR0(void);

void INT_CT32B0_MR0(void);
void INT_CT32B0_MR1(void);
void INT_CT32B0_MR2(void);
void INT_CT32B0_MR3(void);
void INT_CT32B0_CR0(void);

void INT_CT32B1_MR0(void);
void INT_CT32B1_MR1(void);
void INT_CT32B1_MR2(void);
void INT_CT32B1_MR3(void);
void INT_CT32B1_CR0(void);

#endif /* TIMERS_H_ */

uart.c

#include "common.h"
#define MAIN_CLK 72000000

unsigned char spin[20],spin_cnt;

void UART_IRQHandler(void)
{
//UART Interrupt Identification Register
while ((LPC_UART-> IIR&0x01)==0)
{
switch ((LPC_UART-> IIR>>1)&0x07)
{
//case 0x03: INT_UART_RLS();break;
case 0x02: INT_UART_RDA();break;
//case 0x06: INT_UART_CTI();break;
//case 0x01: INT_UART_THRE();break;
//case 0x00: break;
}
}
}

void uart_init(unsigned int baud)


{
unsigned int temp;
NVIC_DisableIRQ(UART_IRQn);
//IOCON registers
LPC_IOCON->PIO1_6 = 0x71;
LPC_IOCON->PIO1_7 = 0x71;

//Clock
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
32
//UART clock divider register
LPC_SYSCON->UARTCLKDIV = 1;

/*
******** Unsued Registers ********
//UART Modem Control Register
LPC_UART->MCR = 0;
//Scratch Pad Register
LPC_UART->SCR = 0;
//UART Auto-baud Control Register
LPC_UART->ACR = 0;
//UART RS485 Control register
LPC_UART->RS485CTRL = 0;
//UART RS-485 Address Match register
LPC_UART->ADRMATCH = 0;
//UART1 RS-485 Delay value register
LPC_UART->RS485DLY = 0;
*/

//UART Fractional Divider Register


LPC_UART->FDR = 0x10;

//UART Line Control Register


LPC_UART->LCR = 0x83;
temp = MAIN_CLK/(LPC_SYSCON->SYSAHBCLKDIV*(baud<<4));
//UART Divisor Latch LSB and MSB Registers
LPC_UART->DLL = (temp&0xff);
LPC_UART->DLM = (temp>>8);
//UART Line Control Register
LPC_UART->LCR = 0x03;
//UART FIFO Control Register
LPC_UART->FCR = 0x07;

temp = LPC_UART->LSR;
while (!(LPC_UART->LSR & 0x60));
while (LPC_UART->LSR & 0x01)
temp = LPC_UART->RBR;

//UART Transmit Enable Register


LPC_UART->TER = 0x80;

//Enable Interrupt
NVIC_EnableIRQ(UART_IRQn);
//UART Interrupt Enable Register
LPC_UART->IER = 0x01;
}

void INT_UART_RLS(void)
{
unsigned char temp;
temp = LPC_UART->LSR;
/*
if (temp & 0x9e)

33
{
temp=LPC_UART->RBR;
}
else
if (LSRValue & LSR_RDR)
INT_UART_RDA();
*/
}

void INT_UART_RDA()
{
spin[spin_cnt]=LPC_UART->RBR;
//spout(spin[spin_cnt]);
//toggle_test_led();
switch (spin[spin_cnt])
{
//case 'z' : set_test_led(0);spin_cnt=0;break;
//case 'x' : set_test_led(1);spin_cnt=0;break;
case ' ' : report_en^=0x01;spin_cnt=0;break;
case 'q' : mode=1;spin_cnt=0;break;
case 'w' : mode=2;spin_cnt=0;break;
case 's' : mode=3;spin_cnt=0;break;
case 'd' : mode=4;spin_cnt=0;break;
case 'a' : mode=5;spin_cnt=0;break;
case 'e' : mode=6;spin_cnt=0;break;
case 'z' : mod_base_duty(1);spin_cnt=0;break;
case 'x' : mod_base_duty(-1);spin_cnt=0;break;
}

if (spin_cnt<19)
spin_cnt++;
}

void INT_UART_CTI(void)
{
unsigned char temp;
temp = LPC_UART->RBR;
}

void INT_UART_THRE(void)
{

void spout(unsigned char value)


{
while (!(LPC_UART->LSR & 0x20));
LPC_UART->THR = value;
}

void outdec8b(int value)


{
spout(' ');
if (value<0)

34
{
value-=1;
value=~value;
spout('-');
}
else
spout(' ');
spout(((value%1000)/100)+48 );
spout(((value%100)/10)+48 );
spout((value%10)+48);
}

void outdec16b(int value)


{
spout(' ');
if (value<0)
{
value-=1;
value=~value;
spout('-');
}
else
spout(' ');
spout((value/10000)+48);
spout(((value%10000)/1000)+48 );
spout(((value%1000)/100)+48 );
spout(((value%100)/10)+48 );
spout((value%10)+48);
}

void out_byte_in_bits(unsigned char data)


{
int i;
spout(' ');
for (i=0;i<8;i++)
{
if (data&(1<<(7-i)))
spout('1');
else
spout('0');
}
}

uart.h

#ifndef UART_H_
#define UART_H_

void uart_init(unsigned int baud);

void INT_UART_RLS(void);
void INT_UART_RDA(void);
35
void INT_UART_CTI(void);
void INT_UART_THRE(void);

void spout(unsigned char value);


void outdec8b (int value);
void outdec16b (int value);
void out_byte_in_bits(unsigned char data);

#endif /* UART_H_ */

36

You might also like