lpcware

LPCXPRESSO LPC1769 UART baudrate

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by Zahoq on Sun Nov 02 04:17:06 MST 2014
Hello everyone!
I'm atempting to create own library with UART functions but i have problems with calculating baudrate. I found very useful example made by Superfred:
else if ( PortNum == 3 )
  {
  LPC_PINCON->PINSEL0 &= ~0x0000000F;
  LPC_PINCON->PINSEL0 |=  0x0000000A;  /* RxD3 is P0.1 and TxD3 is P0.0 */
  LPC_SC->PCONP |= 1<<4 | 1<<25; //Enable PCUART1
  /* 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 UART3 */
  pclkdiv = (LPC_SC->PCLKSEL1 >> 18) & 0x03;
  switch ( pclkdiv )
  {
  case 0x00:
  default:
  pclk = SystemCoreClock/4;
  break;
  case 0x01:
  pclk = SystemCoreClock;
  break;
  case 0x02:
  pclk = SystemCoreClock/2;
  break;
  case 0x03:
  pclk = SystemCoreClock/8;
  break;
  }
  LPC_UART3->LCR = 0x83;/* 8 bits, no Parity, 1 Stop bit */
  Fdiv = ( pclk / 16 ) / baudrate ;/*baud rate */
  LPC_UART3->DLM = Fdiv / 256;
  LPC_UART3->DLL = Fdiv % 256;
  LPC_UART3->LCR = 0x03;/* DLAB = 0 */
  LPC_UART3->FCR = 0x07;/* Enable and reset TX and RX FIFO. */

  NVIC_EnableIRQ(UART3_IRQn);

  LPC_UART3->IER = IER_RBR | IER_THRE | IER_RLS;/* Enable UART3 interrupt */
  }


Example above is for initiating UART3, i don't understand how DLL and DLM is calculated. For example PCLK is 12MHz and desired baudrate is 9600. so Fdiv=78,125, DLM then should be < 1 and DLL = 0?

If i get Fdiv 78.125 can i round it to 78 and asign to DLL so my final calculation will look like BR = PCLK/(16 * 78) with DLL=78 DLM = 0?

Outcomes