0% found this document useful (0 votes)
23 views29 pages

9.UART of LPC2148

The document discusses the UART peripheral in the LPC2148 microcontroller. It describes the two UART modules, associated pins, registers involved, initialization process, transmission and reception of data. Examples of transmitting and receiving data between the microcontroller and PC are also provided.

Uploaded by

M I G H T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views29 pages

9.UART of LPC2148

The document discusses the UART peripheral in the LPC2148 microcontroller. It describes the two UART modules, associated pins, registers involved, initialization process, transmission and reception of data. Examples of transmitting and receiving data between the microcontroller and PC are also provided.

Uploaded by

M I G H T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

UART in LPC2148

UART
• Two Identical UARTs ➔ UART0 & UART1
• We are using UART0 for our example.
PLL

VPBDIV= 0x01;
Pins Involved

P0.0 [Pin 19] ➔ TXD0

P0.1 [Pin 21] ➔ RXD0


SFRs Involved
• P0 Pin Select Register - PINSEL0
• UART0 Line Control Register - U0LCR
• UART0 Line Status Register - U0LSR
• UART0 Divisor Latch MSB - U0DLM
• UART0 Divisor Latch LSB - U0DLL
• UART0 Transmit Holding Register - U0THR
• UART0 Receive Buffer Register - U0RBR
Pin Select SFRs

• PINSEL0 for GPIO pins 0.0 to 0.15


• PINSEL1 for GPIO pins 0.16 to 0.31
• PINSEL2 for GPIO pins 1.16 to 1.31

Each Pin is Associated with multiple functions

Pin Select enable you to select which pin


functions you would like to use
How to Use PINSEL
• Each associated 'pin' in PINSEL0 and PINSEL1
is assigned a 2-bit address.
Function Selection Bits

Example for P0.0 & P0.1 as TxD & RxD


PINSEL0 = 0x00000005
0 1 0 1
UART0 Line Control Register - U0LCR
1 1 1

U0LCR= 0x00000083;
UART0 Line Status Register - U0LSR

Bit – 0 : Receive Data Ready (RDR)


Bit – 5 : Transmit Holding Register Empty (THRE)
Divisor Latch

• UART0 Divisor Latch MSB - U0DLM


• UART0 Divisor Latch LSB - U0DLL
• Holds the value used to divide the clock
supplied by the prescaler in order to produce
the baud rate clock.
• The Divisor Latch Access Bit (DLAB) in U0LCR
must be one in order to access the UART0
Divisor Latches
Baud Rate Clock

LSB
L

U0DLM U0DLL
Baud Rate Calculations

For Pclk = 30MHz For Pclk = 60MHz

Divisor = Pclk / (Baud x 16) Divisor = Pclk / (Baud x 16)


= 30 x 10^6 / (9600 x 16) = 60 x 10^6 / (9600 x 16)
= 195 = 391
= 0x00 C3 = 0x01 87
U0DLM = 0x00 U0DLM = 0x01
U0DLL = 0xC3 U0DLL = 0x87
UART0 Receive Buffer - U0RBR

• Used to Receive data via UART

• The Divisor Latch Access Bit (DLAB) in U0LCR


must be zero in order to access the U0RBR

• The U0RBR is always Read Only


UART0 Transmit Holding Register - U0THR

• Used to Transmit Data Via UART.

• The Divisor Latch Access Bit (DLAB) in U0LCR


must be zero in order to access theU0THR.

• The U0THR is always Write Only


Algorithm for Initializing UART0
1. Generate Pclk for 60MHz
VPBDIV = 0X00000001;
2. Select UART Function for the Pins
PINSEL0 = 0X00000005;
3. Set UART0 Line Control Register for 8 Bit UART, No Parity ,1
Stop Bit with Divisor Latch Access Bit (DLAB) Enabed.
U0LCR = 0X00000083;
4. Set U0DLL & U0DLM for 9600 Baud.
U0DLL = 0X87;
U0DLM = 0X01;
5.Clear Divisor Latch Access Bit (DLAB)
U0LCR = 0X00000003;
Initialization Routine
void initUART0(void)
{
VPBDIV = 0X02;
PINSEL0 =0X00000005;
U0LCR = 0X00000083;
U0DLL = 0X87;
U0DLM = 0X01;
U0LCR = 0X00000003;
}
Algorithm for Transmitting Data using UART0
• Move the data to be transmitted to UART0
Transmit Holding Register.
U0THR = mydata;

Wait until Transmit Holding Register Empty is


set.
while((U0LSR & (0x01<<5))==0);
Send UART0 Routine

void sendUART0(unsigned char serdata)


{
U0THR = serdata;
while((U0LSR & (0x01<<5))==0);
}
Algorithm for Receiving Data using UART0
Wait until Receive data Ready Flag is set.
while((U0LSR & 0x01)==0);

