This Project demonstrates how to use UART in ARM Cortex-M3 LPC1768. In this project we are doing serial communication between PC and LPC1768 Board using MAX232. We are using 9600 baud rate for serial communication. Here data will be sent by PC (Teraterm) and on the other end we are receiving data in LPC1768 Microcontroller Board. Data received in microcontroller now will be sent back again to PC to show on terminal.
Fig. 1: Prototype of LPC1768 ARM Microcontroller UART Serial Communication
First of all we will see how to configure UART in LPC1768 –
- Power: In the PCONP (Power Control for Peripherals) register set bit PCUART0. It will make the UART0 operational.
2) Baud rate: In register U0LCR, set bit DLAB =1. This enables access to registers DLL and DLM for setting the baud rate. Also, if needed, set the fractional baud rate in the fractional divider register.
3) UART FIFO: Use bit FIFO enable (bit 0) in register U0FCR to enable FIFO.
4) Pins: Select UART pins through the PINSEL registers (For UART0 we should use PINSEL0).
5) Tx/Rx: Use U0THR and U0RBR for data transmission and reception.
6) Status: Use U0LSR register for the status of data.
Baud rate Calculation: The Baud rate of this Microcontroller can be calculated by following equation –
DL = PCLK/(16 x BR)
Here,
PCLK = Peripheral Clock Frequency
BR = Required Baud rate i.e. 9600
DL = It is the combination of DLM and DLL registers.
There are two conditions for calculating baud rate –
Example 1 : BR = 9600 and PCLK = 14.7456 MHz
According to the provided algorithm DLest = PCLK/(16 x BR) = 14.7456 MHz / (16 x 9600) = 96. Since this DLest is an integer number, DIVADDVAL = 0, MULVAL = 1, DLM = 0, and DLL = 96.
Fig. 2: Flow Chart of C Code for UART based serial communication over LPC1768 ARM Microcontroller
Example 2 : BR = 115200 and PCLK = 12 MHz
According to the provided algorithm DLest = PCLK/(16 x BR) = 12 MHz / (16 x 115200) = 6.51. This DLest is not an integer number and the next step is to estimate the FR parameter. Using an initial estimate of FRest = 1.5 a new DLest = 4 is calculated and FRest is recalculated as FRest = 1.628. Since FRest = 1.628 is within the specified range of 1.1 and 1.9, DIVADDVAL and MULVAL values can be obtained from the attached look-up table.
The closest value for FRest = 1.628 in the look-up table is FR = 1.625. It is equivalent to DIVADDVAL = 5 and MULVAL = 8.
Based on these findings, the suggested UART setup would be: DLM = 0, DLL = 4, DIVADDVAL = 5, and MULVAL = 8. According to equation the UART rate is 115384.This rate has a relative error of 0.16% from the originally specified 115200.
Code Explanation:
1) SystemInit : This will initialize the system clock frequency, PLL settings and peripheral clock frequency for the LPC1768. It will generate System clock = 72 MHz, PCLK(Peripheral clock) = 18 MHz.
2) uart0_pinconfig : It will configure the P0.2 pin as UART0 Tx and P0.3 pin as UART0 Rx to enable the serial communication over these pins.
3) init_uart0 : This routine will initialize the uart0 at 9600 baud rate. First of all to access the DLL and DLM registers we need to make DLAB bit high in LCR register. After this we have to move baud rate value into DLL and DLM registers. After moving baud rate values, we again need to clear the DLAB bit of LCR register so that we can access the THR and RBR registers.
4) uart0_rx : In this routine we are monitoring the receive status bit RDR in U0LSR register for complete data reception. If the data has been arrived in U0RBR register then this bit will become high. Now you can move the data to any variable.
5) uart0_tx : This routine will send the data byte into U0THR register and after that it will monitor the THRE bit in the U0LSR register until this bit become high.
So we are continuously sending the data byte through PC and receiving the data in LPC1768 microcontroller and again we are sending back the data byte which has been received to the PC and that data byte will be shown on terminal(i.e. Hyper terminal, Tera term etc).
Project Source Code
### #define UART_TXD0 (1 << 4) #define UART_RXD0 (1 << 6) #define DLAB (1 << 7) #define WORD_LEN BITS_8 #define STOP_BITS STOP_1 #define BR_DLL 78 #define BR_DLM 00 #define MUL_VAL (MVAL << 4) #define DIV_VAL (DVAL << 0) #define EN_FIFO (1 << 0) #define RST_RX_FIFO (1 << 1) #define RST_TX_FIFO (1 << 2) #define RDR 0x01 #define THRE 0x20 #define BITS_8 (3 << 0) #define STOP_1 (1 << 2) #define MVAL 02 #define DVAL 01 unsigned char uart0_rx(void); void uart0_tx(unsigned char); void uart0_pinconfig(void); void init_uart0(void); LPC.C #include <LPC17xx.H> #include "uart.h" int main(void) { unsigned char val; SystemInit(); // Initialize the System and Peripheral Clock uart0_pinconfig(); init_uart0(); while(1) { val = uart0_rx(); // Get the data byte from UART0 Rx uart0_tx(val); // Send the data byte to UART0 Tx } } ###
Project Video
Filed Under: Electronic Projects
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.