LPC2148 Intro
LPC2148 Intro
LPC2148
Introduction
• ARM based microcontroller
• It is one of the widely used and highly successful ARM7 based Microcontroller.
• Even though ARM7 family is considered obsolete with the introduction of many advanced
processors and controllers, it is one of the best and easiest microcontrollers to work around
• Due to their tiny size and low power consumption, LPC2142/2148 are ideal for applications
• Input/output pins are the only way to interface with the microcontroller.
• Each Digital Port Pin Has A Direction Bit. The Digital Port Pins On Most
Microcontrollers Are Bidirectional.
PORT
• A Parallel I/O Port is a Simple Mechanism that allows the Software to interact with
external Devices. It is called Parallel because multiple signals can be accessed all at
once.
• An Input Port, which allows the Software to Read External Digital Signals, Is Read Only.
• That Means A Read Cycle Access From The Port Address Returns The Values Existing
On the Inputs at that time.
A read cycle access to an input port usually
produces no effect.
The digital values existing on the input pins are
copied into the microcontroller when the software
executes a read from the port address.
• These two ports are of 32-bit wide and are provided by the 64 pins of the microcontroller.
• Port 0 is a 32bit I/O port with individual direction controls for each bit.
• Total of 28 pins of Port 0 can be used as a general purpose bi directional digital I/O while
P0.31 provides digital output functions only.
• The operation of Port 0 pins depends upon the pin function selected via the pin connector
• Pins P0.24, P0.26 and P0.27 are not available /are invisible pins, remaining 29 are visible i/o
pins
PORTS of LPC2148 (2/2)
• Port 1 is a 32 bit bidirectional i/o port with individual direction controls for each
bit.
• The operation of Port 1 depends upon the pin function selected via to pin select.
Pin 0 through 15 of port1 are not available/not visible and 16 to 31 pins are
visible.
• port 0 and port 1 are controlled by groups of registers.
LPC2148 has two IO ports namely PORT0 (P0) and PORT1 (P1). These two IO ports are of 32-bit wide and
are provided by the 64 pins of the microcontroller.
Hence in order to select one of the four functions, two corresponding bits of the PINSEL register
are needed.
So, a 32-bit PINSEL register can control 16 pins with 2-bits to control
each pin.
There are 5 Fast (also called Enhanced GPIO Features Registers) GPIO Registers and 4 Slow
(also called Legacy GPIO Registers) GPIO Registers available to control PORT0 and PORT1.
The default function of all the Pins is GPIO. But it is a good programming practice to
mention “PINSEL0=0” in order to select the GPIO function of the Pins.
• IODIR: This is GPIO Port Direction control register. This register individually Controls the direction of
each port pin
• IOCLR: This is GPIO Port Output Clear registers. This register controls the state of output pins.
Writing ones produces lows at the corresponding port pins and clears the corresponding bits in the
IOSET register. Writing zeroes has no effect.
• IOSET: This is GPIO Port Output Set registers. This register controls the state of output pins in
conjunction with the IOCLR register. Writing ones produces highs at the corresponding port pins.
Writing zeroes has no effect.
1. IOxPIN (GPIO Port Pin value register):
This is a 32-bit wide register. This register is used to read/write the value on Port (PORT0/PORT1). But care
should be taken while writing. Masking should be used to ensure write to the desired pin.
IOPIN: It is a GPIO Port Pin Value register and can be used to read or write values directly to the
pin. The status of the Pins that are configured as GPIO can always be read from this
register irrespective of the direction set on the pin (Input or Output)
The syntax for this register is IOxPIN, where ‘x’ is the port number i.e. IO0PIN for PORT0 and
IO1PIN for PORT1.
Examples :
a) Writing 1 to P0.4 using IO0PIN
IO0PIN = IO0PIN | (1<<4)
b) Writing 0 to P0.4 using IO0PIN
IO0PIN = IO0PIN & (~(1<<4) )
c) Writing F to P0.7-P0.4
IO0PIN = IO0PIN | (0x000000F0)
2. IOxSET (GPIO Port Output Set register) :
IOSET: It is a GPIO Port Output Set Register and can be used to set the value of a GPIO pin
that is configured as output to High (Logic 1). When a bit in the IOSET register is set to ‘1’, the
corresponding pin is set to Logic 1. Setting a bit ‘0’ in this register has no effect on the pin.
The syntax for this register is IOxSET, where ‘x’ is the port number i.e. IO0SET for PORT0 and
IO1SET for PORT1.
Examples :
a) Configure pin P0.0 to P0.3 as input pins and P0.4 to P0.7 as
output pins.
IO0DIR = 0x000000F0;
b) Configure pin P0.4 as an output. Then set that pin HIGH.
IO0DIR = 0x00000010; OR IO0DIR = (1<<4);
IO0SET = (1<<4);
c) Configure pin P0.4 as an output. Then set that pin LOW.
IO0DIR = 0x00000010; OR IO0DIR = (1<<4);
IO0CLR = (1<<4);
GPIO Register map (2/3)
• IO0DIR
• IO0DIR is used to configure pins of Port 0-P0 as input or output pins.
1= Output Pin, 0= Input Pin
Example: IO0DIR= 0x0000FFFF means P0.0 to PO.15 are configured as output pins and P0.16 to
P0.31 are configured as input pins.
• IO1DIR
• It is used to configure pins of Port 1- P1 as input or output pins
1= Output Pin, 0= Input Pin
Example: IO1DIR=0xAAAAAAAA means even pins(P1.0, p1.2 etc.) are configured as i/p pins and odd
pins (P1.1, P1.3 etc.) are configured as o/p pins.
GPIO Register map (3/3)
• IO0SET
• It is used to set pins of Port0 – P0 to logic 1.
Example: IO0SET= 0x0000FFFFwill set pins P0.0 to P0.15 at logic 1. It will not affect other pins.
• IO0CLR
• It is used to set pins of Port0 – P0 to logic 0.
Example: IO0CLR=0x0000FFFFwill set pins P0.0 to P0.15 at logic 0. It will not affect other pins.
An important note to remember is that since the LPC2148 is a 32-bit microcontroller, the length
of all the registers mentioned is also 32-bits. Each bit in the above mentioned registers is
directly linked to the corresponding pin in the microcontroller i.e. bit ‘a’ in IO0SET
corresponds to Pin ‘a’ in the PORT0.
Registers in LPC2148 follow Big Endian format i.e. bit 0 is the LSB on the extreme
right of the register and bit 31 is the MSB on the extreme left of the register.
Another important note is that when reset, all the pins are set as GPIO pins and
the direction of each pin is set as Input.
Now, we’ll see how to use the above mentioned registers in programming. First, is to set the direction
of a pin. For example, if we want to set 4th pin of PORT0 i.e. P0.3 as output, then it can be set in
various ways as shown below.
We’ll see another example where pin 11 of PORT0 is set as output and the output of this pin is set
to logic 1 and then to logic 0.
For this we need to use three registers: IODIR, IOSET and IOCLR.
IO0DIR | = (1<<11); // Configuring P0.11 as output.
IO0SET | = (1<<11); // Make the O/P pin P0.11 as High.
IO0CLR | = (1<<11); // Make the O/P pin P0.11 as Low.
Now, we’ll see an example where more than one pin has to be set as output and the value of that
pin must be HIGH. Consider pins 7 and 14 of PORT0 (P0.7 and P0.14).
IO0DIR | = (1<<7) | (1<<14); // Configuring pins P0.7 and P0.14 as output.
IO0SET | = (1<<7) | (1<<14); // Make the O/P of pins P0.7 and P0.14 as High.
Examples
• Example: IO0DIR=0x0000FFFF means P0.0 to P0.15 are configured as
output pins and P0.16 to P0.31 are configured as input pins.
• Example: IO1DIR=0xAAAAAAAA means even pins (P1.0, P1.2, P1.4 etc.)
are configured as input pins and odd pins (P1.1, P1.3, P1.5 etc.) are
configured as input pins.
• IO0CLR=0x0000FFFF will set pins P0.0 to P0.15 at logic 0. It will not
affect other pins.
Once we have seen how to set the direction of pins, setting the pin as High or
Low, now we’ll jump into real time embedded programming. Similar to “Hello,
World” program in C Language, blinking an LED is the basic program in
embedded system.
Example: Blink LEDs connected on pins