Beruflich Dokumente
Kultur Dokumente
LPC2148
Manual
Contents
EDITORIAL NOTES...................................................................... 7
ACKNOWLEDGEMENTS ............................................................. 8
ARRAYS............................................................................................................................... 2-21
Array Declaration ..................................................................................................... 2-21
Array Initialization .................................................................................................... 2-22
FUNCTIONS......................................................................................................................... 2-35
Functions with no Type ............................................................................................ 2-37
EXERCISE............................................................................................................................ 4-57
EXERCISE............................................................................................................................ 5-60
EXERCISE............................................................................................................................ 6-64
EXERCISE............................................................................................................................ 7-66
EXERCISE............................................................................................................................ 8-69
EXERCISE............................................................................................................................ 9-72
EXERCISE.......................................................................................................................... 10-76
EXERCISE.......................................................................................................................... 11-80
EXERCISE.......................................................................................................................... 12-82
EXERCISE.......................................................................................................................... 13-87
EXERCISE.......................................................................................................................... 14-93
REFERENCES.......................................................................... 109
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.
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:
Finally, thank to all staffs at NECTEC who helped, and to all users who use this
manual.
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).
Pipelining
An instruction cycle has 3 stages:
1 2 3 4 5 6
Fetch
Decode
Execute
Figure 1: Pipelining
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):
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)
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.
10-bit conversion
Buffer at the output
Power-save mode supported
The rate of conversion configuration
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
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).
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
#include is a preprocessor directive, used to read the whole content of the other
files. Usually it is use to read the header files.
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.
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
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
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
a [0]
[0] 0
[1] 1
[2] 3
Figure 17: Example of an initialized 1-D array
Formatted Output
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.
[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.
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.
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
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
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
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)].
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]
}
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 [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.
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
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)]
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
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.
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.
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.
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.
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
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.
MAIN a = 0
MAIN c = 10
FUNC a = 1
FUNC c = 10
Figure 50: Shows the output of the program above
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
Keil Version 3
DOWNLOAD: Keil ARM tool kit i.e. Keil Vision3
(http://www.keil.com/demo/download.asp)
8. Press Save
5. Select CPU
Under CPU tab, find Philips, if that does not existed, look for NXP (founded
by Philips)
8. Press No
22. Under Select ARM Development Tools > Select Use Keil CARM Compiler
23. Press OK
27. Select Debug Information, Browse Information and Create HEX File
28. Press OK
1. Set Communication
Under Communication
Connected to Port: COM1 / COM5 (for USB)
Baud Rate: 9600
3. Under Device
Set XTAL Freq. (kHz) to 12000
7. Upload to Flash
Noticed the Progress bar
2. Select NO
3. Select CANCEL
5. Select OK
7. Select OK
8. Connect using: COM1 (if serial port is connected directly) or COMx (if connect
through a USB to serial converter)
11. Select OK
13. To reconnect
15. Select OK
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
Function(s)
init
o The board initialization function
delay_ms
o A delay FOR Loop
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
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
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
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
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 {
Exercise
1. Write a program that switch LED P0.22 ON when PRESS switch P0.29
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.
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
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);
Exercise
1. Write a program that switch LED P0.22 ON when ENTER the Interrupt Service
Routine
Pseudo Code
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;
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);
}
Pseudo Code
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
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
Exercise
1. Add more options to control LED P0.21
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
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);
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
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
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
Exercise
1. On the previous example program. Change alarm to 20 second
3. Display Day of the month (DOM), month (MONTH) and year (YEAR)
sprintf(s,"TIME: %d:%d:%d\r",DOM,MONTH,YEAR);
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
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;
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
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;
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);
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
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));
Exercise
1. Add more buttons for switching LED P0.21 ON and OFF
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).
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;
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
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;
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
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 {
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.
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
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;
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.
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;
Function(s)
init
o The board initialization function
delay_ms
o A delay FOR Loop
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;
iNEX. Philips’s LPC2148 32-bit ARM7 microcontroller Basic Experiment manual (คูมือ
ทดลองไมโครคอนโทรลเลอร 32 บิตตระกูล ARM7 เบือ ้ งตน สําหรับ LPC2148 ของ Philips)
Thailand: Innovative Experiment Inc.