•Move the received data from UART0 Receive Buffer Register .


temp = U0RBR ;
Receive UART0 Routine
unsigned char ReceiveUART0(void)
{
unsigned char mydata ;
while((U0LSR & 0x01)==0);
mydata = U0RBR ;
return mydata;
}
UART.h
• void initUART0 (void);
• void sendUART0(unsigned char serdata);
• unsigned char receiveUART0 (void);
initUART0()
void initUART0(void)
{
PINSEL0 =0X05;
// P0.0 = TxD , P0.1 = RxD
U0LCR = 0X83;
// DLAB(7) = 1 , 8 Bit UART (1,0) = 1,
U0DLL = 0X87;
// = 60 x 10^6 / (9600 x 16) = 391 = 0x0187
U0DLM = 0X01; // 9600 Baud
U0LCR = 0X03; // DLAB(7) = 0,8 Bit UART (1,0) = 1,
}
sendUART0()
void sendUART0(unsigned char serdata)
{
U0THR = serdata;
// Move data to be transmitted to U0THR
while((U0LSR & 0x20)==0);
// wait until THR Empty Flag is set.
}
receiveUART0()
unsigned char receiveUART0(void)
{
unsigned char mydata ;
while((U0LSR & 0x01)==0);
// wait until RBR Full Flag is set.
mydata = U0RBR ;
// Move data Received to U0THR
return mydata;
}
Examples
Question
1. Transmit a constant string of “WELCOME TO ELC” at a rate of
9600 Baud to PC with a time delay.

2. Design a PC based control system to receive data from PC and to


perform the following operations.

A ➔ P1.25 – ON
B ➔ P1.25 – OFF
C ➔ P1.26 – ON
D ➔ P1.26 – OFF
#include<lpc214x.h>
Problem-1 (Transmit Serial)
#include "GPIO.h" C6

#include "timer.h" C1 1
1uF

3 U2

#include "UART.h"

1
U1 C1+ C1-
33pF
X1 62
XTAL1 P0.0/TxD0/PW M1
19 11
T1IN T1OUT
14
RXD

// UART Transmit
61 21 12 13 VT52, VT100, ANSI
C2 XTAL2 P0.1/RxD0/PWM3/EINT0 R1OUT R1IN
22 10 7
P0.2/SCL0/CAP0.0 T2IN T2OUT TXD

2
3 26 9 8
RTXC1 P0.3/SDA0/MAT0..0/EINT1 R2OUT R2IN
5 27
RTXC2 P0.4/SCK0/CAP0.1/AD0.6 RTS

unsigned char UARTdata[14]


33pF 29 2
P0.5/MISO0/MAT0.1/AD0.7 VS+ Xmodem, Ymodem, Zmodem
57 30 6
C3 RST P0.6/MOSI0/CAP0.2/AD1.0 VS- CTS
31
P0.7/SSEL0/PWM2/EINT2
P0.8/TxD1/PW M4/AD1.1
33
C2+ C2- C9

= "Welcome to ELC" ;
34 C8

1
P0.9/RxD1/PWM6/EINT3 1uF
22pF 35
P0.10/RTS1/CAP1.0/AD1.2 1uF
X2 37 4 C7 5 MAX232
P0.11/CTS1/CAP1.1/SCL1
38
C4 P0.12/DSR1/MAT1.0/AD1.3

int i;
39
P0.13/DTR1/MAT1.1/AD1.4

2
41 1uF
P0.14/DCD1/EINT1/SDA1
45
P0.15/RI1/EINT2/AD1.5
22pF

int main(void)
46
+3.3V P0.16/EINT0/MAT0.2/CAP0.2
47
P0.17/CAP1.2/SCK1/MAT1.2
53
P0.18/CAP1.3/MISO1/MAT1.3
54
P0.19/MAT1.2/MOSI1/CAP1.2

{
55
P0.20/MAT1.3/SSEL1/EINT3
1
LOAD-1
P0.21/PWM5/AD1.6/CAP1.3
R1 P0.22/AD1.7/CAP0.0/MAT0.0
2
58
1K P0.23

initPLL(); // 60 MHz Pclk


LED-YELLOW
9
P0.25/AD0.4/AOUT
10
P0.26/AD0.5
11
P0.27/AD0.0/CAP0.1/MAT0.1
13

initserial();
P0.28/AD0.1/CAP0.2/MAT0.2
C5 14
P0.29/AD0.2/CAP0.3/MAT0.3
15
100pF P0.30/AD0.3/EINT3/CAP0.0
17
+3.3V P0.31
LOAD-2
16
P1.16/TRACEPKT0
12
P1.17/TRACEPKT1
49 8

while(1)
VBAT P1.18/TRACEPKT2
4 LED-YELLOW
P1.19/TRACEPKT3
63 48
VREF P1.20/TRACESYNC
44
7 P1.21/PIPESTAT0
40
51 P1.22/PIPESTAT1

{
V3A V3 36
43 P1.23/PIPESTAT2
V3 32
23 P1.24/TRACECLK
V3 28
P1.25/EXTIN0
59 24

for(i=0;i<14;i++)
VSSA P1.26/RTCK
50 64
VSS P1.27/TDO
42 60
VSS P1.28/TDI
25 56
VSS P1.29/TCK
18 52
P1.30/TMS

sendserial(UARTdata[i]);
VSS 20
6 P1.31/TRST
VSS
LPC2138

sendserial(13); // carriage return


sendserial(10); // Line feed

delayms(1000);

}
}
#include<lpc214x.h>
#include "GPIO.h"
Problem-2 (Receive Serial)
#include "timer.h"
#include "UART.h" C6

