UART in LPC2148 ARM7 Microcontroller

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

UART in LPC2148 ARM7 Microcontroller-

Serial Communication
In this tutorial, we will communicate microcontroller and PC over serial communication using
UART in LPC2148 ARM7 Microcontroller. Before we move on to UART in LPC2148 ARM7.
Let’s first discuss UART in general. UART (Universal Asynchronous Receiver/Transmitter) is
one of the earliest mode of communication applied to computer (somewhere in 1960s). The
information is transmitted one binary bit at a time; as such it is a serial communication method.
These bits are grouped together in the form of ‘Frames’ (a set format) for conveying one
meaningful piece of data (e.g. character byte). UART is asynchronous because it doesn’t require
a transmitter provided clock to synchronize the transmission and receipt of data.

Serial Data Transfer In UART

Serial Data Transmission in UART

Just because there is no clock signal per se, a start bit is added sent first to tell the receiver to
listen out for data. The receiver monitors for a logic HIGH falling to logic LOW. The receiver
synchronizes its own bus clock to that make up the word being sent, with bit zero, the least
significant bit (LSB) being sent first. The bits are sent as pulses on the wire at specific time
intervals, set at both ends of links to previously agreed values. The receiver looks at the voltage
on the wire at these times; if it sees logic high, it records a binary digit 1 or 0 if the line is low.
The receiver checks half way between the start and the end of the pulse to ensure it does not
miss-read the voltage on the line during the brief interval while the voltage is rising or falling.

Serial Data Transmission in UART

If two devices use a parity bit for rudimentary error checking, that is calculated and sent next, in
sync with data that has been transmitted thus far. Finally, one stop bit is sent by the transmitter.
Word length, parity availability and type, and numbers of stop bits all have to be agreed in
advance for successful communication because UART uses two wires. The transmitter of device-
A connected to receiver of device-B and receiver of device-A connected to transmitter of device-
B. This is how devices can send data simultaneously to each other, a mode of communication
called ‘full duplex’.
Serial Device Communication

INTRODUCTION: UART in LPC2148 ARM7 Microcontroller

As we all know UART is widely used serial communication protocol in embedded system based
applications. Almost all microcontrollers have built-in on-chip UART support. LPC2148 ARM7
core supports two UART in it, UART0 and UART1. UART0 can be used as general purpose
UART and also can support ISP Programming through it, whereas UART1 has additional
modem support. Both have built in baud rate generator and 16-byte transmit and receive FIFOs.
For UART0 the TxD Pin is at P0.0 and RxD Pin is at P0.1 and similarly for UART1 the TxD Pin
is at P0.8 and RxD Pin is at P0.9 as shown in table below;

List of UART in LPC2148

RS232 Level Converter

Most of microchips work on TTL or CMOS voltage level which can’t be used to communicate
over RS-232 protocol. In this case voltage or level converter is needed which can convert TTL to
RS-232 and RS-232 to TTL voltage levels. The most commonly used RS-232 level converter is
MAX3232 chip. This chip includes charge pump which can generate RS232 voltage levels (-10V
and +10V) from 5V power supply. It also includes two receiver and two transmitters and is
capable of full-duplex UART communication. RS232 communication enables point-to-point data
transfer, which often used in data acquisition applications and for data transfer between
microcontroller and PC.

In example project, we will use RS232 cable (USB to Serial Converter) to accomplish
communication between LPC2148 Microcontroller and PC. *Make sure to install appropriate
drivers before you make use of it.

Registers Description: UART in LPC2148 ARM7

Before we use any pin from LPC2148 for serial communication. We have to configure and
initialize UART0 Peripheral by setting register bits. Let’s quickly revise functions of UART0
registers which will be used in our code later on. [It is recommended to keep user manual open
to collect more details about each register if in case needed].

