(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[category: LPC2148 Tutorials]]
 
[[category: LPC2148 Tutorials]]
 
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 ExploreEmbedded libraries to communicate with any of the UART devices.<br>
 
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 ExploreEmbedded libraries to communicate with any of the UART devices.<br>
For more info on UART/RS232 check [[A4.8051 Communication Protocols:UART, RS232|8051 tutorial]].<br>
 
 
[[File:0 UART main.gif]]
 
[[File:0 UART main.gif]]
 
<br><br>
 
<br><br>
Line 42: Line 41:
 
|UxDLM|| Most Significant Byte of the UART baud rate generator value.
 
|UxDLM|| Most Significant Byte of the UART baud rate generator value.
 
|}
 
|}
<br><br>
 
 
*Note: x-> 0/1 (UART0/UART1)
 
*Note: x-> 0/1 (UART0/UART1)
 
<br><br>
 
<br><br>
Line 48: Line 46:
 
=UART Register Configuration=
 
=UART Register Configuration=
 
Now lets see how to configure the individual registers for UART communication.
 
Now lets see how to configure the individual registers for UART communication.
==FCR ( FIFO Control Register )==
+
==UxFCR ( FIFO Control Register )==
 
LPC2148 has inbuilt 16byte FIFO for Receiver/Transmitter. Thus it can store 16-bytes of data received on UART without overwriting. If the data is not read before the Queue(FIFO) is filled then the new data will be lost and the OVERRUN error bit will be set.
 
LPC2148 has inbuilt 16byte FIFO for Receiver/Transmitter. Thus it can store 16-bytes of data received on UART without overwriting. If the data is not read before the Queue(FIFO) is filled then the new data will be lost and the OVERRUN error bit will be set.
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
|-class="info"
 
|-class="info"
|FCR
+
|UxFCR
 
|-
 
|-
 
|31:8|| 7:6 || 5:3 || 2 || 1 || 0   
 
|31:8|| 7:6 || 5:3 || 2 || 1 || 0   
Line 83: Line 81:
 
<br><br><br><br><br>
 
<br><br><br><br><br>
  
==LCR ( Line Control Register )==
+
==UxLCR ( Line Control Register )==
 
This register is used for defining the UART frame format ie. Number of Data bits, STOP bits etc.
 
This register is used for defining the UART frame format ie. Number of Data bits, STOP bits etc.
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
|-class="info"
 
|-class="info"
|LCR
+
|UxLCR
 
|-
 
|-
 
|31:8|| 7 || 6 || 5:4 || 3 || 2 || 1:0   
 
|31:8|| 7 || 6 || 5:4 || 3 || 2 || 1:0   
 
|-
 
|-
|Reserved|| DLAB ||  Break COntrol || Parity Select || Parity Enable || Stop Bit Select || Word Length Select
+
|Reserved|| DLAB ||  Break Control || Parity Select || Parity Enable || Stop Bit Select || Word Length Select
 
|}  
 
|}  
 
   
 
   
Line 129: Line 127:
 
<br><br><br><br><br>
 
<br><br><br><br><br>
  
==LSR (Line Status Register)==
+
==UxLSR (Line Status Register)==
 
The is a read-only register that provides status information of the UART TX and RX blocks.
 
The is a read-only register that provides status information of the UART TX and RX blocks.
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
|-class="info"
 
|-class="info"
|LSR
+
|UxLSR
 
|-
 
|-
 
|31:8 || 7 || 6 || 5 || 4 || 3 || 2 || 1|| 0   
 
|31:8 || 7 || 6 || 5 || 4 || 3 || 2 || 1|| 0   
Line 182: Line 180:
 
1-- RBR contains at least one RX error.<br><br><br>
 
1-- RBR contains at least one RX error.<br><br><br>
  
==TER (Transmitter Enable register)==
+
==UxTER (Transmitter Enable register)==
 
This register is used to Enable/Disable the transmission
 
This register is used to Enable/Disable the transmission
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
|-class="info"
 
|-class="info"
|TER
+
|UxTER
 
|-
 
|-
 
|31:8|| 7 || 6-0   
 