// PC based Home Automation C1 1


1uF

3 U2

int ON = 0;

1
U1 C1+ C1-
33pF

int OFF = 1 ;
X1 62
XTAL1 P0.0/TxD0/PW M1
19 11
T1IN T1OUT
14
RXD
61 21 12 13 VT52, VT100, ANSI
XTAL2 P0.1/RxD0/PWM3/EINT0 R1OUT R1IN
C2 22 10 7
P0.2/SCL0/CAP0.0 T2IN T2OUT TXD

2
3 26 9 8

int Load1 = 25;


RTXC1 P0.3/SDA0/MAT0..0/EINT1 R2OUT R2IN
5 27
RTXC2 P0.4/SCK0/CAP0.1/AD0.6 RTS
33pF 29 2
P0.5/MISO0/MAT0.1/AD0.7 VS+
57 30 6 Xmodem, Ymodem, Zmodem
C3 RST P0.6/MOSI0/CAP0.2/AD1.0 VS- CTS

int Load2 = 26;


31
P0.7/SSEL0/PWM2/EINT2
33 C9
P0.8/TxD1/PW M4/AD1.1 C2+ C2-
34 C8

1
P0.9/RxD1/PWM6/EINT3 1uF
22pF 35
P0.10/RTS1/CAP1.0/AD1.2 1uF
X2 37 4 C7 5 MAX232
P0.11/CTS1/CAP1.1/SCL1
38
C4 P0.12/DSR1/MAT1.0/AD1.3
39
P0.13/DTR1/MAT1.1/AD1.4

unsigned char UARTdata;

2
41 1uF
P0.14/DCD1/EINT1/SDA1
45
P0.15/RI1/EINT2/AD1.5
22pF
46

int main(void)
+3.3V P0.16/EINT0/MAT0.2/CAP0.2
47
P0.17/CAP1.2/SCK1/MAT1.2
53
P0.18/CAP1.3/MISO1/MAT1.3
54
P0.19/MAT1.2/MOSI1/CAP1.2

{
55
P0.20/MAT1.3/SSEL1/EINT3
1 LOAD-1
P0.21/PWM5/AD1.6/CAP1.3
R1 2
P0.22/AD1.7/CAP0.0/MAT0.0
58

initPLL(); // 60 MHz Pclk


1K P0.23
LED-YELLOW
9
P0.25/AD0.4/AOUT
10
P0.26/AD0.5

initserial();
11
P0.27/AD0.0/CAP0.1/MAT0.1
13
P0.28/AD0.1/CAP0.2/MAT0.2
14
C5 P0.29/AD0.2/CAP0.3/MAT0.3
15
P0.30/AD0.3/EINT3/CAP0.0

while(1)
100pF
17
+3.3V P0.31
LOAD-2
16
P1.16/TRACEPKT0

{
12
P1.17/TRACEPKT1
49 8
VBAT P1.18/TRACEPKT2 LED-YELLOW
4
P1.19/TRACEPKT3
63 48
P1.20/TRACESYNC

UARTdata = receiveserial();
VREF 44
7 P1.21/PIPESTAT0
51 40
V3A V3 P1.22/PIPESTAT1
36
43 P1.23/PIPESTAT2
V3

if (UARTdata == 'A')
23 32
V3 P1.24/TRACECLK
28
P1.25/EXTIN0
59 24
VSSA P1.26/RTCK
50 64
VSS P1.27/TDO

pinWrite(Load1,ON);
42 60
VSS P1.28/TDI
25 56
VSS P1.29/TCK
18 52
VSS P1.30/TMS
20

else if (UARTdata == 'B')


6 P1.31/TRST
VSS
LPC2138

pinWrite (Load1,OFF);
else if (UARTdata == 'C')
pinWrite (Load2,ON);
else if (UARTdata == 'D')
pinWrite (Load2,OFF);
else
{
writepin(Load1,OFF);
writepin(Load2,OFF);
}
}
}

You might also like