keil_examples/UART/uart.c
/*****************************************************************************
** Function name:		UARTInit
**
** Descriptions:		Initialize UART port, setup pin select,
**						clock, parity, stop bits, FIFO, etc.
**
** parameters:			portNum(0 or 1) and UART baudrate
** Returned value:		true or false, return false only if the 
**						interrupt handler can't be installed to the 
**						VIC table
** 
*****************************************************************************/
Function name:         UARTInit
Descriptions:          Initialize UART port, setup pin select,
clock, parity, stop bits, FIFO, etc.

portNum(0 or 1) and UART baudrate
Returned value:                true or false, return false only if the
interrupt handler can't be installed to the
VIC table
Function name : UARTInit Descriptions : Initialize UART port setup pin select clock parity stop bits FIFO etc . portNum ( 0 or 1 ) and UART baudrate Returned value : true or false return false only if the interrupt handler can ' t be installed to the VIC table
uint32_t UARTInit( uint32_t PortNum, uint32_t baudrate )
{
  uint32_t Fdiv;
  uint32_t pclkdiv, pclk;
  if ( PortNum == 0 )
  {
	LPC_PINCON->PINSEL0 &= ~0x000000F0;
	LPC_PINCON->PINSEL0 |= 0x00000050;  
	pclkdiv = (LPC_SC->PCLKSEL0 >> 6) & 0x03;
	switch ( pclkdiv )
	{
	  case 0x00:
	  default:
		pclk = SystemFrequency/4;
		break;
	  case 0x01:
		pclk = SystemFrequency;
		break; 
	  case 0x02:
		pclk = SystemFrequency/2;
		break; 
	  case 0x03:
		pclk = SystemFrequency/8;
		break;
	}
    LPC_UART0->LCR = 0x83;		
	Fdiv = ( pclk / 16 ) / baudrate ;	
    LPC_UART0->DLM = Fdiv / 256;							
    LPC_UART0->DLL = Fdiv % 256;
	LPC_UART0->LCR = 0x03;		
    LPC_UART0->FCR = 0x07;		
   	NVIC_EnableIRQ(UART0_IRQn);
    LPC_UART0->IER = IER_RBR | IER_THRE | IER_RLS;	
    return (TRUE);
  }
  else if ( PortNum == 1 )
  {
	LPC_PINCON->PINSEL4 &= ~0x0000000F;
	LPC_PINCON->PINSEL4 |= 0x0000000A;	
	pclkdiv = (LPC_SC->PCLKSEL0 >> 8) & 0x03;
	switch ( pclkdiv )
	{
	  case 0x00:
	  default:
		pclk = SystemFrequency/4;
		break;
	  case 0x01:
		pclk = SystemFrequency;
		break; 
	  case 0x02:
		pclk = SystemFrequency/2;
		break; 
	  case 0x03:
		pclk = SystemFrequency/8;
		break;
	}
    LPC_UART1->LCR = 0x83;		
	Fdiv = ( pclk / 16 ) / baudrate ;	
    LPC_UART1->DLM = Fdiv / 256;							
    LPC_UART1->DLL = Fdiv % 256;
	LPC_UART1->LCR = 0x03;		
    LPC_UART1->FCR = 0x07;		
   	NVIC_EnableIRQ(UART1_IRQn);
    LPC_UART1->IER = IER_RBR | IER_THRE | IER_RLS;	
    return (TRUE);
  }
  return( FALSE ); 
}
uint32_t UARTInit ( uint32_t PortNum , uint32_t baudrate ) { uint32_t Fdiv ; uint32_t pclkdiv , pclk ; if ( PortNum == 0 ) { LPC_PINCON -> PINSEL0 &= ~ 0x000000F0 ; LPC_PINCON -> PINSEL0 |= 0x00000050 ; pclkdiv = ( LPC_SC -> PCLKSEL0 >> 6 ) & 0x03 ; switch ( pclkdiv ) { case 0x00 : default : pclk = SystemFrequency / 4 ; break ; case 0x01 : pclk = SystemFrequency ; break ; case 0x02 : pclk = SystemFrequency / 2 ; break ; case 0x03 : pclk = SystemFrequency / 8 ; break ; } LPC_UART0 -> LCR = 0x83 ; Fdiv = ( pclk / 16 ) / baudrate ; LPC_UART0 -> DLM = Fdiv / 256 ; LPC_UART0 -> DLL = Fdiv % 256 ; LPC_UART0 -> LCR = 0x03 ; LPC_UART0 -> FCR = 0x07 ; NVIC_EnableIRQ ( UART0_IRQn ) ; LPC_UART0 -> IER = IER_RBR | IER_THRE | IER_RLS ; return ( TRUE ) ; } else if ( PortNum == 1 ) { LPC_PINCON -> PINSEL4 &= ~ 0x0000000F ; LPC_PINCON -> PINSEL4 |= 0x0000000A ; pclkdiv = ( LPC_SC -> PCLKSEL0 >> 8 ) & 0x03 ; switch ( pclkdiv ) { case 0x00 : default : pclk = SystemFrequency / 4 ; break ; case 0x01 : pclk = SystemFrequency ; break ; case 0x02 : pclk = SystemFrequency / 2 ; break ; case 0x03 : pclk = SystemFrequency / 8 ; break ; } LPC_UART1 -> LCR = 0x83 ; Fdiv = ( pclk / 16 ) / baudrate ; LPC_UART1 -> DLM = Fdiv / 256 ; LPC_UART1 -> DLL = Fdiv % 256 ; LPC_UART1 -> LCR = 0x03 ; LPC_UART1 -> FCR = 0x07 ; NVIC_EnableIRQ ( UART1_IRQn ) ; LPC_UART1 -> IER = IER_RBR | IER_THRE | IER_RLS ; return ( TRUE ) ; } return ( FALSE ) ; }
Function name:         UARTInit
Descriptions:          Initialize UART port, setup pin select,
clock, parity, stop bits, FIFO, etc.
Function name : UARTInit Descriptions : Initialize UART port setup pin select clock parity stop bits FIFO etc .
/* RxD0 is P0.3 and TxD0 is P0.2 */ /* By default, the PCLKSELx value is zero, thus, the PCLK for
	all the peripherals is 1/4 of the SystemFrequency. */ /* Bit 6~7 is for UART0 */ /* 8 bits, no Parity, 1 Stop bit */ /*baud rate */ /* DLAB = 0 */ /* Enable and reset TX and RX FIFO. */ /* Enable UART0 interrupt */ /* Enable RxD1 P2.1, TxD1 P2.0 */ /* By default, the PCLKSELx value is zero, thus, the PCLK for
	all the peripherals is 1/4 of the SystemFrequency. */ /* Bit 8,9 are for UART1 */ /* 8 bits, no Parity, 1 Stop bit */ /*baud rate */ /* DLAB = 0 */ /* Enable and reset TX and RX FIFO. */ /* Enable UART1 interrupt */