|31:8|| 7 || 6-0   
Line 203: Line 201:
 
LPC1768 generates the baud rate depending on the values of DLM,DLL.<br>  
 
LPC1768 generates the baud rate depending on the values of DLM,DLL.<br>  
 
<b>Baudrate = PCLK/ (16 * ( 256 * DLM + DLL) * (1+ DivAddVal/MulVal))</b><br>
 
<b>Baudrate = PCLK/ (16 * ( 256 * DLM + DLL) * (1+ DivAddVal/MulVal))</b><br>
 
<b>Getting the PCLK value.</b><br>
 
PCLKSELx registers contains the PCLK info for all the clock dependent peripherals in which Bit6,Bit7 contains the Uart Clock(ie.UART_PCLK) information.<br>
 
The UART_PCLK and the actual Peripheral Clock(PCLK) is calculated as below.<br>
 
(Refer data sheet for more info)
 
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
|-class="info"
 
|UART_PCLK  || PCLK
 
|-
 
|0|| SystemFreq/4
 
|-
 
|1|| SystemFreq
 
|-
 
|2|| SystemFreq/2
 
|-
 
|3|| SystemFreq/8
 
|}
 
 
   
 
   
 
   
 
   
Line 227: Line 207:
 
Using the above parameters , DLL/DLM is calculated as below.<br>
 
Using the above parameters , DLL/DLM is calculated as below.<br>
 
<b>(256 * DLL + DLM) = PCLK / (16* Baudrate).</b><br><br><br>
 
<b>(256 * DLL + DLM) = PCLK / (16* Baudrate).</b><br><br><br>
 
 
  
 
=Steps for Configuring UART0=
 
=Steps for Configuring UART0=
 
Below are the steps for configuring the UART0.
 
Below are the steps for configuring the UART0.
#Step1: Configure the GPIO pin for UART0 function using PINSEL register.
+
#.Configure the GPIO pin for UART0 function using PINSEL register.
#Step2: Configure the FCR for enabling the FIXO and Reste both the Rx/Tx FIFO.
+
#.Configure the FCR for enabling the FIXO and Reste both the Rx/Tx FIFO.
#Step3: Configure LCR for 8-data bits, 1 Stop bit, Disable Parity and Enable DLAB.
+
#.Configure LCR for 8-data bits, 1 Stop bit, Disable Parity and Enable DLAB.
#Step4: Get the PCLK from PCLKSELx register 7-6 bits.
+
#.Calculate the DLM,DLL values for required baudrate from PCLK.
#Step5: Calculate the DLM,DLL vaues for required baudrate from PCLK.
+
#.Update the DLM,DLL with the calculated values.
#Step6: Updtae the DLM,DLL with the calculated values.
+
#.Finally clear DLAB to disable the access to DLM,DLL.
#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.<br>
After this the UART will be ready to Transmit/Receive Data at the specified baudrate.<br>
+
  
 
<b>Code sniffet:</b>
 
<b>Code sniffet:</b>
 
<html>
 
<html>
<script src="https://gist.github.com/Amritach/35437ef514c0ad0f9089.js"></script>
+
<script src="https://gist.github.com/SaheblalBagwan/772a7a88913fe806a500d4c0ec4660f0.js"></script>
 
</html>
 
</html>
 
<br><br>
 
<br><br>
Line 252: Line 229:
 
<b>Code snippet</b>
 
<b>Code snippet</b>
 
<html>
 
<html>
<script src="https://gist.github.com/Amritach/50b91b48e5722b03ad4c.js"></script>
+
<script src="https://gist.github.com/SaheblalBagwan/7ed8bd46d15946fbaa53f7175202b5a1.js"></script>
 
</html>
 
</html>
 
<br><br>
 
<br><br>
Line 261: Line 238:
 
<b>Code snippet</b>
 
<b>Code snippet</b>
 
<html>
 
<html>
<script src="https://gist.github.com/Amritach/c6b329d11c5406347279.js"></script>
+
<script src="https://gist.github.com/SaheblalBagwan/7e53caf9e2a53ff8f38582555892d5c7.js"></script>
 
