Top Banner
Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] Department of EIE / PEC UART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College [email protected]
30

UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Jul 22, 2018

Download

Documents

dinhkhuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

UART in LPC2148

Dr.R.SundaramurthyDepartment of EIE

Pondicherry Engineering College

[email protected]

Page 2: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

UART

• Two Identical UARTs UART0 & UART1

• We are using UART0 for our example.

Page 3: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

PLL

VPBDIV = 0x00000001;

Page 4: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Pins Involved

P0.0 [Pin 19] TXD0

P0.1 [Pin 21] RXD0

Page 5: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

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

Page 6: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

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

Page 7: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

How to Use PINSEL

• Each associated 'pin' in PINSEL0 and PINSEL1 is assigned a 2-bit address.

Page 8: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Function Selection Bits

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

PINSEL0 = 0x00000005

0 1 0 1

Page 9: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

UART0 Line Control Register - U0LCR

U0LCR = 0x00000083;

1 1 1

Page 10: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

UART0 Line Status Register - U0LSR

Bit – 0 : Receive Data Ready (RDR)

Bit – 5 : Transmit Holding Register Empty (THRE)

Page 11: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Baud Rate Clock

U0DLM U0DLL

195 (Or) 0xC3

Page 12: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Baud Rate Calculations

Divisor = Pclk / (Baud x 16)

= 60 x 10^6 / (9600 x 16)

= 391

= 0x01 87

U0DLM = 0x01

U0DLL = 0x87

Divisor = Pclk / (Baud x 16)

= 30 x 10^6 / (9600 x 16)

= 195

= 0x00 C3

U0DLM = 0x00

U0DLL = 0xC3

For Pclk = 60MHzFor Pclk = 30MHz

Page 13: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

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

Page 14: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

UART0 Receive Buffer - U0RBR

• The U0RBR is the top byte of the UART0 Rx FIFO

• The Divisor Latch Access Bit (DLAB) in U0LCR must be zero in order to access the U0RBR

• The U0RBR is always Read Only

Page 15: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

UART0 Transmit Holding Register - U0THR

• The U0THR is the top byte of the UART0 TX FIFO.

• The Divisor Latch Access Bit (DLAB) in U0LCR must be zero in order to access theU0THR.

• The U0THR is always Write Only

Page 16: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Algorithm for Initializing UART0

1. Generate Pclk for 30MHz VPBDIV = 0X00000002;

2. Select UART Function for the PinsPINSEL0 = 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 = 0XC3;U0DLM = 0X00;

5.Clear Divisor Latch Access Bit (DLAB)U0LCR = 0X00000003;

Page 17: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Initialization Routine

void initserial(void){VPBDIV = 0X02;PINSEL0 =0X00000005; U0LCR = 0X00000083;U0DLL = 0XC3;U0DLM = 0X00;U0LCR = 0X00000003;}

Page 18: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

• Move the data to be transmitted to UART0 Transmit Holding Register.

U0THR = mydata;

Wait until Transmit Holding Register Empty is set.

while((U0LSR & 0x20)==0);

Algorithm for Transmitting Data using UART0

Page 19: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Send Serial Routine

void sendserial(unsigned char serdata)

{

U0THR = serdata;

while((U0LSR & 0x20)==0);

}

Page 20: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Wait until Receive data Ready Flag is set.

while((U0LSR & 0x01)==0);

• Move the received data from UART0 Receive Buffer Register .

temp = U0RBR ;

Algorithm for Receiving Data using UART0

Page 21: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Receive Serial Routine

unsigned char ReceiveSerial(void)

{

unsigned char mydata ;

while((U0LSR & 0x01)==0);

mydata = U0RBR ;

return mydata;

}

Page 22: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

UART.h

• void initserial(void);

• void sendserial(unsigned char serdata);

• unsigned char receiveserial(void);

Page 23: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

initserial

void initserial(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,

}

Page 24: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

sendserial

void sendserial(unsigned char serdata)

{

U0THR = serdata;

// Move data to be transmitted to U0THR

while((U0LSR & 0x20)==0);

// wait until THR Empty Flag is set.

}

Page 25: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

receiveserial

unsigned char receiveserial(void)

{

unsigned char mydata ;

while((U0LSR & 0x01)==0);

// wait until RBR Full Flag is set.

mydata = U0RBR ;

// Move data Received to U0THR

return mydata;

}

Page 26: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Examples

Page 27: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Question

1. Transmit a constant string of “WELCOME TO EIE” 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

Page 28: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Problem-1 (Transmit Serial) #include<lpc214x.h>

#include "GPIO.h"

#include "timer.h"

#include "UART.h"

// UART Transmit

unsigned char UARTdata[14]

= "Welcome to EIE" ;

int i;

int main(void)

{

initPLL(); // 60 MHz Pclk

initserial();

while(1)

{

for(i=0;i<14;i++)

sendserial(UARTdata[i]);

sendserial(13); // carriage return

sendserial(10); // Line feed

delayms(1000);

}

}

XTAL162

XTAL261

P0.0/TxD0/PWM119

P0.1/RxD0/PWM3/EINT021

P0.2/SCL0/CAP0.022

P0.3/SDA0/MAT0..0/EINT126

P0.4/SCK0/CAP0.1/AD0.627

P0.5/MISO0/MAT0.1/AD0.729

P0.6/MOSI0/CAP0.2/AD1.030

P0.7/SSEL0/PWM2/EINT231

P0.8/TxD1/PWM4/AD1.133

P0.9/RxD1/PWM6/EINT334

P0.10/RTS1/CAP1.0/AD1.235

P0.11/CTS1/CAP1.1/SCL137

P0.12/DSR1/MAT1.0/AD1.338

P0.13/DTR1/MAT1.1/AD1.439

P0.14/DCD1/EINT1/SDA141

P0.15/RI1/EINT2/AD1.545

P0.16/EINT0/MAT0.2/CAP0.246

P0.17/CAP1.2/SCK1/MAT1.247

P0.18/CAP1.3/MISO1/MAT1.353

P0.19/MAT1.2/MOSI1/CAP1.254

P0.20/MAT1.3/SSEL1/EINT355

P0.21/PWM5/AD1.6/CAP1.31

P0.22/AD1.7/CAP0.0/MAT0.02

P0.2358

P0.25/AD0.4/AOUT9

P0.27/AD0.0/CAP0.1/MAT0.111

P0.28/AD0.1/CAP0.2/MAT0.213

P0.29/AD0.2/CAP0.3/MAT0.314

P0.30/AD0.3/EINT3/CAP0.015

V323

RST57

VREF63

VSS6

VSSA59

P1.16/TRACEPKT016

P1.17/TRACEPKT112

P1.18/TRACEPKT28

P1.19/TRACEPKT34

P1.20/TRACESYNC48

P1.21/PIPESTAT044

P1.22/PIPESTAT140

P1.23/PIPESTAT236

P1.24/TRACECLK32

P1.25/EXTIN028

P1.26/RTCK24

P1.27/TDO64

P1.28/TDI60

P1.29/TCK56

P1.30/TMS52

P1.31/TRST20

V343

V351

VSS18

VSS25

VSS42

VSS50

RTXC13

RTXC25

V3A7

VBAT49

P0.3117

P0.26/AD0.510

U1

LPC2138

+3.3V

12

X1

C1

33pF

C2

33pF

12

X2

C3

22pF

C4

22pF

R11K

C5100pF

+3.3V

Xmodem, Ymodem, Zmodem

VT52, VT100, ANSIRXD

RTS

TXD

CTS

T1IN11

R1OUT12

T2IN10

R2OUT9

T1OUT14

R1IN13

T2OUT7

R2IN8

C2+

4

C2-

5

C1+

1

C1-

3

VS+2

VS-6

U2

MAX232

C6

1uF

C7

1uF

C81uF

C91uF

LOAD-1

LED-YELLOW

LOAD-2

LED-YELLOW

Page 29: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

Problem-2 (Receive Serial) #include<lpc214x.h>

#include "GPIO.h"

