Вы находитесь на странице: 1из 109

32-bit Microcontroller ARM7

LPC2148

Manual
Contents

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

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

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


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

ARM FEATURES ................................................................................................................... 1-9


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

LPC2148 TECHNICAL FEATURES .................................................................................... 1-10


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

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


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

VARIABLES ......................................................................................................................... 2-18


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

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

© 2007 Apaporn Boonyarattaphan 2


32-bit Microcontroller ARM7 LPC2148 Manual
FORMATTED OUTPUT ....................................................................................................... 2-22
printf() .................................................................................................................... 2-22
sprintf()................................................................................................................... 2-22

EXPRESSIONS AND OPERATORS ................................................................................... 2-22


Arithmetic Operators ................................................................................................ 2-23
Relational Operators................................................................................................. 2-23
Logical Operators ..................................................................................................... 2-24
Precedence of Operators .......................................................................................... 2-24

ASSIGNMENT STATEMENT............................................................................................... 2-25


Compound Statements ............................................................................................. 2-25

CONTROL STATEMENTS .................................................................................................. 2-26


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

LOOPS ................................................................................................................................. 2-31


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

FUNCTIONS......................................................................................................................... 2-35
Functions 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-40
Philip 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

© 2007 Apaporn Boonyarattaphan 3


32-bit Microcontroller ARM7 LPC2148 Manual
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

© 2007 Apaporn Boonyarattaphan 4


32-bit Microcontroller ARM7 LPC2148 Manual
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

© 2007 Apaporn Boonyarattaphan 5


32-bit Microcontroller ARM7 LPC2148 Manual
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

© 2007 Apaporn Boonyarattaphan 6


32-bit Microcontroller ARM7 LPC2148 Manual
Editorial Notes

This manual is designed for a person who has no background knowledge on ARM7
32-bit Microcontroller or C programming, but wishes to program ARM7 processor in a
short period of time. The knowledge in C programming is not a prerequisite; the user
can 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 C
programming before hand to ensure more understanding. It is also recommended
that 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 with
descriptions on how it works (line by line), along with explanation of the programs
focus on the implementation on the board rather than its working mechanism. In
other words, the aim is, the user should be able to program the board using only the
source code examples as references. An in depth understanding of the
microprocessor 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 and
LPC2148 modules; a C Programming Tutorial, with examples provided; a step by
step guide on the setup procedure, how to upload the program on the board and the
installation of necessary programs; Example programs and exercises with
descriptions and explanations on LPC2148 modules; and lastly the manual
references.

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

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

© 2007 Apaporn Boonyarattaphan 7


32-bit Microcontroller ARM7 LPC2148 Manual
Acknowledgements

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

In particular, I thank:

Dr. Sawat Tantiphanwadi for giving me the opportunity to do self-study on


microcontroller 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 the
manual.

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 this
manual.

© 2007 Apaporn Boonyarattaphan 8


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 1: ARM7 and LPC2148
Background

ARM or Advanced RISC Machine, uses a 32-bit RISC (Reduced Instruction Set
Computer) 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, for
production. It was until 1990 that ARM Limited was established to support and
develop ARM processor.

ARM Features

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

ARM does not support memory-to-memory operations like a CISC (Complex


Instruction Set Computer) machine. The processor works as follow:

1. Data processing instructions: for dealing with the registers only


2. Data transfer instructions: for copying data from the memory to the
register or from the register to the memory
3. 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 Thumb
instruction 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

© 2007 Apaporn Boonyarattaphan 1-9


32-bit Microcontroller ARM7 LPC2148 Manual
During the execution of the 1st instruction, the 2nd instruction being decode and the
3rd instruction is being fetch. See the Figure 1 below.

1 2 3 4 5 6
Fetch
Decode
Execute
Figure 1: Pipelining

LPC2148 Technical Features

CPU and Data Bus


LPC2148 used ARM7TDMI-S as its core and two types of buses to increase the
board’s performance. The modules inside are connected by the CPU high-
performance bus called Advance High-Performance Bus (AHB) and the
peripheral are connected by VLSI Peripheral Bus (VPB). The data between the
two 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 Thumb
instruction set. The used of Thumb instruction set can reduced the size of the control
program 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 data
o 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 and
APB peripherals. Each peripheral has 16kb in size. Also, all peripheral register are
aligned to 32-bit (regardless of their original size). (See Table 1 on next page)

© 2007 Apaporn Boonyarattaphan 1-10


32-bit Microcontroller ARM7 LPC2148 Manual
Figure 2: System memory map (user program viewpoint)

APB Peripheral Base Address Peripheral Name


0 0xE000 0000 Watchdog timer
1 0xE000 4000 Timer 0
2 0xE000 8000 Timer 1
3 0xE000 C000 UART0
4 0xE001 0000 UART1
5 0xE001 4000 PWM
6 0xE001 8000 Not used
7 0xE001 C000 I2C0
8 0xE002 0000 SPI0
9 0xE002 4000 RTC
10 0xE002 8000 GPIO
11 0xE002 C000 Pin connect block
12 0xE003 0000 Not used
13 0xE003 4000 ADC0
14 - 22 0xE003 8000 Not used
0xE005 8000

© 2007 Apaporn Boonyarattaphan 1-11


32-bit Microcontroller ARM7 LPC2148 Manual
23 0xE005 C000 I2C1
24 0xE006 0000 ADC1
25 0xE006 4000 Not used
26 0xE006 8000 SSP
27 0xE006 C000 DAC
28 - 35 0xE007 0000 Not used
0xE008 C000
36 0xE009 0000 USB
37 - 126 0xE009 4000 Not used
0xE01F 8000
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, the
bulk remains at fixed position, thus, there is no need to design code to run at
different address range.

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

Vectored Interrupt Controller


Vectored Interrupt Controller (VIC) receives all interrupt signals initiated. The priority
of 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 types
o 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 the
values of all pins are reset, by default, it is set as input. Moreover, if the pins are
configured 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 channels
and ADC1 has 8 channels. The output is a 10-bit digital signal. The functional of this
module is as listed below:

© 2007 Apaporn Boonyarattaphan 1-12


32-bit Microcontroller ARM7 LPC2148 Manual
 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 of
this 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, the
endpoint buffer memory and DMA controller. The functional of this module is as
listed 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 support
program download through ISP process. UART1, on the other hand, is fully designed
as a parallel communication port. The baud rate of both ports can be configured
separately. 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’s
maximum data transfer rate is 400 Kbit/s.

© 2007 Apaporn Boonyarattaphan 1-13


32-bit Microcontroller ARM7 LPC2148 Manual
The I2C module can be configured as a master or slave mode. It also supported
multi-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 Synchronous
Serial Peripheral (SSP) can communication with SPI, SSI 4 lines and Microwire
Bus.

Timer / Counter Module


This module has 2 set of Timer/Counter: Timer0 and Timer1. The size of both
Timer/Counter is 32-bit. The timer uses the Clock peripheral signal (PCLK) or outer
source 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 an
interrupt (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 from
errors. It can be configured through a 32-bit timer and enabled through software
and reset through hardware control only.

Real Time Clock


LPC2148 has a Real Time Clock in itself that uses a 3V reserved battery. Unlike other
microprocessors, there is no need for an extra RTC IC. To read the RTC values, a
program 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 the
signal). The PCLK increment the Timer and the Master Reset register set the period
of the signal.

© 2007 Apaporn Boonyarattaphan 1-14


32-bit Microcontroller ARM7 LPC2148 Manual
In addition, the PWN signal can be configured as active high or low, thus, it can be
use to control a 3 phase motor. 2 MR register are in use: one to control the signal
cycle, the other for active edge configuration. The functional of this module is as
listed 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 then
convert 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, the
board setup itself. If the reset signal latches longer than the time taken for the board
to setup, once the signal is gone, the CPU starts immediately. However, if the reset
signal latches shorter than the time taken for the board to setup, once the signal is
gone, the board waits for the wake up time to finish its first loop before starting the
CPU (See Figure 3).

© 2007 Apaporn Boonyarattaphan 1-15


32-bit Microcontroller ARM7 LPC2148 Manual
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 interrupt
signal. 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 frequency
is 0.5-0.25 times lower than CPU clock) and to monitor the power on the bus, if the
peripheral 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 TRACE
port. Both ports use P1 therefore, if P1 in use, P0 should be use for work purpose
instead.

© 2007 Apaporn Boonyarattaphan 1-16


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 2: C Programming
Overview

C is a programming language invented by Dennis Ritchie. It is one type of structural


programming suitable for system programming. A C program begin its execute at the
main() function.

An execution is a program on its running stage.

Steps in Executing the C Program


1. Write a program in C language
2. Save it as .c extension
3. Compile the program
4. If necessary, make corrections to any errors found during compilation and re-
compile it
5. Run the program

