Professional Documents
Culture Documents
3058 Appendix
3058 Appendix
#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>
int main(void) {
//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;
// 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>
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;
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;
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>
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;
//Load Match REgister MR3 for achieving the counter rate = 2 counter / sec
LPC_TMR16B0 -> MR3 = 4999;
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;
}
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"
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);
}
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;
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_
void move_fw();
void move_bw();
void move_cw();
void move_ccw();
void stop();
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 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"
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
//*****************************************************************************
#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
//
//*****************************************************************************
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
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
};
//*****************************************************************************
// 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;
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;
#ifdef __USE_CMSIS
SystemInit();
#endif
//*****************************************************************************
// 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"
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>
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
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;
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"
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
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
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;
}
}
}
//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;
*/
temp = LPC_UART->LSR;
while (!(LPC_UART->LSR & 0x60));
while (LPC_UART->LSR & 0x01)
temp = LPC_UART->RBR;
//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)
{
34
{
value-=1;
value=~value;
spout('-');
}
else
spout(' ');
spout(((value%1000)/100)+48 );
spout(((value%100)/10)+48 );
spout((value%10)+48);
}
uart.h
#ifndef UART_H_
#define UART_H_
void INT_UART_RLS(void);
void INT_UART_RDA(void);
35
void INT_UART_CTI(void);
void INT_UART_THRE(void);
#endif /* UART_H_ */
36