Registers Description/Function
Transmit Hold Register: This register contains 8-bit write data which can be
U0THR
transmitted through UART0. This is write only register.
U0RBR Receive Buffer Register: This register contains 8-bit received data from UART0. This
data is nothing but top most byte of Rx FIFO. When we use 5, 6 or 7-bit data then
remaining bits are padded with 0’s by default. This is read only register.
Line Control Register: The value or settings in this register configure the UART0
block. As this is an 8-bit register. There are several parameters configured through this
U0LCR register such as word length, stop bit, parity enable, parity select, break control, divisor
latch access bit. This register setting plays important role while initializing UART0
before using it.
U0DLL & U0DLM are standard UART0 baud rate generator divider registers. Each
U0DLL
of this register holds 8-bit values. Together these registers form a 16-bit divisor value
&
which will be used for baud rate generation. This will be discussed further while code
U0DLM
explanation with respect to real world example.
Fractional Divider Register: This is another very important register, which plays
significant role in baud rate generation. In this 8-bit register, first four bits i.e. Bit[3 to
0]-DIVADDVAL: This is the Prescale Divisor value. If this value is 0 then fractional
U0FDR
baud rate generator have no effect on UART0 baud rate. The remaining 4-bits i.e.
Bit[4 to 7]-MULVAL: This defines Prescale Multiplier value. Even if fractional baud
rate generator is not used the value in this register must be more than or equal to ‘1’.

Calculate Baud Rate for UART in LPC2148 ARM7

Baud Rate for UART in LPC2148 ARM7 Microcontroller can be calculated by given equation:

Ba
udrate Calculation in LPC2148

Example Project:

Let’s look at real world example where we will send string from LPC2148. This message will be
received and displayed on terminal emulator or serial console which configured to appropriate
COM Port of PC. I believe now you’re familiar with how UART works. In our program we will
use following configuration to establish proper communication:

 Baud Rate = 9600 baud (with PCLK=60Mhz)


 Data Length = 8 bits
 No Parity Bit
 and 1 Stop Bit
To work out on this project we need following things to be setup:
Software Requirements: Install Keil uVision4, Flash Magic, PuTTY (terminal emulator or
serial console)
Hardware Requirements: LPC2148 Development Board, RS232 Cable (USB Serial
Converter), Power Adapter (9V-500mA).

Circuit Diagram: UART in LPC2148 ARM7 Microcontroller

Circuit Diagram-UART in LPC2148 ARM


Microcontroller

Co
nnection between LPC2148 and PC

Source Code: UART in LPC2148 ARM7 Microcontroller

/************************************************************/
/* PROJECT NAME: UART in LPC2148 ARM7 */
/* Device: LPC2148 */
/* Filename: uart.c */
/* Language: C */
/* Compiler: Keil ARM */
/* For more detail visit www.binaryupdates.com */
/************************************************************/
#include <lpc214x.h>

void initClocks(void);
void initUART0(void);
void U0Write(char data);
void Send_String(char* StringPtr);

char String[]="Hello from BINARYUPDATES.COM !!! \n\r\n";


unsigned int delay;

int main(void)
{
initClocks(); // Set CCLK=60Mhz and PCLK=60Mhz
initUART0();

while(1)
{
Send_String(String); //Pass the string to the USART_putstring function and sends it
over the serial
for(delay=0; delay<500000; delay++); // delay
}
}

void initUART0(void)
{
PINSEL0 = 0x5; /* Select TxD for P0.0 and RxD for P0.1 */
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit | DLAB set to 1 */
U0DLL = 110;
U0DLM = 1;
U0FDR = 0xF1; /* MULVAL=15(bits - 7:4) , DIVADDVAL=0(bits - 3:0)*/
U0LCR &= 0x0F; // Set DLAB=0 to lock MULVAL and DIVADDVAL
//BaudRate is now ~9600 and we are ready for UART communication!
}

void U0Write(char data)


{
while (!(U0LSR & (1<<5))); // wait till the THR is empty
// now we can write to the Tx FIFO
U0THR = data;
}

void initClocks(void)
{
PLL0CON = 0x01; //Enable PLL
PLL0CFG = 0x24; //Multiplier and divider setup
PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;

while(!(PLL0STAT & 0x00000400)); //is locked?

PLL0CON = 0x03; //Connect PLL after PLL is locked


PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;
VPBDIV = 0x01; // PCLK is same as CCLK i.e.60 MHz
}

