Está en la página 1de 10

LAB TUTORIAL ON UART

A universal asynchronous receiver/transmitter, abbreviated UART, is a type of "asynchronous receiver/transmitter", a piece of computer hardware that translates data between parallel and serial forms. UARTs are commonly used in conjunction with communication standards such as EIA RS-232, RS-422 or RS-485. The universal designation indicates that the data format and transmission speeds are configurable and that the actual electric signaling levels and methods (such as differential signaling etc) typically are handled by a special driver circuit external to the UART. A UART is usually an individual (or part of an) integrated circuit used for serial communications over a computer or peripheral device serial port. UARTs are now commonly included in microcontrollers. A dual UART, or DUART, combines two UARTs into a single chip. Many modern ICs now come with a UART that can also communicate synchronously; these devices are called USARTs (universal synchronous/asynchronous receiver/transmitter).
About UART : UART is one of the most simple serial interface. Generally UART is used to transfer data between different PCBs (Printed Circuit Boards). These PCBs can be either in the same system or across different systems. In its simplest configuration, UART consists of two pin interface. One pin is used for Transmission, and other for Reception. The data on UART is transferred word by word. A word consists of Start Bit, Data bits (5 to 8), (and optional parity bit) and (1, 1.5 or 2) Stop Bit

Asynchronous Communication : Deals with Baud Rate Baud is a measurement of transmission speed in asynchronous communication. It is defined as the number of distinct symbol changes made to the transmission media per second. Since UART signal has only two levels (high and low), baud rate here is also equal to the bit rate. What we shall do with UART ? LPC2148 has two on-chip UART devices UART0 and UART1. You need to know about following before we program with UART : 1. VPBDIV register : Set PCLK to the frequency that is to be program using 2. PINSEL register : Configure Pin for UART TxD and RxD 3. LatchesU0LCR register : Configure UART Line Control Register for word length, parity, stop bit and enable/disable access to Divisor latch. 4. U0DLL register: Configure UART Divisor Latch LSB reg. UART employs LSB first trans. 5. U0LSR register: UART Line Status Register to check status for U0THR reg 6. U0THR register : Transmits a character by writing it to the Transmit Holding Register (U0THR)

register. (i.e. U0THR = c; /*c is the char to be transmitted*/) Before writing into U0THR it needs to be empty always. The 5th bit of U0THR is for this status that can be checked with U0LSR register value. If 5th bit is 1 then it is empty and can be used now. 7. U0RBR register : UART Receiver Byte(Data) Ready reg is used on the receiver side. The 0th bit of U0LSR is RDR and checked if data is there and used for U0RBR.

This tutorial demonstrates how to program UART0 or UART1 available in LPC2148 to transmit characters/string to some other device that supports hyper terminal. Try to understand the steps in consultation with the manual for different UART specific registers. Listed below are the steps to program, followed by an explanation of it. Steps : 1 : Take the string into the array. Say string Hello World. 2. Set PCLK to the frequency, say 60 MHz. 3. Configure PINSEL for TxD and RxD for two pins say P0.0 and P0.1 for example. 4. Configure Line Control Register (LCR) as per the problem statement requirement. 5. Configure Divisor Latch Registers for baud rate at the specified frequency VPB clock. Calculate with the formula Divisor = PCLK / (16 x baud rate). 6. Configure the LCR to handle DLAB bit (refer manual) to be disabled.( Why? refer manual) 7. Loop to send the characters of the string. You may write a separate function to send characters one by one. Also send the carriage return char i.e.\r and new line char i.e. \n. This two chars will be required to keep the alignment of the string in the receiver side and to keep track of the memory space of the string that will be displayed. 8. Now fall in infinite loop keeping the sender (in our case the LPC2148) in prompt to send the string everytime you press PB1 in the kit. 9. In the function that will send the chars, you need to do two things. First keep looping until the transmit holding register is empty. Second put the char in the transmit holding register. 10.This step is optional. This will be required to for sending from one UART to other UART. In this step you will require another function say get_char(), that will keep looping until there is a char in RBR and return the char received. Explanation: First the program sets the PCLK to be a <value>MHz clock. Then it selects the functionality for pins Px.y and Px.y. Why? For Transmit (TxD) and Receive (RxD) pins for UART0/1.

