Data Transmission using UART in LPC2148

UART stand for Universal Asynchronous Receiver/transmitter. We can use UART to transmit data between different devices. The term Asynchronous here means that the data is independent of the clock synchronization.This minimizes the pin requirements. Also most modules like GPS, GSM, bluetooth, Wifi, Xbee etc uses UART to communicate to the microcontroller.
Data transmission in UART is as follows
A start bit with logic 0 indicates start of data transfer followed by 8 data bits (least significant bit first) followed by a parity bit for error checking and at last a stop bit with logic 1.

Baud Rate:-
The Baud Rate specifies how fast the data is sent over a serial line. It's usually expressed in bps(bits per second). Generally 9600 bps is used in serial communication and this is what we are going to use. 

Note:- To transmit data to or receive from some device like Bluetooth, GSM or GPS etc, Rx of the controller should be connected to Tx of the device and vice versa.
For data transfer between controller and PC, a TTL logic converter (eg - MAX232) along with a rs232 cable is required. MAX232 IC coverts UART volatge level (+3v - 0) to rs232 voltage level (-12v - +12v).

Registers Used:-
1. U0FCR- UART0 FIFO Control Register controls the operation of UART0 Rx and Tx FIFOs.
     bit 0 - FIFO enable
     bit 1- Rx FIFO reset
     bit 2- Tx FIFO reset
2. U0LCR- UART0 Line Control Register determines the format of the data character that is to be transmitted or received.
    bit 1:0  word length select (we will be using 11 here to select 8 bit word length)
    bit 2  Stop bit select (we will use 0 here to select 1 stop bit)
    bit 3 Parity bit select   (we will use 0 here to select no parity bit)
    bit 7 DLAB- Divisor Latch Access Bit controls access to Divisor latch
3. U0THR- UART0 Transmit Holding Register is the top byte of the UART0 Tx FIFO. It holds the data to be transmitted.
4. U0RBR- UART0 Receive Buffer Register is the top byte of the UART0 Rx FIFO. The data received is saved in U0RBR.
5. U0LSR- UART0 Line Status Register is used to read the status of Rx and Tx blocks
    bit 0 - RDR- Receive Data Register- (0 means RDR is empty, 1 means RDR conatins data)
    bit 5 - THRE- Transmit Holding Register Empty- (0 means THRE contains data, 1 means empty)
6. U0DLL and U0DLM- UART0 Divisor Latch Registers is the part of the UART0 fractional Baud Rate generator and holds the value used to divide the clock supplied by prescaler in order to produce the baud rate clock.
Now the most important step is Baud rate generation. This can get a lot complicated with big formula and use of multival and divaddval. But there is a simpler way, so the formula goes like -
Now as I mentioned in the previous tutorials that although the crystal provides only 12 MHz, LPC2148 have capability to reach 60 MHz using PLLs. And we are going to divide 60 MHz by 4 using VPBDIV function inorder to get the Pclk as 15 MHz. 
According to formula, if PCLK is 15 MHz and we want the BR of 9600, U0DLL will be around 97 in decimal.
7. As we are using Port0.0 and Port0.1 as Tx and Rx pins, we have to set PINSEL0 register as Alternating functions rather than normal GPIO.
To enable the Tx and Rx pins, we have to set bit 0 and bit 2 of the pinsel register.  PINSEL0|= (1<<0)|(1<<2)

1. Set the corresponding bits in PINSEL0 for alternating functions.
2. Enable FIFO and reset Rx and Tx FIFOs by setting and clearing bits in U0FCR
3. Select the word length, stop bit, parity bit and dlab bit in U0LCR
4. Set the Baud rate by writing specific values to U0DLL and U0DLM
5. Clear the DLAB bit in U0LCR
6. To transmit data, wait for THRE bit in U0LSR to be cleared and than write data in U0THR
7. To receive data, wait for RDR bit in U0LSR to be cleared and than receive data from U0RBR.

#include <lpc214x.h>
#include "pll.h"

void Uart_init ()
    PINSEL0 |= (1<<0) | (1<<2);   //Configure P0.0/P0.1 as RX0 and TX0 
    U0FCR = (1<<0) | (1<<1) | (1<<2);  // Enable FIFO and reset Rx/Tx FIFO buffers    
    U0LCR = 3 | (1<<7);   // 8bit data(0x03), 1Stop bit(bit 4), No parity(bit 5) and enable DLAB(bit 7)
    U0DLL = 0x61;   // (15MHz)/(16*9600)
    U0DLM = 0;
    U0LCR &= ~(1<<7);   // clear the DLAB bit

void Uart_Tx (char data)   // transmit a character via uart
while (!(U0LSR & (1<<5)));   // wait for THRE to clear
U0THR = data;

void Uart_Tx_string (char *data)   // transmit whole string
while (*data) Uart_Tx (*data++);   

int main (void)
char hello[]= "hello world!";   // data to be transmitted 
pll_init ();   // initialize pll clocks
VPBDIV = 0x00;   // pclk = cclk/4, pclk = 15MHz
Uart_init();   // initialize UART0
Uart_Tx_string (hello);   // send hello string

To view data you will be required to use any serial monitor of course. There are many serial monitors available such as Realterm, hercules etc. Also make sure to check the com port in device manager, mine is com1

Again check for com port, Baud rate, stop and parity bits in serial monitor
That's it guys. Now you can transfer data between controller and computer using uart.
Subscribe for more tutorials
To download full working code click HERE
Data Transmission using UART in LPC2148 Data Transmission using UART in LPC2148 Reviewed by arun singh on June 11, 2017 Rating: 5

No comments:

Powered by Blogger.