Serial Interface Programming LPC1768
Serial Interface Programming LPC1768
h>
#include "stdutils.h"
#define SBIT_WordLenght 0x00u #define SBIT_DLAB 0x07u
#define SBIT_FIFO 0x00u
#define SBIT_RxFIFO 0x01u
#define SBIT_TxFIFO 0x02u
#define SBIT_RDR 0x00u
#define SBIT_THRE 0x05u /* Function to initialize the UART0 at specifief baud
rate */
break;
case 0x03: var_Pclk_u32 = SystemCoreClock/8;
break;
}
var_RegValue_u32 = ( var_Pclk_u32 / (16 * baudrate ));
LPC_UART0->DLL = var_RegValue_u32 & 0xFF; LPC_UART0->DLM =
(var_RegValue_u32 >> 0x08) & 0xFF; util_BitClear(LPC_UART0->LCR,
(SBIT_DLAB)); // Clear DLAB after setting DLL,DLM
} /* Function to transmit a char */ void uart_TxChar(char ch)
{
while(util_IsBitCleared(LPC_UART0->LSR,SBIT_THRE)); // Wait for Previous
transmission
LPC_UART0->THR=ch; // Load the data to be transmitted
}
/* Function to Receive a char */
char uart_RxChar()
{
charch; while(util_IsBitCleared(LPC_UART0->LSR,SBIT_RDR)); // Wait till the data
is received
ch = LPC_UART0->RBR; // Read received data
return ch;
}
int main()
{
char ch,a[]="\n\rExploreEmbedded"; int i;
SystemInit();
uart_init(9600); // Initialize the UART0 for 9600 baud rate uart_TxChar('h');
//Transmit "hello" char by char
uart_TxChar('e');
uart_TxChar('l');
uart_TxChar('l');
uart_TxChar('o');
for(i=0;a[i];i++) //transmit a predefined string
uart_TxChar(a[i]);
while(1)
{
//Finally receive a char and transmit it infinitely
ch = uart_RxChar();
uart_TxChar(ch);
}
} - See more at:
http://exploreembedded.com/wiki/LPC1768:_UART_Programming#Code