#include "timer.h"

#include "UART.h"

// PC based Home Automation

int ON = 0;

int OFF = 1 ;

int Load1 = 25;

int Load2 = 26;

unsigned char UARTdata;

int main(void)

{

initPLL(); // 60 MHz Pclk

initserial();

while(1)

{

UARTdata = receiveserial();

if (UARTdata == 'A')

writepin(Load1,ON);

else if (UARTdata == 'B')

writepin(Load1,OFF);

else if (UARTdata == 'C')

writepin(Load2,ON);

else if (UARTdata == 'D')

writepin(Load2,OFF);

else

{

writepin(Load1,OFF);

writepin(Load2,OFF);

}

}

}

XTAL162

XTAL261

P0.0/TxD0/PWM119

P0.1/RxD0/PWM3/EINT021

P0.2/SCL0/CAP0.022

P0.3/SDA0/MAT0..0/EINT126

P0.4/SCK0/CAP0.1/AD0.627

P0.5/MISO0/MAT0.1/AD0.729

P0.6/MOSI0/CAP0.2/AD1.030

P0.7/SSEL0/PWM2/EINT231

P0.8/TxD1/PWM4/AD1.133

P0.9/RxD1/PWM6/EINT334

P0.10/RTS1/CAP1.0/AD1.235

P0.11/CTS1/CAP1.1/SCL137

P0.12/DSR1/MAT1.0/AD1.338

P0.13/DTR1/MAT1.1/AD1.439

P0.14/DCD1/EINT1/SDA141

P0.15/RI1/EINT2/AD1.545

P0.16/EINT0/MAT0.2/CAP0.246

P0.17/CAP1.2/SCK1/MAT1.247

P0.18/CAP1.3/MISO1/MAT1.353

P0.19/MAT1.2/MOSI1/CAP1.254

P0.20/MAT1.3/SSEL1/EINT355

P0.21/PWM5/AD1.6/CAP1.31

P0.22/AD1.7/CAP0.0/MAT0.02

P0.2358

P0.25/AD0.4/AOUT9

P0.27/AD0.0/CAP0.1/MAT0.111

P0.28/AD0.1/CAP0.2/MAT0.213

P0.29/AD0.2/CAP0.3/MAT0.314

P0.30/AD0.3/EINT3/CAP0.015

V323

RST57

VREF63

VSS6

VSSA59

P1.16/TRACEPKT016

P1.17/TRACEPKT112

P1.18/TRACEPKT28

P1.19/TRACEPKT34

P1.20/TRACESYNC48

P1.21/PIPESTAT044

P1.22/PIPESTAT140

P1.23/PIPESTAT236

P1.24/TRACECLK32

P1.25/EXTIN028

P1.26/RTCK24

P1.27/TDO64

P1.28/TDI60

P1.29/TCK56

P1.30/TMS52

P1.31/TRST20

V343

V351

VSS18

VSS25

VSS42

VSS50

RTXC13

RTXC25

V3A7

VBAT49

P0.3117

P0.26/AD0.510

U1

LPC2138

+3.3V

12

X1

C1

33pF

C2

33pF

12

X2

C3

22pF

C4

22pF

R11K

C5100pF

+3.3V

Xmodem, Ymodem, Zmodem

VT52, VT100, ANSIRXD

RTS

TXD

CTS

T1IN11

R1OUT12

T2IN10

R2OUT9

T1OUT14

R1IN13

T2OUT7

R2IN8

C2+

4

C2-

5

C1+

1

C1-

3

VS+2

VS-6

U2

MAX232

C6

1uF

C7

1uF

C81uF

C91uF

LOAD-1

LED-YELLOW

LOAD-2

LED-YELLOW

Page 30: UART in LPC2148 - vrpsundar.weebly.com fileUART in LPC2148 Dr.R.Sundaramurthy Department of EIE Pondicherry Engineering College ... How to Use PINSEL •Each associated 'pin' in PINSEL0

Dr.R.Sundaramurthy.,M.E.,Ph.D., [email protected] of EIE / PEC

End of Session

[email protected]