</html>
 
</html>
  
Line 270: Line 247:
 
Below is the code for transmitting and receiving chars at 9600 baud<br>
 
Below is the code for transmitting and receiving chars at 9600 baud<br>
 
<html>
 
<html>
<script src="https://gist.github.com/SaheblalBagwan/dfa8847f40f1cf14f20fae2bb2a6d83c.js"></script>
+
<script src="https://gist.github.com/SaheblalBagwan/006072f686bf9fa6eca5ebe38367f5de.js"></script>
 
</html><br><br><br>
 
</html><br><br><br>
  
 
===Using Explore Embedded Libraries===
 
===Using Explore Embedded Libraries===
In the above tutorial we discussed how to configure and use the inbuilt LPC1768 UART.<br>
+
In the above tutorial, we discussed how to configure and use the inbuilt LPC2148 UART.<br>
 
Now we will see how to use the ExploreEmbededd UART libraries to communicate on all the four UART channels.<br>
 
Now we will see how to use the ExploreEmbededd UART libraries to communicate on all the four UART channels.<br>
 
For this you have to include the uart.c/uart.h files and associated gpio/stdutils files.<br>
 
For this you have to include the uart.c/uart.h files and associated gpio/stdutils files.<br>
Line 281: Line 258:
 
UART0_Printf()<br>
 
UART0_Printf()<br>
 
UART1_Printf()<br>
 
UART1_Printf()<br>
UART2_Printf()<br>
 
UART3_Printf()<br>
 
  
 
#Note:Refer the uart.h file for more info.
 
#Note:Refer the uart.h file for more info.
  
 
<html>   
 
<html>   
<script src="https://gist.github.com/SaheblalBagwan/7db91d1895e3bfe9534c8f657af89a79.js"></script>
+
<script src="https://gist.github.com/SaheblalBagwan/618282d6fc7654ee0b5b2271871e320e.js"></script>
 
</html>
 
</html>
  
Line 304: Line 279:
  
 
= Downloads=
 
