of 109 /109
32-bit Microcontroller ARM7 LPC2148 Manual

ENG 32bit Micro Controller ARM7 LPC2148 Manual

Embed Size (px)

Citation preview

Page 1: ENG 32bit Micro Controller ARM7 LPC2148 Manual

32-bit Microcontroller ARM7

LPC2148

Manual

Page 2: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2

Contents

EDITORIAL NOTES...................................................................... 7

ACKNOWLEDGEMENTS............................................................. 8

CHAPTER 1: ARM7 AND LPC2148 ..........................................1-9

BACKGROUND ..................................................................................................................... 1-9

ARM FEATURES ................................................................................................................... 1-9Load-Store Architecture.............................................................................................. 1-9ARM Processor Instruction Set .................................................................................... 1-9Pipelining................................................................................................................... 1-9

LPC2148 TECHNICAL FEATURES .................................................................................... 1-10CPU and Data Bus.................................................................................................... 1-10Memory................................................................................................................... 1-10Vectored Interrupt Controller .................................................................................... 1-12Fast I/O Port............................................................................................................ 1-12Analogy to Digital Module ......................................................................................... 1-12Digital to Analogy Module ......................................................................................... 1-13USB 2.0 Module ....................................................................................................... 1-13Universal Asynchronous Receiver Transmitter (UART) Module..................................... 1-13I2C Bus Module........................................................................................................ 1-13SPI & SSP Bus Communication Module ...................................................................... 1-14Timer / Counter Module ........................................................................................... 1-14Watch Dog Timer..................................................................................................... 1-14Real Time Clock ....................................................................................................... 1-14PWM Signal Module.................................................................................................. 1-14System Control ........................................................................................................ 1-15

CHAPTER 2: C PROGRAMMING ...........................................2-17

OVERVIEW .......................................................................................................................... 2-17Steps in Executing the C Program ............................................................................. 2-17Structure of a Program............................................................................................. 2-17Header .................................................................................................................... 2-18Global Variables ....................................................................................................... 2-18User-defined Functions............................................................................................. 2-18Main Function .......................................................................................................... 2-18

VARIABLES ......................................................................................................................... 2-18Types of Variables.................................................................................................... 2-19Variants of the Variable ............................................................................................ 2-19Name of the Variable ............................................................................................... 2-19Reserved Keywords.................................................................................................. 2-20Variable Declaration ................................................................................................. 2-20Constant Declaration ................................................................................................ 2-20

ARRAYS............................................................................................................................... 2-21Array Declaration ..................................................................................................... 2-21Array Initialization .................................................................................................... 2-22

Page 3: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3

FORMATTED OUTPUT ....................................................................................................... 2-22printf() .................................................................................................................... 2-22sprintf()................................................................................................................... 2-22

EXPRESSIONS AND OPERATORS ................................................................................... 2-22Arithmetic Operators ................................................................................................ 2-23Relational Operators................................................................................................. 2-23Logical Operators ..................................................................................................... 2-24Precedence of Operators .......................................................................................... 2-24

ASSIGNMENT STATEMENT............................................................................................... 2-25Compound Statements ............................................................................................. 2-25

CONTROL STATEMENTS .................................................................................................. 2-26IF statement............................................................................................................ 2-26IF ELSE statement (1) .............................................................................................. 2-27IF ELSE statement (2) .............................................................................................. 2-28SWITCH statement .................................................................................................. 2-30

LOOPS ................................................................................................................................. 2-31WHILE Loop ............................................................................................................ 2-31DO WHILE Loop....................................................................................................... 2-32FOR Loop ................................................................................................................ 2-33Nested Loop ............................................................................................................ 2-34

FUNCTIONS......................................................................................................................... 2-35Functions with no Type ............................................................................................ 2-37

SCOPE OF VARIABLES ..................................................................................................... 2-38

CHAPTER 3: SETUP...............................................................3-39

COMPONENTS.................................................................................................................... 3-39

SYSTEM REQUIREMENTS................................................................................................. 3-39

SOFTWARE ......................................................................................................................... 3-40

Keil Version 3......................................................................................................... 3-40Philip Conductor Flash Utility LPC210x ISP ................................................................. 3-49

HYPER TERMINAL.............................................................................................................. 3-51

CHAPTER 4: OUTPUT PORT .................................................4-55

DESCRIPTION OF THE PROGRAM ................................................................................... 4-55

PSEUDO CODE ................................................................................................................... 4-55

SOURCE CODE ................................................................................................................... 4-56

EXERCISE............................................................................................................................ 4-57

Page 4: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

4

CHAPTER 5: INPUT PORT .....................................................5-58

DESCRIPTION OF THE PROGRAM ................................................................................... 5-58

PSEUDO CODE ................................................................................................................... 5-58

SOURCE CODE ................................................................................................................... 5-59

EXERCISE............................................................................................................................ 5-60

CHAPTER 6: EXTERNAL INTERRUPT ..................................6-61

DESCRIPTION OF THE PROGRAM ................................................................................... 6-61

PSEUDO CODE ................................................................................................................... 6-62

SOURCE CODE ................................................................................................................... 6-63

EXERCISE............................................................................................................................ 6-64

CHAPTER 7: UART0...............................................................7-65

DESCRIPTION OF THE PROGRAM ................................................................................... 7-65

PSEUDO CODE ................................................................................................................... 7-65

SOURCE CODE ................................................................................................................... 7-65

EXERCISE............................................................................................................................ 7-66

CHAPTER 8: UART1...............................................................8-67

DESCRIPTION OF THE PROGRAM ................................................................................... 8-67

PSEUDO CODE ................................................................................................................... 8-67

SOURCE CODE ................................................................................................................... 8-68

EXERCISE............................................................................................................................ 8-69

CHAPTER 9: ANALOG TO DIGITAL ......................................9-70

DESCRIPTION OF THE PROGRAM ................................................................................... 9-70

PSEUDO CODE ................................................................................................................... 9-70

SOURCE CODE ................................................................................................................... 9-71

EXERCISE............................................................................................................................ 9-72

Page 5: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

5

CHAPTER 10: REAL TIME CLOCK (1).................................10-73

DESCRIPTION OF THE PROGRAM ................................................................................. 10-73

PSEUDO CODE ................................................................................................................. 10-73

SOURCE CODE ................................................................................................................. 10-74

EXERCISE.......................................................................................................................... 10-76

CHAPTER 11: REAL TIME CLOCK (2).................................11-77

DESCRIPTION OF THE PROGRAM ................................................................................. 11-77

PSEUDO CODE ................................................................................................................. 11-77

SOURCE CODE ................................................................................................................. 11-78

EXERCISE.......................................................................................................................... 11-80

CHAPTER 12: KEYBOARD (1) .............................................12-81

DESCRIPTION OF THE PROGRAM ................................................................................. 12-81

PSEUDO CODE ................................................................................................................. 12-81

SOURCE CODE ................................................................................................................. 12-81

EXERCISE.......................................................................................................................... 12-82

CHAPTER 13: KEYBOARD (2) .............................................13-83

DESCRIPTION OF THE PROGRAM ................................................................................. 13-83

PSEUDO CODE ................................................................................................................. 13-83

SOURCE CODE ................................................................................................................. 13-84

EXERCISE.......................................................................................................................... 13-87

CHAPTER 14: REAL TIME CLOCK AND KEYBOARD ........14-88

DESCRIPTION OF THE PROGRAM ................................................................................. 14-88

PSEUDO CODE ................................................................................................................. 14-88

SOURCE CODE ................................................................................................................. 14-90

EXERCISE.......................................................................................................................... 14-93

Page 6: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

6

CHAPTER 15: ANALOG TO DIGITAL AND DIP SWITCH....15-94

DESCRIPTION OF THE PROGRAM ................................................................................. 15-94

PSEUDO CODE ................................................................................................................. 15-94

SOURCE CODE ................................................................................................................. 15-95

CHAPTER 16: EXTERNAL INPUT (DIP SWITCH)................16-97

DESCRIPTION OF THE PROGRAM ................................................................................. 16-97

PSEUDO CODE ................................................................................................................. 16-98

SOURCE CODE ............................................................................................................... 16-100

CHAPTER 17: EXTERNAL OUTPUT (LEDS) ..................... 17-103

DESCRIPTION OF THE PROGRAM ............................................................................... 17-103

PSEUDO CODE ............................................................................................................... 17-105

SOURCE CODE ............................................................................................................... 17-107

REFERENCES.......................................................................... 109

Page 7: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

7

Editorial Notes

This manual is designed for a person who has no background knowledge on ARM732-bit Microcontroller or C programming, but wishes to program ARM7 processor in ashort period of time. The knowledge in C programming is not a prerequisite; the usercan refer to the C programming section (chapter 2) of this manual for details.Nevertheless, it is still recommended that the user have some backgrounds on Cprogramming before hand to ensure more understanding. It is also recommendedthat the user use this manual in conjunction with Philip’s LPC2148 User Manual(www.nxp.com) for more details and further understanding.

The manual attempts to guide users by using example of programs, provided withdescriptions on how it works (line by line), along with explanation of the programsfocus on the implementation on the board rather than its working mechanism. Inother words, the aim is, the user should be able to program the board using only thesource code examples as references. An in depth understanding of themicroprocessor is not necessary.

In this manual, the users are expected to find; an overview of the ARM processor,including information on the background and features of ARM7 processor andLPC2148 modules; a C Programming Tutorial, with examples provided; a step bystep guide on the setup procedure, how to upload the program on the board and theinstallation of necessary programs; Example programs and exercises withdescriptions and explanations on LPC2148 modules; and lastly the manualreferences.

The example of the program section has two main parts: the description of theprogram and the pseudo code. The description of the program gives an overview ofeach program and how it works such as the inputs and outputs and port used. Thepseudo code, on the other hand, provides detailed explanations of each instructionand what task they performed.

The main purpose of this manual is to guide users. All example programs providedon this manual have been tested and work properly. Thus, I hereby will not take anyresponsibility on problems which may have occurred or caused by the programs.

Page 8: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

8

Acknowledgements

I would like to sincerely thank the following person for their contributions andadvices on this 32-bit Microcontroller ARM7 LPC2148 manual.

In particular, I thank:

Dr. Sawat Tantiphanwadi for giving me the opportunity to do self-study onmicrocontroller and develop this manual.

Researcher Rachaporn Keinprasit for the opportunity to access NECTEC department,for his kind advices, and overview and clarification of some topics regarding themanual.

Researcher Assistant Prachumpong Dangsakul for his valuable suggestions,clarification on some topics and time spent on verifying the manual.

Finally, thank to all staffs at NECTEC who helped, and to all users who use thismanual.

Page 9: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

1-9

Chapter 1: ARM7 and LPC2148

Background

ARM or Advanced RISC Machine, uses a 32-bit RISC (Reduced Instruction SetComputer) processor.

On April 26th, 1985, ARM was first developed by Acorn Computers in Cambridge,England. Later it was sent to VLSI Technology in San Jose, California, USA, forproduction. It was until 1990 that ARM Limited was established to support anddevelop ARM processor.

ARM Features

Load-Store Architecture

A load-store architecture focus on the process, i.e. reading a value by copying fromthe memory to the register (load instructions) or writing a value from the registerto the memory (store instructions).

ARM does not support memory-to-memory operations like a CISC (ComplexInstruction Set Computer) machine. The processor works as follow:

1. Data processing instructions: for dealing with the registers only2. Data transfer instructions: for copying data from the memory to the

register or from the register to the memory3. Control flow instructions: for controlling the order of execution

ARM Processor Instruction Set

The size of ARM processor instruction is 32-bit, excluding the 16-bit Thumbinstruction set (for the microcontroller only) that has the following features:

Support load-store architecture Process 3 registers at a time (2 address registers and 1 data register) Support various registers Took 1 instruction (or 1 clock cycle) to transfer and process arithmetic and

logic operations. Is an open instruction set; more registers or data can be added

Pipelining

An instruction cycle has 3 stages:

Fetch: to get the instruction Decode: to interpret the instruction Execute: to perform an action as instruct

Page 10: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

1-10

During the execution of the 1st instruction, the 2nd instruction being decode and the3rd instruction is being fetch. See the Figure 1 below.

Figure 1: Pipelining

LPC2148 Technical Features

CPU and Data Bus

LPC2148 used ARM7TDMI-S as its core and two types of buses to increase theboard’s performance. The modules inside are connected by the CPU high-performance bus called Advance High-Performance Bus (AHB) and theperipheral are connected by VLSI Peripheral Bus (VPB). The data between thetwo buses are exchange at the AHB and VPB bus connection.

ARM7TDMI-S has two sets of instruction set: the 32-bit Standard and 16-bit Thumbinstruction set. The used of Thumb instruction set can reduced the size of the controlprogram up to 65% and increases the performance up to 160%

Memory

LPC2148 has 2 types of memory (see Figure 2 on next page):

Flash Memory 512 kb (0x0000 0000 – 0x0007 FFFF)o 12 kb used for storing the boot loader firmware (software resided in a

chip)o 500 kb working space that can store the program or data

Static ram 40 kb (0x4000 0000 – 0x4000 7FFF)o 32 kb for the program or datao 8 kb for DMA when USB is in use (this space can also be used to store

the program or data)

