6.LPC2148 - UART Tutorial
6.LPC2148 - UART Tutorial
In this tutorial, we are going to discuss the LPC2148 UART communication. LPC2148 has two
inbuilt USARTs. We are going to discuss the only UART0. After this tutorial, you should be
able to extend it to UART1. After understating the basics of LPC2148 UART module, we will
discuss how to use the Explore Embedded libraries to communicate with any of the UART
devices.
UART module
UART module and registers. LPC2148 has 2-UARTs numbering 0-3, similarly, the pins are also
named as RXD0-RXD1 and TXD0-TXD1.As the LPC2148 pins are multiplexed for multiple
functionalities, first they have to be configured as UART pins.
Below table shows the multiplexed UART's pins.
Register Description
UxLCR Controls the UART frame formatting(Number of Data Bits, Stop bits)
UxDLL Least Significant Byte of the UART baud rate generator value.
UxDLM Most Significant Byte of the UART baud rate generator value.
UxFCR
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.
UxLCR
Reserved DLAB Break Control Parity Select Parity Enable Stop Bit Select Word Length Select
UxLSR
31:8 7 6 5 4 3 2 1 0
UxTER
31:8 7 6-0
Baudrate Calculation
LPC1768 generates the baud rate depending on the values of DLM,DLL.
Baudrate = PCLK/ (16 * ( 256 * DLM + DLL) * (1+ DivAddVal/MulVal))
DivAddVal/MulVal == 0
1. Configure the GPIO pin for UART0 function using PINSEL register.
2. Configure the FCR for enabling the FIXO and Reset both the Rx/Tx FIFO.
3. Configure LCR for 8-data bits, 1 Stop bit, Disable Parity and Enable DLAB.
4. Calculate the DLM,DLL values for required baudrate from PCLK.
5. Update the DLM,DLL with the calculated values.
6. 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:
char uart_RxChar()
{
char ch;
while(util_IsBitCleared(U0LSR,SBIT_RDR)); // Wait till the data is received
ch = U0RBR; // Read received data
return ch;
}
view rawlpc2148_uartRxChar.c hosted with ❤ by GitHub
Code
Example 1
Below is the code for transmitting and receiving chars at 9600 baud
#include <lpc214x.h>
#include "stdutils.h"
#include "systemInit.h"
#define TX0_PINSEL 0
#define RX0_PINSEL 2
/* Function to initialize the UART0 at specifief baud rate */
void uart_init(uint32_t baudrate)
{
uint32_t var_RegValue_u32;
int main()
{
char ch,a[]="\n\rExploreEmbedded";
int i;
SystemInit();
uart_init(9600); // Initialize the UART0 for 9600 baud rate
while(1)
{
//Finally receive a char and transmit it infinitely
ch = uart_RxChar();
uart_TxChar(ch);
}
}
view rawlpc2148_uartExample1.c hosted with ❤ by GitHub
Using Explore Embedded Libraries
In the above tutorial, we discussed how to configure and use the inbuilt LPC2148 UART.
Now we will see how to use the ExploreEmbededd UART libraries to communicate on all the
four UART channels.
For this you have to include the uart.c/uart.h files and associated gpio/stdutils files.
As LPC1768 has four inbuilt UART channels, the interfaces are suffixed with channel number as
shown below.
UART0_Printf()
UART1_Printf()
int main()
{
SystemInit();
while(1)
{
UART0_Printf("Welcome to LPC2148 UART Programming on channel Zero at 9600 baud\n\r");
UART1_Printf("Welcome to LPC2148 UART Programming on channel One at 19200 baud\n\r");
}
}
view rawlpc2148_uartExample2.c hosted with ❤ by GitHub
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.