Question

I've some problems with my way to change the uart's baudrate on my lpc1768.

To init and configure my uarts, I use the following code and that works fine with 9600 baud or 38400.

/* RxD0 is P0.3 and TxD0 is P0.2 */
LPC_PINCON->PINSEL0 &= ~(0x03<<4);          // Reset P0.2 = GPIO
LPC_PINCON->PINSEL0 |=  (0x01<<4);          // Config P0.2 = TxD0
LPC_PINCON->PINSEL0 &= ~(0x03<<6);      // Reset P0.3 = GPIO
LPC_PINCON->PINSEL0 |=  (0x01<<6);          // Config P0.3 = RxD0

LPC_UART0->LCR = 0x87; //8bits, no parity, 2 stop bits
switch (baudrate)
{
default :
case 9600 :
    LPC_UART0->DLM = 0x00;  //calculated with datasheet
    LPC_UART0->DLL = 0x4E;

    LPC_UART0->FDR = 0x21;
    break;

case 38400 :
    LPC_UART0->DLM = 0x00;  //calculated with datasheet
    LPC_UART0->DLL = 0x14;

    LPC_UART0->FDR = 0xF7;
}




LPC_UART0->LCR = 0x07;//0x03;       /* DLAB = 0 */
LPC_UART0->FCR = 0x07;      /* Enable and reset TX and RX FIFO. */

NVIC_EnableIRQ(UART0_IRQn);

LPC_UART0->IER = IER_RBR | IER_THRE | IER_RLS;  /* Enable UART0 interrupt */

But to change baudrate from 9600 to 38400, I try to just change the DLM/DLL and FDR registers to the appropriate value (same as in the code above). But that does not work... (baudrate is undefined).

My pclk is 18MHz

Changing only those three registers is not sufficient ? Am I wrong ?

Was it helpful?

Solution

I found the solution: I forgot to set the DLAB bit in the LCR register. Without this change it is not possible to change the baudrate.

And a simple way to change baudrate ca be that :

   LPC_UART0->LCR = 0x87;
   switch (baudrate)
   {
     default :
     case 9600 :
      LPC_UART0->DLM = 0x00;    //fhn calculated with algorithm in the datasheet
      LPC_UART0->DLL = 0x4E;//0x06;

      LPC_UART0->FDR = 0x21;//0x85;calculation, but not sure to need
  break;

      case 38400 :
       LPC_UART0->DLM = 0x00;   
       LPC_UART0->DLL = 0x14;//0x06;

       LPC_UART0->FDR = 0xF7;
  break;
}
LPC_UART0->LCR = 0x07;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top