Memory at the address range of (0xE000 0000 – 0xFFFF FFFF) is for the AHB andAPB peripherals. Each peripheral has 16kb in size. Also, all peripheral register arealigned to 32-bit (regardless of their original size). (See Table 1 on next page)

1 2 3 4 5 6Fetch

DecodeExecute

Page 11: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

1-11

Figure 2: System memory map (user program viewpoint)

APB Peripheral Base Address Peripheral Name0 0xE000 0000 Watchdog timer1 0xE000 4000 Timer 02 0xE000 8000 Timer 13 0xE000 C000 UART04 0xE001 0000 UART15 0xE001 4000 PWM6 0xE001 8000 Not used7 0xE001 C000 I2C08 0xE002 0000 SPI09 0xE002 4000 RTC10 0xE002 8000 GPIO11 0xE002 C000 Pin connect block12 0xE003 0000 Not used13 0xE003 4000 ADC014 - 22 0xE003 8000

0xE005 8000Not used

Page 12: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

1-12

23 0xE005 C000 I2C124 0xE006 0000 ADC125 0xE006 4000 Not used26 0xE006 8000 SSP27 0xE006 C000 DAC28 - 35 0xE007 0000

0xE008 C000Not used

36 0xE009 0000 USB37 - 126 0xE009 4000

0xE01F 8000Not used

127 0xE01F C000 System Control Block

Table 1: APB peripheries and base addresses

Memory map concepts and operating modes

LPC2148 used the concept that each memory area has a “natural” location. Also, thebulk remains at fixed position, thus, there is no need to design code to run atdifferent address range.

Memory re-mapping

The Boot Block is mapped to the top of the on-chip memory space, thus, there is noneed to change the location of the Boot Block or changing the mapping of theinterrupt vectors. Memory space other than interrupt vector remains at fix location.

Vectored Interrupt Controller

Vectored Interrupt Controller (VIC) receives all interrupt signals initiated. The priorityof the interrupt is changeable by software. There are 3 types of interrupt request:

Fast Interrupt Request (FIQ)o Has the highest priority out of 3 types

Vectored Interrupt Request (IRQ)o Has the 2nd highest priority out of 3 typeso Has 16 interrupt sources in total, called slot (slot 0 has the highest

priority and slot 15 has the lowest priority) Non-vectored IRQ

o Has the lowest priority out of 3 types

Fast I/O Port

Each of LPC2148 pins can be configured to various peripheral modules. When thevalues of all pins are reset, by default, it is set as input. Moreover, if the pins areconfigured as input/output digital, the GPIO register takes charge of the control.

Analogy to Digital Module

This module has 2 analogy signal input from ADC0 and ADC1. ADC0 has 6 channelsand ADC1 has 8 channels. The output is a 10-bit digital signal. The functional of thismodule is as listed below:

Page 13: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

1-13

Conversion process used is Successive Approximation Input Analog signal 0 to Voltage reference value (+3.3V) Sampling Rate of 400,000 per second Analog to Digital converted value are stored in a register

Digital to Analogy Module

This module has 1 10-bit digital input and 1 output analogy signal. The functional ofthis module is as listed below:

10-bit conversion Buffer at the output Power-save mode supported The rate of conversion configuration

USB 2.0 Module

The USB module consisted of the register interface, the serial interface engine, theendpoint buffer memory and DMA controller. The functional of this module is aslisted below:

Compatibility with USB 2.0 Full-Speed port Support 32 physical end-points (or 16 logical end-points) Support control, bulk, interrupt and asynchronous End-point package can be selected from software Size of buffer depends on the types of end-points and the size of package USB connection status shown on LED Power by bus supported Support DMA Buffer size can be doubled to support bulk end-points and asynchronous

Universal Asynchronous Receiver Transmitter (UART) Module

2 UART ports are available: UART0 and UART1. Part of UART0 is designed to supportprogram download through ISP process. UART1, on the other hand, is fully designedas a parallel communication port. The baud rate of both ports can be configuredseparately. The functional of this module is as listed below:

FIFO buffer for receiving or transmitting 16-byte size data FIFO trigger point at the 1st, 4th, 8th and 14th byte The source of the baud rate is inside the board The baud rate can be configured through software UART1 port signal configuration is fully arranged, including DTR, DSR, CTS,

RTS, DCD, RI

I2C Bus Module

This module has 2 set of I2C Buses: the Clock (SCL) bus and Data (SDA). SDA’smaximum data transfer rate is 400 Kbit/s.

Page 14: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

1-14

The I2C module can be configured as a master or slave mode. It also supportedmulti-master and other communication devices that have different data transfer rate.In addition, the clock frequency can be also be configured.

SPI & SSP Bus Communication Module

LPC2148 has 1 set of SPI that support 2-way communication and SynchronousSerial Peripheral (SSP) can communication with SPI, SSI 4 lines and MicrowireBus.

Timer / Counter Module

This module has 2 set of Timer/Counter: Timer0 and Timer1. The size of bothTimer/Counter is 32-bit. The timer uses the Clock peripheral signal (PCLK) or outersource clock signal as reference. The functional of this module is as listed below:

2 sets of Timer/Counter 32-bit with 32 bit prescaler Able to use the clock signal to increment the timer. The clock source can be

from the peripheral clock (PCLK) or outer source clock. Timer1 has 4 slots input source for detecting or capturing signal. The slots

can detect a change in logic of the input signal that is use to trigger aninterrupt (if enable before hand).

4 32-bit registers for storing comparison values The timer can be configured to reset itself, continuing or stop working after

an interrupt occur An output generates a signal when the count matches or when an interrupt

occur.

Watch Dog Timer

Watch Dog Timer prevents the microcontroller from stop functioning as a result fromerrors. It can be configured through a 32-bit timer and enabled through softwareand reset through hardware control only.

Real Time Clock

LPC2148 has a Real Time Clock in itself that uses a 3V reserved battery. Unlike othermicroprocessors, there is no need for an extra RTC IC. To read the RTC values, aprogram to access is not needed, just connect some registers and enable the RTC.Thus, LPC2148 consumes less memory space when accessing the RTC values.

PWM Signal Module

LPC2148 has 6 channels for generating the PWM signal (the Timer generates thesignal). The PCLK increment the Timer and the Master Reset register set the periodof the signal.

Page 15: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

1-15

In addition, the PWN signal can be configured as active high or low, thus, it can beuse to control a 3 phase motor. 2 MR register are in use: one to control the signalcycle, the other for active edge configuration. The functional of this module is aslisted below:

7 MR registers MR register is in use when the timer continues to increment, stops or is reset. The edge of the active signal can be configured 32-bit Timer/Counter is used along with 32-bit prescaler 6 output slots for PWM signal

System Control

Crystal Oscillator

Crystal Oscillator is the CPU clock signal generator; the range is at 1MHz to 25MHz

Phase Lock Loop (PLL)

Phase Lock Loop received an input CLK signal of the range 10MHz-25MHz thenconvert the range 10MHz-60MHz

Reset and Wakeup Timer

LPC2148 can be reset in 2 ways; from the RESET button or Watch Dog Timer.

Once the reset signal is initiated, the wake up timer starts. During this time, theboard setup itself. If the reset signal latches longer than the time taken for the boardto setup, once the signal is gone, the CPU starts immediately. However, if the resetsignal latches shorter than the time taken for the board to setup, once the signal isgone, the board waits for the wake up time to finish its first loop before starting theCPU (See Figure 3).

Page 16: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

1-16

Figure 3: Reset and Wakeup Timing Diagram

Low power supply / Brownout Detector

If power supply is less than 2.9V, the Brownout Detector will generate an interruptsignal. However, if the power supply is less than 2.6V, the Brownout Detector (BOD)generates a signal to reset the microprocessor.

Signal Separator Bus VPB

VPB bus has 2 usages: to allot the PCLK signal on the VPB bus (usually the frequencyis 0.5-0.25 times lower than CPU clock) and to monitor the power on the bus, if theperipheral is not in use, that peripheral will be set on power safe mode.

Testing / Debugging Connector

LPC2148 supports testing or debugging through JTAG port up to tracing from TRACEport. Both ports use P1 therefore, if P1 in use, P0 should be use for work purposeinstead.

Page 17: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-17

Chapter 2: C Programming

Overview

C is a programming language invented by Dennis Ritchie. It is one type of structuralprogramming suitable for system programming. A C program begin its execute at themain() function.

An execution is a program on its running stage.

Steps in Executing the C Program

1. Write a program in C language2. Save it as .c extension3. Compile the program4. If necessary, make corrections to any errors found during compilation and re-

compile it5. Run the program

Structure of a Program

A C program consisted of the following parts: the Header, the Global Variable(s), theUser-defined Function(s) and the Main Function. Out of these 4 parts, the GlobalVariable(s) and the User-defined Function(s) are optional.

Header

Global Variable(s)

User-defined Function(s)

Main Function

Figure 4: Structure of a Program

Below is an example of a simple C program that consisted of the Header and theMain Function only.123456

#include <stdio.h>

main(){

printf("Hello World!!");}

Figure 5: A Simple C Program

(line 1) #include <stdio.h> is the Header

(line 3-6) main() function

Page 18: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-18

Header

Header files contain information on the data types and pre-made functions of the Cprogram. In C Programming, in order to use the pre-defined functions or initializedvariables in the program, prior information on those data types and functions mustbe known (note that these information are stored in separate .h extension files, inthe library folder). One way for the program to access these information is to use#include.

#include is a preprocessor directive, used to read the whole content of the otherfiles. Usually it is use to read the header files.

#include <[file name]>

Figure 6: Structure of #include preprocessor directive

1 #include <stdio.h>

Figure 7: Example of how to include the header file by using #include

(line 1) stdio.h is the header [file name]stdio.h contains information on the standard input/output

Global VariablesNOTE: Please refer to Variables(page 2-18) and Scope of Variables(page 2-38)

User-defined FunctionsNOTE: Please refer to Functions(page 2-35)

Main Function

A main function is a must have function in every program. It marks the entry pointwhere the program start it execution.

Variables

Variables are used to hold a value, of any data type, during the program execution.A variable must have a type, a name and a value.

A constant is a type of variable (of any data type) that the value remainsunchanged throughout the program execution.

Page 19: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-19

Types of Variables

Types define the type of the variables.

Type Meaningint A Integerfloat A Decimaldouble A double precision range decimals (float)char A Character

Table 2: Types of Variable

Variants of the Variable

Variants overwrite the original range of the variables.

Variants Meaningshort A reduce Integer rangelong An increase Integer rangeunsigned A positive-only Integer rangeunsigned long A positive-only Integer with an increase range

Table 3: Variants of a variable

Name of the Variable

Characteristics of the variable names: Consist of characters, numbers and underscore (_) only No space between the name No special characters, except for underscore (_) Must begin with a character Cannot consist of numbers alone Cannot end with an underscore (_) Is case sensitive

A summary is shown on Table 4 below.

Names Validity0078 NO1cat NOcat YEScat1 YEScat_ NOcat_1 YES

Table 4: Examples of variable names

Page 20: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-20

Reserved Keywords

Reserved keywords are names of the variables that cannot be use.auto break case char continue defaultdo double else enum extern floatfor goto if int long registerreturn short sizeof static struct switchtypedef union unsigned void while

Figure 8: C Language reserved keywords

Variable Declaration[variant] [type] [name]=[value];

Figure 9: Structure of how to declare a variable

1. OPTIONAL[variant] specify a change on the length of the variable.

2. [type] defines the type of a variable3. [name] is the name of a variable4. OPTIONAL

= is an assignment.5. OPTIONAL

[value] is the value of the variable6. ; IS NOT optional

NOTE: A variable should be define at the beginning out the function

char a = ‘a’;

Figure 10: Example of a declared variable (1)

On the example above, char is the [type], a is the [name] and ‘a’ is the [value] ofthe variable.

unsigned int i = 0;

Figure 11: Example of a declared variable (2)

On the example above, unsigned is the [variant], char is the [type], a is the[name] and ‘a’ is the [value] of the variable.

Constant Declaration#define [name] [value]

Figure 12: Structure of how to declare a constant variable

1. [name] is the name of a constant variable2. [value] is the value of the constant variable

NOTE: A constant should be define between the [header] and [body]

Page 21: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-21

#define SECONDS_PER_HOUR 60

Figure 13: Example of a declared constant variable

On the example above, SECONDS_PER_HOUR is the name and 60 is the value ofthe variable.

Arrays

An array is a collection of variables of the same type. In C Programming, an arraybegins at position 0. An array can have up to 0 to n dimensions; however, the mostfrequent used ones are 1-Dimension and 2-Dimension arrays.

The following figures are examples of a 1-Dimension and 2-Dimension arrays.

a[3]a [0]

[0][1][2]

Figure 14: Example of 1-Dimensional Array

a[4][3]a [0] [1] [2]

[0][1][2][3]

Figure 15: Example of 2-Dimensional Array

NOTE: These diagrams are just a logical view of the arrays

