Академический Документы
Профессиональный Документы
Культура Документы
UART0 is Null modem (Rx & Tx pin only ) where as UART1 is modem
capable.
Pins: TxD RxD Both the UART modules are identical, except the
UART1 block has an additional full modem interface.
UART0 P0.0 P0.1 This includes all the pins for RS232 compatibility like
UART1 P0.8 P0.9 flow control pins (CTS, RTS) etc.
Pins associated with UART0 & UART1
Pins associated with UART1
Pins associated with UART0
Registers used in UART
Data Related 1. U0RBR: Receiver Buffer Register (READ ONLY) -> Stores Received Data
Registers 2. U0THR:Transmit Holding Register (WRITE ONLY) -> Data in this will be
transmitted
Control and Status 1. U0FCR: FIFO Control Register -> Rx and Tx FIFO Controller
Registers 2. U0LCR: Line Control Register -> Selects bits for word
length, stop, parity and DLAB
3. U0LSR: Line Status Register -> Show Tx & Rx status
4. U0TER: Transmit Enable Register -> Enable Data Transmission
Interrupt Related 1. U0IER: Interrupt Enable Register -> RBR & THRE interrupt enabler
Registers 2. U0IIR: Interrupt Identification Register -> Interrupts can be identified
Baud Rate Set-up 1. U0DLL & U0DLM: Divisor Latch registers -> used to set baud rate
related Registers 2. U0FDR: Fractional Divisor Register -> baud rate fractional
divider
3. U0ACR: Auto-baud Control Register -> Auto baud rate controller
Other Registers 1. U0SCR: Scratch Pad Reg.
Registers used for UART Programming in LPC214x
Data Related Registers :
1. U0RBR – Receiver Buffer Register (READ ONLY!):
• This register contains the top most byte(8-bit data chunk) in the Rx
FIFO i.e the oldest received data in FIFO.
• To properly read data from U0RBR , the DLAB(Divisor Latch Access) bit
in U0LCR register must be first set to 0.
• Also , as per the user manual “The right approach for fetching the valid
pair of received byte and its status bits is first to read the content of
the U0LSR register, and then to read a byte from the U0RBR.”
• (Note : If you are using 7,6 or 5 Bits for data then other remaining bits
are automatically padded with 0s)
7 6 5 4 3 2 1 Bit 0
RX Trigger Level R R R TX FIFO RX FIFO FIFO
Reset Reset Enable
Reserved 7 6 5 4 3 2 1 0
This register actually holds divisor and a multiplier values for prescaling which are:
Remark from User manual : “If the fractional divider is active (DIVADDVAL > 0) and
DLM = 0, the value of the DLL register must be 2 or greater!”
UART Baud Rate Generation:
Note :
In real world there are very less chances that you will
get the actual baudrate same as the desired baudrate.
In most cases the actual baudrate will drift a little above
or below the desired baud and also, as the desired
baudrate increases this drift or error will also increase –
this is because of the equation itself and the
limitations on MULVAL , DIVADDVAL! For e.g. if the
desired baud rate is 9600 and you get a baud like 9590 ,
9610 , 9685 , 9615 , etc.. then in almost all cases it will
work as required.
In short , a small amount of error in actual baudrate is
generally tolerable in most systems.
Calculating Baud Rate in LPC 2148: UART0
The formula for calculating
baud rate is given as :
As it can been seen this formula has 2 prominent parts which are : A Base value and
a Fractional Multiplier i.e:
BaudRate = [ Base ] x [ Fraction(i.e. Prescale) ]
This Fractional Multiplier can be used to scale down or keep the base value as it is ..
hence its very useful for fine-tuning and getting the baudrate as accurate as possible.
Examples for calculating Baud Rates
1. We start with DLM=0 , DIVADDVAL=0 and MULVAL=1 and
get an initial value for DLL.
2. We will get a very close baudrate to the desired one. (if we
are lucky)
3. If not then we perform some finetuning using DLM ,
MULVAL and DIVADDVAL and get a new value for DLM
U0DLL = = 195.3125
We get U0DLL = 195.3125 , since it must be an integer we use 195.
With U0DLL = 195 our actual Now all we have to do is to get this
baud rate will be = 9615.38 with error as low as possible.
an error of +15.38 from 9600.
Luckily when U0DLL = 183 we get a Now , one thing can be done is
baud rate of 9605.53 i.e ~9605! that we set MULVAL = 15 and now
Yea! start decreasing U0DLL from 195
For PCLK = 30Mhz and Required
Baudrate is 9600 bauds for UART0
U0DLL = 183
U0DLM = 0
MULVAL = 15
DIVADDVAL = 1
Examples for calculating Baud Rates
Ex 2 : PCLK = 60Mhz and Required Baudrate is 9600 bauds for UART0
Initially we made U0DLM ‘0’, Now set U0DLM = 1 to get U0DLL in range
With U0DLL = 135 our actual Now all we have to do is to get this
baud rate will be = 9590.79 with error as low as possible.
an error of -9.21 from 9600.
First lets compute the required Let us say this can be done by
fraction given as: multiplying it by a suitable fraction
[Desired Baud / Actual Baud defined using MULVAL and
Rate]. = 9600/ 9590.79 = 1.0009 DIVADDVAL – as given in equation.
Now What?
The fractional value we need to
multiply our current baud to get Hence, in such case we have to use
back to ~9600 is 1.0009 but that’s MULVAL = 15 and find new value
out of range(>1) for a factor for U0DLL..
This the value
we use
Here we get U0DLL = 110 which Now , one thing can be done is
gives a baud of 9605.53 which is that we set MULVAL = 15 and now
~9605! start decreasing U0DLL from 110
For PCLK = 60Mhz and Required
Baudrate is 9600 bauds for UART0
U0DLL = 110
U0DLM = 1
MULVAL = 15
DIVADDVAL = 1
Program: type on keyboard & display on serial terminal
In this example you will see the characters that you type on your
keyboard - seems like typing in an editor? NO!.
The serial Terminal only displays the character that it receives via
serial Port.
When you type a character it is directly sent to the other side via
serial port.
So , in order for us to see the character which we typed, we need to
send back the same character i.e. echo it back to PC.
This is what the program does.
As soon as the MCU receives a character from the Rx pin .. it then
sends the same character back to PC via the Tx Pin and it gets
displayed in the Serial Terminal.
UART0: Read & Write Function
U0Read() - Read Data from UART0:
void initUART0(void);
void U0Write(char data);
char U0Read(void);
void initClocks(void);
void setupPLL0(void);
void feedSeq(void);
void connectPLL0(void);
Program:type on keyboard & display on serial terminal
void initUART0(void)
{
PINSEL0 = 0x5; /* Select TxD for P0.0 and RxD for P0.1 */
U0LCR = 3 | (1<<7) ; /* 8 bits, no Parity, 1 Stop bit | DLAB set to 1 */
U0DLL = 110;
U0DLM = 1;
U0FDR = (MULVAL<<4) | DIVADDVAL; /* MULVAL=15(bits - 7:4) , DIVADDVAL=0(bits - 3:0) */
U0LCR &= 0x0F; //Set DLAB=0 to lock MULVAL and DIVADDVAL
//BaudRate is now ~9600 and we are ready for UART communication!
}
void U0Write(char data)
{
// wait till the THR is empty
while ( !(U0LSR & THRE ) );
// now we can write to the Tx FIFO
U0THR = data;
}
char U0Read(void)
{
while( !(U0LSR & RDR ) ); // wait till any data arrives into Rx FIFO
return U0RBR;
}
Configuring PLL and Clock Initalization
while(1)
{
char c = U0Read(); // Read Data from Rx
if( c == ENTER ) // Check if user pressed Enter key
{
U0Write(NEW_LINE); // Send New Line ASCII code change line
}
else
{
U0Write(c); // Write it to Tx to send it back
}
}
return 0;
}
Program 2: Configuring and Initializing UART
We will use the following configuration for UART0:
•BaudRate = 9600 buads (with PCLK=15Mhz)
• Data Length = 8 bits
• No Parity Bit
• and 1 Stop Bit
In order to get bauds at 15Mhz PCLK we must use the
following settings for baud generation :
RxD TxD
PINSEL0 =x05
/* 8 bits, no Parity, 1 Stop bit, DLAB set to 1 */
//We now use Line Control Register (U0LCR) to configure the
UART block (i.e the data format used in transmission)
long int n;
PINSEL0 = 0x00000005; //set line ctrl reg. for UART0 with above parameters
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock*/