= Downloads=
Download the complete project folder from the below link:  
+
Download the complete project folder from [https://github.com/ExploreEmbedded/LPC2148_Stick this link]
 
<br><br>
 
<br><br>
 
    
 
    
 
Have an opinion, suggestion , question or feedback about the article let it out here!
 
Have an opinion, suggestion , question or feedback about the article let it out here!
 
{{DISQUS}}
 
{{DISQUS}}

Latest revision as of 13:43, 24 August 2016

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 ExploreEmbedded libraries to communicate with any of the UART devices.
0 UART main.gif

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.

Port Pin Pin Number PINSEL_FUNC_0 PINSEL_FUNC_1 PINSEL_FUNC_2 PINSEL_FUNC_3
P0.0 19 GPIO TXD0 PWM1
P0.1 21 GPIO RXD0 PWM3 EINT0
P0_8 33 GPIO TXD1 PWM4 AD1.1
P0.9 34 GPIO RXD1 PWM6 EINT3





UART Registers

The below table shows the registers associated with LPC2148 UART.

Register Description
UxRBR Contains the recently received Data
UxTHR Contains the data to be transmitted
UxFCR FIFO Control Register
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.
  • Note: x-> 0/1 (UART0/UART1)



UART Register Configuration

Now lets see how to configure the individual registers for UART communication.

UxFCR ( FIFO Control Register )

LPC2148 has inbuilt 16byte FIFO for Receiver/Transmitter. Thus it can store 16-bytes of data received on UART without overwriting. If the data is not read before the Queue(FIFO) is filled then the new data will be lost and the OVERRUN error bit will be set.

UxFCR
31:8 7:6 5:3 2 1 0
RESERVED RX TRIGGER RESERVED TX FIFO RESET RX FIFO RESET FIFO 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 7:6 – Rx_TRIGGER:
This bit is used to select the number of bytes of the receiver data to be written so as to enable the interrupt/DMA.
00-- Trigger level 0 (1 character or 0x01)
01-- Trigger level 1 (4 characters or 0x04)
10-- Trigger level 2 (8 characters or 0x08)
11-- Trigger level 3 (14 characters or 0x0E)




UxLCR ( Line Control Register )

This register is used for defining the UART frame format ie. Number of Data bits, STOP bits etc.

UxLCR
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

Bit 2 – Stop Bit Selection:
This bit is used to select the number(1/2) of stop bits
0-- 1 Stop bit
1-- 2 Stop Bits

Bit 3 – Parity Enable:
This bit is used to Enable or Disable the Parity generation and checking.
0-- Disable parity generation and checking.
1-- Enable parity generation and checking.

Bit 5:4 – Parity Selection:
These two bits will be used to select the type of parity.
00-- Odd parity. Number of 1s in the transmitted character and the attached parity bit will be odd.
01-- Even Parity. Number of 1s in the transmitted character and the attached parity bit will be even.
10-- Forced "1" stick parity.
11-- Forced "0" stick parity

Bit 6 – Break Control
0-- Disable break transmission.
1-- Enable break transmission. Output pin UARTn TXD is forced to logic 0


Bit 8 – DLAB: Divisor Latch Access Bit
This bit is used to enable the access to divisor latch.
0-- Disable access to divisor latch
0-- Enable access to divisor latch




UxLSR (Line Status Register)

The is a read-only register that provides status information of the UART TX and RX blocks.

UxLSR
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.

Bit 1 – OE: Overrun Error
The overrun error condition is set when the UART Rx FIFO is full and a new character is received. In this case, the UARTn RBR FIFO will not be overwritten and the character in the UARTn RSR will be lost.
0-- No overrun
1-- Buffer over run

Bit 2 – PE: Parity Error
This bit is set when the receiver detects a error in the Parity.
0-- No Parity Error
1-- Parity Error

Bit 3 – FE: Framing Error
This bit is set when there is error in the STOP bit(LOGIC 0)
0-- No Framing Error
1-- Framing Error

Bit 4 – BI: Break Interrupt
This bit is set when the RXDn is held in the spacing state (all zeroes) for one full character transmission
0-- No Break interrupt
1-- Break Interrupt detected.

Bit 5 – THRE: Transmitter Holding Register Empty
THRE is set immediately upon detection of an empty THR. It is automatically cleared when the THR is written.
0-- THR register is Empty
1-- THR has valid data to be transmitted

Bit 6 – TEMT: Transmitter Empty
TEMT is set when both UnTHR and UnTSR are empty; TEMT is cleared when any of them contain valid data.
0-- THR and/or the TSR contains valid data.
1-- THR and the TSR are empty.


Bit 7 – RXFE: Error in Rx FIFO
This bit is set when the received data is affected by Framing Error/Parity Error/Break Error.
0-- RBR contains no UARTn RX errors.
1-- RBR contains at least one RX error.


UxTER (Transmitter Enable register)

This register is used to Enable/Disable the transmission

UxTER
31:8 7 6-0
Reserved TXEN Reserved


Bit 7 – TXEN: Trsnamitter Enable
When this bit is 1, the data written to the THR is output on the TXD pin.
If this bit is cleared to 0 while a character is being sent, the transmission of that character is completed, but no further characters are sent until this bit is set again.
In other words, a 0 in this bit blocks the transfer of characters.

  • Note: By default this bit will be set after Reset.


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

Using the above parameters , DLL/DLM is calculated as below.
(256 * DLL + DLM) = PCLK / (16* Baudrate).


Steps for Configuring UART0

Below are the steps for configuring the UART0.

  1. .Configure the GPIO pin for UART0 function using PINSEL register.
  2. .Configure the FCR for enabling the FIXO and Reste 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:

Steps for transmitting a char

  1. Step1: Wait till the previous char is transmitted ie. till THRE becomes high.
  2. Step2: Load the new char to be transmitted into THR.

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



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()

  1. Note:Refer the uart.h file for more info.

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.

Lpc1768SerialConnection.JPG

Open the terminal software , select the COM port, set baud rate and hit the connect button.



Downloads

Download the complete project folder from this link

Have an opinion, suggestion , question or feedback about the article let it out here!