In this tutorial, we will communicate microcontroller and PC over serial communication using UART in LPC2148 ARM7 Microcontroller. Before we move on to UART in LPC2148 ARM7. Let’s first discuss UART in general. UART (Universal Asynchronous Receiver/Transmitter) is one of the earliest mode of communication applied to computer (somewhere in 1960s). The information is transmitted one binary bit at a time; as such it is a serial communication method. These bits are grouped together in the form of ‘Frames’ (a set format) for conveying one meaningful piece of data (e.g. character byte). UART is asynchronous because it doesn’t require a transmitter provided clock to synchronize the transmission and receipt of data.
Serial Data Transmission in UART
Just because there is no clock signal per se, a start bit is added sent first to tell the receiver to listen out for data. The receiver monitors for a logic HIGH falling to logic LOW. The receiver synchronizes its own bus clock to that make up the word being sent, with bit zero, the least significant bit (LSB) being sent first. The bits are sent as pulses on the wire at specific time intervals, set at both ends of links to previously agreed values. The receiver looks at the voltage on the wire at these times; if it sees logic high, it records a binary digit 1 or 0 if the line is low. The receiver checks half way between the start and the end of the pulse to ensure it does not miss-read the voltage on the line during the brief interval while the voltage is rising or falling.
If two devices use a parity bit for rudimentary error checking, that is calculated and sent next, in sync with data that has been transmitted thus far. Finally, one stop bit is sent by the transmitter. Word length, parity availability and type, and numbers of stop bits all have to be agreed in advance for successful communication because UART uses two wires. The transmitter of device-A connected to receiver of device-B and receiver of device-A connected to transmitter of device-B. This is how devices can send data simultaneously to each other, a mode of communication called ‘full duplex’.
INTRODUCTION: UART in LPC2148 ARM7 Microcontroller
As we all know UART is widely used serial communication protocol in embedded system based applications. Almost all microcontrollers have built-in on-chip UART support. LPC2148 ARM7 core supports two UART in it, UART0 and UART1. UART0 can be used as general purpose UART and also can support ISP Programming through it, whereas UART1 has additional modem support. Both have built in baud rate generator and 16-byte transmit and receive FIFOs. For UART0 the TxD Pin is at P0.0 and RxD Pin is at P0.1 and similarly for UART1 the TxD Pin is at P0.8 and RxD Pin is at P0.9 as shown in table below;
RS232 Level Converter
Most of microchips work on TTL or CMOS voltage level which can’t be used to communicate over RS-232 protocol. In this case voltage or level converter is needed which can convert TTL to RS-232 and RS-232 to TTL voltage levels. The most commonly used RS-232 level converter is MAX3232 chip. This chip includes charge pump which can generate RS232 voltage levels (-10V and +10V) from 5V power supply. It also includes two receiver and two transmitters and is capable of full-duplex UART communication. RS232 communication enables point-to-point data transfer, which often used in data acquisition applications and for data transfer between microcontroller and PC.
In example project, we will use RS232 cable (USB to Serial Converter) to accomplish communication between LPC2148 Microcontroller and PC. *Make sure to install appropriate drivers before you make use of it.
Registers Description: UART in LPC2148 ARM7
Before we use any pin from LPC2148 for serial communication. We have to configure and initialize UART0 Peripheral by setting register bits. Let’s quickly revise functions of UART0 registers which will be used in our code later on. [It is recommended to keep user manual open to collect more details about each register if in case needed].
|U0THR||Transmit Hold Register: This register contains 8-bit write data which can be transmitted through UART0. This is write only register.|
|U0RBR||Receive Buffer Register: This register contains 8-bit received data from UART0. This data is nothing but top most byte of Rx FIFO. When we use 5, 6 or 7-bit data then remaining bits are padded with 0’s by default. This is read only register.|
|U0LCR||Line Control Register: The value or settings in this register configure the UART0 block. As this is an 8-bit register. There are several parameters configured through this register such as word length, stop bit, parity enable, parity select, break control, divisor latch access bit. This register setting plays important role while initializing UART0 before using it.|
|U0DLL & U0DLM||U0DLL & U0DLM are standard UART0 baud rate generator divider registers. Each of this register holds 8-bit values. Together these registers form a 16-bit divisor value which will be used for baud rate generation. This will be discussed further while code explanation with respect to real world example.|
|U0FDR||Fractional Divider Register: This is another very important register, which plays significant role in baud rate generation. In this 8-bit register, first four bits i.e. Bit[3 to 0]-DIVADDVAL: This is the Prescale Divisor value. If this value is 0 then fractional baud rate generator have no effect on UART0 baud rate. The remaining 4-bits i.e. Bit[4 to 7]-MULVAL: This defines Prescale Multiplier value. Even if fractional baud rate generator is not used the value in this register must be more than or equal to ‘1’.|
Calculate Baud Rate for UART in LPC2148 ARM7
Baud Rate for UART in LPC2148 ARM7 Microcontroller can be calculated by given equation:
Let’s look at real world example where we will send string from LPC2148. This message will be received and displayed on terminal emulator or serial console which configured to appropriate COM Port of PC. I believe now you’re familiar with how UART works. In our program we will use following configuration to establish proper communication:
- Baud Rate = 9600 baud (with PCLK=60Mhz)
- Data Length = 8 bits
- No Parity Bit
- and 1 Stop Bit
To work out on this project we need following things to be setup:
Software Requirements: Install Keil uVision4, Flash Magic, PuTTY (terminal emulator or serial console)
Hardware Requirements: LPC2148 Development Board, RS232 Cable (USB Serial Converter), Power Adapter (9V-500mA).
Circuit Diagram: UART in LPC2148 ARM7 Microcontroller
Source Code: UART in LPC2148 ARM7 Microcontroller[crayon-5c6ac09a7c7cd453002045/]
The program is well commented in code itself. Let’s run code on real hardware to see its impact. Now just compile the code above and load .HEX file onto the LPC2148. We need PuTTY (Terminal Emulator) to observe data sent by LPC2148 Microcontroller over UART0. So here is the setting to configure PuTTY for UART with appropriate COM port, data bits and baud rate etc etc.
IMPORTANT: After loading .hex file, Make sure that you’re not into ISP Mode. We will be using same UART0 for programming as well as for receiving data. In case if you’re using STK2148-UltraLite Board then turned off SW7 switches to read string on PuTTY.
Explore Settings Further
Once we have done with all settings, just open console. And we will start receiving string on PuTTY, sent by LPC2148 Microcontroller. Here is an output from our project:
This is how we can accomplish serial communication using UART in LPC2148 ARM7 Microcontroller to transmit character string from microcontroller to PC. I recommend you to modify the code to explore UART feature in LPC2148. I will try my best to share one more post where we will not only receive but also transmit data. UART will be useful for our future projects where we will display ADC data or output of sensor on terminal emulator. If you have any questions then feel free to leave a comment.