Array Declaration[variant] [type] [name][row][column] = {[value #1], [value #2],

...,[value #(row x column)]};

Figure 16: Example of how to define an array

1. OPTIONAL[variant] specify a change on the length of the variable.

2. [type] defines the type of all elements within the array3. [name] is the name of the array4. [row] is the number of rows on the array5. [column] is number of the columns on the array6. OPTIONAL

= is an assignment[value] is the value of the variables within the array

Page 22: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-22

Array Initializationint a[3] = {0,1,3};

a [0][0] 0[1] 1[2] 3

Figure 17: Example of an initialized 1-D array

int a[4][3] = {0,1,2,3,4,5,6,7,8,9,10,11};

a [0] [1] [2][0] 0 4 8[1] 1 5 9[2] 2 6 10[3] 3 7 11

Figure 18: Example of an initialized 2-D array

Formatted Output

NOTE: Always include #include <stdio.h>

printf()

Control String Prints%d An Integer%f A Decimal%c A Character%s A String of characters

Table 5: Control String

sprintf()

Works the same as printf(), but stores output inside a buffer instead of displaying theoutput on screen.

Expressions and Operators

An expression is a statement that has value. It consists of operators and operands;

it is in the form of [operand] [operator] [operand] [operator] [operand]....

[operand][operator][operand]

Figure 19: Structure of a simple expression

Operands are inputs to the operator. It can either be constants or variables. Forexample, 9, 3, 4, 56, 17, 8, a, g, s, l and so on.

Page 23: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-23

Operators are notations that represents arithmetic operations, for instance, addition(+), subtraction (-), multiplication (*) or division (/). It tells what operations shouldbe performed on the operands.

b + 3

Figure 20: Example of a simple expression

On the example above, b and 3 are the operands and + is the operator.

(b + 3)*4

Figure 21: Example of a complex expression

On the example above, b, 3 and 4 are the operands and + and * are the operators.Noticed that the form of the expression above is more complex, however, the basicstructure of the expression is still the same. That is [operand] [operator]

[operand] [operator] [operand]....

Arithmetic Operators

Arithmetic operators are used to perform operations on the operands.

Operator Meaning+ Addition- Subtraction* Multiplication/ Division

% Modulation

Table 6: Arithmetic Operators

NOTE: Multiplication, Division and Modulation operators will beevaluate first, following by Addition and Subtraction.

Relational Operators

Relational operator is used to compare two operands.

C Notation Meaning= = Equal to> Greater than< Less than

>= Greater than or Equal to<= Less than or Equal to!= Not Equal to

Table 7: C comparison notations

NOTE: In C Programming, == is used to check for equality, while = isthe assignment symbol.

Page 24: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-24

Logical Operators

Symbol Meaning&& And

|| Or! Not

Table 8: Logical operation symbols

Logical operator is used to combine relational operators. See example on Figure 22below.

(x >= 10) && (x < 20)

Figure 22: Example of logical operator usage

Here && is used to combine the expression (x >= 10) and (x < 20)

Precedence of Operators

Level ofPrecedence

Description Representations

1 Parenthesis () []2 (Unary) Not

Negative SignIncrement Value by 1

!–

++3 Multiplication

DivisionModulus

*/

%4 Addition

Subtraction+-

5 Shift RightShift Left

>><<

6 Greater ThanLess Than

Greater Than or Equal ToLess Than or Equal To

><

>=<=

7 EqualNot Equal

= =!=

8 Bitwise AND &9 Bitwise Exclusive OR ^10 Bitwise OR |11 Logical AND &&12 Logical OR ||13 Conditional Expression ?:14 Assignment =15 Comma ,

Table 9: Operator Precedence

Page 25: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-25

Assignment Statement

A statement is a block of code that does something.

An assignment statement is used to store a result of an expression (on the righthand side of an equation) to a variable (on the left hand side of an equation).

[value to be assigned] = [expression];

Figure 23: Structure of an assignment statement

The equal sign (=) in Figure 23 above, is called an assignment. In C Programming,= does not represent equality, but is interpreted as to evaluate what ever expression(on the right hand side) and store/assign the value (on the left hand side).

NOTE: Notice that all statements end with a semicolon (;)

a = b + c;

Figure 24: Example of an assignment statement

Figure 24 is an example of an assignment statement. In this example, the value of b+ c will be evaluated and store/assigned into variable a

On Figure 25 below is an example of how an assignment statement works.123456789

10

#include <stdio.h>

main(){

int a = 0;int b = 7;int c = 3;a = b + c;printf(“%d”,a);

}

Figure 25: Example of how an assignment statement works

(line 5-7) Define variables a, b, c as type IntegerAssign 0 to a, 7 to b and 3 to c

(line 8) Evaluate b + c, that is 7 + 3, then assign the result to a on the lefthand side of the equation

(line 9) Display variable a on screen, which is 10

Compound Statements

In addition, take notes of these Compound statements in Figure 26 as well.i++; is equivalent to i = i + 1;i--; is equivalent to i = i – 1;

Figure 26: Compound statements

Page 26: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-26

Control Statements

One characteristics of a program is it executes all statements in sequence, meaningthat parts of the program cannot be skipped. The whole program must run from topto the bottom in step by step. Thus, the program is not flexible in terms of solvingcomplex problems.

In order to increase the flexibility of the program, control statements areintroduced to control the order of statements execution, allowing some statementswithin the program to be skipped.

IF statementif([condition]){

[statement(s)]}

Figure 27: Structure of an IF statement

1. If the [condition] is TRUE then execute [statement(s)].2. Otherwise, skip [statement(s)].

Below is an example of an IF statement.123456789

10

#include <stdio.h>

main(){

int a=0;if(a==0){

printf("Hello World!!");}

}

Figure 28: Example of an IF statement

(line 5) Define variable a as type IntegerAssign 0 to a

(line 6) a==0 is the [condition]Check if a equals to 0 or not.

If a is 0, then the [condition] is TRUEIf a is not 0, then the [condition] is FALSE

(line 8) printf("Hello World!!"); is the [statement(s)]

Display Hello World!! on screen.

If the [condition] is TRUE, execute [statement(s)]If the [condition] is FALSE, skip [statement(s)]

The program displays “Hello World!!” if and only if a equals to 0, otherwise,nothing is display on screen.

Page 27: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-27

IF ELSE statement (1)if([condition]){

[statement(s) #1]}else{

[statement(s) #2]}

Figure 29: Structure of an IF ELSE statement (1)

1. If the [condition] is TRUE then execute [statement(s) #1] only.2. Otherwise, skip [statement(s) #1] and execute [statement(s) #2] only.

NOTE: [statement(s) #1] and [statement(s) #2] will never be executetogether

Below is an example of an IF ELSE statement (1).123456789

1011121314

#include <stdio.h>

main(){

int a=0;if(a==0){

printf("Hello World!!");}else{

printf("Hello People!!");}

}

Figure 30: Example of an IF ELSE statement (1)

(line 5) Define variable a as type IntegerAssign 0 to a

(line 6) a==0 is the [condition].Check if a equals to 0 or not.

If a is 0, then the [condition] is TRUEIf a is not 0, then the [condition] is FALSE

(line 8) printf("Hello World!!"); is [statement(s) #1]Display Hello World!! on screen.

If the [condition] is TRUE, execute [statement(s) #1]If the [condition] is FALSE, skip [statement(s) #1]

(line 12) printf("Hello People!!"); is [statement(s) #2]

Display Hello People!! on screen.

If the [condition] is TRUE, skip [statement(s) #2]If the [condition] is FALSE, execute [statement(s) #2]

The program displays “Hello World!!” when a equals 0, otherwise, “HelloPeople!!” is display on screen.

Page 28: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-28

IF ELSE statement (2)if([condition #1]){

[statement(s) #1]}else if([condition #2]){

[statement(s) #2]}....else if([condition #n-1]){

[statement(s) #n-1]}else{

[statement(s) #n]}

Figure 31: Structure of an IF ELSE statement (2)

1. If [condition #1] is TRUE then execute [statement(s) #1] only.2. If [condition #1] is FALSE, check if [condition #2] is TRUE or not.3. If [condition #2] is TRUE then execute [statement(s) #2] only.4. If [condition #2] is FALSE, check if [condition #3] is TRUE or not.5. And so on to [condition #n-1].6. If the [condition #n-1] is TRUE then execute [statement(s) #n-1] only.7. Otherwise, execute [statement(s) #n] only.

NOTE:else{

[statement(s) #n]}Is not necessary. It is possible to end the IF ELSE statement (2)with:else if([condition #n-1]){

[statement(s) #n-1]}

Page 29: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-29

Below is an example of an IF ELSE statement (2).123456789

101112131415161718

#include <stdio.h>

main(){

int a=0;if(a==0){

printf("Hello World!!");}else if(a==1){

printf("Hello People!!");}else{

printf("Hello Aliens!!");}

}

Figure 32: Example of an IF ELSE statement (2)

(line 5) Define variable a as type IntegerAssign 0 to a

(line 6) a==0 is the [condition #1]Check if a equals to 0 or not.

If a is 0, then [condition #1] is TRUEIf a is not 0, then [condition #1] is FALSE

(line 8) printf("Hello World!!"); is [statement(s) #1]

Display Hello World!! on screen.

If [condition #1] is TRUE, execute [statement(s) #1] and skip the rest ofthe IF ELSE statement.If [condition #1] is FALSE, skip [statement(s) #1], and go to [condition#2].

(line 10) a==1 is [condition #2]Check if a equals to 1 or not.

If a is 1, then [condition #2] is TRUEIf a is not 1, then [condition #2] is FALSE

(line 12) printf("Hello People!!"); is [statement(s) #2]

Display Hello People!! on screen.

If [condition #2] is TRUE, execute [statement(s) #2] and skip the rest ofthe IF ELSE statement.If [condition #2] is FALSE, skip [statement(s) #2], and execute[statement(s) #3].

(line 16) printf("Hello Aliens!!"); is [statement(s) #3]

Display Hello Aliens!! on screen.

The program displays “Hello World!!” when a equals 0 and displays “HelloPeople!!” when a equals 1, otherwise, “Hello Aliens!!” is display on screen.

Page 30: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-30

SWITCH statement

SWITCH statement works the same as the IF ELSE statement.switch([variable]){

case [character/integer #1] : [statement(s) #1];break;

case [character/integer #2] : [statement(s) #2];break;

...case [character/integer #n-1] : [statement(s) #n-1];

break;default : [statement(s) #n];

break;

}

Figure 33: Structure of a SWITCH statement

1. If [variable] matches [character/integer #1] then execute [statement(s) #1];2. If [variable] matches [character/integer #2] then execute [statement(s) #2];3. And so on to case [character/integer #n-1].4. If [variable] matches [character/integer #n-1] then execute [statement(s)

#n-1];5. If [variable] does not match anything then execute [statement(s) #n];

NOTE: break; caused a break out of the SWITCH statement.

Below is an example of a SWITCH statement.123456789

1011121314151617

#include <stdio.h>

main(){

int number=1;switch(number){

case 1 : printf("One");break;

case 2 : printf("Two");break;

case 3 : printf("Three");break;

default : printf("None");break;

}}

Figure 34: Example of an IF ELSE statement (2)

(line 5) Define variable number as IntegerAssign 1 to number

(line 6) number is the trigger [variable]

(line 8) printf("One"); is [statement(s) #1]Display One on screen.

If the number is 1 then execute [statement(s) #1]

(line 9) break; causes a break out of the SWITCH statement

Page 31: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-31

(line 10) printf("Two"); is [statement(s) #2]

Display Two on screen.

If the number is 2 then execute [statement(s) #2]

(line 11) break; causes a break out of the SWITCH statement

(line 12) printf("Three"); is [statement(s) #3]Display Three on screen.

If the number is 3 then execute [statement(s) #3]

(line 13) break; causes a break out of the SWITCH statement

(line 14) printf("None"); is [statement(s) #4]

Display None on screen.

If the number is not 1, 2 or 3 then execute [statement(s) #4]

(line 15) break; causes a break out of the SWITCH statement

The program displays “One” if number equals 1, displays “Two” if number equals2 and displays “Three” if number equals 3, otherwise, “None” is display on screen.

Loops

One characteristics of a program is it executes all statements in sequence, meaningthat parts of the program cannot be repeated. The whole program must from top tothe bottom in step by step. Thus, the program is not flexible in terms of solvingcomplex problems.

In order to increase the flexibility of the program, loops are introduced to control theorder of statements execution, allowing some statements within the program to berepeated.

WHILE Loopwhile([condition]){

[statement(s)]}

Figure 35: Structure of a WHILE loop

1. If [condition] is TRUE then execute [statement(s)] until [condition] becomesFALSE or there is a break in the loop.

2. If [condition] is FALSE then skip [statement(s)]

NOTE: If [condition] never becomes FALSE, [statement(s)] will beexecuted forever.

NOTE: break; can be used to cause a break in the WHILE loop.

Page 32: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-32

Below is an example of a WHILE loop.123456789

#include <stdio.h>

main(){

while(1){

printf("Hello World!!");}

}

Figure 36: Example of a WHILE loop

(line 5) 1 is the [condition]When [condition] is 1, it is always TRUE

(line 7) printf("Hello World!!"); is the [statement(s)]

Display Hello World!! on screen.

If the [condition] is TRUE, execute [statement(s)] until [condition]becomes FALSE or there is a break in the loop.If the [condition] is FALSE, skip [statement(s)].

The program displays “Hello World!!” for an infinite number of times.

DO WHILE Loopdo{

[statement(s)]}while([condition])

Figure 37: Statement of a DO WHILE loop

1. For the first time execute [statement(s)]2. If [condition] is TRUE then execute [statement(s)] again until [condition]

becomes FALSE or there is a break in the loop.3. If [condition] is FALSE then exit the loop

NOTE: If [condition] never becomes FALSE, [statement(s)] will beexecuted forever.

NOTE: break; can be used to cause a break in the DO WHILE loop.

Below is an example of a DO WHILE loop.123456789

10

#include <stdio.h>

main(){

do{

printf("Hello World!!");}while(1)

}

Figure 38: Example of a DO WHILE loop

Page 33: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-33

(line 7) printf("Hello World!!"); is [statement(s)]

Display Hello World!! on screen.Execute [statement(s)].

(line 9) 1 is the [condition]When [condition] is 1, it is always TRUE

If the [condition] is TRUE, Go to (line 6) and execute [statement(s)]again.If the [condition] is FALSE, skip [statement(s)], exit DO WHILE loop.

The program displays “Hello World!!” for an infinite number of times.

FOR Loop

A FOR loop contains 3 parts:

Initialize variables when the loop is entered. A check that when proves false, will exits the loop. A statement used to modify loop counters on each loop iterations after

the first.

for([initialization]; [exit condition]; [increment/decrement]){

[statement(s)]}

Figure 39: Structure of a FOR loop

1. [initialization] set the starting loop count number.On the first time entering the FOR loop.

2. The [exit condition] makes a relational comparison between the loop countnumber and a value.If the [exit condition] is TRUE, execute [statement(s)].

3. If the [exit condition] is FALSE, exit FOR loop.4. [increment/decrement], depends on which one is selected, either increase or

decrease the loop count number.

NOTE: If the FOR loop is programmed in a way that the [exitcondition] will never be FALSE, the FOR loop becomes an infiniteloop.

Below is an example of a FOR loop.123456789

#include <stdio.h>

main(){

for(int i=0; i<10; i++){

printf("Hello World!!");}

}

Figure 40: Example of a FOR loop

Page 34: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-34

(line 5) int i=0; is the [initialization]

Define variable i as type IntegerAssign 0 to i, on the first time entering the FOR loop.

i<10; is the [exit condition]

Check if i is less than 10 or notIf the [exit condition] is TRUE, execute [statement(s)].If the [exit condition] is FALSE, exit FOR loop.

i++ is the [increment] value.Increase the value of i by 1

(line 7) printf("Hello World!!"); is the [statement(s)]

Display Hello World!! on screen.Go to (line 5)

The program display “Hello World!!” on screen for 10 numbers of times.

Nested Loop

There is no universal structure of a nested loop, any kinds of loop (such as WHILEloop, DO WHILE loop or FOR loop) inside a loop is called a Nested Loop.

Below is an example of a FOR loop within a FOR loop, one kind of Nested loop.123456789

10111213

#include <stdio.h>

main(){

int a[5][10]={0};for(int i=0; i<5; i++){

for(int j=0; j<10; j++){

a[i][j] = i*j;}

}}

Figure 41: Example of a Nested Loop

(line 5) Define a as a 2-D array of type IntegerDimension of a is 5 by 10Assign 0 to all elements of a

(line 6) int i=0; is the [initialization]

Define variable i as type IntegerAssign 0 to i, on the first time entering the FOR loop.

i<5; is the [exit condition]

Check if i is less than 5 or notIf the [exit condition] is TRUE, execute [statement(s) #1].If the [exit condition] is FALSE, exit FOR loop.Note: (line 8-11) are the [statement(s) #1] in this case!

i++ is the [increment] value.Increase the value of i by 1

Page 35: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-35

(line 8) int j=0; is the [initialization]

Define variable j as type IntegerAssign 0 to j, on the first time entering the FOR loop.

j<10; is the [exit condition]

Check if j is less than 10 or notIf the [exit condition] is TRUE, execute [statement(s) #2].If the [exit condition] is FALSE, exit FOR loop.

j++ is the [increment] value.Increase the value of j by 1

(line 10) a[i][j] = i*j; is the [statement(s) #2]

Assign the value of i*j to a at column i, row jDisplay Hello World!! on screen.Go to (line 5)

Functions

All functions must be pre-defined in the header or define before the main() function.main() is a function where the program begin its execution.

NOTE: A function in the program must located above the main()function.

[type] [name]([parameter #1], [parameter #2], ...){

[statement(s)]}

Figure 42: Structure of a function

[parameter] = [type] [variable]

Figure 43: Structure of the parameter

1. [type] specify the data type of the return value of the function2. [name] is the name of the function3. OPTIONAL

[parameter] is similar to a variable declaration. Parameter describe the datatype of the input value(s) of the function

Page 36: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-36

Below is an example of a user-defined function.123456789

101112131415

#include <stdio.h>

int addition(int a, int b){

int r;r = a + b;return(r);

}

void main(){

int z;z = addition(5,3);printf("The result is %d", z);

}

Figure 44: Example of a program with a user-defined function

NOTE: A C program begin its execute at the main() function.Therefore, begin the program walkthrough at (line 10)

(line 3) int is the [type]

int indicates that the function will return an integer value back to the

main() function.

addition is the [name] of the functionint a is [parameter #1], that is 5, received from (line 13)

int b is [parameter #2], that is 3, received from (line 13)

(line 5) Define variable r as type Integer(line 6) Evaluate a + b, that is 5 + 3, then assign the result to r on the left

hand side of the equation

(line 7) return(r) is a return statement.

The value of r is returned to z, to the statement in the main() functionthat calls for the function. Go to (line 13)

(line 10) void is the [type]

void indicates that the function does not return a value.

main is the [name] of the functionmain function is where the program begin its execution.

(line 12) Define variable z as type Integer(line 13) addition(5,3) calls for a function named addition. Go to (line 3)

Two values, 5 and 3, are passed to the function.

z will be assigned with the value returned from additional function, thatis 3.

(line 14) printf("The result is %d", z);

Display The result is 8 on screen.

Page 37: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-37

3...

13

int addition(int a, int b)

z = addition( 5 , 3 );

Figure 45: A closer look at (line 13) to (line 3)

3...

13

int addition(int a, int b)

z = addition( 5 , 3 );

Figure 46: A closer look on at (line 3) to (line 13)

Functions with no Type

A function with no type is used when there is no need to return a value back to themain() function. For example, a function that shows a message on screen.

Refer to Figure 42, the structure of a function with no type is like a function withtypes. The differences are [type] is replaced with void and [parameter] is removed.

void [name](){

[statement(s)]}

Figure 47: Structure of a function with no Type

Below is an example of a user-defined function with no Type.123456789

1011

#include <stdio.h>

void printFUNC(){

printf("Hello World!");}

void main(){

printFUNC();}

Figure 48: : Example of a program with a user-defined function with no Type

(line 3) void is the [type]

void indicates that the function will not return a value back to the

main() function.

printFUNC is the [name] of the function() has no [parameter]

Once the function execute all statements, it returns to the main()function with returning a value. Go to (line 11)

Page 38: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

2-38

(line 5) printf("Hello World!");

Display The result is 8 on screen.

(line 8) void is the [type]

void indicates that the function does not return a value.

main is the [name] of the functionmain function is where the program begin its execution.

(line 10) printFUNC() calls for a function named printFUNC, without passing a

value. Go to (line 3)

Scope of Variables

Local variables are declared within the function. The variables can be use withinthat function only.

Global variables are declared outside the function. The variables can be usedthroughout the program.

Below is an example showing the scope of the variable.

Figure 49: Example of a program showing the scope of a variable

MAIN a = 0MAIN c = 10FUNC a = 1FUNC c = 10

Figure 50: Shows the output of the program above

123456789

10111213141516171819

#include <stdio.h>

int c = 10; GLOBAL VARIABLE

void printFUNC(){

int a = 1; LOCAL VARIABLEprintf("FUNC a = %d \n", a);printf("FUNC c = %d", c);

}

void main(){

int a = 0; LOCAL VARIABLEprintf("MAIN a = %d \n", a);printf("MAIN c = %d \n", c);

printFUNC();}

Page 39: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-39

Chapter 3: Setup

Components

The board components required for the setup.

Item List QuantityBoard: JX-2148 1Serial Port CX-232 Cable 1DC Adaptor: +6V 500mA (Maximum +9V) 1Serial to USB Converter (optional) 1

Table 10: Board Component List

Figure 51: Board components

System Requirements

A Computer Operating System: Window XP (Service Pack 2) At least 1 Serial Port OR Serial Port to USB converter Hyper Terminal installed

Check: START > All programs > Accessories >Communication > HyperTerminal

UART CH0(connected

to PC)

UART CH1

+4.5V-9V

ON/OFF

RESET

ISP SWITCH

Page 40: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-40

Software

Keil Version 3

DOWNLOAD: Keil ARM tool kit i.e. Keil Vision3(http://www.keil.com/demo/download.asp)

NOTE: Another optional program is mVisio3

5. Create a New ProjectGo to: Project > New Project

Figure 52: “Keil Version 3”

6. Input a project name

7. Select file extension as .uv2

8. Press Save

Page 41: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-41

Figure 53: “Create New Project”

5. Select CPUUnder CPU tab, find Philips, if that does not existed, look for NXP (foundedby Philips)

Page 42: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-42

Figure 54: “Select Device” (1)

6. Next find LPC2148

Page 43: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-43

7. Press OK

Figure 55: “Select Device” (2)

8. Press No

Figure 56: “Keil Version 3” alert box

9. Add files to source groupUnder Project Workspace, make sure that the File tab is selected

10. Click + at Target 1 then click right on Source Group 1

Page 44: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-44

11. Select “Add Files to Group ‘Source Group 1’ ”

Figure 57: Selecting “Add Files to Group ‘Source Group 1’”

12. Files of type: C Source file (*.c)

13. Select xxx.c and press Add once!

Page 45: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-45

Figure 58: Adding xxxx.c to Group ‘Source Group 1’

14. Adding Startup.s fileSelect Files of type: All files (*.*)

15. Select Startup.s

16. Press Add once!

Figure 59: Adding Startup.s to Group ‘Source Group 1’

17. Click + at Source Group 1

18. See if Startup.s and xxxx.c exist or not

Page 46: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-46

Figure 60: ‘Source Group 1’ files

19. Edit Source CodeTo edit the source code, double click on xxxx.c

Figure 61: Displaying xxxx.c source code for edition

20. Compilation SettingsProject > Manage > Components, Environment, Books...

Page 47: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-47

Figure 62: Selecting “Components, Environment, Books...”

21. Click on Folders/Extensions tab

22. Under Select ARM Development Tools > Select Use Keil CARM Compiler

23. Press OK

Figure 63: “Components, Environment and Books”

24. Press the icon next to Target 1

25. Click on Output tab

Page 48: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-48

26. Select Create Executable

27. Select Debug Information, Browse Information and Create HEX File

28. Press OK

Figure 64: “Options for Target ‘Target 1’ ”

29. To compile the programPress F7

30. Or go to Project > Build TargetNote: the .HEX will not be created if error(s) exist.

Figure 65: Compilation Message

Page 49: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-49

Philip Conductor Flash Utility LPC210x ISP

DOWNLOAD: LPC2000 Philips(www.nxp.com/products/microcontroller/support/software_download/lpc2000)

Figure 66: “LPC2000 Flash Utility” Communication Setting

NOTE: Serial Port 0 must be connected to the computer

1. Set CommunicationUnder CommunicationConnected to Port: COM1 / COM5 (for USB)Baud Rate: 9600

2. Press Reset and ISP switch

3. Under DeviceSet XTAL Freq. (kHz) to 12000

4. Press Read Device IDIf successful, the Device, Part ID and Boot Loader ID will appear.

Page 50: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-50

Figure 67: “LPC2000 Flash Utility” Device Setting

5. Select a file to uploadUnder Flash Programming

6. Press ... and browse for the file

7. Upload to FlashNoticed the Progress bar

Figure 68: “LPC2000 Flash Utility” Flash Programming Setting

Page 51: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-51

Hyper Terminal

NOTE: Close Philip Conductor Flash Utility LPC2148 program

1. Hyper Terminal SetupSTART > Accessories > Communications > Hyper Terminal

2. Select NO

Figure 69: “Default Telnet Program?” alert box

3. Select CANCEL

Figure 70: “Location Information” alert box

Page 52: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-52

4. Select YES

Figure 71: “Confirm Cancel” alert box

5. Select OK

Figure 72: “HyperTerminal”

6. Create a new connectionName: Keil

7. Select OK

Figure 73: “Connection Description”

NOTE: Repeat step 2 to 5 if necessary

8. Connect using: COM1 (if serial port is connected directly) or COMx (if connectthrough a USB to serial converter)

Page 53: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-53

9. Select OK

Figure 74: “Connect To”

10. Properties settingBits per second: 9600Data bits: 8Parity: NoneStop bits: 1Flow control: None

11. Select OK

Figure 75: “COMx Properties”

Page 54: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

3-54

12. To disconnect

Figure 76: Disconnect session

13. To reconnect

Figure 77: Connect session

14. Open existing connection configuration

15. Select OK

Figure 78: Open existing connection configuration, “Keil”

Page 55: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

4-55

Chapter 4: Output Port

Description of the Program

This program demonstrates the use of the General Purpose Input/Output (GPIO)port. In this example, there is no input, the outputs are the 2 LED (at Port P0.21and P0.22). Both LED blinks in sequence as defined within the program.

Pseudo Code

1. Initialize the boardinit();

2. Set SCS (System Control and Status flags)GPIO mode selection

SCS = 0x03;3. Set Control Direction

Select Port P0.21 & P0.22 as outputFIO0DIR = 0x00600000;

4. WHILE the program is runningo LED blink in sequence repeatedlyo Switch LED ON by writing 0 to the output

FIO0CLR = 0x00200000; (Port P0.21)FIO0CLR = 0x00400000; (Port P0.22)

o Switch LED OFF by writing 1 to the outputFIO0SET = 0x00200000; (Port P0.21)FIO0SET = 0x00400000; (Port P0.22)

Function(s) init

o The board initialization function delay_ms

o A delay FOR Loop

Page 56: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

4-56

Source Code

123456789

1011121314151617181920212223242526272829303132333435363738394041424344454647484950

#include "lpc214x.h"#define LED1_ON FIO0CLR = 0x00400000#define LED1_OFF FIO0SET = 0x00400000#define LED2_ON FIO0CLR = 0x00200000#define LED2_OFF FIO0SET = 0x00200000

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for(i=0; i<ms; i++)

for(j=0; j<6659; j++);}

void main(){

init();SCS = 0x03;

FIO0DIR |= 0x00600000;while(1){

LED1_ON;LED2_OFF;delay_ms(500);

LED2_ON;LED1_OFF;delay_ms(500);

}}

Figure 79: Example of an LED program

Page 57: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

4-57

Exercise

1. Display the LED in the following sequence

LED P0.21 ON LED P0.22 OFF Delay LED P0.22 ON LED P0.21 OFF Delay LED P0.21 ON Delay LED P0.21 OFF LED P0.22 OFF Delay

2. Display the LED in the following sequence

P0.21 P0.22

Page 58: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

5-58

Chapter 5: Input Port

Description of the Program

This program demonstrates the use of the General Purpose Input/Output (GPIO)port. In this example, the input is the switch (at Port P0.28) and the output is theLED (at Port P0.21). When the switch is press, the LED toggles.

Pseudo Code

1. Initialize the boardinit();

2. Set SCS (System Control and Status flags)GPIO mode selection

SCS = 0x03;3. Set Control Direction

Select Port P0.21 as outputFIO0DIR = 0x00200000;

4. Switch LED at Port P0.21 OFF by writing 1 to Port P0.21Write 1 to Port P0.21, selected output

FIO0SET = 0x00200000;5. WHILE the program is running

o Check 3 times IF switch at Port P0.28 is pressed or notif(inp0(28)==0)WHILE switch is pressedwhile(inp0(28)==0); IF YES, and IF switch is NOT pressed

if(inp0(28)==1)Toggle LED at Port P0.21

FIO0PIN = 0x00200000;o Check 3 times IF switch at Port P0.28 is NOT pressed or not

if(inp0(28)==0) IF YES, and IF switch is NOT pressed for 3 times

if(inp0(28)==1)BREAK WHILE loop

break;

Function(s)

inito The board initialization function

delay_mso A delay FOR Loop

inp0o A function that reads, from Port 0, an input from switch

Page 59: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

5-59

Source Code

123456789

101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657

#include "lpc214x.h"

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for(i=0; i<ms; i++)

for(j=0; j<6659; j++);}

char inp0(char _bit){

unsigned long c;c = 1<<_bit;FIO0DIR &= ~c;return((FIO0PIN & c)>>_bit);

}

void main(){

init();SCS = 0x03;FIO0DIR |= 0x00200000;FIO0SET |= 0x00200000;while(1){

if(inp0(28)==0){

delay_ms(1);if(inp0(28)==0){

delay_ms(1);if(inp0(28)==0){

while(inp0(28)==0);if(inp0(28)==1){

Page 60: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

5-60

58596061626364656667686970717273747576

FIO0PIN ^= (1<<21);delay_ms(1);if(inp0(28)==1){

delay_ms(1);if(inp0(28)==1){

while(inp0(28)==1){

break;}

}}

}}

}}

}}

Figure 80: Example of a SWITCH program

Exercise

1. Write a program that switch LED P0.22 ON when PRESS switch P0.29

Output at LED P0.22Select Port P0.22 as output

FIO0DIR = 0x00400000; Switch LED at Port P0.22 OFF by writing 1 to Port P0.22

Write 1 to Port P0.22, selected outputFIO0SET = 0x00400000;

Check if switch at P0.29 is PRESS or notif(inp0(29)==0);

o Switch LED at Port P0.22 ON by writing 0 to Port P0.22FIO0CLR = 0x00400000;

Page 61: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

6-61

Chapter 6: External Interrupt

Description of the Program

This program demonstrates the use of the external interrupt at PIN P0.07. In thisexample, the input is the external switch (that generates an external interrupt inputsignal when it is press) attached on the board and the outputs are the sound of theBuzzer and the LED (at Port P0.21). When the external switch is press, the LEDtoggles and the buzzer make a sound.

The switch must be connected as follow:

Figure 81: Switch Circuit

Figure 82: Switch Circuit Arrangement

Page 62: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

6-62

Pseudo Code

1. Initialize the boardinit();

2. Set SCS (System Control and Status flags)GPIO mode selection

SCS = 0x03;3. Initialize UART0, Baud rate 9600

uart0_init(9600);4. Set Control Direction

Select Port P0.1 & P0.21 as outputFIO0DIR = 0x00200001;

5. Switch LED at Port P0.21 OFF by writing 1 to Port P0.21Write 1 to Port P0.21, selected output

FIO0SET = 0x00200000;6. Set External Interrupt Mode

Set EXTMODE2 (EINT2) as edge sensitiveEXTMODE = 0x04;

Select active-low/falling-edge sensitive for EINT2EXTPOLAR = 0x00;

7. Set PIN function, set P0.7 as EINT2PINSEL0 = 0xC000;

8. Set Interrupt VectorAssign isr_int2 to Interrupt Vector Slot 0

VICVectAddr0 = (unsigned)isr_int2;Control Enable isr_int2 to Interrupt Vector 0

VICVectCntl0 = 0x20 | 16;Enable isr_int2 at Interrupt Vector 0

VICIntEnable |= 1 << 16;9. WHILE the program is running

o Service the Interrupt if there is one (whenever an interrupt occurs,the buzzer sound is ON).

Function(s) init

o The board initialization function delay_ms

o A delay FOR Loop isr_int2

o An Interrupt service routine; the program jumps to this function whenan interrupt occurs.

o If switch is PRESS, Toggle LEDSelect active-high/rising-edge sensitive for EINT2

EXTPOLAR = 0x04;o Buzzer Sound

Page 63: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

6-63

Source Code

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657

#include "lpc214x.h"#include "sound.h"#include "uart.h"

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for(i=0; i<ms; i++)

for(j=0; j<6659; j++);}

void isr_int2(void) __irq{

delay_ms(20);

if((EXTINT & 0x04)== 0x04){

beep();FIO0PIN ^= (1<<21);uart0_puts("BEEP!\r\n");EXTPOLAR = 0x04;

}

EXTINT |= 0x4;VICVectAddr = 0;

}

void main(){

init();SCS = 0x03;

uart0_init(9600);uart0_puts("Yo!\r\n");

FIO0DIR |= 0x00200001;FIO0SET |= (1<<21);

Page 64: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

6-64

58596061626364656667

EXTMODE |= 0x4;EXTPOLAR = 0x00;PINSEL0 |= 0xC000;

VICVectAddr0 = (unsigned)isr_int2;VICVectCntl0 = 0x20 | 16;VICIntEnable |= 1 << 16;

while(1);}

Figure 83: Example of an EXTERNAL INTERRUPT program

Exercise

1. Write a program that switch LED P0.22 ON when ENTER the Interrupt ServiceRoutine

(in Main function) Select Port P0.22 as output

FIO0DIR = 0x00400000; Switch LED at Port P0.22 OFF by writing 1 to Port P0.22

Write 1 to Port P0.22, selected outputFIO0SET = 0x00400000;

(in interrupt service routine)void isr_int2(void) __irq

o Switch ON LED at Port P0.22 after enteredFIO0CLR = 0x00400000;

Page 65: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

7-65

Chapter 7: UART0

Description of the Program

This program demonstrates the use of the Universal Asynchronous ReceiverTransmitter Port 0 (UART0). In this example, the program sends a string ofmessage (embedded within the program) to display it, as output, on the computerin Hyper Terminal. Note that the cable must connect to the terminal computer andUART0 during the program execution.

Pseudo Code

1. Initialize the boardinit();

2. Set SCS (System Control and Status flags)GPIO mode selection

SCS = 0x03;3. Initialize UART0, Baud rate 9600

uart0_init(9600);4. WHILE the program is running

o Display Hello World! PORT0

Function(s) init

o The board initialization function

Source Code

123456789

1011121314151617181920212223

#include "lpc214x.h"#include "uart.h"#include "stdio.h"

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

Page 66: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

7-66

2425262728293031323334353637

}

void main(){

init();SCS = 0x03;

uart0_init(9600);

while (1){

uart0_puts("Hello World! PORT0\r\n");}

}

Figure 84: Example of UART0 program

Exercise

1. Display number 10 9 8 7 6 5 4 3 2 1 repeatedlyHint: use FOR loop

o Define Bufferchar s[50];

o Send output of characters to UART0int i = 10;for (i = 10; i>0; i--){

sprintf(s,”%d”,i);uart0_puts(s);

}

2. Display number 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0 repeatedlyHint: use FOR loop

3. Display number 0 2 4 6 8 10 repeatedlyHint: use FOR loop

Page 67: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

8-67

Chapter 8: UART1

Description of the Program

This program demonstrates the use of the Universal Asynchronous ReceiverTransmitter Port 1 (UART1). In this example, the program echoes each characterpressed on the keyboard (connected at PS/2 on the board) and displays it, asoutput, on the computer in the Hyper Terminal. Note that the cable must connect tothe terminal computer and UART1 during the program execution.

Pseudo Code

1. Initialize the boardinit();

2. Set SCS (System Control and Status flags)GPIO mode selection

SCS = 0x03;3. Initialize UART1, Baud rate 9600

uart1_init(9600);4. Enable Interrupt at UART1

Enable RDA and THRE interruptsU1IER = 3;

5. Set PIN function, set P0.9 as RxD (UART1)PINSEL0 = (1<<18);

6. Set Interrupt VectorAssign isr_uart1 to Interrupt Vector Slot 0

VICVectAddr0 = (unsigned)isr_uart1;Control Enable isr_uart1 to Interrupt Vector 0

VICVectCntl0 = 0x20 | 7;Enable isr_uart1 at Interrupt Vector 0

VICIntEnable = 1 << 7;7. WHILE the program is running

o Echo character from Keyboard to Hyper Terminal screen

Function(s) init

o The board initialization function isr_uart1

o An Interrupt service routine; the program jumps to this function whenan interrupt occurs.

o Echo character from Keyboardo Reset Interrupt vector

Page 68: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

8-68

Source Code

123456789

101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657

#include "lpc214x.h"#include "uart.h"#include "stdio.h"#include "stdlib.h"

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void isr_uart1(void) __irq{

char msg;

if(((msg = U1IIR) & 0x01) == 0){

switch (msg & 0x0E){

case 0x04: while(!(U1LSR & 0x20));U1THR = U1RBR;break;

case 0x02: break;default: break;

}}VICVectAddr = 0;

}

void main(){

init();SCS = 0x03;

uart1_init(9600);U1IER = 3;PINSEL0 |= (1<<18);

VICVectAddr0 = (unsigned)isr_uart1;VICVectCntl0 = 0x20 | 7;VICIntEnable = 1 << 7;

Page 69: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

8-69

58596061

puts("Now test UART1 for echo character\n");puts("Press any key for test!\n");while(1);

}

Figure 85: Example of UART1 program

Exercise

1. Add more options to control LED P0.21

(in Main function) Select Port P0.21 as output

FIO0DIR = 0x00200000;

(in UART1 interrupt service routine)void isr_uart1(void) __irq

IF echo character (U1THR) equals ‘a’, switch LED P0.21 ONFIO0CLR = 0x00200000;

IF echo character (U1THR) equals ‘b’, switch LED P0.21 OFFFIO0SET = 0x00200000;

Hint: Use IF Statement and also, create a new variable to storethe value of U1RBR

Page 70: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

9-70

Chapter 9: Analog to Digital

Description of the Program

This program demonstrates the use of the Analog to Digital module. In this example,the program converts the input analog signal at Port P0.30 (A/D) to digital signal.The digital single values will be display, as output, on the Hyper Terminal screen.Additionally, if the converted value is in between 400-500, the LED at Port P0.21 isswitch on.

Pseudo Code

1. Initialize variables2. Initialize the board

init();3. Set SCS (System Control and Status flags)

GPIO mode selectionSCS = 0x03;

4. Initialize UART0, Baud rate 9600uart0_init(9600);

5. Set Control DirectionSelect Port P0.21 as output

FIO0DIR = 0x00200000;6. Switch LED at Port P0.21 OFF by writing 1 to Port P0.21

Write 1 to Port P0.21, selected outputFIO0SET = 0x00200000;

7. Set PIN function, set P0.30 as AD0.3PINSEL1 = 0x10000000;

8. Set A/D ControlAD0CR = 0x00210608;

9. WHILE the program is runningo Start conversion of ANALOG to DIGITAL signal

AD0CR |= 0x01000000;while ((AD0DR3 & 0x80000000) == 0);val = ((AD0DR3 >> 6) & 0x03FF);

o Display DIGITAL signal valueo Check IF the value of DIGITAL sign is in between 400-500 or not

if(adc<=500 && adc>=400) IF YES

Switch ON LED at Port P0.21

Function(s) init

o The board initialization function delay_ms

o A delay FOR Loop

Page 71: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

9-71

Source Code

123456789

10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758

#include "lpc214x.h"#include "stdio.h"#include "uart.h"

#define LED2_ON FIO0CLR = 0x00200000#define LED2_OFF FIO0SET = 0x00200000

int adc=0;

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400)) ;

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for (i = 0; i < ms; i++ )

for (j = 0; j < 6659; j++ );}

void main(){

int val=0;int a=0;char s[30];

init();SCS = 0x03;uart0_init(9600);

FIO0DIR |= 0x00200000;LED2_OFF;

PINSEL1 |= 0x10000000;

AD0CR = 0x00210608;

while (1){

AD0CR |= 0x01000000;while ((AD0DR3 & 0x80000000) == 0);

Page 72: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

9-72

596061626364656667686970717273747576777879808182

val = ((AD0DR3 >> 6) & 0x03FF);adc = val;

for(a=0; a <(adc/20); a++ ){

uart0_puts(" ");}

if(adc<=500 && adc>=400){

LED2_ON;delay_ms(100);LED2_OFF;delay_ms(100);LED2_ON;delay_ms(100);LED2_OFF;

}

sprintf(s,"[%d]\r\n",adc);uart0_puts(s);delay_ms(250);

}}

Figure 86: Example of an ANALOG to DIGITAL program

Exercise

1. Modify the example program above, so that when the DIGITAL SIGNAL range isin between 300-400, LED Port P0.22 will switch ON then OFF

Select Port P0.22 as outputFIO0DIR = 0x00400000;

Switch LED at Port P0.22 OFF by writing 1 to Port P0.22Write 1 to Port P0.22, selected output

FIO0SET = 0x00400000; IF adc between 300-400

if(adc<=400 && adc>=300)o IF YES

Switch OFF LED at Port P0.22FIO0SET = 0x00400000;

Delay Switch ON LED at Port P0.22

FIO0CLR = 0x00400000;

Page 73: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

10-73

Chapter 10: Real Time Clock (1)

Description of the Program

This program demonstrates the use of the Real Time Clock module. In this example,the program accesses the Real Time Clock values and display them on HyperTerminal as output.

In addition, an ALARM is enabled at the 3rd second, i.e. when the second is 3 (forexample, 01:09:03 or 04:34:03), the buzzer is switch on. Note that the cable mustconnect to the terminal computer and UART0 during the program execution.

Pseudo Code

1. Initialize variables2. Initialize the board

init();3. Set SCS (System Control and Status flags)

GPIO mode selectionSCS = 0x03;

4. Set Control DirectionSelect Port P0.22 as output

FIO0DIR = 0x00400000;5. Initialize UART0, Baud rate 9600

uart0_init(9600);6. Initialize RTC

Set Prescaler Integer valuePREINT = 0x00000392;

Set Prescaler Fraction valuePREFRAC = 0x00004380;

Select: an increment of the Second value generates an interruptCIIR = 0x00000001;

7. Initialize Alarm / TimerSet alarm SECOND value

ALSEC = 0x00000003;Enable alarm for SECOND

AMR = 0x000000FE;8. Set Interrupt Vector

Assign isr_rtc to Interrupt Vector Slot 13VICVectAddr13 = (unsigned)isr_rtc13;

Control Enable isr_rtc to Interrupt Vector 13VICVectCntl13 = 0x20 | 13;

Enable isr_rtc at Interrupt Vector 13VICIntEnable = 1 << 13;

9. WHILE the program is runningo Check IF an Interrupt has been generated or not

IF YESBuzzer soundSet BUZZER value as OFF

Page 74: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

10-74

Clear Interrupt vectoro Display Current Time on Hyper Terminal screen

Function(s) init

o The board initialization function delay_ms

o A delay FOR Loop isr_rtc

o An Interrupt service routine; the program jumps to this function whenan interrupt occurs.

o Check IF an Interrupt has NOT been generate LED ON /OFF

o Check IF an Interrupt has BEEN generate Set BUZZER value as ON

o Reset Interrupt vector

Source Code

123456789

10111213141516171819202122232425262728293031323334353637

#include "lpc214x.h"#include "sound.h"#include "stdio.h"#include "uart.h"#define LED1_ON FIO0CLR = 0x00400000#define LED1_OFF FIO0SET = 0x00400000char alarm = 0;

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for (i = 0; i < ms; i++ )

for (j = 0; j < 6659; j++ );}

void isr_rtc(void) __irq

Page 75: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

10-75

38394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798

{if(ILR & 0x01){

LED1_ON;delay_ms(100);LED1_OFF;ILR = 0x01;

}if(ILR & 0x02){

alarm = 1;ILR = 0x02;

}VICVectAddr = 0;

}

void main(){

char i=0;char s[50];

init();SCS = 0x03;FIO0DIR |= 0x00400000;uart0_init(9600);

PREINT = 0x00000392;PREFRAC = 0x00004380;CIIR = 0x00000001;

ALSEC = 0x00000003;AMR = 0x000000FE;CCR = 0x00000001;

VICVectAddr13 = (unsigned)isr_rtc;VICVectCntl13 = 0x20 | 13;VICIntEnable |= (1<<13);

while (1){

if(alarm==1){

beep();i++;if(i>10){

i=0;alarm = 0;

}}

if(HOUR < 10){

sprintf(s,"TIME: 0%d:",HOUR);}else{

sprintf(s,"TIME: %d:",HOUR);}uart0_puts(s);

Page 76: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

10-76

99100101102103104105106107108109110111112113114115116117118119120121

if(MIN < 10){

sprintf(s,"0%d:",MIN);}else{

sprintf(s,"%d:",MIN);}uart0_puts(s);

if(SEC < 10){

sprintf(s,"0%d\r",SEC);}else{

sprintf(s,"%d\r",SEC);}uart0_puts(s);

delay_ms(100);}

}

Figure 87: Example of a Real Time Clock (1) program

Exercise

1. On the previous example program. Change alarm to 20 second

Set alarm SECOND to 20 secondALSEC = 0x00000014;

2. Set minute alarm

Set alarm MINUTE value at 5 minuteALMIN = 0x00000005;

Enable alarm for MINUTEAMR = 0x000000FD;

3. Display Day of the month (DOM), month (MONTH) and year (YEAR)sprintf(s,"TIME: %d:%d:%d\r",DOM,MONTH,YEAR);

Page 77: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

11-77

Chapter 11: Real Time Clock (2)

Description of the Program

This program demonstrates the use of the Real Time Clock module. In this example,the program accesses the Real Time Clock values and display them on HyperTerminal as output. Also, a SET TIME option is available, press * on the keyboardto access it. Note that the cable must connect the terminal computer and UART1during the program execution.

Pseudo Code

1. Initialize variables2. Initialize the board

init();3. Set SCS (System Control and Status flags)

GPIO mode selectionSCS = 0x03;

4. Initialize UART1, Baud rate 9600uart1_init(9600);

Enable RDA and THRE interruptsU1IER = 3;

5. Set PIN function, set P0.7 as RxD (UART1)PINSEL0 = (1<<18);

6. Set Interrupt VectorAssign isr_uart1 to Interrupt Vector Slot 0

VICVectAddr0 = (unsigned)isr_uart1;Control Enable isr_uart1 to Interrupt Vector 0

VICVectCntl0 = 0x20 | 7;Enable isr_uart1 at Interrupt Vector 0

VICIntEnable = 1 << 7;7. Set Capture Control

CCR = 0x00000011;8. WHILE the program is running

o Display Current Time on Hyper Terminal screeno Check IF the character last hit on the keyboard is ‘*’ or not

IF YESReset keyboard character keySet Time

Function(s) init

o The board initialization function delay_ms

o A delay FOR Loop

Page 78: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

11-78

isr_uart1o An Interrupt service routine; the program jumps to this function when

an interrupt occurs.o Echo character from Keyboardo Reset Interrupt vector

isr_uart1_setupo Get 2 integers theno Update the value of the given addresso Reset Interrupt vector

Source Code

123456789

101112131415161718192021222324252627282930313233343536373839404142434445

#include "lpc214x.h"#include "stdio.h"#include "uart.h"#include "ctype.h"#include "stdlib.h"char key = 0;char s[30] = {0};

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for(i=0; i<ms; i++)

for(j=0; j<6659; j++);}

void isr_uart1(void) __irq{

char msg;if (((msg = U1IIR) & 0x01) == 0){

switch (msg & 0x0E){

case 0x04: while(!(U1LSR & 0x20));key = U1RBR;

Page 79: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

11-79

464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

100101102

break;case 0x02: break;default: break;

}}VICVectAddr = 0;

}

void rtc_uart1_setup(char *s){

unsigned char tm;char i = 0;for (i=0; i<2; i++){

while(!isdigit(key));if(i==0)

tm = 10 * atoi(&key);if(i==1)

tm = tm + atoi(&key);putchar(key);key = 0;

}*s = tm;

}

void main(){

init();SCS = 0x03;

uart1_init(9600);U1IER = 3;PINSEL0 |= (1<<18);

VICVectAddr0 = (unsigned)isr_uart1;VICVectCntl0 = 0x20 | 7;VICIntEnable |= 1 << 7;

CCR = 0x00000011;

while (1){

printf("TIME: %d:%d:%d \r",HOUR,MIN,SEC);delay_ms(100);if (key == '*'){

key = 0;printf("\nSet Time:");rtc_uart1_setup(&HOUR);uart1_putc(':');rtc_uart1_setup(&MIN);uart1_putc(':');rtc_uart1_setup(&SEC);printf("\nTIME: %d:%d:%d \r",HOUR,MIN,SEC);

}}

}

Figure 88: Example of a Real Time Clock (2) program

Page 80: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

11-80

Exercise

1. Add 1 more option to the previous example program; press ‘&’ to set DAY OFTHE MONTH (DOM)rtc_uart1_setup(&DOM);

Page 81: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

12-81

Chapter 12: Keyboard (1)

Description of the Program

This program demonstrates the use of an external Keyboard connected to the ARMprocessor at PS/2 Port. In this example, each character press on the keyboard isdisplay on the computer in the Hyper Terminal as output. Note that the cable mustconnect to the terminal computer and UART1 during the program execution.

Pseudo Code

1. Initialize the boardinit();

2. Set SCS (System Control and Status flags)GPIO mode selection

SCS = 0x03;3. Initialize the keyboard

kb_init();4. Initialize UART1, Baud rate 9600

uart1_init(9600);5. WHILE the program is running

o Display character from keyboard

Function(s) init

o The board initialization function

Source Code

123456789

10111213141516171819202122

#include "lpc214x.h"#include "stdio.h"#include "sound.h"#include "uart.h"#include "keyboard.h"char key = 0;

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

Page 82: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

12-82

232425262728293031323334353637383940

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void main(){

init();SCS = 0x03;kb_init();uart1_init(9600);

while (1){

printf("%c",kb_getchar());}

}

Figure 89: Example of a Keyboard (1) program

Exercise

1. On the previous program, change from UART1 to UART0

Initialize UART0;uart0_init(9600);

Send output to UART0 by:o Define Buffer

char s[50];o Send output of characters to UART0

sprintf(s,"%c",kb_getchar());uart0_puts(s);

Page 83: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

13-83

Chapter 13: Keyboard (2)

Description of the Program

This program demonstrates the use of an external Keyboard connected to the ARMprocessor, at PS/2 Port, to control the LED and buzzer (for further details see thePseudo Code below). Note that the cable must connect to the terminal computer andUART0 during the program execution.

Pseudo Code

1. Initialize variables2. Initialize the board

init();3. Set SCS (System Control and Status flags)

GPIO mode selectionSCS = 0x03;

4. Select Port P0.21 & P0.22 as outputFIO0DIR = 0x00600000;

5. Switch LED at Port P0.21 & P0.22 OFF, by writing 1 to Port P0.21 &P0.22. Write 1 to Port P0.21 & P0.22, selected output

FIO0SET = 0x00600000;6. Initialize the keyboard

kb_init();7. Initialize UART0, Baud rate 9600

uart0_init(9600);8. Display Menu9. WHILE the program is running

o Get character from keyboardo Check IF the character last hit on the keyboard is ‘a’ or not

IF YES Display Menu for LED Get character from keyboard

o IF character is ‘1’Switch LED at Port P0.21 ON

o IF character is ‘2’Switch LED at Port P0.21 OFF

o IF character is ‘3’Switch LED at Port P0.22 ON

o IF character is ‘4’Switch LED at Port P0.22 OFF

o IF character is ‘q’Back to Main Menu

o Check IF the character last hit on the keyboard is ‘b’ or not IF YES

Display Menu for BUZZER Get character from keyboard

o IF character is ‘1’Switch BUZZER ON

Page 84: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

13-84

o IF character is ‘q’Back to Main Menu

Function(s) init

o The board initialization function print_menu

o Print Menu delay_ms

o A delay FOR Loop isr_rtc

o An Interrupt service routine; the program jumps to this function whenan interrupt occurs.

o Check IF an Interrupt has NOT been generate LED ON /OFF

o Check IF an Interrupt has BEEN generate Set BUZZER value as ON

o Reset Interrupt vector rtc_kb_setup

o Get 2 integers theno Update the value of the given addresso Reset Interrupt vector

Source Code

123456789

101112131415161718192021222324252627282930

#include "lpc214x.h"#include "stdio.h"#include "sound.h"#include "uart.h"#include "keyboard.h"

#include "ctype.h"#include "stdlib.h"

#define LED1_ON FIO0CLR = 0x00400000#define LED1_OFF FIO0SET = 0x00400000#define LED2_ON FIO0CLR = 0x00200000#define LED2_OFF FIO0SET = 0x00200000

char alarm = 0;char key = 0;char s[30] = {0};int a = 1;

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

Page 85: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

13-85

31323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void print_menu(){

uart0_puts("A: LED\r\n");uart0_puts("B: SOUND\r\n");

}

void delay_ms(long ms){

long i,j;for (i = 0; i < ms; i++ )

for (j = 0; j < 6659; j++ );}

void isr_rtc(void) __irq{

if(ILR & 0x01){

LED1_ON;delay_ms(100);LED1_OFF;ILR = 0x01;

}if(ILR & 0x02){

alarm = 1;ILR = 0x02;

}VICVectAddr = 0;

}

void rtc_kb_setup(char *s){

unsigned char tm;char i = 0;char aa[30] = {0};char num = 0;for (i=0; i<2; i++){

while(!isdigit(num)){

num = kb_getchar();}if(i==0){

tm = 10 * atoi(&num);}if(i==1){

tm = tm + atoi(&num);}sprintf(aa,"%c",num);

Page 86: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

13-86

9293949596979899

100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152

uart0_puts(aa);num = 0;

}*s = tm;

}

void main(){

char ch = 0;init();SCS = 0x03;FIO0DIR |= 0x00600000;FIO0SET |= 0x00600000;kb_init();uart0_init(9600);

print_menu();

while (1){

ch = kb_getchar();if (ch == 'a'){

uart0_puts(" 1: P.21 ON\r\n 2: P.21 OFF\r\n");uart0_puts(" 3: P.22 ON\r\n 4: P.22 OFF\r\n");uart0_puts(" Q: BACK\r\n\n");

while (a == 1){

ch = kb_getchar();switch (ch){

case '1': LED2_ON;break;

case '2': LED2_OFF;break;

case '3': LED1_ON;break;

case '4': LED1_OFF;break;

default: uart0_puts(" ** PLEASE INPUT1,2,3,4 or Q\r");

break;}

if(ch == 'q'){

uart0_puts("\n\n");print_menu();a = 0;

}}

}else if (ch == 'b'){

uart0_puts(" 1: BEEP ON\r\n Q: BACK\r\n\n");

while (a == 1){

ch = kb_getchar();switch (ch)

Page 87: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

13-87

153154155156157158159160161162163164165166167168169170171

{case '1': beep();

break;default: uart0_puts(" ** PLEASE

INPUT 1 or Q\r");break;

}

if(ch == 'q'){

uart0_puts("\n\n");print_menu();a = 0;

}}

}a = 1;

}}

Figure 90: Example of a Keyboard (2) program

Exercise

1. Add more buttons for switching LED P0.21 ON and OFF

Press ‘5’ to switch LED P0.21 ON Press ‘6’ to switch LED P0.21 OFF

Page 88: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

14-88

Chapter 14: Real Time Clock and Keyboard

Description of the Program

This program demonstrates the use of the Real Time Clock module and thekeyboard. In this example, the program accesses the Real Time Clock values anddisplay them on Hyper Terminal, as output, and use it as reference for the interruptat every 1 second (interrupt LED at P0.22, by making it blink every 1 second).

In addition, a SET ALARM option is available, press 1 on the keyboard to ENABLE,press 2 to DISABLE and press 3 to set alarm interrupt second. Note that the cablemust connect to the terminal and UART1 during the program execution.

Pseudo Code

1. Initialize variables2. Initialize the board

init();3. Set SCS (System Control and Status flags)

GPIO mode selectionSCS = 0x03;

4. Initialize the keyboardkb_init();

5. Set Control DirectionSelect Port P0.22 as output

FIO0DIR = 0x00400000;6. Set Interrupt Vector

Assign isr_uart1 to Interrupt Vector Slot 5VICVectAddr5 = (unsigned)isr_uart1;

Control Enable isr_uart1 to Interrupt Vector 5VICVectCntl5 = 0x20 | 7;

Enable isr_uart1 at Interrupt Vector 5VICIntEnable = 1 << 7;

7. Initialize UART1, Baud rate 9600uart1_init(9600);

Enable RDA and THRE interruptsU1IER = 3;

8. Set PIN function, set P0.9 as RxD (UART1)PINSEL0 = (1<<18);

9. Initialize RTCSet Prescaler Integer value

PREINT = 0x00000392;Set Prescaler Fraction value

PREFRAC = 0x00004380;Select: an increment of the Second value generates an interrupt

CIIR = 0x00000001;10. Initialize Alarm / Timer

Set alarm SECOND valueALSEC = 0x00000003;

Page 89: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

14-89

Disable alarm for SECONDAMR = 0x000000FF;

11. Set Interrupt VectorAssign isr_rtc to Interrupt Vector Slot 13

VICVectAddr13 = (unsigned)isr_rtc13;Control Enable isr_rtc to Interrupt Vector 13

VICVectCntl13 = 0x20 | 13;Enable isr_rtc at Interrupt Vector 13

VICIntEnable = 1 << 13;12. Set Capture Control

CCR = 0x00000011;13. WHILE the program is running

o Display Current Time on Hyper Terminal screeno Check IF the character last hit on the keyboard is ‘1’ or not

IF YES Enable Alarm

AMR = 0x000000FE;o Check IF the character last hit on the keyboard is ‘2’ or not

IF YES Disable Alarm

AMR = 0x000000FE;o Check IF the character last hit on the keyboard is ‘3’ or not

IF YES Set Alarm

rtc_kb_setup(&ALSEC);o Check IF an Interrupt has been generated or not

IF YES (Alarm must be enable!)Buzzer soundClear Interrupt vector

Function(s) init

o The board initialization function delay_ms

o A delay FOR Loop isr_rtc

o An Interrupt service routine; the program jumps to this function whenan interrupt occurs.

o Check IF an Interrupt has NOT been generate LED ON /OFF

o Check IF an Interrupt has BEEN generate Set BUZZER value as ON

o Reset Interrupt vector rtc_kb_setup

o Get 2 integers theno Update the value of the given addresso Reset Interrupt vector

isr_uart1o An Interrupt service routine; the program jumps to this function when

an interrupt occurs.o Echo character from Keyboardo Reset Interrupt vector

Page 90: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

14-90

Source Code

123456789

10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758

#include "lpc214x.h"#include "sound.h"#include "stdio.h"#include "uart.h"#include "keyboard2.h"

#include "ctype.h"#include "stdlib.h"

#define LED1_ON FIO0CLR = 0x00400000#define LED1_OFF FIO0SET = 0x00400000#define LED0_ON FIO0CLR = 0x00200000#define LED0_OFF FIO0SET = 0x00200000

char alarm = 0;char s[30];char key = 0;

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for (i = 0; i < ms; i++ )

for (j = 0; j < 6659; j++ );}

void isr_rtc(void) __irq{

if(ILR & 0x01){

LED1_ON;delay_ms(100);LED1_OFF;ILR = 0x01;

}if(ILR & 0x02){

alarm = 1;ILR = 0x02;

Page 91: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

14-91

5960616263646566676869707172737475767778798081828384858687888990919293949596979899

100101102103104105106107108109110111112113114115116117118119

}VICVectAddr = 0;

}

void rtc_kb_setup(char *s){

unsigned char tm;char i = 0;for (i=0; i<2; i++){

while(!isdigit(key));if(i==0){

tm = 10 * atoi(&key);}if(i==1){

tm = tm + atoi(&key);}putchar(key);key = 0;

}*s = tm;

}

void isr_uart1(void) __irq{

char msg;if (((msg = U1IIR) & 0x01) == 0){

switch (msg & 0x0E){

case 0x04: while(!(U1LSR & 0x20));key = U1RBR;break;

case 0x02: break;default: break;

}}VICVectAddr = 0;

}

void main(){

char i=0;

init();SCS = 0x03;kb_init();

FIO0DIR |= 0x00400000;VICVectAddr5 = (unsigned)isr_uart1;VICVectCntl5 = 0x20 | 7;VICIntEnable |= 1 << 7;

uart1_init(9600);U1IER = 3;PINSEL0 |= (1<<18);PREINT = 0x00000392;PREFRAC = 0x00004380;

Page 92: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

14-92

120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166

CIIR = 0x00000001;ALSEC = 0x00000003;AMR = 0x000000FF;

VICVectAddr13 = (unsigned)isr_rtc;VICVectCntl13 = 0x20 | 13;VICIntEnable |= (1<<13);

CCR = 0x00000001;

while (1){

printf("%c TIME: %d:%d:%d\r",key,HOUR,MIN,SEC);delay_ms(100);

if (key == '1'){

key = 0;AMR = 0x000000FE;printf("\n[AMR=0xFE]\r\n");

}else if (key == '2'){

key = 0;AMR = 0x000000FF;printf("\n[AMR=0xFF]\r\n");

}else if (key == '3'){

key = 0;printf("\n\n Set ALARM Second: ");rtc_kb_setup(&ALSEC);AMR = 0x000000FE;

}

if(alarm==1){

beep();i++;if(i>10){

i=0;alarm = 0;

}}

}}

Figure 91: Example of a Keyboard (3) program

Page 93: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

14-93

Exercise

1. Add more options to the previous example program

Select: an increment of the Minute value generates an interruptCIIR = 0x00000003;

Enable Alarm for minuteAMR = 0x000000FD;

Disable Alarm for minuteAMR = 0x000000FD;

Set Alarm for minutertc_kb_setup(&ALMIN);

2. Change Port to UART0

(in main function) Initialize UART0, Baud rate 9600

uart0_init(9600);Enable RDA and THRE interrupts

U0IER = 3; Set PIN function, set P0.1 as RxD (UART0)

PINSEL0 = (1<<2); Select interrupt vector for UART0

VICVectCntl5 = 0x20 | 6;VICIntEnable |= 1 << 6;

(in UART0 interrupt service routine function) Rename the following variables

U1IIR to U0IIRU1LSR to U0LSRU1RBR to U0RBR

Page 94: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

15-94

Chapter 15: Analog to Digital and DIP Switch

Description of the Program

This program demonstrates the use of the Analogy to Digital converter module withan input signal from an outside source at Port P0.5. In this example, the programconverts the input analog signal at Port P0.5 (A/D) to digital signal. If theconverted digital value is above 200, the LED at P0.21 will blink. In addition, theconverted digital value is displayed on the Hyper Terminal monitor as output.

Pseudo Code

1. Initialize variables2. Initialize the board

init();3. Set SCS (System Control and Status flags)

GPIO mode selectionSCS = 0x03;

4. Set Control DirectionSelect LED Port P0.21 as output

FIO0DIR = 0x00200000;5. Switch LED at Port P0.21 OFF by writing 1 to Port P0.21

Write 1 to Port P0.21, selected outputFIO0SET = 0x00200000;

6. Set PIN function, set P0.5 as Analog to Digital converter port AD0.7PINSEL0 = 0x0C00;

7. Set A/D ControlAD0CR = 0x00210608;

8. WHILE the program is runningo Start conversion of ANALOG to DIGITAL signal

AD0CR |= 0x01000000;while ((AD0DR7 & 0x80000000) == 0);val = ((AD0DR7 >> 6) & 0x03FF);

o Check IF the value of DIGITAL sign is greater than 200 or notif(adc>200) IF YES

Switch ON/OFF LED at Port P0.21o Display DIGITAL signal value

Function(s) init

o The board initialization function delay_ms

o A delay FOR Loop

Page 95: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

15-95

Source Code

123456789

101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657

#include "lpc214x.h"#include "stdio.h"#include "sound.h"#include "uart.h"

#define LED2_ON FIO0CLR = 0x00200000#define LED2_OFF FIO0SET = 0x00200000

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for (i = 0; i < ms; i++ )

for (j = 0; j < 6659; j++ );}

void main(){

int val = 0;int adc = 0;int a = 0;char s[20] = {0};

init();SCS = 0x03;

uart0_init(9600);uart0_puts("Yo!\r\n");

FIO0DIR |= 0x00200000;FIO0SET |= (1<<21);

PINSEL0 |= 0x0C00; // Select PIN 0.5 as AD0.7AD0CR = 0x00210680;

while(1){

Page 96: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

15-96

585960616263646566676869707172737475767778798081828384

AD0CR |= 0x01000000;while ((AD0DR7 & 0x80000000) == 0);val = ((AD0DR7 >> 6) & 0x03FF);

adc = val;

for(a=0; a <(adc/20); a++ ){

uart0_puts(" ");}

if(adc > 200){

LED2_ON;delay_ms(100);LED2_OFF;delay_ms(100);LED2_ON;delay_ms(100);LED2_OFF;

}

sprintf(s,"[%d]\r\n",adc);uart0_puts(s);delay_ms(250);

}}

Figure 92: Example of DIP Switch (1) program

Page 97: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

16-97

Chapter 16: External Input (DIP Switch)

Description of the Program

This program demonstrates the use of the DIP switch as an external input to Port0 (at switch 1-4) and Port 1 (at switch 5-8). First, the program checks the inputs atPort 0 then Port 1. If the ‘IF condition’ is true (see Pseudo code below), an action isperformed on the LED, otherwise, no action will be perform.

The DIP switch must be connected as follow:

Figure 93: Switches Connection

Page 98: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

16-98

Figure 94: Switches Circuit Arrangement

(Starting from the left) Connect Switch 1 to P0.25 Connect Switch 2 to P1.19 Connect Switch 3 to P1.16 Connect Switch 4 to P0.4 Connect Switch 5 to P0.5 Connect Switch 6 to P0.6 Connect Switch 7 to P1.21 Connect Switch 8 to P1.22

NOTE: Set switch 2,4,6,8 as ON and switch 1,3,5,7 as OFF.

Pseudo Code

1. Initialize variables2. Initialize the board

init();3. Set SCS (System Control and Status flags)

GPIO mode selectionSCS = 0x03;

4. Set Control Direction

Page 99: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

16-99

Select LED Port P0.21 and Port P0.22 as outputFIO0DIR = 0x00600000;

Configure all Port P1.xx as inputFIO1DIR = 0x00000000;

5. Switch LED at Port P0.21 and Port P0.22 OFF by writing 1 to Port P0.21and Port P0.22 at the same time.Write 1 to Port P0.21 and Port P0.22, selected output

FIO0SET = 0x00600000;6. Set PIN function for all P0.xx and P1.xx as a General Input/Output Port

PINSEL0 = 0x00000000;PINSEL1 = 0x00000000;

7. WHILE the program is runningo Display P0 status on Hyper Terminal screen

sprintf(s,"%d\r\n",FIO0PIN);uart0_puts(s);

o Check IF input at switch 1 (connected to P0.25) is 1 or notif(inp0(25)==1) IF YES

Display A on Hyper Terminal screenSwitch ON LED at Port P0.22

FIO0CLR = 0x00400000;o Delay for 500 ms

delay_ms(500)o Check IF input at switch 4 (connected to P0.4) is 0 or not

if(inp1(4)==0) IF YES

Display B on Hyper Terminal screenSwitch OFF LED at Port P0.22

FIO0SET = 0x00400000;o Delay for 500 ms

delay_ms(500)o Check IF input at switch 5 (connected to P0.5) is 1 or not

if(inp0(5)==1) IF YES

Display C on Hyper Terminal screenSwitch ON LED at Port P0.21

FIO0CLR = 0x00200000;o Delay for 500 ms

delay_ms(500)o Check IF input at switch 6 (connected to P0.6) is 0 or not

if(inp1(6)==0) IF YES

Display D on Hyper Terminal screenSwitch OFF LED at Port P0.21

FIO0SET = 0x00200000;o Delay for 500 ms

delay_ms(500)o Check IF input at switch 2 (connected to P1.19) is 0 or not

if(inp1(19)==0) IF YES

Display AA on Hyper Terminal screenSwitch ON LED at Port P0.22

Page 100: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

16-100

FIO0CLR = 0x00400000;o Delay for 1000 ms

delay_ms(1000)o Check IF input at switch 8 (connected to P1.22) is 0 or not

if(inp1(22)==0) IF YES

Display BB on Hyper Terminal screenSwitch OFF LED at Port P0.22

FIO0SET = 0x00400000;o Delay for 1000 ms

delay_ms(1000)o Check IF input at switch 3 (connected to P1.16) is 1 or not

if(inp0(16)==1) IF YES

Display CC on Hyper Terminal screenSwitch ON LED at Port P0.21

FIO0CLR = 0x00200000;o Delay for 1000 ms

delay_ms(1000)o Check IF input at switch 7 (connected to P1.21) is 1 or not

if(inp1(21)==0) IF YES

Display DD on Hyper Terminal screenSwitch OFF LED at Port P0.21

FIO0SET = 0x00200000;o Delay for 1000 ms

delay_ms(1000)

Function(s) init

o The board initialization function delay_ms

o A delay FOR Loop inp0

o A function that reads, from Port 0, an input from switches inp1

o A function that reads, from Port 1, an input from switches

Source Code

123456789

101112

#include "lpc214x.h"#include "stdio.h"#include "sound.h"#include "uart.h"

#define LED2_ON FIO0CLR = 0x00200000#define LED2_OFF FIO0SET = 0x00200000

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;

Page 101: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

16-101

13141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273

PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for (i = 0; i < ms; i++ )

for (j = 0; j < 6659; j++ );}

char inp0(char _bit){

unsigned long c;c = 1<<_bit;FIO0DIR &= ~c;return((FIO0PIN & c)>>_bit);

}

char inp1(char _bit){

unsigned long d;d = 1<<_bit;FIO1DIR &= ~d;return((FIO1PIN & d)>>_bit);

}

void main(){

int val = 0;int adc = 0;int a = 0;char s[20] = {0};

init();SCS = 0x03;

uart0_init(9600);uart0_puts("Yo!\r\n");

FIO0DIR |= 0x00600000;FIO1DIR |= 0x00000000;

FIO0SET |= 0x00600000;

PINSEL0 |= 0x00000000;PINSEL1 |= 0x00000000;

Page 102: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

16-102

7475767778798081828384858687888990919293949596979899

100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132

while(1){

sprintf(s,"%d\r\n",FIO0PIN);uart0_puts(s);

/* PORT 0 */if(inp0(25)==1){

uart0_puts("A\r\n");FIO0CLR = 0x00400000;

}delay_ms(500);if(inp0(4)==0){

uart0_puts("B\r\n");FIO0SET = 0x00400000;

}delay_ms(500);if(inp0(5)==1){

uart0_puts("C\r\n");FIO0CLR = 0x00200000;

}delay_ms(500);if(inp0(6)==0){

uart0_puts("D\r\n");FIO0SET = 0x00200000;

}delay_ms(500);

/* PORT 1 */if(inp1(19)==0){

uart0_puts("AA\r\n");FIO0CLR = 0x00400000;

}delay_ms(1000);if(inp1(22)==0){

uart0_puts("BB\r\n");FIO0SET = 0x00400000;

}delay_ms(1000);if(inp1(16)==1){

uart0_puts("CC\r\n");FIO0CLR = 0x00200000;

}delay_ms(1000);if(inp1(21)==1){

uart0_puts("DD\r\n");FIO0SET = 0x00200000;

}delay_ms(1000);

}}

Figure 95: Example of DIP Switch (2) program

Page 103: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

17-103

Chapter 17: External Output (LEDs)

Description of the Program

This program demonstrates the use of the LEDs as an external output from Port 0and Port 1. If the output is 0 then LED is ON, where as 1, LED is OFF.

The LEDs must be connected as follow:

Figure 96: LED Circuit (1)

Page 104: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

17-104

Figure 97: LEDs Circuit (2)

Page 105: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

17-105

Figure 98: LEDs Circuit Arrangement

(Starting from the left) Connect Switch 1 to P0.25 Connect Switch 2 to P1.18 Connect Switch 3 to P1.16 Connect Switch 4 to P0.4 Connect Switch 5 to P0.5 Connect Switch 6 to P0.6 Connect Switch 7 to P1.21 Connect Switch 8 to P1.22

Pseudo Code

1. Initialize variables2. Initialize the board

init();3. Set SCS (System Control and Status flags)

GPIO mode selectionSCS = 0x03;

4. Set Control DirectionSelect LED Port P0.21 as output

FIO0DIR = 0x00200000;Select Port P0.4, Port P0.5, Port P0.6 and Port P0.25 as output

FIO0DIR = 0x02000070;Select Port P1.16, Port P1.18, Port P1.21 and Port P1.22 as output

FIO1DIR = 0x00650000;

Page 106: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

17-106

5. Switch LED at Port P0.21 OFF by writing 1 to Port P0.21.Write 1 to Port P0.21

FIO0SET = 0x00200000;6. WHILE the program is running

o Switch LED at Port P0.5 and Port P0.25 OFF by writing 1 to PortP0.5 and Port P0.25.Write 1 to Port P0.5 and Port P0.25.

FIO0SET = 0x02000020;o Switch LED at Port P1.16 and Port P1.21 OFF by writing 1 to

Port P1.16 and Port P1.21.Write 1 to Port P1.16 and Port P1.21.

FIO1SET = 0x00210000;o Switch LED at Port P0.4 and Port P0.6 ON by writing 0 to Port

P0.4 and Port P0.6.Write 0 to Port P0.4 and Port P0.6.

FIO0CLR = 0x00000050;o Switch LED at Port P1.18 and Port P1.22 ON by writing 0 to Port

P1.18 and Port P1.22.Write 0 to Port P1.18 and Port P1.22.

FIO1CLR = 0x00440000;o Delay for 500 ms

delay_ms(500)o Switch LED at Port P0.4 and Port P0.6 OFF by writing 1 to Port

P0.4 and Port P0.6.Write 1 to Port P0.4 and Port P0.6.

FIO0CLR = 0x00000050;o Switch LED at Port P1.18 and Port P1.22 OFF by writing 1 to

Port P1.18 and Port P1.22.Write 1 to Port P1.18 and Port P1.22.

FIO1CLR = 0x00440000;o Switch LED at Port P0.5 and Port P0.25 ON by writing 0 to Port

P0.5 and Port P0.25.Write 0 to Port P0.5 and Port P0.25.

FIO0SET = 0x02000020;o Switch LED at Port P1.16 and Port P1.21 ON by writing 0 to Port

P1.16 and Port P1.21.Write 0 to Port P1.16 and Port P1.21.

FIO1SET = 0x00210000;o Delay for 500 ms

delay_ms(500)

Function(s) init

o The board initialization function delay_ms

o A delay FOR Loop

Page 107: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

17-107

Source Code

123456789

101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657

#include "lpc214x.h"#include "stdio.h"#include "sound.h"#include "uart.h"

void init(){

PLL0CFG=0x24;PLL0FEED=0xAA;PLL0FEED=0x55;

PLL0CON=0x1;PLL0FEED=0xAA;PLL0FEED=0x55;

while(!(PLL0STAT & 0x400));

PLL0CON=0x3;PLL0FEED=0xAA;PLL0FEED=0x55;

MAMCR=0x2;MAMTIM=0x4;VPBDIV=0x02;

}

void delay_ms(long ms){

long i,j;for (i = 0; i < ms; i++ )

for (j = 0; j < 6659; j++ );}

void main(){

init();SCS = 0x03;

uart0_init(9600);uart0_puts("Yo!\r\n");

FIO0DIR |= 0x00200000;FIO0DIR |= 0x02000070;FIO1DIR |= 0x00650000;

FIO0SET |= 0x00200000;

while(1){

FIO0SET |= 0x02000020;FIO1SET |= 0x00210000;FIO0CLR |= 0x00000050;FIO1CLR |= 0x00440000;delay_ms(500);FIO0SET |= 0x00000050;FIO1SET |= 0x00440000;FIO0CLR |= 0x02000020;

Page 108: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

17-108

58596061

FIO1CLR |= 0x00210000;delay_ms(500);

}}

Figure 99; Example of DIP Switch (2) program

Page 109: ENG 32bit Micro Controller ARM7 LPC2148 Manual

© 2007 Apaporn Boonyarattaphan32-bit Microcontroller ARM7 LPC2148 Manual

109

References

Exforsys Inc (2007). C Programming – An Overview.Website: http://www.exforsys.com/tutorials/c-language/c-programming-an-overview.html

Holmes, S. (1995). C Programming Overview by Steve Holmes.Website: http://www.bringyou.to/games/CPROG.htm

IBM Corporation. (2003). IBM Mac OS X Compilers.Website: http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc07mainf.htm

iNEX. Philips’s LPC2148 32-bit ARM7 microcontroller Basic Experiment manual (คูมือทดลองไมโครคอนโทรลเลอร 32 บิตตระกลู ARM7 เบือ้งตน สาํหรับ LPC2148 ของ Philips)

Thailand: Innovative Experiment Inc.

Keil. (2007). Evaluate Software.Website: http://www.keil.com/demo/download.asp

Koninklijke Philips Electronics N.V. (2006). User Manual LPC214X.Website: http://www.standardics.nxp.com/support/documents/microcontrollers/pdf/user.manual.lpc2141.lpc2142.lpc2144.lpc2146.lpc2148.pdf