Skip to content

Instantly share code, notes, and snippets.

@Embedded-linux
Created September 4, 2013 14:17
Show Gist options
  • Select an option

  • Save Embedded-linux/6437560 to your computer and use it in GitHub Desktop.

Select an option

Save Embedded-linux/6437560 to your computer and use it in GitHub Desktop.
Serial Interfacing for LPC2129
Reference user manual :
http://www.keil.com/dd/docs/datashts/philips/user_manual_lpc2119_2129_2194_2292_2294.pdf
/*Serial interfacing for LPC2129 [NXP] UART1*/
#include <LPC210x.H> /* LPC21xx definitions */
#define CR 0x0D
void init_serial (void) { /* Initialize Serial Interface */
PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */
U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U1DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock */
U1LCR = 0x03; /* DLAB = 0 */
}
/* implementation of putchar (also used by printf function to output data) */
int sendchar (int ch) { /* Write character to Serial Port */
if (ch == '\n') {
while (!(U1LSR & 0x20));
U1THR = CR; /* output CR */
}
while (!(U1LSR & 0x20));
return (U1THR = ch);
}
int getkey (void) { /* Read character from Serial Port */
while (!(U1LSR & 0x01));
return (U1RBR);
}
Here U1RBR :
The top byte of the RX FIFO contains the oldest character received and can be read via the bus interface.
U1THR:
The top byte is the newest character in the TX FIFO and can be written via the bus interface.
U1LCR:
The U1LCR determines the format of the data character that is to be transmitted or
received.
U1LSR:
The U1LSR is a read-only register that provides status information on the UART1 TX and
RX blocks.
U1DLL:
The UART1 Divisor Latch is part of the UART1 Fractional Baud Rate Generator and holds
the value used to divide the clock supplied by the fractional prescaler in order to produce
the baud rate clock, which must be 16x the desired baud rate (Equation 4)
UART1 baudrate can be calculated as:
UART1baudrate
PCLK
16 256 U1DLM     + U1DLL 1
DivAddVal
MulVal
+ -----------------------------     
= ------------------------------------------------------------
Where PCLK is the peripheral clock, U1DLM and U1DLL are the standard UART1 baud
rate divider registers, and DIVADDVAL and MULVAL are UART1 fractional baudrate
generator specific parameters.
------------------Writing Serial Interfacing for UART0--------------------------------------
#include <LPC210x.H>
#define CR 0x0D
void init_serial(void) {
PINSEL0 = 0x00000005; //For Tx0 and Rx0
U0LCR = 0x83;
U0DLL = 0x97;
U0LCR = 0x03;
}
int sendchar (int ch) {
if (ch == '\n') {
while (! (U0LSR & 0x20));
U0HTR = CD;
}
while(!(U0LSR & 0x20));
return (U1THR = ch);
}
int getkey(void) {
while(! (U0LSR & 0x01));
return (U0RBR);
}
Try to execute on Keil compiler.
--Thanks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment