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 32-bit Microcontroller ARM7 LPC2148 Manual
2
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 32-bit Microcontroller ARM7 LPC2148 Manual
3
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 32-bit Microcontroller ARM7 LPC2148 Manual
4
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 32-bit Microcontroller ARM7 LPC2148 Manual
5
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 32-bit Microcontroller ARM7 LPC2148 Manual
6
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 32-bit Microcontroller ARM7 LPC2148 Manual
7
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 32-bit Microcontroller ARM7 LPC2148 Manual
8
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 32-bit Microcontroller ARM7 LPC2148 Manual
1-9
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 highperformance 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 32-bit Microcontroller ARM7 LPC2148 Manual
1-10
Figure 2: System memory map (user program viewpoint)
APB Peripheral 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - 22
Base Address 0xE000 0000 0xE000 4000 0xE000 8000 0xE000 C000 0xE001 0000 0xE001 4000 0xE001 8000 0xE001 C000 0xE002 0000 0xE002 4000 0xE002 8000 0xE002 C000 0xE003 0000 0xE003 4000 0xE003 8000 0xE005 8000
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
Peripheral Name Watchdog timer Timer 0 Timer 1 UART0 UART1 PWM Not used I2C0 SPI0 RTC GPIO Pin connect block Not used ADC0 Not used
1-11
23 24 25 26 27 28 - 35 36 37 - 126 127
0xE005 C000 0xE006 0000 0xE006 4000 0xE006 8000 0xE006 C000 0xE007 0000 0xE008 C000 0xE009 0000 0xE009 4000 0xE01F 8000 0xE01F C000
I2C1 ADC1 Not used SSP DAC Not used USB Not used 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 32-bit Microcontroller ARM7 LPC2148 Manual
1-12
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 32-bit Microcontroller ARM7 LPC2148 Manual
1-13
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 microprocessors, there is program to access is not Thus, LPC2148 consumes
Clock in itself that uses a 3V reserved battery. Unlike other no need for an extra RTC IC. To read the RTC values, a needed, just connect some registers and enable the RTC. 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 32-bit Microcontroller ARM7 LPC2148 Manual
1-14
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 32-bit Microcontroller ARM7 LPC2148 Manual
1-15
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 32-bit Microcontroller ARM7 LPC2148 Manual
1-16
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. 2. 3. 4.
Write a program in C language Save it as .c extension Compile the program If necessary, make corrections to any errors found during compilation and recompile 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 2 3 4 5 6
#include main() { printf("Hello World!!"); }
Figure 5: A Simple C Program (line 1) (line 3-6)
#include is the Header
main() function
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
2-17
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
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 32-bit Microcontroller ARM7 LPC2148 Manual
2-18
Types of Variables Types define the type of the variables.
Type int float double char
Meaning A Integer A Decimal A double precision range decimals (float) A Character
Table 2: Types of Variable
Variants of the Variable Variants overwrite the original range of the variables.
Variants short long unsigned unsigned long
Meaning A reduce Integer range An increase Integer range A positive-only Integer range 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 0078 1cat cat cat1 cat_ cat_1
Validity NO NO YES YES NO YES
Table 4: Examples of variable names
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
2-19
Reserved Keywords Reserved keywords are names of the variables that cannot be use. auto break case char continue do double else enum extern for goto if int long return short sizeof static struct typedef union unsigned void while
default float register switch
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 32-bit Microcontroller ARM7 LPC2148 Manual
2-20
#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] [0] [1] [2] [3]
[1]
[2]
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 32-bit Microcontroller ARM7 LPC2148 Manual
2-21
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] [3]
[0] 0 1 2 3
[1] 4 5 6 7
[2] 8 9 10 11
Figure 18: Example of an initialized 2-D array
Formatted Output NOTE: Always include #include
printf() Control String %d %f %c %s
Prints An Integer A Decimal A Character 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 32-bit Microcontroller ARM7 LPC2148 Manual
2-22
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 32-bit Microcontroller ARM7 LPC2148 Manual
2-23
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 Precedence 1 2
3
4 5 6
7 8 9 10 11 12 13 14 15
Description
Representations
Parenthesis (Unary) Not Negative Sign Increment Value by 1 Multiplication Division Modulus Addition Subtraction Shift Right Shift Left Greater Than Less Than Greater Than or Equal To Less Than or Equal To Equal Not Equal Bitwise AND Bitwise Exclusive OR Bitwise OR Logical AND Logical OR Conditional Expression Assignment Comma
() [] ! – ++ * / % + >> << > < >= <= == != & ^ | && || ?: = ,
Table 9: Operator Precedence
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
2-24
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 2 3 4 5 6 7 8 9 10
#include main() { int a = 0; int b = 7; int c = 3; a = b + c; printf(“%d”,a); }
Figure 25: Example of how an assignment statement works (line 5-7) (line 8) (line 9)
Define variables a, b, c as type Integer Assign 0 to a, 7 to b and 3 to c Evaluate b + c, that is 7 + 3, then assign the result to a on the left hand side of the equation 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 32-bit Microcontroller ARM7 LPC2148 Manual
2-25
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 2 3 4 5 6 7 8 9 10
#include main() { int a=0; if(a==0) { printf("Hello World!!"); } }
Figure 28: Example of an IF statement (line 5) (line 6)
(line 8)
Define variable a as type Integer Assign 0 to a 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 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 32-bit Microcontroller ARM7 LPC2148 Manual
2-26
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 2 3 4 5 6 7 8 9 10 11 12 13 14
#include main() { int a=0; if(a==0) { printf("Hello World!!"); } else { printf("Hello People!!"); } }
Figure 30: Example of an IF ELSE statement (1) (line 5) (line 6)
(line 8)
(line 12)
Define variable a as type Integer Assign 0 to a 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 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] 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 32-bit Microcontroller ARM7 LPC2148 Manual
2-27
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. 2. 3. 4. 5. 6. 7.
If [condition #1] is TRUE then execute [statement(s) #1] only. If [condition #1] is FALSE, check if [condition #2] is TRUE or not. If [condition #2] is TRUE then execute [statement(s) #2] only. If [condition #2] is FALSE, check if [condition #3] is TRUE or not. And so on to [condition #n-1]. If the [condition #n-1] is TRUE then execute [statement(s) #n-1] only. 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 32-bit Microcontroller ARM7 LPC2148 Manual
2-28
Below is an example of an IF ELSE statement (2). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include main() { int a=0; if(a==0) { printf("Hello World!!"); } else if(a==1) { printf("Hello People!!"); } else { printf("Hello Aliens!!"); } }
Figure 32: Example of an IF ELSE statement (2) (line 5) (line 6)
(line 8)
(line 10)
(line 12)
(line 16)
Define variable a as type Integer Assign 0 to a 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 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]. 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 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]. 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 32-bit Microcontroller ARM7 LPC2148 Manual
2-29
SWITCH statement SWITCH statement works the same as the IF ELSE statement. switch([variable]) { case [character/integer #1] case [character/integer #2]
: [statement(s) #1]; break; : [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. 2. 3. 4.
If [variable] matches [character/integer #1] then execute [statement(s) #1]; If [variable] matches [character/integer #2] then execute [statement(s) #2]; And so on to case [character/integer #n-1]. 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include main() { int number=1; switch(number) { case 1 : printf("One"); break; case 2 : printf("Two"); break; case 3 : printf("Three"); break; default : printf("None"); break; } }
Figure 34: Example of an IF ELSE statement (2) (line 5) (line 6) (line 8)
(line 9)
Define variable number as Integer Assign 1 to number number is the trigger [variable] printf("One"); is [statement(s) #1] Display One on screen. If the number is 1 then execute [statement(s) #1] break; causes a break out of the SWITCH statement
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
2-30
(line 10)
printf("Two"); is [statement(s) #2]
Display Two on screen. (line 11) (line 12)
If the number is 2 then execute [statement(s) #2] break; causes a break out of the SWITCH statement printf("Three"); is [statement(s) #3] Display Three on screen.
(line 13) (line 14)
If the number is 3 then execute [statement(s) #3] break; causes a break out of the SWITCH statement printf("None"); is [statement(s) #4] Display None on screen.
(line 15)
If the number is not 1, 2 or 3 then execute [statement(s) #4] 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 32-bit Microcontroller ARM7 LPC2148 Manual
2-31
Below is an example of a WHILE loop. 1 2 3 4 5 6 7 8 9
#include main() { while(1) { printf("Hello World!!"); } }
Figure 36: Example of a WHILE loop (line 5) (line 7)
1 is the [condition] When [condition] is 1, it is always TRUE 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 2 3 4 5 6 7 8 9 10
#include main() { do { printf("Hello World!!"); } while(1) }
Figure 38: Example of a DO WHILE loop
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
2-32
(line 7)
(line 9)
printf("Hello World!!"); is [statement(s)]
Display Hello World!! on screen. Execute [statement(s)]. 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 2 3 4 5 6 7 8 9
#include main() { for(int i=0; i<10; i++) { printf("Hello World!!"); } }
Figure 40: Example of a FOR loop
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
2-33
(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 2 3 4 5 6 7 8 9 10 11 12 13
#include main() { int a[5][10]={0}; for(int i=0; i<5; i++) { for(int j=0; j<10; j++) { a[i][j] = i*j; } } }
Figure 41: Example of a Nested Loop (line 5) (line 6)
Define a as a 2-D array of type Integer Dimension of a is 5 by 10 Assign 0 to all elements of a 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 32-bit Microcontroller ARM7 LPC2148 Manual
2-34
(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.
(line 10)
Increase the value of j by 1 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 function.
in
the
program
must
located
above
the
main()
[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 32-bit Microcontroller ARM7 LPC2148 Manual
2-35
Below is an example of a user-defined function. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include int addition(int a, int b) { int r; r = a + b; return(r); } void main() { int z; z = addition(5,3); printf("The result is %d", z); }
Figure 44: Example of a program with a user-defined function NOTE: A C program begin its execute at the main() Therefore, begin the program walkthrough at (line 10)
(line 3)
function.
int is the [type] int indicates that the function will return an integer value back to the main() function.
(line 5) (line 6) (line 7)
(line 10)
(line 12) (line 13)
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) Define variable r as type Integer Evaluate a + b, that is 5 + 3, then assign the result to r on the left hand side of the equation 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) 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. Define variable z as type Integer 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 32-bit Microcontroller ARM7 LPC2148 Manual
2-36
3 . . . 13
int
addition(int a, int b)
z = addition(
5
,
3
);
Figure 45: A closer look at (line 13) to (line 3) 3 . . . 13
int
addition(int a, int b)
z = addition(
5
,
3
);
Figure 46: A closer look on at (line 3) to (line 13)
Functions with no Type A function with no type is used when there is no need to return a value back to 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 2 3 4 5 6 7 8 9 10 11
#include void printFUNC() { printf("Hello World!"); } void main() { printFUNC(); }
Figure 48: : Example of a program with a user-defined function with no Type (line 3)
void is the [type] void indicates that the function will not return a value back to the main() function. printFUNC is the [name] of the function () has no [parameter] Once the function execute all statements, it returns to the main() function with returning a value. Go to (line 11)
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
2-37
(line 5)
printf("Hello World!");
Display The result is 8 on screen. void is the [type] void indicates that the function does not return a value.
(line 8)
main is the [name] of the function main function is where the program begin its execution. printFUNC() calls for a function named printFUNC, without passing a value. Go to (line 3)
(line 10)
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include int c = 10; GLOBAL VARIABLE void printFUNC() { int a = 1; LOCAL VARIABLE printf("FUNC a = %d \n", a); printf("FUNC c = %d", c); } void main() { int a = 0; LOCAL VARIABLE printf("MAIN a = %d \n", a); printf("MAIN c = %d \n", c); printFUNC(); }
Figure 49: Example of a program showing the scope of a variable
MAIN MAIN FUNC FUNC
a c a c
= = = =
0 10 1 10
Figure 50: Shows the output of the program above
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
2-38
Chapter 3: Setup Components The board components required for the setup.
Item List Board: JX-2148 Serial Port CX-232 Cable DC Adaptor: +6V 500mA (Maximum +9V) Serial to USB Converter (optional)
Quantity 1 1 1 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 32-bit Microcontroller ARM7 LPC2148 Manual
3-39
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-40
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-41
Figure 54: “Select Device” (1) 6. Next find LPC2148
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
3-42
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-43
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-44
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-45
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-46
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-47
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-48
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-49
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-50
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-51
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-52
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-53
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 32-bit Microcontroller ARM7 LPC2148 Manual
3-54
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 32-bit Microcontroller ARM7 LPC2148 Manual
4-55
Source Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "lpc214x.h" #define LED1_ON FIO0CLR = 0x00400000 #define LED1_OFF FIO0SET = 0x00400000 #define LED2_ON FIO0CLR = 0x00200000 #define LED2_OFF FIO0SET = 0x00200000 void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for(i=0; i
Figure 79: Example of an LED program
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
4-56
Exercise 1. Display the LED in the following sequence
LED P0.21 LED P0.22 Delay LED P0.22 LED P0.21 Delay LED P0.21 Delay LED P0.21 LED P0.22 Delay
ON OFF ON OFF ON OFF OFF
2. Display the LED in the following sequence P0.21 P0.22
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
4-57
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 32-bit Microcontroller ARM7 LPC2148 Manual
5-58
Source Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "lpc214x.h" void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for(i=0; i>_bit); } void main() { init(); SCS = 0x03; FIO0DIR |= 0x00200000; FIO0SET |= 0x00200000; while(1) { if(inp0(28)==0) { delay_ms(1); if(inp0(28)==0) { delay_ms(1); if(inp0(28)==0) { while(inp0(28)==0); if(inp0(28)==1) {
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
5-59
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
FIO0PIN ^= (1<<21); delay_ms(1); if(inp0(28)==1) { delay_ms(1); if(inp0(28)==1) { while(inp0(28)==1) { break; } } } } } } } } }
Figure 80: Example of a SWITCH program
Exercise 1. Write a program that switch LED P0.22 ON when PRESS switch P0.29
Output at LED P0.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 32-bit Microcontroller ARM7 LPC2148 Manual
5-60
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 32-bit Microcontroller ARM7 LPC2148 Manual
6-61
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 32-bit Microcontroller ARM7 LPC2148 Manual
6-62
Source Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "lpc214x.h" #include "sound.h" #include "uart.h" void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for(i=0; i
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
6-63
58 59 60 61 62 63 64 65 66 67
EXTMODE |= 0x4; EXTPOLAR = 0x00; PINSEL0 |= 0xC000; VICVectAddr0 = (unsigned)isr_int2; VICVectCntl0 = 0x20 | 16; VICIntEnable |= 1 << 16; while(1); }
Figure 83: Example of an EXTERNAL INTERRUPT program
Exercise 1. Write a program that switch LED P0.22 ON when ENTER the Interrupt 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 32-bit Microcontroller ARM7 LPC2148 Manual
6-64
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include "lpc214x.h" #include "uart.h" #include "stdio.h" void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02;
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
7-65
24 25 26 27 28 29 30 31 32 33 34 35 36 37
} void main() { init(); SCS = 0x03; uart0_init(9600); while (1) { uart0_puts("Hello World! PORT0\r\n"); } }
Figure 84: Example of UART0 program
Exercise 1. Display number 10 9 8 7 6 5 4 3 2 1 repeatedly Hint: use FOR loop
o o
Define Buffer char s[50]; 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 32-bit Microcontroller ARM7 LPC2148 Manual
7-66
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 32-bit Microcontroller ARM7 LPC2148 Manual
8-67
Source Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include #include #include #include
"lpc214x.h" "uart.h" "stdio.h" "stdlib.h"
void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void isr_uart1(void) __irq { char msg; if(((msg = U1IIR) & 0x01) == 0) { switch (msg & 0x0E) { case 0x04: while(!(U1LSR & 0x20)); U1THR = U1RBR; break; case 0x02: break; default: break; } } VICVectAddr = 0; } void main() { init(); SCS = 0x03; uart1_init(9600); U1IER = 3; PINSEL0 |= (1<<18); VICVectAddr0 = (unsigned)isr_uart1; VICVectCntl0 = 0x20 | 7; VICIntEnable = 1 << 7;
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
8-68
58 59 60 61
puts("Now test UART1 for echo character\n"); puts("Press any key for test!\n"); while(1); }
Figure 85: Example of UART1 program
Exercise 1. Add more options to control LED P0.21 (in Main function) Select Port P0.21 as output FIO0DIR = 0x00200000; (in UART1 interrupt service routine) void isr_uart1(void) __irq IF echo character (U1THR) equals ‘a’, switch LED P0.21 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 32-bit Microcontroller ARM7 LPC2148 Manual
8-69
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 32-bit Microcontroller ARM7 LPC2148 Manual
9-70
Source Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "lpc214x.h" #include "stdio.h" #include "uart.h" #define LED2_ON FIO0CLR = 0x00200000 #define LED2_OFF FIO0SET = 0x00200000 int adc=0; void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)) ; PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 6659; j++ ); } void main() { int val=0; int a=0; char s[30]; init(); SCS = 0x03; uart0_init(9600); FIO0DIR |= 0x00200000; LED2_OFF; PINSEL1 |= 0x10000000; AD0CR = 0x00210608; while (1) { AD0CR |= 0x01000000; while ((AD0DR3 & 0x80000000) == 0);
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
9-71
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
val = ((AD0DR3 >> 6) & 0x03FF); adc = val; for(a=0; a <(adc/20); a++ ) { uart0_puts(" "); } if(adc<=500 && adc>=400) { LED2_ON; delay_ms(100); LED2_OFF; delay_ms(100); LED2_ON; delay_ms(100); LED2_OFF; } sprintf(s,"[%d]\r\n",adc); uart0_puts(s); delay_ms(250); } }
Figure 86: Example of an ANALOG to DIGITAL program
Exercise 1. Modify the example program above, so that when the DIGITAL SIGNAL range 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 32-bit Microcontroller ARM7 LPC2148 Manual
9-72
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 32-bit Microcontroller ARM7 LPC2148 Manual
10-73
o
Clear Interrupt vector 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "lpc214x.h" #include "sound.h" #include "stdio.h" #include "uart.h" #define LED1_ON FIO0CLR = 0x00400000 #define LED1_OFF FIO0SET = 0x00400000 char alarm = 0; void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 6659; j++ ); } void isr_rtc(void) __irq
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
10-74
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
{ if(ILR & 0x01) { LED1_ON; delay_ms(100); LED1_OFF; ILR = 0x01; } if(ILR & 0x02) { alarm = 1; ILR = 0x02; } VICVectAddr = 0; } void main() { char i=0; char s[50]; init(); SCS = 0x03; FIO0DIR |= 0x00400000; uart0_init(9600); PREINT = 0x00000392; PREFRAC = 0x00004380; CIIR = 0x00000001; ALSEC = 0x00000003; AMR = 0x000000FE; CCR = 0x00000001; VICVectAddr13 = (unsigned)isr_rtc; VICVectCntl13 = 0x20 | 13; VICIntEnable |= (1<<13); while (1) { if(alarm==1) { beep(); i++; if(i>10) { i=0; alarm = 0; } } if(HOUR < 10) { sprintf(s,"TIME: 0%d:",HOUR); } else { sprintf(s,"TIME: %d:",HOUR); } uart0_puts(s);
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
10-75
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
if(MIN < 10) { sprintf(s,"0%d:",MIN); } else { sprintf(s,"%d:",MIN); } uart0_puts(s); if(SEC < 10) { sprintf(s,"0%d\r",SEC); } else { sprintf(s,"%d\r",SEC); } uart0_puts(s); delay_ms(100); } }
Figure 87: Example of a Real Time Clock (1) program
Exercise 1. On the previous example program. Change alarm to 20 second
Set alarm SECOND to 20 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 32-bit Microcontroller ARM7 LPC2148 Manual
10-76
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 32-bit Microcontroller ARM7 LPC2148 Manual
11-77
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "lpc214x.h" #include "stdio.h" #include "uart.h" #include "ctype.h" #include "stdlib.h" char key = 0; char s[30] = {0}; void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for(i=0; i
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
11-78
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
break; case 0x02: break; default: break; } } VICVectAddr = 0; } void rtc_uart1_setup(char *s) { unsigned char tm; char i = 0; for (i=0; i<2; i++) { while(!isdigit(key)); if(i==0) tm = 10 * atoi(&key); if(i==1) tm = tm + atoi(&key); putchar(key); key = 0; } *s = tm; } void main() { init(); SCS = 0x03; uart1_init(9600); U1IER = 3; PINSEL0 |= (1<<18); VICVectAddr0 = (unsigned)isr_uart1; VICVectCntl0 = 0x20 | 7; VICIntEnable |= 1 << 7; CCR = 0x00000011; while (1) { printf("TIME: %d:%d:%d \r",HOUR,MIN,SEC); delay_ms(100); if (key == '*') { key = 0; printf("\nSet Time:"); rtc_uart1_setup(&HOUR); uart1_putc(':'); rtc_uart1_setup(&MIN); uart1_putc(':'); rtc_uart1_setup(&SEC); printf("\nTIME: %d:%d:%d \r",HOUR,MIN,SEC); } } }
Figure 88: Example of a Real Time Clock (2) program
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
11-79
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 32-bit Microcontroller ARM7 LPC2148 Manual
11-80
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include #include #include #include #include char key
"lpc214x.h" "stdio.h" "sound.h" "uart.h" "keyboard.h" = 0;
void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55;
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
12-81
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void main() { init(); SCS = 0x03; kb_init(); uart1_init(9600); while (1) { printf("%c",kb_getchar()); } }
Figure 89: Example of a Keyboard (1) program
Exercise 1. On the previous program, change from UART1 to UART0
Initialize UART0; uart0_init(9600); Send output to UART0 by: o Define Buffer char s[50]; o Send output of characters to UART0 sprintf(s,"%c",kb_getchar()); uart0_puts(s);
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
12-82
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 32-bit Microcontroller ARM7 LPC2148 Manual
13-83
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include #include #include #include #include
"lpc214x.h" "stdio.h" "sound.h" "uart.h" "keyboard.h"
#include "ctype.h" #include "stdlib.h" #define #define #define #define
LED1_ON FIO0CLR = 0x00400000 LED1_OFF FIO0SET = 0x00400000 LED2_ON FIO0CLR = 0x00200000 LED2_OFF FIO0SET = 0x00200000
char alarm = 0; char key = 0; char s[30] = {0}; int a = 1; void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400));
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
13-84
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void print_menu() { uart0_puts("A: LED\r\n"); uart0_puts("B: SOUND\r\n"); } void delay_ms(long ms) { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 6659; j++ ); } void isr_rtc(void) __irq { if(ILR & 0x01) { LED1_ON; delay_ms(100); LED1_OFF; ILR = 0x01; } if(ILR & 0x02) { alarm = 1; ILR = 0x02; } VICVectAddr = 0; } void rtc_kb_setup(char *s) { unsigned char tm; char i = 0; char aa[30] = {0}; char num = 0; for (i=0; i<2; i++) { while(!isdigit(num)) { num = kb_getchar(); } if(i==0) { tm = 10 * atoi(&num); } if(i==1) { tm = tm + atoi(&num); } sprintf(aa,"%c",num);
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
13-85
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
uart0_puts(aa); num = 0; } *s = tm; } void main() { char ch = 0; init(); SCS = 0x03; FIO0DIR |= 0x00600000; FIO0SET |= 0x00600000; kb_init(); uart0_init(9600); print_menu(); while (1) { ch = kb_getchar(); if (ch == 'a') { uart0_puts(" uart0_puts(" uart0_puts("
1: P.21 ON\r\n 2: P.21 OFF\r\n"); 3: P.22 ON\r\n 4: P.22 OFF\r\n"); Q: BACK\r\n\n");
while (a == 1) { ch = kb_getchar(); switch (ch) { case '1': LED2_ON; break; case '2': LED2_OFF; break; case '3': LED1_ON; break; case '4': LED1_OFF; break; default: uart0_puts(" ** PLEASE INPUT 1,2,3,4 or Q\r"); break; } if(ch == 'q') { uart0_puts("\n\n"); print_menu(); a = 0; } } } else if (ch == 'b') { uart0_puts("
1: BEEP ON\r\n Q: BACK\r\n\n");
while (a == 1) { ch = kb_getchar(); switch (ch)
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
13-86
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
{ case '1': default:
beep(); break; uart0_puts(" ** PLEASE INPUT 1 or Q\r"); break;
} if(ch == 'q') { uart0_puts("\n\n"); print_menu(); a = 0; } } } a = 1; } }
Figure 90: Example of a Keyboard (2) program
Exercise 1. Add more buttons for switching LED P0.21 ON and OFF
Press ‘5’ to switch LED P0.21 ON Press ‘6’ to switch LED P0.21 OFF
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
13-87
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 32-bit Microcontroller ARM7 LPC2148 Manual
14-88
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 32-bit Microcontroller ARM7 LPC2148 Manual
14-89
Source Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include #include #include #include #include
"lpc214x.h" "sound.h" "stdio.h" "uart.h" "keyboard2.h"
#include "ctype.h" #include "stdlib.h" #define #define #define #define
LED1_ON FIO0CLR = 0x00400000 LED1_OFF FIO0SET = 0x00400000 LED0_ON FIO0CLR = 0x00200000 LED0_OFF FIO0SET = 0x00200000
char alarm = 0; char s[30]; char key = 0; void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 6659; j++ ); } void isr_rtc(void) __irq { if(ILR & 0x01) { LED1_ON; delay_ms(100); LED1_OFF; ILR = 0x01; } if(ILR & 0x02) { alarm = 1; ILR = 0x02;
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
14-90
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
} VICVectAddr = 0; } void rtc_kb_setup(char *s) { unsigned char tm; char i = 0; for (i=0; i<2; i++) { while(!isdigit(key)); if(i==0) { tm = 10 * atoi(&key); } if(i==1) { tm = tm + atoi(&key); } putchar(key); key = 0; } *s = tm; } void isr_uart1(void) __irq { char msg; if (((msg = U1IIR) & 0x01) == 0) { switch (msg & 0x0E) { case 0x04: while(!(U1LSR & 0x20)); key = U1RBR; break; case 0x02: break; default: break; } } VICVectAddr = 0; } void main() { char i=0; init(); SCS = 0x03; kb_init(); FIO0DIR |= 0x00400000; VICVectAddr5 = (unsigned)isr_uart1; VICVectCntl5 = 0x20 | 7; VICIntEnable |= 1 << 7; uart1_init(9600); U1IER = 3; PINSEL0 |= (1<<18); PREINT = 0x00000392; PREFRAC = 0x00004380;
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
14-91
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
CIIR = 0x00000001; ALSEC = 0x00000003; AMR = 0x000000FF; VICVectAddr13 = (unsigned)isr_rtc; VICVectCntl13 = 0x20 | 13; VICIntEnable |= (1<<13); CCR = 0x00000001; while (1) { printf("%c TIME: %d:%d:%d\r",key,HOUR,MIN,SEC); delay_ms(100); if (key == '1') { key = 0; AMR = 0x000000FE; printf("\n[AMR=0xFE]\r\n"); } else if (key == '2') { key = 0; AMR = 0x000000FF; printf("\n[AMR=0xFF]\r\n"); } else if (key == '3') { key = 0; printf("\n\n Set ALARM Second: "); rtc_kb_setup(&ALSEC); AMR = 0x000000FE; } if(alarm==1) { beep(); i++; if(i>10) { i=0; alarm = 0; } } } }
Figure 91: Example of a Keyboard (3) program
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
14-92
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 32-bit Microcontroller ARM7 LPC2148 Manual
14-93
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 32-bit Microcontroller ARM7 LPC2148 Manual
15-94
Source Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include #include #include #include
"lpc214x.h" "stdio.h" "sound.h" "uart.h"
#define LED2_ON FIO0CLR = 0x00200000 #define LED2_OFF FIO0SET = 0x00200000 void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 6659; j++ ); } void main() { int val = 0; int adc = 0; int a = 0; char s[20] = {0}; init(); SCS = 0x03; uart0_init(9600); uart0_puts("Yo!\r\n"); FIO0DIR |= 0x00200000; FIO0SET |= (1<<21); PINSEL0 |= 0x0C00; // Select PIN 0.5 as AD0.7 AD0CR = 0x00210680; while(1) {
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
15-95
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
AD0CR |= 0x01000000; while ((AD0DR7 & 0x80000000) == 0); val = ((AD0DR7 >> 6) & 0x03FF); adc = val; for(a=0; a <(adc/20); a++ ) { uart0_puts(" "); } if(adc > 200) { LED2_ON; delay_ms(100); LED2_OFF; delay_ms(100); LED2_ON; delay_ms(100); LED2_OFF; } sprintf(s,"[%d]\r\n",adc); uart0_puts(s); delay_ms(250); } }
Figure 92: Example of DIP Switch (1) program
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
15-96
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 32-bit Microcontroller ARM7 LPC2148 Manual
16-97
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 32-bit Microcontroller ARM7 LPC2148 Manual
16-98
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 32-bit Microcontroller ARM7 LPC2148 Manual
16-99
FIO0CLR = 0x00400000; o o
o o
o o
o
Delay for 1000 ms delay_ms(1000) 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; Delay for 1000 ms delay_ms(1000) 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; Delay for 1000 ms delay_ms(1000) 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; 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 2 3 4 5 6 7 8 9 10 11 12
#include #include #include #include
"lpc214x.h" "stdio.h" "sound.h" "uart.h"
#define LED2_ON FIO0CLR = 0x00200000 #define LED2_OFF FIO0SET = 0x00200000 void init() { PLL0CFG=0x24; PLL0FEED=0xAA;
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
16-100
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 6659; j++ ); } char inp0(char _bit) { unsigned long c; c = 1<<_bit; FIO0DIR &= ~c; return((FIO0PIN & c)>>_bit); } char inp1(char _bit) { unsigned long d; d = 1<<_bit; FIO1DIR &= ~d; return((FIO1PIN & d)>>_bit); } void main() { int val = 0; int adc = 0; int a = 0; char s[20] = {0}; init(); SCS = 0x03; uart0_init(9600); uart0_puts("Yo!\r\n"); FIO0DIR |= 0x00600000; FIO1DIR |= 0x00000000; FIO0SET |= 0x00600000; PINSEL0 |= 0x00000000; PINSEL1 |= 0x00000000;
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
16-101
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
while(1) { sprintf(s,"%d\r\n",FIO0PIN); uart0_puts(s); /* PORT 0 */ if(inp0(25)==1) { uart0_puts("A\r\n"); FIO0CLR = 0x00400000; } delay_ms(500); if(inp0(4)==0) { uart0_puts("B\r\n"); FIO0SET = 0x00400000; } delay_ms(500); if(inp0(5)==1) { uart0_puts("C\r\n"); FIO0CLR = 0x00200000; } delay_ms(500); if(inp0(6)==0) { uart0_puts("D\r\n"); FIO0SET = 0x00200000; } delay_ms(500);
/* PORT 1 */ if(inp1(19)==0) { uart0_puts("AA\r\n"); FIO0CLR = 0x00400000; } delay_ms(1000); if(inp1(22)==0) { uart0_puts("BB\r\n"); FIO0SET = 0x00400000; } delay_ms(1000); if(inp1(16)==1) { uart0_puts("CC\r\n"); FIO0CLR = 0x00200000; } delay_ms(1000); if(inp1(21)==1) { uart0_puts("DD\r\n"); FIO0SET = 0x00200000; } delay_ms(1000); } }
Figure 95: Example of DIP Switch (2) program
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
16-102
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 32-bit Microcontroller ARM7 LPC2148 Manual
17-103
Figure 97: LEDs Circuit (2)
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
17-104
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 32-bit Microcontroller ARM7 LPC2148 Manual
17-105
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 32-bit Microcontroller ARM7 LPC2148 Manual
17-106
Source Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include #include #include #include
"lpc214x.h" "stdio.h" "sound.h" "uart.h"
void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)); PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } void delay_ms(long ms) { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 6659; j++ ); } void main() { init(); SCS = 0x03; uart0_init(9600); uart0_puts("Yo!\r\n"); FIO0DIR |= 0x00200000; FIO0DIR |= 0x02000070; FIO1DIR |= 0x00650000; FIO0SET |= 0x00200000; while(1) { FIO0SET |= 0x02000020; FIO1SET |= 0x00210000; FIO0CLR |= 0x00000050; FIO1CLR |= 0x00440000; delay_ms(500); FIO0SET |= 0x00000050; FIO1SET |= 0x00440000; FIO0CLR |= 0x02000020;
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
17-107
58 59 60 61
FIO1CLR |= 0x00210000; delay_ms(500); } }
Figure 99; Example of DIP Switch (2) program
© 2007 Apaporn Boonyarattaphan 32-bit Microcontroller ARM7 LPC2148 Manual
17-108
References Exforsys Inc (2007). C Programming – An Overview. Website: http://www.exforsys.com/tutorials/c-language/c-programming-anoverview.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 32-bit Microcontroller ARM7 LPC2148 Manual
109