LPC2148 UART Send and Receive

Description

Today in this tutorial, we are going to learn How to send and receive data using UART in LP2148. Obviously there is more than 1 way to do this, so we will start with the simplest one first, and that is using the blocking mode.

In blocking mode, MCU will keep waiting for the data to be received, or keep waiting for all the data to be transmitted. It can’t perform any other tasks during this waiting period. Let’s first take a look at the registers that we are going to use.

Registers to use

uart registers

Above is the list of registers that we are going to use in this tutorial. There functions are as describe below

  • U0RBR is the Register, where the data received from the UART is saved.
  • U0THR is the register, where we are going to write data, so that it can be transmitted via UART
  • U0DLL and U0DLM are used to setup the desired Baud Rate
  • U0LCR is used to configure the UART
  • U0LSR is to check the status of different bits

We will discuss all these in details.

Some insight into the code

UART0_Init

void UART0_Init (void)
{
	U0LCR |= (1<<7);  // DLAB=1
	/* Set the BR = 9600 for PCLK = 60MHz*/
	U0DLM = 1;
	U0DLL = 134;
	U0LCR = 0x03;
}

Enable the DLAB (bit 7 of U0LCR) bit first, as it must be enabled to set the DLL and DLM registers.

Below is the formula to calculate the values of DLL and DLM for the desired Baud Rate. This formula is the modified version of the one from the datasheet

uart formula
uart formula

for the PCLK of 60 MHz and UART Baud Rate of 9600, the values of DLM is 1 and DLL is 134.

Below is the setup needed for the U0LCR register

LCR register
LCR register

The above setup along with DLB bit as 0 would result in U0LCR = 0x03;

UART0_Rx

uint8_t UART0_Rx (void)
{
	while (!(U0LSR & (1<<0)));  // wait until RDR bit is 1, which indicates that U0RBR have a valid data
	return U0RBR;
}

We will wait for the data to be received in the U0RBR register. We can check it by monitoring the RDR bit (bit 0 of U0LSR). If this bit is set to 1, this means that the U0RBR is not empty, and than we can read data by reading the U0RBR register

UART0_Tx

void UART0_Tx (uint8_t data)
{
	U0THR = data;  // load the data into THR
	while (!(U0LSR &(1<<5)));  // wait for the THRE bit to become 1, which indicated that the data is transmitted
}

Similarly, to send the data, we will first load it in the U0THR register. And than monitor the THRE bit (bit 5 of U0LSR). If this bit is set to 1, this indicates that the U0THR is empty and the data has been transmitted.

UART0_Send_String

void UART0_Send_String (char *str)
{
	while (*str)
	{
		UART0_Tx (*str++);
	}
}

UART0_Send_String is to send the string to the UART.

uint8_t Rx_data;

int main ()
{
	PINSEL0 |= (1<<0)|(1<<2);  // 0101 == select alternate functions for RX and Tx pins
	PLL_Init ();
	VPBDIV = 0x01;  // PCLK =HCLK = 60MHz
	
	UART0_Init();
	while (1)
	{
		Rx_data = UART0_Rx ();
		UART0_Tx (Rx_data);
	}
}

We have to select the alternate function for the P0.0 and P0.1 pins. Below is the setup for the PINSEL0 register

pinsel0 register

To set the pins as Rx and Tx, we have to write a ‘1’ to the position 0 and position 2.

The rest of the main function is simple to understand. Inside the while loop, we will wait for the data to be received, and than send the received data back to the UART.

Result

UART LPC2148 Result
UART LPC2148 Result

In the picture above, the PINK color is the transmitted data, and the black color is the received data. All the data which is received by the MCU is transmitted back to the UART and that’s what is being displayed on the console.

Check out the VIDEO Below

100%
100%

DOWNLOAD

You can buy me a coffee sensor ūüôā

Download LPC2148 UART code below

0 0 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
VAIBHAV SAREEN

how to get config bits for this prog ??

Menu
2
0
Would love your thoughts, please comment.x
()
x