LPC2148 UART Send and Receive
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
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;
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
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 is to send the string to the UART.
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.
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 acontrollerstech
coffeeSensor by clicking DONATE OR Just click DOWNLOAD to download the code
- 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