Structure of a Program
A C program consisted of the following parts: the Header, the Global Variable(s), the
User-defined Function(s) and the Main Function. Out of these 4 parts, the Global
Variable(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 the
Main Function only.
1 #include <stdio.h>
2
3 main()
4 {
5 printf("Hello World!!");
6 }
Figure 5: A Simple C Program
(line 1) #include <stdio.h> is the Header
(line 3-6) main() function

© 2007 Apaporn Boonyarattaphan 2-17


32-bit Microcontroller ARM7 LPC2148 Manual
Header
Header files contain information on the data types and pre-made functions of the C
program. In C Programming, in order to use the pre-defined functions or initialized
variables in the program, prior information on those data types and functions must
be known (note that these information are stored in separate .h extension files, in
the 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 other
files. 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 Variables
NOTE: Please refer to Variables(page 2-18) and Scope of Variables
(page 2-38)

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

Main Function
A main function is a must have function in every program. It marks the entry point
where 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 remains
unchanged throughout the program execution.

© 2007 Apaporn Boonyarattaphan 2-18


32-bit Microcontroller ARM7 LPC2148 Manual
Types of Variables
Types define the type of the variables.
Type Meaning
int A Integer
float A Decimal
double 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 Meaning
short A reduce Integer range
long An increase Integer range
unsigned A positive-only Integer range
unsigned 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 Validity
0078 NO
1cat NO
cat YES
cat1 YES
cat_ NO
cat_1 YES
Table 4: Examples of variable names

© 2007 Apaporn Boonyarattaphan 2-19


32-bit Microcontroller ARM7 LPC2148 Manual
Reserved Keywords
Reserved keywords are names of the variables that cannot be use.
auto break case char continue default
do double else enum extern float
for goto if int long register
return short sizeof static struct switch
typedef 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 variable
3. [name] is the name of a variable
4. OPTIONAL
= is an assignment.
5. OPTIONAL
[value] is the value of the variable
6. ; 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] of
the 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 variable
2. [value] is the value of the constant variable

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

© 2007 Apaporn Boonyarattaphan 2-20


32-bit Microcontroller ARM7 LPC2148 Manual
#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 of
the variable.

Arrays

An array is a collection of variables of the same type. In C Programming, an array


begins at position 0. An array can have up to 0 to n dimensions; however, the most
frequent 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 array
3. [name] is the name of the array
4. [row] is the number of rows on the array
5. [column] is number of the columns on the array
6. OPTIONAL
= is an assignment
[value] is the value of the variables within the array

© 2007 Apaporn Boonyarattaphan 2-21


32-bit Microcontroller ARM7 LPC2148 Manual
Array Initialization
int 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 the
output 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. For
example, 9, 3, 4, 56, 17, 8, a, g, s, l and so on.

© 2007 Apaporn Boonyarattaphan 2-22


32-bit Microcontroller ARM7 LPC2148 Manual
Operators are notations that represents arithmetic operations, for instance, addition
(+), subtraction (-), multiplication (*) or division (/). It tells what operations should
be 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 basic
structure 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 be
evaluate 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 = is
the assignment symbol.

© 2007 Apaporn Boonyarattaphan 2-23


32-bit Microcontroller ARM7 LPC2148 Manual
Logical Operators
Symbol Meaning
&& And
|| Or
! Not
Table 8: Logical operation symbols
Logical operator is used to combine relational operators. See example on Figure 22
below.

(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 of
Description Representations
Precedence
1 Parenthesis () []
2 (Unary) Not !
Negative Sign –
Increment Value by 1 ++
3 Multiplication *
Division /
Modulus %
4 Addition +
Subtraction -
5 Shift Right >>
Shift Left <<
6 Greater Than >
Less Than <
Greater Than or Equal To >=
Less Than or Equal To <=
7 Equal ==
Not 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

© 2007 Apaporn Boonyarattaphan 2-24


32-bit Microcontroller ARM7 LPC2148 Manual
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 right


hand 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.


1 #include <stdio.h>
2
3 main()
4 {
5 int a = 0;
6 int b = 7;
7 int c = 3;
8 a = b + c;
9 printf(“%d”,a);
10 }
Figure 25: Example of how an assignment statement works
(line 5-7) Define variables a, b, c as type Integer
Assign 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 left
hand 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

© 2007 Apaporn Boonyarattaphan 2-25


32-bit Microcontroller ARM7 LPC2148 Manual
Control Statements

One characteristics of a program is it executes all statements in sequence, meaning


that parts of the program cannot be skipped. The whole program must run from top
to the bottom in step by step. Thus, the program is not flexible in terms of solving
complex problems.

In order to increase the flexibility of the program, control statements are


introduced to control the order of statements execution, allowing some statements
within the program to be skipped.

IF statement
if([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.


1 #include <stdio.h>
2
3 main()
4 {
5 int a=0;
6 if(a==0)
7 {
8 printf("Hello World!!");
9 }
10 }
Figure 28: Example of an IF statement
(line 5) Define variable a as type Integer
Assign 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 TRUE


If 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.

© 2007 Apaporn Boonyarattaphan 2-26


32-bit Microcontroller ARM7 LPC2148 Manual
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 execute


together

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


1 #include <stdio.h>
2
3 main()
4 {
5 int a=0;
6 if(a==0)
7 {
8 printf("Hello World!!");
9 }
10 else
11 {
12 printf("Hello People!!");
13 }
14 }
Figure 30: Example of an IF ELSE statement (1)
(line 5) Define variable a as type Integer
Assign 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 TRUE


If 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, “Hello


People!!” is display on screen.

© 2007 Apaporn Boonyarattaphan 2-27


32-bit Microcontroller ARM7 LPC2148 Manual
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]
}

© 2007 Apaporn Boonyarattaphan 2-28


32-bit Microcontroller ARM7 LPC2148 Manual
Below is an example of an IF ELSE statement (2).
1 #include <stdio.h>
2
3 main()
4 {
5 int a=0;
6 if(a==0)
7 {
8 printf("Hello World!!");
9 }
10 else if(a==1)
11 {
12 printf("Hello People!!");
13 }
14 else
15 {
16 printf("Hello Aliens!!");
17 }
18 }
Figure 32: Example of an IF ELSE statement (2)
(line 5) Define variable a as type Integer
Assign 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 TRUE


If 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 of
the 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 TRUE


If 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 of
the 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 “Hello
People!!” when a equals 1, otherwise, “Hello Aliens!!” is display on screen.

© 2007 Apaporn Boonyarattaphan 2-29


32-bit Microcontroller ARM7 LPC2148 Manual
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.


1 #include <stdio.h>
2
3 main()
4 {
5 int number=1;
6 switch(number)
7 {
8 case 1 : printf("One");
9 break;
10 case 2 : printf("Two");
11 break;
12 case 3 : printf("Three");
13 break;
14 default : printf("None");
15 break;
16 }
17 }
Figure 34: Example of an IF ELSE statement (2)
(line 5) Define variable number as Integer
Assign 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

© 2007 Apaporn Boonyarattaphan 2-30


32-bit Microcontroller ARM7 LPC2148 Manual
(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 equals
2 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, meaning


that parts of the program cannot be repeated. The whole program must from top to
the bottom in step by step. Thus, the program is not flexible in terms of solving
complex problems.

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

WHILE Loop
while([condition])
{
[statement(s)]
}
Figure 35: Structure of a WHILE loop
1. If [condition] is TRUE then execute [statement(s)] until [condition] becomes
FALSE 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 be


executed forever.

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

© 2007 Apaporn Boonyarattaphan 2-31


32-bit Microcontroller ARM7 LPC2148 Manual
Below is an example of a WHILE loop.
1 #include <stdio.h>
2
3 main()
4 {
5 while(1)
6 {
7 printf("Hello World!!");
8 }
9 }
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 Loop
do
{
[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 be


executed forever.

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

Below is an example of a DO WHILE loop.


1 #include <stdio.h>
2
3 main()
4 {
5 do
6 {
7 printf("Hello World!!");
8 }
9 while(1)
10 }
Figure 38: Example of a DO WHILE loop

© 2007 Apaporn Boonyarattaphan 2-32


32-bit Microcontroller ARM7 LPC2148 Manual
(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 count
number 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 [exit


condition] will never be FALSE, the FOR loop becomes an infinite
loop.

Below is an example of a FOR loop.


1 #include <stdio.h>
2
3 main()
4 {
5 for(int i=0; i<10; i++)
6 {
7 printf("Hello World!!");
8 }
9 }
Figure 40: Example of a FOR loop

© 2007 Apaporn Boonyarattaphan 2-33


32-bit Microcontroller ARM7 LPC2148 Manual
(line 5) int i=0; is the [initialization]
Define variable i as type Integer
Assign 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 not
If 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 WHILE
loop, 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.
1 #include <stdio.h>
2
3 main()
4 {
5 int a[5][10]={0};
6 for(int i=0; i<5; i++)
7 {
8 for(int j=0; j<10; j++)
9 {
10 a[i][j] = i*j;
11 }
12 }
13 }
Figure 41: Example of a Nested Loop
(line 5) Define a as a 2-D array of type Integer
Dimension of a is 5 by 10
Assign 0 to all elements of a
(line 6) int i=0; is the [initialization]
Define variable i as type Integer
Assign 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 not
If 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

© 2007 Apaporn Boonyarattaphan 2-34


32-bit Microcontroller ARM7 LPC2148 Manual
(line 8) int j=0; is the [initialization]
Define variable j as type Integer
Assign 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 not
If 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 j
Display 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 function
2. [name] is the name of the function
3. OPTIONAL
[parameter] is similar to a variable declaration. Parameter describe the data
type of the input value(s) of the function

© 2007 Apaporn Boonyarattaphan 2-35


32-bit Microcontroller ARM7 LPC2148 Manual
Below is an example of a user-defined function.
1 #include <stdio.h>
2
3 int addition(int a, int b)
4 {
5 int r;
6 r = a + b;
7 return(r);
8 }
9
10 void main()
11 {
12 int z;
13 z = addition(5,3);
14 printf("The result is %d", z);
15 }
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 function


int 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() function
that 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 function


main 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, that
is 3.
(line 14) printf("The result is %d", z);
Display The result is 8 on screen.

© 2007 Apaporn Boonyarattaphan 2-36


32-bit Microcontroller ARM7 LPC2148 Manual
3 int addition(int a, int b)
.
.
.
13 z = addition( 5 , 3 );

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

3 int addition(int a, int b)


.
.
.
13 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 the
main() 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 with
types. 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.


1 #include <stdio.h>
2
3 void printFUNC()
4 {
5 printf("Hello World!");
6 }
7
8 void main()
9 {
10 printFUNC();
11 }
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)

© 2007 Apaporn Boonyarattaphan 2-37


32-bit Microcontroller ARM7 LPC2148 Manual
(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 function


main 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 within
that function only.

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

Below is an example showing the scope of the variable.


1 #include <stdio.h>
2
3 int c = 10; GLOBAL VARIABLE
4
5 void printFUNC()
6 {
7 int a = 1; LOCAL VARIABLE
8 printf("FUNC a = %d \n", a);
9 printf("FUNC c = %d", c);
10 }
11
12 void main()
13 {
14 int a = 0; LOCAL VARIABLE
15 printf("MAIN a = %d \n", a);
16 printf("MAIN c = %d \n", c);
17
18 printFUNC();
19 }
Figure 49: Example of a program showing the scope of a variable

MAIN a = 0
MAIN c = 10
FUNC a = 1
FUNC c = 10
Figure 50: Shows the output of the program above

© 2007 Apaporn Boonyarattaphan 2-38


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 3: Setup
Components

The board components required for the setup.


Item List Quantity
Board: JX-2148 1
Serial Port CX-232 Cable 1
DC Adaptor: +6V 500mA (Maximum +9V) 1
Serial to USB Converter (optional) 1
Table 10: Board Component List
RESET
+4.5V-9V

ON/OFF UART CH1

UART CH0
(connected
to PC)

ISP SWITCH
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

© 2007 Apaporn Boonyarattaphan 3-39


32-bit Microcontroller ARM7 LPC2148 Manual
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 Project


Go to: Project > New Project

Figure 52: “Keil Version 3”

6. Input a project name

7. Select file extension as .uv2

8. Press Save

© 2007 Apaporn Boonyarattaphan 3-40


32-bit Microcontroller ARM7 LPC2148 Manual
Figure 53: “Create New Project”

5. Select CPU
Under CPU tab, find Philips, if that does not existed, look for NXP (founded
by Philips)

© 2007 Apaporn Boonyarattaphan 3-41


32-bit Microcontroller ARM7 LPC2148 Manual
Figure 54: “Select Device” (1)

6. Next find LPC2148

© 2007 Apaporn Boonyarattaphan 3-42


32-bit Microcontroller ARM7 LPC2148 Manual
7. Press OK

Figure 55: “Select Device” (2)

8. Press No

Figure 56: “Keil Version 3” alert box

9. Add files to source group


Under Project Workspace, make sure that the File tab is selected

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

© 2007 Apaporn Boonyarattaphan 3-43


32-bit Microcontroller ARM7 LPC2148 Manual
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!

© 2007 Apaporn Boonyarattaphan 3-44


32-bit Microcontroller ARM7 LPC2148 Manual
Figure 58: Adding xxxx.c to Group ‘Source Group 1’
14. Adding Startup.s file
Select 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

© 2007 Apaporn Boonyarattaphan 3-45


32-bit Microcontroller ARM7 LPC2148 Manual
Figure 60: ‘Source Group 1’ files

19. Edit Source Code


To edit the source code, double click on xxxx.c

Figure 61: Displaying xxxx.c source code for edition

20. Compilation Settings


Project > Manage > Components, Environment, Books...

© 2007 Apaporn Boonyarattaphan 3-46


32-bit Microcontroller ARM7 LPC2148 Manual
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

© 2007 Apaporn Boonyarattaphan 3-47


32-bit Microcontroller ARM7 LPC2148 Manual
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 program


Press F7

30. Or go to Project > Build Target


Note: the .HEX will not be created if error(s) exist.

Figure 65: Compilation Message

© 2007 Apaporn Boonyarattaphan 3-48


32-bit Microcontroller ARM7 LPC2148 Manual
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 Communication
Under Communication
Connected to Port: COM1 / COM5 (for USB)
Baud Rate: 9600

2. Press Reset and ISP switch

3. Under Device
Set XTAL Freq. (kHz) to 12000

4. Press Read Device ID


If successful, the Device, Part ID and Boot Loader ID will appear.

© 2007 Apaporn Boonyarattaphan 3-49


32-bit Microcontroller ARM7 LPC2148 Manual
Figure 67: “LPC2000 Flash Utility” Device Setting

5. Select a file to upload


Under Flash Programming

6. Press ... and browse for the file

7. Upload to Flash
Noticed the Progress bar

Figure 68: “LPC2000 Flash Utility” Flash Programming Setting

© 2007 Apaporn Boonyarattaphan 3-50


32-bit Microcontroller ARM7 LPC2148 Manual
Hyper Terminal

NOTE: Close Philip Conductor Flash Utility LPC2148 program

1. Hyper Terminal Setup


START > Accessories > Communications > Hyper Terminal

2. Select NO

Figure 69: “Default Telnet Program?” alert box

3. Select CANCEL

Figure 70: “Location Information” alert box

© 2007 Apaporn Boonyarattaphan 3-51


32-bit Microcontroller ARM7 LPC2148 Manual
4. Select YES

Figure 71: “Confirm Cancel” alert box

5. Select OK

Figure 72: “HyperTerminal”

6. Create a new connection


Name: 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 connect
through a USB to serial converter)

© 2007 Apaporn Boonyarattaphan 3-52


32-bit Microcontroller ARM7 LPC2148 Manual
9. Select OK

Figure 74: “Connect To”

10. Properties setting


Bits per second: 9600
Data bits: 8
Parity: None
Stop bits: 1
Flow control: None

11. Select OK

Figure 75: “COMx Properties”

© 2007 Apaporn Boonyarattaphan 3-53


32-bit Microcontroller ARM7 LPC2148 Manual
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”

© 2007 Apaporn Boonyarattaphan 3-54


32-bit Microcontroller ARM7 LPC2148 Manual
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.21
and P0.22). Both LED blinks in sequence as defined within the program.

Pseudo Code

1. Initialize the board


init();
2. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
3. Set Control Direction
Select Port P0.21 & P0.22 as output
FIO0DIR = 0x00600000;
4. WHILE the program is running
o LED blink in sequence repeatedly
o 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 output
FIO0SET = 0x00200000; (Port P0.21)
FIO0SET = 0x00400000; (Port P0.22)

Function(s)
 init
o The board initialization function
 delay_ms
o A delay FOR Loop

© 2007 Apaporn Boonyarattaphan 4-55


32-bit Microcontroller ARM7 LPC2148 Manual
Source Code

1 #include "lpc214x.h"
2 #define LED1_ON FIO0CLR = 0x00400000
3 #define LED1_OFF FIO0SET = 0x00400000
4 #define LED2_ON FIO0CLR = 0x00200000
5 #define LED2_OFF FIO0SET = 0x00200000
6
7 void init()
8 {
9 PLL0CFG=0x24;
10 PLL0FEED=0xAA;
11 PLL0FEED=0x55;
12
13 PLL0CON=0x1;
14 PLL0FEED=0xAA;
15 PLL0FEED=0x55;
16
17 while(!(PLL0STAT & 0x400));
18 PLL0CON=0x3;
19 PLL0FEED=0xAA;
20 PLL0FEED=0x55;
21
22 MAMCR=0x2;
23 MAMTIM=0x4;
24 VPBDIV=0x02;
25 }
26
27 void delay_ms(long ms)
28 {
29 long i,j;
30 for(i=0; i<ms; i++)
31 for(j=0; j<6659; j++);
32 }
33
34 void main()
35 {
36 init();
37 SCS = 0x03;
38
39 FIO0DIR |= 0x00600000;
40 while(1)
41 {
42 LED1_ON;
43 LED2_OFF;
44 delay_ms(500);
45
46 LED2_ON;
47 LED1_OFF;
48 delay_ms(500);
49 }
50 }
Figure 79: Example of an LED program

© 2007 Apaporn Boonyarattaphan 4-56


32-bit Microcontroller ARM7 LPC2148 Manual
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
 
 
 
 
 
 
 
 

© 2007 Apaporn Boonyarattaphan 4-57


32-bit Microcontroller ARM7 LPC2148 Manual
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 the
LED (at Port P0.21). When the switch is press, the LED toggles.

Pseudo Code

1. Initialize the board


init();
2. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
3. Set Control Direction
Select Port P0.21 as output
FIO0DIR = 0x00200000;
4. Switch LED at Port P0.21 OFF by writing 1 to Port P0.21
Write 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 not
if(inp0(28)==0)
WHILE switch is pressed
while(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)
 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 switch

© 2007 Apaporn Boonyarattaphan 5-58


32-bit Microcontroller ARM7 LPC2148 Manual
Source Code

1 #include "lpc214x.h"
2
3 void init()
4 {
5 PLL0CFG=0x24;
6 PLL0FEED=0xAA;
7 PLL0FEED=0x55;
8
9 PLL0CON=0x1;
10 PLL0FEED=0xAA;
11 PLL0FEED=0x55;
12
13 while(!(PLL0STAT & 0x400));
14
15 PLL0CON=0x3;
16 PLL0FEED=0xAA;
17 PLL0FEED=0x55;
18
19 MAMCR=0x2;
20 MAMTIM=0x4;
21 VPBDIV=0x02;
22 }
23
24 void delay_ms(long ms)
25 {
26 long i,j;
27 for(i=0; i<ms; i++)
28 for(j=0; j<6659; j++);
29 }
30
31 char inp0(char _bit)
32 {
33 unsigned long c;
34 c = 1<<_bit;
35 FIO0DIR &= ~c;
36 return((FIO0PIN & c)>>_bit);
37 }
38
39 void main()
40 {
41 init();
42 SCS = 0x03;
43 FIO0DIR |= 0x00200000;
44 FIO0SET |= 0x00200000;
45 while(1)
46 {
47 if(inp0(28)==0)
48 {
49 delay_ms(1);
50 if(inp0(28)==0)
51 {
52 delay_ms(1);
53 if(inp0(28)==0)
54 {
55 while(inp0(28)==0);
56 if(inp0(28)==1)
57 {

© 2007 Apaporn Boonyarattaphan 5-59


32-bit Microcontroller ARM7 LPC2148 Manual
58 FIO0PIN ^= (1<<21);
59 delay_ms(1);
60 if(inp0(28)==1)
61 {
62 delay_ms(1);
63 if(inp0(28)==1)
64 {
65 while(inp0(28)==1)
66 {
67 break;
68 }
69 }
70 }
71 }
72 }
73 }
74 }
75 }
76 }
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.22


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 output
FIO0SET = 0x00400000;
 Check if switch at P0.29 is PRESS or not
if(inp0(29)==0);
o Switch LED at Port P0.22 ON by writing 0 to Port P0.22
FIO0CLR = 0x00400000;

© 2007 Apaporn Boonyarattaphan 5-60


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 6: External Interrupt
Description of the Program

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

The switch must be connected as follow:

Figure 81: Switch Circuit

Figure 82: Switch Circuit Arrangement

© 2007 Apaporn Boonyarattaphan 6-61


32-bit Microcontroller ARM7 LPC2148 Manual
Pseudo Code

1. Initialize the board


init();
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 output
FIO0DIR = 0x00200001;
5. Switch LED at Port P0.21 OFF by writing 1 to Port P0.21
Write 1 to Port P0.21, selected output
FIO0SET = 0x00200000;
6. Set External Interrupt Mode
Set EXTMODE2 (EINT2) as edge sensitive
EXTMODE = 0x04;
Select active-low/falling-edge sensitive for EINT2
EXTPOLAR = 0x00;
7. Set PIN function, set P0.7 as EINT2
PINSEL0 = 0xC000;
8. Set Interrupt Vector
Assign 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 when
an interrupt occurs.
o If switch is PRESS, Toggle LED
Select active-high/rising-edge sensitive for EINT2
EXTPOLAR = 0x04;
o Buzzer Sound

© 2007 Apaporn Boonyarattaphan 6-62


32-bit Microcontroller ARM7 LPC2148 Manual
Source Code

1 #include "lpc214x.h"
2 #include "sound.h"
3 #include "uart.h"
4
5 void init()
6 {
7 PLL0CFG=0x24;
8 PLL0FEED=0xAA;
9 PLL0FEED=0x55;
10
11 PLL0CON=0x1;
12 PLL0FEED=0xAA;
13 PLL0FEED=0x55;
14
15 while(!(PLL0STAT & 0x400));
16 PLL0CON=0x3;
17 PLL0FEED=0xAA;
18 PLL0FEED=0x55;
19
20 MAMCR=0x2;
21 MAMTIM=0x4;
22 VPBDIV=0x02;
23 }
24
25 void delay_ms(long ms)
26 {
27 long i,j;
28 for(i=0; i<ms; i++)
29 for(j=0; j<6659; j++);
30 }
31
32 void isr_int2(void) __irq
33 {
34 delay_ms(20);
35
36 if((EXTINT & 0x04)== 0x04)
37 {
38 beep();
39 FIO0PIN ^= (1<<21);
40 uart0_puts("BEEP!\r\n");
41 EXTPOLAR = 0x04;
42 }
43
44 EXTINT |= 0x4;
45 VICVectAddr = 0;
46 }
47
48 void main()
49 {
50 init();
51 SCS = 0x03;
52
53 uart0_init(9600);
54 uart0_puts("Yo!\r\n");
55
56 FIO0DIR |= 0x00200001;
57 FIO0SET |= (1<<21);

© 2007 Apaporn Boonyarattaphan 6-63


32-bit Microcontroller ARM7 LPC2148 Manual
58 EXTMODE |= 0x4;
59 EXTPOLAR = 0x00;
60 PINSEL0 |= 0xC000;
61
62 VICVectAddr0 = (unsigned)isr_int2;
63 VICVectCntl0 = 0x20 | 16;
64 VICIntEnable |= 1 << 16;
65
66 while(1);
67 }
Figure 83: Example of an EXTERNAL INTERRUPT program

Exercise
1. Write a program that switch LED P0.22 ON when ENTER the Interrupt Service
Routine

(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 output
FIO0SET = 0x00400000;

(in interrupt service routine)


void isr_int2(void) __irq
o Switch ON LED at Port P0.22 after entered
FIO0CLR = 0x00400000;

© 2007 Apaporn Boonyarattaphan 6-64


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 7: UART0
Description of the Program

This program demonstrates the use of the Universal Asynchronous Receiver


Transmitter Port 0 (UART0). In this example, the program sends a string of
message (embedded within the program) to display it, as output, on the computer
in Hyper Terminal. Note that the cable must connect to the terminal computer and
UART0 during the program execution.

Pseudo Code

1. Initialize the board


init();
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

1 #include "lpc214x.h"
2 #include "uart.h"
3 #include "stdio.h"
4
5 void init()
6 {
7 PLL0CFG=0x24;
8 PLL0FEED=0xAA;
9 PLL0FEED=0x55;
10
11 PLL0CON=0x1;
12 PLL0FEED=0xAA;
13 PLL0FEED=0x55;
14
15 while(!(PLL0STAT & 0x400));
16
17 PLL0CON=0x3;
18 PLL0FEED=0xAA;
19 PLL0FEED=0x55;
20
21 MAMCR=0x2;
22 MAMTIM=0x4;
23 VPBDIV=0x02;

© 2007 Apaporn Boonyarattaphan 7-65


32-bit Microcontroller ARM7 LPC2148 Manual
24 }
25
26 void main()
27 {
28 init();
29 SCS = 0x03;
30
31 uart0_init(9600);
32
33 while (1)
34 {
35 uart0_puts("Hello World! PORT0\r\n");
36 }
37 }
Figure 84: Example of UART0 program

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

o Define Buffer
char s[50];
o Send output of characters to UART0
int 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 repeatedly


Hint: use FOR loop

3. Display number 0 2 4 6 8 10 repeatedly


Hint: use FOR loop

© 2007 Apaporn Boonyarattaphan 7-66


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 8: UART1
Description of the Program

This program demonstrates the use of the Universal Asynchronous Receiver


Transmitter Port 1 (UART1). In this example, the program echoes each character
pressed on the keyboard (connected at PS/2 on the board) and displays it, as
output, on the computer in the Hyper Terminal. Note that the cable must connect to
the terminal computer and UART1 during the program execution.

Pseudo Code

1. Initialize the board


init();
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 interrupts
U1IER = 3;
5. Set PIN function, set P0.9 as RxD (UART1)
PINSEL0 = (1<<18);
6. Set Interrupt Vector
Assign 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 when
an interrupt occurs.
o Echo character from Keyboard
o Reset Interrupt vector

© 2007 Apaporn Boonyarattaphan 8-67


32-bit Microcontroller ARM7 LPC2148 Manual
Source Code

1 #include "lpc214x.h"
2 #include "uart.h"
3 #include "stdio.h"
4 #include "stdlib.h"
5
6 void init()
7 {
8 PLL0CFG=0x24;
9 PLL0FEED=0xAA;
10 PLL0FEED=0x55;
11
12 PLL0CON=0x1;
13 PLL0FEED=0xAA;
14 PLL0FEED=0x55;
15
16 while(!(PLL0STAT & 0x400));
17
18 PLL0CON=0x3;
19 PLL0FEED=0xAA;
20 PLL0FEED=0x55;
21
22 MAMCR=0x2;
23 MAMTIM=0x4;
24 VPBDIV=0x02;
25 }
26
27 void isr_uart1(void) __irq
28 {
29 char msg;
30
31 if(((msg = U1IIR) & 0x01) == 0)
32 {
33 switch (msg & 0x0E)
34 {
35 case 0x04: while(!(U1LSR & 0x20));
36 U1THR = U1RBR;
37 break;
38 case 0x02: break;
39 default: break;
40 }
41 }
42 VICVectAddr = 0;
43 }
44
45 void main()
46 {
47 init();
48 SCS = 0x03;
49
50 uart1_init(9600);
51 U1IER = 3;
52 PINSEL0 |= (1<<18);
53
54 VICVectAddr0 = (unsigned)isr_uart1;
55 VICVectCntl0 = 0x20 | 7;
56 VICIntEnable = 1 << 7;
57

© 2007 Apaporn Boonyarattaphan 8-68


32-bit Microcontroller ARM7 LPC2148 Manual
58 puts("Now test UART1 for echo character\n");
59 puts("Press any key for test!\n");
60 while(1);
61 }
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 ON
FIO0CLR = 0x00200000;
 IF echo character (U1THR) equals ‘b’, switch LED P0.21 OFF
FIO0SET = 0x00200000;
Hint: Use IF Statement and also, create a new variable to store
the value of U1RBR

© 2007 Apaporn Boonyarattaphan 8-69


32-bit Microcontroller ARM7 LPC2148 Manual
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 is
switch on.

Pseudo Code

1. Initialize variables
2. Initialize the board
init();
3. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
4. Initialize UART0, Baud rate 9600
uart0_init(9600);
5. Set Control Direction
Select 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 output
FIO0SET = 0x00200000;
7. Set PIN function, set P0.30 as AD0.3
PINSEL1 = 0x10000000;
8. Set A/D Control
AD0CR = 0x00210608;
9. WHILE the program is running
o Start conversion of ANALOG to DIGITAL signal
AD0CR |= 0x01000000;
while ((AD0DR3 & 0x80000000) == 0);
val = ((AD0DR3 >> 6) & 0x03FF);
o Display DIGITAL signal value
o 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

© 2007 Apaporn Boonyarattaphan 9-70


32-bit Microcontroller ARM7 LPC2148 Manual
Source Code

1 #include "lpc214x.h"
2 #include "stdio.h"
3 #include "uart.h"
4
5 #define LED2_ON FIO0CLR = 0x00200000
6 #define LED2_OFF FIO0SET = 0x00200000
7
8 int adc=0;
9
10 void init()
11 {
12 PLL0CFG=0x24;
13 PLL0FEED=0xAA;
14 PLL0FEED=0x55;
15
16 PLL0CON=0x1;
17 PLL0FEED=0xAA;
18 PLL0FEED=0x55;
19
20 while(!(PLL0STAT & 0x400)) ;
21
22 PLL0CON=0x3;
23 PLL0FEED=0xAA;
24 PLL0FEED=0x55;
25
26 MAMCR=0x2;
27 MAMTIM=0x4;
28 VPBDIV=0x02;
29 }
30
31 void delay_ms(long ms)
32 {
33 long i,j;
34 for (i = 0; i < ms; i++ )
35 for (j = 0; j < 6659; j++ );
36 }
37
38 void main()
39 {
40 int val=0;
41 int a=0;
42 char s[30];
43
44 init();
45 SCS = 0x03;
46 uart0_init(9600);
47
48 FIO0DIR |= 0x00200000;
49 LED2_OFF;
50
51 PINSEL1 |= 0x10000000;
52
53 AD0CR = 0x00210608;
54
55 while (1)
56 {
57 AD0CR |= 0x01000000;
58 while ((AD0DR3 & 0x80000000) == 0);

© 2007 Apaporn Boonyarattaphan 9-71


32-bit Microcontroller ARM7 LPC2148 Manual
59 val = ((AD0DR3 >> 6) & 0x03FF);
60 adc = val;
61
62 for(a=0; a <(adc/20); a++ )
63 {
64 uart0_puts(" ");
65 }
66
67 if(adc<=500 && adc>=400)
68 {
69 LED2_ON;
70 delay_ms(100);
71 LED2_OFF;
72 delay_ms(100);
73 LED2_ON;
74 delay_ms(100);
75 LED2_OFF;
76 }
77
78 sprintf(s,"[%d]\r\n",adc);
79 uart0_puts(s);
80 delay_ms(250);
81 }
82 }
Figure 86: Example of an ANALOG to DIGITAL program

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

 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 output
FIO0SET = 0x00400000;
 IF adc between 300-400
if(adc<=400 && adc>=300)
o IF YES
 Switch OFF LED at Port P0.22
FIO0SET = 0x00400000;
 Delay
 Switch ON LED at Port P0.22
FIO0CLR = 0x00400000;

© 2007 Apaporn Boonyarattaphan 9-72


32-bit Microcontroller ARM7 LPC2148 Manual
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 Hyper
Terminal as output.

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

Pseudo Code

1. Initialize variables
2. Initialize the board
init();
3. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
4. Set Control Direction
Select Port P0.22 as output
FIO0DIR = 0x00400000;
5. Initialize UART0, Baud rate 9600
uart0_init(9600);
6. Initialize RTC
Set Prescaler Integer value
PREINT = 0x00000392;
Set Prescaler Fraction value
PREFRAC = 0x00004380;
Select: an increment of the Second value generates an interrupt
CIIR = 0x00000001;
7. Initialize Alarm / Timer
Set alarm SECOND value
ALSEC = 0x00000003;
Enable alarm for SECOND
AMR = 0x000000FE;
8. Set Interrupt Vector
Assign 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;
9. WHILE the program is running
o Check IF an Interrupt has been generated or not
 IF YES
Buzzer sound
Set BUZZER value as OFF

© 2007 Apaporn Boonyarattaphan 10-73


32-bit Microcontroller ARM7 LPC2148 Manual
Clear Interrupt vector
o 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 when
an 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

1 #include "lpc214x.h"
2 #include "sound.h"
3 #include "stdio.h"
4 #include "uart.h"
5 #define LED1_ON FIO0CLR = 0x00400000
6 #define LED1_OFF FIO0SET = 0x00400000
7 char alarm = 0;
8
9 void init()
10 {
11 PLL0CFG=0x24;
12 PLL0FEED=0xAA;
13 PLL0FEED=0x55;
14
15 PLL0CON=0x1;
16 PLL0FEED=0xAA;
17 PLL0FEED=0x55;
18
19 while(!(PLL0STAT & 0x400));
20
21 PLL0CON=0x3;
22 PLL0FEED=0xAA;
23 PLL0FEED=0x55;
24
25 MAMCR=0x2;
26 MAMTIM=0x4;
27 VPBDIV=0x02;
28 }
29
30 void delay_ms(long ms)
31 {
32 long i,j;
33 for (i = 0; i < ms; i++ )
34 for (j = 0; j < 6659; j++ );
35 }
36
37 void isr_rtc(void) __irq

© 2007 Apaporn Boonyarattaphan 10-74


32-bit Microcontroller ARM7 LPC2148 Manual
38 {
39 if(ILR & 0x01)
40 {
41 LED1_ON;
42 delay_ms(100);
43 LED1_OFF;
44 ILR = 0x01;
45 }
46 if(ILR & 0x02)
47 {
48 alarm = 1;
49 ILR = 0x02;
50 }
51 VICVectAddr = 0;
52 }
53
54 void main()
55 {
56 char i=0;
57 char s[50];
58
59 init();
60 SCS = 0x03;
61 FIO0DIR |= 0x00400000;
62 uart0_init(9600);
63
64 PREINT = 0x00000392;
65 PREFRAC = 0x00004380;
66 CIIR = 0x00000001;
67
68 ALSEC = 0x00000003;
69 AMR = 0x000000FE;
70 CCR = 0x00000001;
71
72 VICVectAddr13 = (unsigned)isr_rtc;
73 VICVectCntl13 = 0x20 | 13;
74 VICIntEnable |= (1<<13);
75
76 while (1)
77 {
78 if(alarm==1)
79 {
80 beep();
81 i++;
82 if(i>10)
83 {
84 i=0;
85 alarm = 0;
86 }
87 }
88
89 if(HOUR < 10)
90 {
91 sprintf(s,"TIME: 0%d:",HOUR);
92 }
93 else
94 {
95 sprintf(s,"TIME: %d:",HOUR);
96 }
97 uart0_puts(s);
98

© 2007 Apaporn Boonyarattaphan 10-75


32-bit Microcontroller ARM7 LPC2148 Manual
99 if(MIN < 10)
100 {
101 sprintf(s,"0%d:",MIN);
102 }
103 else
104 {
105 sprintf(s,"%d:",MIN);
106 }
107 uart0_puts(s);
108
109 if(SEC < 10)
110 {
111 sprintf(s,"0%d\r",SEC);
112 }
113 else
114 {
115 sprintf(s,"%d\r",SEC);
116 }
117 uart0_puts(s);
118
119 delay_ms(100);
120 }
121 }
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 second


ALSEC = 0x00000014;

2. Set minute alarm

 Set alarm MINUTE value at 5 minute


ALMIN = 0x00000005;
 Enable alarm for MINUTE
AMR = 0x000000FD;

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

© 2007 Apaporn Boonyarattaphan 10-76


32-bit Microcontroller ARM7 LPC2148 Manual
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 Hyper
Terminal as output. Also, a SET TIME option is available, press * on the keyboard
to access it. Note that the cable must connect the terminal computer and UART1
during the program execution.

Pseudo Code

1. Initialize variables
2. Initialize the board
init();
3. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
4. Initialize UART1, Baud rate 9600
uart1_init(9600);
Enable RDA and THRE interrupts
U1IER = 3;
5. Set PIN function, set P0.7 as RxD (UART1)
PINSEL0 = (1<<18);
6. Set Interrupt Vector
Assign 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 screen
o Check IF the character last hit on the keyboard is ‘*’ or not
 IF YES
Reset keyboard character key
Set Time

Function(s)
 init
o The board initialization function
 delay_ms
o A delay FOR Loop

© 2007 Apaporn Boonyarattaphan 11-77


32-bit Microcontroller ARM7 LPC2148 Manual
 isr_uart1
o An Interrupt service routine; the program jumps to this function when
an interrupt occurs.
o Echo character from Keyboard
o Reset Interrupt vector
 isr_uart1_setup
o Get 2 integers then
o Update the value of the given address
o Reset Interrupt vector

Source Code

1 #include "lpc214x.h"
2 #include "stdio.h"
3 #include "uart.h"
4 #include "ctype.h"
5 #include "stdlib.h"
6 char key = 0;
7 char s[30] = {0};
8
9 void init()
10 {
11 PLL0CFG=0x24;
12 PLL0FEED=0xAA;
13 PLL0FEED=0x55;
14
15 PLL0CON=0x1;
16 PLL0FEED=0xAA;
17 PLL0FEED=0x55;
18
19 while(!(PLL0STAT & 0x400));
20
21 PLL0CON=0x3;
22 PLL0FEED=0xAA;
23 PLL0FEED=0x55;
24
25 MAMCR=0x2;
26 MAMTIM=0x4;
27 VPBDIV=0x02;
28 }
29
30 void delay_ms(long ms)
31 {
32 long i,j;
33 for(i=0; i<ms; i++)
34 for(j=0; j<6659; j++);
35 }
36
37 void isr_uart1(void) __irq
38 {
39 char msg;
40 if (((msg = U1IIR) & 0x01) == 0)
41 {
42 switch (msg & 0x0E)
43 {
44 case 0x04: while(!(U1LSR & 0x20));
45 key = U1RBR;

© 2007 Apaporn Boonyarattaphan 11-78


32-bit Microcontroller ARM7 LPC2148 Manual
46 break;
47 case 0x02: break;
48 default: break;
49 }
50 }
51 VICVectAddr = 0;
52 }
53
54 void rtc_uart1_setup(char *s)
55 {
56 unsigned char tm;
57 char i = 0;
58 for (i=0; i<2; i++)
59 {
60 while(!isdigit(key));
61 if(i==0)
62 tm = 10 * atoi(&key);
63 if(i==1)
64 tm = tm + atoi(&key);
65 putchar(key);
66 key = 0;
67 }
68 *s = tm;
69 }
70
71 void main()
72 {
73 init();
74 SCS = 0x03;
75
76 uart1_init(9600);
77 U1IER = 3;
78 PINSEL0 |= (1<<18);
79
80 VICVectAddr0 = (unsigned)isr_uart1;
81 VICVectCntl0 = 0x20 | 7;
82 VICIntEnable |= 1 << 7;
83
84 CCR = 0x00000011;
85
86 while (1)
87 {
88 printf("TIME: %d:%d:%d \r",HOUR,MIN,SEC);
89 delay_ms(100);
90 if (key == '*')
91 {
92 key = 0;
93 printf("\nSet Time:");
94 rtc_uart1_setup(&HOUR);
95 uart1_putc(':');
96 rtc_uart1_setup(&MIN);
97 uart1_putc(':');
98 rtc_uart1_setup(&SEC);
99 printf("\nTIME: %d:%d:%d \r",HOUR,MIN,SEC);
100 }
101 }
102 }
Figure 88: Example of a Real Time Clock (2) program

© 2007 Apaporn Boonyarattaphan 11-79


32-bit Microcontroller ARM7 LPC2148 Manual
Exercise
1. Add 1 more option to the previous example program; press ‘&’ to set DAY OF
THE MONTH (DOM)
rtc_uart1_setup(&DOM);

© 2007 Apaporn Boonyarattaphan 11-80


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 12: Keyboard (1)
Description of the Program

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

Pseudo Code

1. Initialize the board


init();
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

1 #include "lpc214x.h"
2 #include "stdio.h"
3 #include "sound.h"
4 #include "uart.h"
5 #include "keyboard.h"
6 char key = 0;
7
8 void init()
9 {
10 PLL0CFG=0x24;
11 PLL0FEED=0xAA;
12 PLL0FEED=0x55;
13
14 PLL0CON=0x1;
15 PLL0FEED=0xAA;
16 PLL0FEED=0x55;
17
18 while(!(PLL0STAT & 0x400));
19
20 PLL0CON=0x3;
21 PLL0FEED=0xAA;
22 PLL0FEED=0x55;

© 2007 Apaporn Boonyarattaphan 12-81


32-bit Microcontroller ARM7 LPC2148 Manual
23
24 MAMCR=0x2;
25 MAMTIM=0x4;
26 VPBDIV=0x02;
27 }
28
29 void main()
30 {
31 init();
32 SCS = 0x03;
33 kb_init();
34 uart1_init(9600);
35
36 while (1)
37 {
38 printf("%c",kb_getchar());
39 }
40 }
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);

© 2007 Apaporn Boonyarattaphan 12-82


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 13: Keyboard (2)
Description of the Program

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

Pseudo Code

1. Initialize variables
2. Initialize the board
init();
3. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
4. Select Port P0.21 & P0.22 as output
FIO0DIR = 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 Menu
9. WHILE the program is running
o Get character from keyboard
o 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

© 2007 Apaporn Boonyarattaphan 13-83


32-bit Microcontroller ARM7 LPC2148 Manual
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 when
an 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 then
o Update the value of the given address
o Reset Interrupt vector

Source Code

1 #include "lpc214x.h"
2 #include "stdio.h"
3 #include "sound.h"
4 #include "uart.h"
5 #include "keyboard.h"
6
7 #include "ctype.h"
8 #include "stdlib.h"
9
10 #define LED1_ON FIO0CLR = 0x00400000
11 #define LED1_OFF FIO0SET = 0x00400000
12 #define LED2_ON FIO0CLR = 0x00200000
13 #define LED2_OFF FIO0SET = 0x00200000
14
15 char alarm = 0;
16 char key = 0;
17 char s[30] = {0};
18 int a = 1;
19
20 void init()
21 {
22 PLL0CFG=0x24;
23 PLL0FEED=0xAA;
24 PLL0FEED=0x55;
25
26 PLL0CON=0x1;
27 PLL0FEED=0xAA;
28 PLL0FEED=0x55;
29
30 while(!(PLL0STAT & 0x400));

© 2007 Apaporn Boonyarattaphan 13-84


32-bit Microcontroller ARM7 LPC2148 Manual
31
32 PLL0CON=0x3;
33 PLL0FEED=0xAA;
34 PLL0FEED=0x55;
35
36 MAMCR=0x2;
37 MAMTIM=0x4;
38 VPBDIV=0x02;
39 }
40
41 void print_menu()
42 {
43 uart0_puts("A: LED\r\n");
44 uart0_puts("B: SOUND\r\n");
45 }
46
47 void delay_ms(long ms)
48 {
49 long i,j;
50 for (i = 0; i < ms; i++ )
51 for (j = 0; j < 6659; j++ );
52 }
53
54 void isr_rtc(void) __irq
55 {
56 if(ILR & 0x01)
57 {
58 LED1_ON;
59 delay_ms(100);
60 LED1_OFF;
61 ILR = 0x01;
62 }
63 if(ILR & 0x02)
64 {
65 alarm = 1;
66 ILR = 0x02;
67 }
68 VICVectAddr = 0;
69 }
70
71 void rtc_kb_setup(char *s)
72 {
73 unsigned char tm;
74 char i = 0;
75 char aa[30] = {0};
76 char num = 0;
77 for (i=0; i<2; i++)
78 {
79 while(!isdigit(num))
80 {
81 num = kb_getchar();
82 }
83 if(i==0)
84 {
85 tm = 10 * atoi(&num);
86 }
87 if(i==1)
88 {
89 tm = tm + atoi(&num);
90 }
91 sprintf(aa,"%c",num);

© 2007 Apaporn Boonyarattaphan 13-85


32-bit Microcontroller ARM7 LPC2148 Manual
92 uart0_puts(aa);
93 num = 0;
94 }
95 *s = tm;
96 }
97
98 void main()
99 {
100 char ch = 0;
101 init();
102 SCS = 0x03;
103 FIO0DIR |= 0x00600000;
104 FIO0SET |= 0x00600000;
105 kb_init();
106 uart0_init(9600);
107
108 print_menu();
109
110 while (1)
111 {
112 ch = kb_getchar();
113 if (ch == 'a')
114 {
115 uart0_puts(" 1: P.21 ON\r\n 2: P.21 OFF\r\n");
116 uart0_puts(" 3: P.22 ON\r\n 4: P.22 OFF\r\n");
117 uart0_puts(" Q: BACK\r\n\n");
118
119 while (a == 1)
120 {
121 ch = kb_getchar();
122 switch (ch)
123 {
124 case '1': LED2_ON;
125 break;
126 case '2': LED2_OFF;
127 break;
128 case '3': LED1_ON;
129 break;
130 case '4': LED1_OFF;
131 break;
132 default: uart0_puts(" ** PLEASE INPUT
133 1,2,3,4 or Q\r");
134 break;
135 }
136
137 if(ch == 'q')
138 {
139 uart0_puts("\n\n");
140 print_menu();
141 a = 0;
142 }
143 }
144 }
145 else if (ch == 'b')
146 {
147 uart0_puts(" 1: BEEP ON\r\n Q: BACK\r\n\n");
148
149 while (a == 1)
150 {
151 ch = kb_getchar();
152 switch (ch)

© 2007 Apaporn Boonyarattaphan 13-86


32-bit Microcontroller ARM7 LPC2148 Manual
153 {
154 case '1': beep();
155 break;
156 default: uart0_puts(" ** PLEASE
157 INPUT 1 or Q\r");
158 break;
159 }
160
161 if(ch == 'q')
162 {
163 uart0_puts("\n\n");
164 print_menu();
165 a = 0;
166 }
167 }
168 }
169 a = 1;
170 }
171 }
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

© 2007 Apaporn Boonyarattaphan 13-87


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 14: Real Time Clock and Keyboard
Description of the Program

This program demonstrates the use of the Real Time Clock module and the
keyboard. In this example, the program accesses the Real Time Clock values and
display them on Hyper Terminal, as output, and use it as reference for the interrupt
at 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 cable
must connect to the terminal and UART1 during the program execution.

Pseudo Code

1. Initialize variables
2. Initialize the board
init();
3. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
4. Initialize the keyboard
kb_init();
5. Set Control Direction
Select Port P0.22 as output
FIO0DIR = 0x00400000;
6. Set Interrupt Vector
Assign isr_uart1 to Interrupt Vector Slot 5
VICVectAddr5 = (unsigned)isr_uart1;
Control Enable isr_uart1 to Interrupt Vector 5
VICVectCntl5 = 0x20 | 7;
Enable isr_uart1 at Interrupt Vector 5
VICIntEnable = 1 << 7;
7. Initialize UART1, Baud rate 9600
uart1_init(9600);
Enable RDA and THRE interrupts
U1IER = 3;
8. Set PIN function, set P0.9 as RxD (UART1)
PINSEL0 = (1<<18);
9. Initialize RTC
Set 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 value
ALSEC = 0x00000003;

© 2007 Apaporn Boonyarattaphan 14-88


32-bit Microcontroller ARM7 LPC2148 Manual
Disable alarm for SECOND
AMR = 0x000000FF;
11. Set Interrupt Vector
Assign 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 screen
o 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 sound
Clear 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 when
an 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 then
o Update the value of the given address
o Reset Interrupt vector
 isr_uart1
o An Interrupt service routine; the program jumps to this function when
an interrupt occurs.
o Echo character from Keyboard
o Reset Interrupt vector

© 2007 Apaporn Boonyarattaphan 14-89


32-bit Microcontroller ARM7 LPC2148 Manual
Source Code

1 #include "lpc214x.h"
2 #include "sound.h"
3 #include "stdio.h"
4 #include "uart.h"
5 #include "keyboard2.h"
6
7 #include "ctype.h"
8 #include "stdlib.h"
9
10 #define LED1_ON FIO0CLR = 0x00400000
11 #define LED1_OFF FIO0SET = 0x00400000
12 #define LED0_ON FIO0CLR = 0x00200000
13 #define LED0_OFF FIO0SET = 0x00200000
14
15 char alarm = 0;
16 char s[30];
17 char key = 0;
18
19 void init()
20 {
21 PLL0CFG=0x24;
22 PLL0FEED=0xAA;
23 PLL0FEED=0x55;
24
25 PLL0CON=0x1;
26 PLL0FEED=0xAA;
27 PLL0FEED=0x55;
28
29 while(!(PLL0STAT & 0x400));
30 PLL0CON=0x3;
31 PLL0FEED=0xAA;
32 PLL0FEED=0x55;
33
34 MAMCR=0x2;
35 MAMTIM=0x4;
36 VPBDIV=0x02;
37 }
38
39 void delay_ms(long ms)
40 {
41 long i,j;
42 for (i = 0; i < ms; i++ )
43 for (j = 0; j < 6659; j++ );
44 }
45
46 void isr_rtc(void) __irq
47 {
48 if(ILR & 0x01)
49 {
50 LED1_ON;
51 delay_ms(100);
52 LED1_OFF;
53 ILR = 0x01;
54 }
55 if(ILR & 0x02)
56 {
57 alarm = 1;
58 ILR = 0x02;

© 2007 Apaporn Boonyarattaphan 14-90


32-bit Microcontroller ARM7 LPC2148 Manual
59 }
60 VICVectAddr = 0;
61 }
62
63 void rtc_kb_setup(char *s)
64 {
65 unsigned char tm;
66 char i = 0;
67 for (i=0; i<2; i++)
68 {
69 while(!isdigit(key));
70 if(i==0)
71 {
72 tm = 10 * atoi(&key);
73 }
74 if(i==1)
75 {
76 tm = tm + atoi(&key);
77 }
78 putchar(key);
79 key = 0;
80 }
81 *s = tm;
82 }
83
84 void isr_uart1(void) __irq
85 {
86 char msg;
87 if (((msg = U1IIR) & 0x01) == 0)
88 {
89 switch (msg & 0x0E)
90 {
91 case 0x04: while(!(U1LSR & 0x20));
92 key = U1RBR;
93 break;
94 case 0x02: break;
95 default: break;
96 }
97 }
98 VICVectAddr = 0;
99 }
100
101 void main()
102 {
103 char i=0;
104
105 init();
106 SCS = 0x03;
107 kb_init();
108
109 FIO0DIR |= 0x00400000;
110 VICVectAddr5 = (unsigned)isr_uart1;
111 VICVectCntl5 = 0x20 | 7;
112 VICIntEnable |= 1 << 7;
113
114 uart1_init(9600);
115 U1IER = 3;
116 PINSEL0 |= (1<<18);
117 PREINT = 0x00000392;
118 PREFRAC = 0x00004380;
119

© 2007 Apaporn Boonyarattaphan 14-91


32-bit Microcontroller ARM7 LPC2148 Manual
120 CIIR = 0x00000001;
121 ALSEC = 0x00000003;
122 AMR = 0x000000FF;
123
124 VICVectAddr13 = (unsigned)isr_rtc;
125 VICVectCntl13 = 0x20 | 13;
126 VICIntEnable |= (1<<13);
127
128 CCR = 0x00000001;
129
130 while (1)
131 {
132 printf("%c TIME: %d:%d:%d\r",key,HOUR,MIN,SEC);
133 delay_ms(100);
134
135 if (key == '1')
136 {
137 key = 0;
138 AMR = 0x000000FE;
139 printf("\n[AMR=0xFE]\r\n");
140 }
141 else if (key == '2')
142 {
143 key = 0;
144 AMR = 0x000000FF;
145 printf("\n[AMR=0xFF]\r\n");
146 }
147 else if (key == '3')
148 {
149 key = 0;
150 printf("\n\n Set ALARM Second: ");
151 rtc_kb_setup(&ALSEC);
152 AMR = 0x000000FE;
153 }
154
155 if(alarm==1)
156 {
157 beep();
158 i++;
159 if(i>10)
160 {
161 i=0;
162 alarm = 0;
163 }
164 }
165 }
166 }
Figure 91: Example of a Keyboard (3) program

© 2007 Apaporn Boonyarattaphan 14-92


32-bit Microcontroller ARM7 LPC2148 Manual
Exercise
1. Add more options to the previous example program

Select: an increment of the Minute value generates an interrupt


CIIR = 0x00000003;
 Enable Alarm for minute
AMR = 0x000000FD;
 Disable Alarm for minute
AMR = 0x000000FD;
 Set Alarm for minute
rtc_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 U0IIR
U1LSR to U0LSR
U1RBR to U0RBR

© 2007 Apaporn Boonyarattaphan 14-93


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 15: Analog to Digital and DIP Switch
Description of the Program

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

Pseudo Code

1. Initialize variables
2. Initialize the board
init();
3. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
4. Set Control Direction
Select 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 output
FIO0SET = 0x00200000;
6. Set PIN function, set P0.5 as Analog to Digital converter port AD0.7
PINSEL0 = 0x0C00;
7. Set A/D Control
AD0CR = 0x00210608;
8. WHILE the program is running
o 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 not
if(adc>200)
 IF YES
Switch ON/OFF LED at Port P0.21
o Display DIGITAL signal value

Function(s)
 init
o The board initialization function
 delay_ms
o A delay FOR Loop

© 2007 Apaporn Boonyarattaphan 15-94


32-bit Microcontroller ARM7 LPC2148 Manual
Source Code

1 #include "lpc214x.h"
2 #include "stdio.h"
3 #include "sound.h"
4 #include "uart.h"
5
6 #define LED2_ON FIO0CLR = 0x00200000
7 #define LED2_OFF FIO0SET = 0x00200000
8
9 void init()
10 {
11 PLL0CFG=0x24;
12 PLL0FEED=0xAA;
13 PLL0FEED=0x55;
14
15 PLL0CON=0x1;
16 PLL0FEED=0xAA;
17 PLL0FEED=0x55;
18
19 while(!(PLL0STAT & 0x400));
20
21 PLL0CON=0x3;
22 PLL0FEED=0xAA;
23 PLL0FEED=0x55;
24
25 MAMCR=0x2;
26 MAMTIM=0x4;
27 VPBDIV=0x02;
28 }
29
30 void delay_ms(long ms)
31 {
32 long i,j;
33 for (i = 0; i < ms; i++ )
34 for (j = 0; j < 6659; j++ );
35 }
36
37 void main()
38 {
39 int val = 0;
40 int adc = 0;
41 int a = 0;
42 char s[20] = {0};
43
44 init();
45 SCS = 0x03;
46
47 uart0_init(9600);
48 uart0_puts("Yo!\r\n");
49
50 FIO0DIR |= 0x00200000;
51 FIO0SET |= (1<<21);
52
53 PINSEL0 |= 0x0C00; // Select PIN 0.5 as AD0.7
54 AD0CR = 0x00210680;
55
56 while(1)
57 {

© 2007 Apaporn Boonyarattaphan 15-95


32-bit Microcontroller ARM7 LPC2148 Manual
58 AD0CR |= 0x01000000;
59 while ((AD0DR7 & 0x80000000) == 0);
60 val = ((AD0DR7 >> 6) & 0x03FF);
61
62 adc = val;
63
64 for(a=0; a <(adc/20); a++ )
65 {
66 uart0_puts(" ");
67 }
68
69 if(adc > 200)
70 {
71 LED2_ON;
72 delay_ms(100);
73 LED2_OFF;
74 delay_ms(100);
75 LED2_ON;
76 delay_ms(100);
77 LED2_OFF;
78 }
79
80 sprintf(s,"[%d]\r\n",adc);
81 uart0_puts(s);
82 delay_ms(250);
83 }
84 }
Figure 92: Example of DIP Switch (1) program

© 2007 Apaporn Boonyarattaphan 15-96


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 16: External Input (DIP Switch)
Description of the Program

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

The DIP switch must be connected as follow:

Figure 93: Switches Connection

© 2007 Apaporn Boonyarattaphan 16-97


32-bit Microcontroller ARM7 LPC2148 Manual
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 variables
2. Initialize the board
init();
3. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
4. Set Control Direction

© 2007 Apaporn Boonyarattaphan 16-98


32-bit Microcontroller ARM7 LPC2148 Manual
Select LED Port P0.21 and Port P0.22 as output
FIO0DIR = 0x00600000;
Configure all Port P1.xx as input
FIO1DIR = 0x00000000;
5. Switch LED at Port P0.21 and Port P0.22 OFF by writing 1 to Port P0.21
and 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 running
o 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 not
if(inp0(25)==1)
 IF YES
Display A on Hyper Terminal screen
Switch 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 screen
Switch 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 screen
Switch 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 screen
Switch 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 screen
Switch ON LED at Port P0.22

© 2007 Apaporn Boonyarattaphan 16-99


32-bit Microcontroller ARM7 LPC2148 Manual
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 screen
Switch 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 screen
Switch 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 screen
Switch 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

1 #include "lpc214x.h"
2 #include "stdio.h"
3 #include "sound.h"
4 #include "uart.h"
5
6 #define LED2_ON FIO0CLR = 0x00200000
7 #define LED2_OFF FIO0SET = 0x00200000
8
9 void init()
10 {
11 PLL0CFG=0x24;
12 PLL0FEED=0xAA;

© 2007 Apaporn Boonyarattaphan 16-100


32-bit Microcontroller ARM7 LPC2148 Manual
13 PLL0FEED=0x55;
14
15 PLL0CON=0x1;
16 PLL0FEED=0xAA;
17 PLL0FEED=0x55;
18
19 while(!(PLL0STAT & 0x400));
20
21 PLL0CON=0x3;
22 PLL0FEED=0xAA;
23 PLL0FEED=0x55;
24
25 MAMCR=0x2;
26 MAMTIM=0x4;
27 VPBDIV=0x02;
28 }
29
30 void delay_ms(long ms)
31 {
32 long i,j;
33 for (i = 0; i < ms; i++ )
34 for (j = 0; j < 6659; j++ );
35 }
36
37 char inp0(char _bit)
38 {
39 unsigned long c;
40 c = 1<<_bit;
41 FIO0DIR &= ~c;
42 return((FIO0PIN & c)>>_bit);
43 }
44
45 char inp1(char _bit)
46 {
47 unsigned long d;
48 d = 1<<_bit;
49 FIO1DIR &= ~d;
50 return((FIO1PIN & d)>>_bit);
51 }
52
53 void main()
54 {
55 int val = 0;
56 int adc = 0;
57 int a = 0;
58 char s[20] = {0};
59
60 init();
61 SCS = 0x03;
62
63 uart0_init(9600);
64 uart0_puts("Yo!\r\n");
65
66 FIO0DIR |= 0x00600000;
67 FIO1DIR |= 0x00000000;
68
69 FIO0SET |= 0x00600000;
70
71 PINSEL0 |= 0x00000000;
72 PINSEL1 |= 0x00000000;
73

© 2007 Apaporn Boonyarattaphan 16-101


32-bit Microcontroller ARM7 LPC2148 Manual
74 while(1)
75 {
76 sprintf(s,"%d\r\n",FIO0PIN);
77 uart0_puts(s);
78
79 /* PORT 0 */
80 if(inp0(25)==1)
81 {
82 uart0_puts("A\r\n");
83 FIO0CLR = 0x00400000;
84 }
85 delay_ms(500);
86 if(inp0(4)==0)
87 {
88 uart0_puts("B\r\n");
89 FIO0SET = 0x00400000;
90 }
91 delay_ms(500);
92 if(inp0(5)==1)
93 {
94 uart0_puts("C\r\n");
95 FIO0CLR = 0x00200000;
96 }
97 delay_ms(500);
98 if(inp0(6)==0)
99 {
100 uart0_puts("D\r\n");
101 FIO0SET = 0x00200000;
102 }
103 delay_ms(500);
104
105
106 /* PORT 1 */
107 if(inp1(19)==0)
108 {
109 uart0_puts("AA\r\n");
110 FIO0CLR = 0x00400000;
111 }
112 delay_ms(1000);
113 if(inp1(22)==0)
114 {
115 uart0_puts("BB\r\n");
116 FIO0SET = 0x00400000;
117 }
118 delay_ms(1000);
119 if(inp1(16)==1)
120 {
121 uart0_puts("CC\r\n");
122 FIO0CLR = 0x00200000;
123 }
124 delay_ms(1000);
125 if(inp1(21)==1)
126 {
127 uart0_puts("DD\r\n");
128 FIO0SET = 0x00200000;
129 }
130 delay_ms(1000);
131 }
132 }
Figure 95: Example of DIP Switch (2) program

© 2007 Apaporn Boonyarattaphan 16-102


32-bit Microcontroller ARM7 LPC2148 Manual
Chapter 17: External Output (LEDs)
Description of the Program

This program demonstrates the use of the LEDs as an external output from Port 0
and 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)

© 2007 Apaporn Boonyarattaphan 17-103


32-bit Microcontroller ARM7 LPC2148 Manual
Figure 97: LEDs Circuit (2)

© 2007 Apaporn Boonyarattaphan 17-104


32-bit Microcontroller ARM7 LPC2148 Manual
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 variables
2. Initialize the board
init();
3. Set SCS (System Control and Status flags)
GPIO mode selection
SCS = 0x03;
4. Set Control Direction
Select 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;

© 2007 Apaporn Boonyarattaphan 17-105


32-bit Microcontroller ARM7 LPC2148 Manual
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 Port
P0.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

© 2007 Apaporn Boonyarattaphan 17-106


32-bit Microcontroller ARM7 LPC2148 Manual
Source Code

1 #include "lpc214x.h"
2 #include "stdio.h"
3 #include "sound.h"
4 #include "uart.h"
5
6 void init()
7 {
8 PLL0CFG=0x24;
9 PLL0FEED=0xAA;
10 PLL0FEED=0x55;
11
12 PLL0CON=0x1;
13 PLL0FEED=0xAA;
14 PLL0FEED=0x55;
15
16 while(!(PLL0STAT & 0x400));
17
18 PLL0CON=0x3;
19 PLL0FEED=0xAA;
20 PLL0FEED=0x55;
21
22 MAMCR=0x2;
23 MAMTIM=0x4;
24 VPBDIV=0x02;
25 }
26
27 void delay_ms(long ms)
28 {
29 long i,j;
30 for (i = 0; i < ms; i++ )
31 for (j = 0; j < 6659; j++ );
32 }
33
34 void main()
35 {
36 init();
37 SCS = 0x03;
38
39 uart0_init(9600);
40 uart0_puts("Yo!\r\n");
41
42 FIO0DIR |= 0x00200000;
43 FIO0DIR |= 0x02000070;
44 FIO1DIR |= 0x00650000;
45
46 FIO0SET |= 0x00200000;
47
48 while(1)
49 {
50 FIO0SET |= 0x02000020;
51 FIO1SET |= 0x00210000;
52 FIO0CLR |= 0x00000050;
53 FIO1CLR |= 0x00440000;
54 delay_ms(500);
55 FIO0SET |= 0x00000050;
56 FIO1SET |= 0x00440000;
57 FIO0CLR |= 0x02000020;

© 2007 Apaporn Boonyarattaphan 17-107


32-bit Microcontroller ARM7 LPC2148 Manual
58 FIO1CLR |= 0x00210000;
59 delay_ms(500);
60 }
61 }
Figure 99; Example of DIP Switch (2) program

© 2007 Apaporn Boonyarattaphan 17-108


32-bit Microcontroller ARM7 LPC2148 Manual
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

© 2007 Apaporn Boonyarattaphan 109


32-bit Microcontroller ARM7 LPC2148 Manual

Вам также может понравиться