Next it configures Line Control Register for character length, parity/no parity and stop bits. It also enables the access to Divisor Latches. How? Refer to the snapshot of the Line Control register below to understand it. As can be seen bits 1 and 0 in the register determines the Word Length Select. These two bits should be set according to the length of the char. Bit 2 in this register is the Stop Bit Select bit. This bit should be set to 0 to use 1 stop bit. Bit 3 in this register is the Parity Enable bit. This bit should be set to 0 to disable parity. Bit 7 is the Divisor Latch Access Bit (DLAB). This bit should be set to 1 to enable access to the Divisor Latches. Say for example, for 8-bit character length, 1 stop bit, no parity and Divisor Latches access enabled the bits 7, 3, 2, 1 and 0 should be set to 1, 0, 0, 1 and 1 respectively. The other bits can be 0. This means that the following values should be written for 8-bits char to be 10000011. This value will be assigned U0LCR register. To understand this follow the manual.

Next it configures the Divisor Latch Registers for baud rate given at <value> MHz VPB clock. As mentioned in above in steps, the VPB clock is configured to be <value> MHz. The baud rate clock must be 16 times the desired baud rate. The baud rate clock is obtained by dividing the PCLK with a 16-bit divisor. This means that, (PCLK/Divisor) = 16 x Baud Rate. Here DLL and DLM registers together form the 16-bit divisor. DLL contains the lower eight bits of the divisor and DLM contains the higher eight bits of the divisor. Suppose, the divisor value is 0xC3. Then DLL is set to 0xC3 (the lower eight bits of the divisor is 0xC3). The upper eight bits of the divisor is 0x00. This needs to be stored in DLM. However, as DLM register already have the 0x00 value on reset, this assignment is not required.

Next it disables the access to the Divisor latches. The description for this statement is the same as above. The only difference is that the DLAB bit is set to 0, to disable access to the Divisor Latches. Now loop to transmit the chars calling the SendChar function. This function is described below. Description of the SendChar function It is to make sure that THR is empty before writing to the THR register. How can we know if THR register is empty or not? This can be known by checking the Transmitter Holding Register Empty bit in the Line Status Register. Bit number 5 is the Transmitter Holding Register Empty bit. THR register is empty if bit number 5 is equal to 1. THR register is not empty if bit number 5 is equal to 0. So check for bit number 5 is set or not. Thus if bit number 5 is equal to 0, this means that the loop will continue till bit number 5 of LSR becomes equal to 1. Please see description of the step below

Description of the function to receive a char It keeps looping until there is a character available in RBR. How do we know if a character is available in RBR? This can be known by checking the bit number 0 in the Line Status Register (LSR). Bit number 0 of LSR is the Receiver Data Ready bit. This bit will be set to 1 if RBR contains valid data. This bit will be 0 if RBR is empty. So, do accordingly. Please see description of the LSR register below

Verifying the program:

1. First try with simulator. In debug go to view->serial windows-> UART1, UART2 etc. Serial window with option UART1 is for UART0 and so on. Now use the simulated UART terminal window and verify the result. 2. If everything is OK, then connect the LPC2148 into the COM port of the PC and load the image. Now create the hyper terminal as follows with option COM. We can select modem option depending on availability for connecting to any telephone n/w also.

How to create a HyperTerminal connection

Go to Start Programs Accessories Communications HyperTerminal Click No on the following prompt.

Provide a Name for the connection. Then click OK. Enter the data as shown below. Use the default value for Connect Using. Click OK. Click Cancel. Select File->Properties.

Enter Port Speed to be 9600. Change this value for a different baud rate. Parity, Stop bit as per your problem.

Select Connect To. Select Configure.

Select Settings. Select ASCII Setup. Check the box for Echo typed characters locally. Press OK. Press OK.

Now press PB1 in LPC2148 kit. It should send the string Hello World to the PC and will be displayed in the Hyper Terminal window. Output will look like as below:

Hello World Hello World Hello World _

También podría gustarte