LPC1768 - UART Programming
LPC1768 - UART Programming
Contents
• 1 Objective
• 2 UART module
• 3 UART Registers
• 4 UART Register Configuration
♦ 4.1 FCR ( FIFO Control Register )
♦ 4.2 LCR ( Line Control Register )
♦ 4.3 LSR (Line Status Register)
♦ 4.4 TER (Transmitter Enable register)
♦ 4.5 Baudrate Calculation
• 5 Steps for Configuring UART0
• 6 Steps for transmitting a char
• 7 Steps for Receiving a char
• 8 Code
♦ 8.1 Example 1
♦ 8.2 Using Explore Embedded Libraries
• 9 Testing
♦ 9.1 Using the Terminal Software
♦ 9.2 Using The Keil Simulator
• 10 Downloads
Objective
In this tutorial we are going to discuss the serial communication using UART.
For more info on UART/RS232 check 8051 tutorial.
LPC1768 has four inbuilt USARTs. We are going to discuss only UART0. After this tutorial you should
be able to extend it to remaining three UARTS.
After understating the basics of LPC1768 UART module, We will discuss how to use the
ExploreEmbedded libraries to communicate with any of the UART devices.
UART module
UART module and registers. LPC1768 has 4-UARTs numbering 0-3, similarly the pins are also
named as RXD0-RXD3 and TXD0-TXD3.As the LPC1768 pins are multiplexed for multiple
functionalities, first they have to be configured as UART pins.
Below table shows the multiplexed UARTs pins.
Register Description
RBR Contains the recently received Data
THR Contains the data to be transmitted
FCR FIFO Control Register
LCR Controls the UART frame formatting(Number of Data Bits, Stop bits)
DLL Least Significant Byte of the UART baud rate generator value.
DLM Most Significant Byte of the UART baud rate generator value.
FCR
31:8 7:6 5:4 3 2 1 0
RESERVED RX RESERVED DMA TX FIFO RX FIFO FIFO
TRIGGER MODE RESET RESET ENABLE
Bit 0 ? FIFO:
This bit is used to enable/disable the FIFO for the data received/transmitted.
0--FIFO is Disabled.
1--FIFO is Enabled for both Rx and Tx.
Bit 1 ? RX_FIFO:
This is used to clear the 16-byte Rx FIFO.
0--No impact.
1--CLears the 16-byte Rx FIFO and the resets the FIFO pointer.
Bit 2 ? Tx_FIFO:
This is used to clear the 16-byte Tx FIFO.
0--No impact.
1--Clears the 16-byte Tx FIFO and the resets the FIFO pointer.
Bit 3 ? DMA_MODE:
This is used for Enabling/Disabling DMA mode.
0--Disables the DMA.
1--Enables DMA only when the FIFO(bit-0) bit is SET.
LCR
31:8 7 6 5:4 3 2 1:0
Reserved DLAB Break COntrol Parity Select Parity Enable Stop Bit Select Word Length Select
Bit 1:0 ? WLS : WordLenghtSelect
These two bits are used to select the character length
00-- 5-bit character length
01-- 6-bit character length
10-- 7-bit character length
11-- 8-bit character length
LSR
31:8 7 6 5 4 3 2 1 0
Reserved RXFE TEMT THRE BI FE PE OE RDR
Bit 0 ? RDR: Receive Data Ready
This bit will be set when there is a received data in RBR register. This bit will be automatically cleared
when RBR is empty.
0-- The UARTn receiver FIFO is empty.
1-- The UARTn receiver FIFO is not empty.
TER
31:8 7 6-0
Reserved TXEN Reserved
Baudrate Calculation
LPC1768 generates the baud rate depending on the values of DLM,DLL.
Baudrate = PCLK/ (16 * ( 256 * DLM + DLL) * (1+ DivAddVal/MulVal))
UART_PCLK PCLK
0 SystemFreq/4
1 SystemFreq
2 SystemFreq/2
3 SystemFreq/8
DivAddVal/MulVal == 0
1. Step1: Configure the GPIO pin for UART0 function using PINSEL register.
2. Step2: Configure the FCR for enabling the FIXO and Reste both the Rx/Tx FIFO.
3. Step3: Configure LCR for 8-data bits, 1 Stop bit, Disable Parity and Enable DLAB.
4. Step4: Get the PCLK from PCLKSELx register 7-6 bits.
5. Step5: Calculate the DLM,DLL vaues for required baudrate from PCLK.
6. Step6: Updtae the DLM,DLL with the calculated values.
7. Step6: Finally clear DLAB to disable the access to DLM,DLL.
After this the UART will be ready to Transmit/Receive Data at the specified baudrate.
Code sniffet:
Code snippet
Steps for Receiving a char
1. Step1: Wait till the a char is received ie. till RDR becomes high.
2. Step2: Copy the received data from receive buffer(RBR).
Code snippet
Code
Example 1
Below is the code for transmitting and receiving chars at 9600 baud
As LPC1768 has four inbuilt UART channels, the interfaces are suffixed with channel number as
shown below.
UART0_Printf()
UART1_Printf()
UART2_Printf()
UART3_Printf()
Testing
Using the Terminal Software
After generating the hex/bin file, flash it to the controller. Now connect the LPC1768 to your system
using a Usb to Serial converter.
Open the terminal software , select the COM port, set baud rate and hit the connect button.
Using The Keil Simulator
Code can be tested on the Keil simulator as well. For this the target memory options needs to be set
to default as shown below.
Now Rebuild the project and Run the code using the Keil simulator as shown below.
Downloads
Download the complete project folder from the below link:
https://codeload.github.com/ExploreEmbedded/Explore-Cortex-M3-LPC1768-Stick-DVB-14001/zip/master
Have a opinion, suggestion , question or feedback about the article let it out here!