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
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
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
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
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
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.
. . . . . DOWNLOAD . . . . .
You can buy me a
controllerstechcoffeeSensor by clicking DONATE OR Just click DOWNLOAD to download the code
Recent Posts
- WavePlayer using STM32 Discovery January 16, 2021
- How to use SPI in LPC2148 December 26, 2020
- STM32 ADC MULTI CHANNEL WITHOUT DMA December 24, 2020
- Send and Receive data to PC without UART (STM32 USB COM) December 10, 2020
- STM32 GPIO INPUT Configuration December 4, 2020
- STM32 GPIO OUTPUT Config using REGISTERS November 24, 2020