void Send_String(char* StringPtr){

while(*StringPtr != 0x00){
U0Write(*StringPtr);
StringPtr++;}
}

1 /************************************************************/
2 /* PROJECT NAME: UART in LPC2148 ARM7              */
3 /* Device:       LPC2148                 */
4 /* Filename:     uart.c                                   */
5 /* Language:     C                                        */
6 /* Compiler:     Keil ARM              */
7 /* For more detail visit www.binaryupdates.com        */
8 /************************************************************/
9 #include <lpc214x.h>
10  
11
12
void initClocks(void);
13
void initUART0(void);
14
void U0Write(char data);
15
void Send_String(char* StringPtr);
16
 
17
char String[]="Hello from BINARYUPDATES.COM !!! \n\r\n";
18
unsigned int delay;
19
 
20
int main(void)
21
{
22
initClocks(); // Set CCLK=60Mhz and PCLK=60Mhz
23
initUART0();
24
while(1)
25
{
26
Send_String(String);    //Pass the string to the USART_putstring function and sends it over the
27
serial
28
for(delay=0; delay<500000; delay++); // delay
29
}
30
}
31
 
32
void initUART0(void)
33
{
34
PINSEL0 = 0x5;  /* Select TxD for P0.0 and RxD for P0.1 */
35
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit | DLAB set to 1 */
36
U0DLL = 110;
37
U0DLM = 1;  
38
U0FDR = 0xF1; /* MULVAL=15(bits - 7:4) , DIVADDVAL=0(bits - 3:0)*/
39
U0LCR &= 0x0F; // Set DLAB=0 to lock MULVAL and DIVADDVAL
40
//BaudRate is now ~9600 and we are ready for UART communication!
41
}
42
 
43
void U0Write(char data)
44
{
45
while (!(U0LSR & (1<<5))); // wait till the THR is empty
46
// now we can write to the Tx FIFO
47
U0THR = data;
48
}
49
 
50
void initClocks(void)
51
{
52
PLL0CON = 0x01;   //Enable PLL
53
PLL0CFG = 0x24;   //Multiplier and divider setup
54
PLL0FEED = 0xAA;  //Feed sequence
55
PLL0FEED = 0x55;
56
while(!(PLL0STAT & 0x00000400)); //is locked?
57
PLL0CON = 0x03;   //Connect PLL after PLL is locked
58
PLL0FEED = 0xAA;  //Feed sequence
59
PLL0FEED = 0x55;
60
VPBDIV = 0x01;    // PCLK is same as CCLK i.e.60 MHz
61
}
62
 
63
void Send_String(char* StringPtr){
64
while(*StringPtr != 0x00){
65
U0Write(*StringPtr);
66
StringPtr++;}
67
}
68
69

Download Project: Click Here


The program is well commented in code itself.  Let’s run code on real hardware to see its impact.
Now just compile the code above and load .HEX file onto the LPC2148. We need PuTTY
(Terminal Emulator) to observe data sent by LPC2148 Microcontroller over UART0. So here is
the setting to configure PuTTY for UART with appropriate COM port, data bits and baud rate
etc etc.

IMPORTANT: After loading .hex file, Make sure that you’re not into ISP Mode. We will be using
same UART0 for programming as well as for receiving data. In case if you’re using STK2148-
UltraLite Board then turned off SW7 switches to read string on PuTTY.

PuTTy Configuration for UART0

Explore Settings Further

Open PuTTY to Read from COM Port

Once we have done with all settings, just open console. And we will start receiving string on
PuTTY, sent by LPC2148 Microcontroller. Here is an output from our project:
Output of UART in LPC2148 ARM7 Microcontroller

This is how we can accomplish serial communication using UART in LPC2148 ARM7
Microcontroller to transmit character string from microcontroller to PC. I recommend you to
modify the code to explore UART feature in LPC2148. I will try my best to share one more post
where we will not only receive but also transmit data. UART will be useful for our future
projects where we will display ADC data or output of sensor on terminal emulator. If you have
any questions then feel free to leave a comment.

You might also like