This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

problems with initialization UART2,3 - LPC1766

Hello! I study LPC1766, having trouble initializing 2 and 3 UART, I initialize all for example, when adding a row UARTInit (2, 57600); UARTInit (3, 57600); in main.s does not work even 0 and 1 UART. Help!

else if ( PortNum == 2 ) { LPC_PINCON->PINSEL0 &= ~0x00F00000; LPC_PINCON->PINSEL0 |= 0x00500000; /* RxD2 is P0.11 and TxD2 is P0.10 */

/* By default, the PCLKSELx value is zero, thus, the PCLK for all the peripherals is 1/4 of the SystemFrequency. */ /* Bit 16,17 are for UART2 */ pclkdiv = (LPC_SC->PCLKSEL1 >> 16) & 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_UART2->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */ Fdiv = ( pclk / 16 ) / baudrate ; /*baud rate */ LPC_UART2->DLM = Fdiv / 256; LPC_UART2->DLL = Fdiv % 256; LPC_UART2->LCR = 0x03; /* DLAB = 0 */ LPC_UART2->FCR = 0x07; /* Enable and reset TX and RX FIFO. */

NVIC_EnableIRQ(UART2_IRQn);

LPC_UART2->IER = IER_RBR | IER_THRE | IER_RLS; /* Enable UART1 interrupt */ return (TRUE); }

else if ( PortNum == 3 ) { LPC_PINCON->PINSEL9 &= ~0x0F000000; LPC_PINCON->PINSEL9 |= 0x0F000000; /* RxD3 is P4.29 and TxD3 is P4.28 */

/* By default, the PCLKSELx value is zero, thus, the PCLK for all the peripherals is 1/4 of the SystemFrequency. */ /* Bit 18,19 are for UART2 */ pclkdiv = (LPC_SC->PCLKSEL1 >> 18) & 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_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 UART1 interrupt */ return (TRUE); }

  • Did you dislike the suggested markup for posting source code?

    Did that decision result in easy-to-read code?

  • Excuse me!!!

    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;  /* 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 */
            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;           /* 8 bits, no Parity, 1 Stop bit */
            Fdiv = ( pclk / 16 ) / baudrate ;       /*baud rate */
        LPC_UART0->DLM = Fdiv / 256;
        LPC_UART0->DLL = Fdiv % 256;
            LPC_UART0->LCR = 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 */
        return (TRUE);
      }
      else if ( PortNum == 1 )
      {
            LPC_PINCON->PINSEL4 &= ~0x0000000F;
            LPC_PINCON->PINSEL4 |= 0x0000000A;   /* 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 */
            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;           /* 8 bits, no Parity, 1 Stop bit */
            Fdiv = ( pclk / 16 ) / baudrate ;       /*baud rate */
        LPC_UART1->DLM = Fdiv / 256;
        LPC_UART1->DLL = Fdiv % 256;
            LPC_UART1->LCR = 0x03;               /* DLAB = 0 */
        LPC_UART1->FCR = 0x07;           /* Enable and reset TX and RX FIFO. */
    
            NVIC_EnableIRQ(UART1_IRQn);
    
        LPC_UART1->IER = IER_RBR | IER_THRE | IER_RLS;   /* Enable UART1 interrupt */
        return (TRUE);
      }
    
    
     else if ( PortNum == 2 )
      {
            LPC_PINCON->PINSEL0 &= ~0x00F00000;
            LPC_PINCON->PINSEL0 |= 0x00500000; /* RxD2 is P0.11 and TxD2 is P0.10 */
    
            /* By default, the PCLKSELx value is zero, thus, the PCLK for
            all the peripherals is 1/4 of the SystemFrequency. */
            /* Bit 16,17 are for UART2 */
            pclkdiv = (LPC_SC->PCLKSEL1 >> 16) & 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_UART2->LCR = 0x83;           /* 8 bits, no Parity, 1 Stop bit */
            Fdiv = ( pclk / 16 ) / baudrate ;       /*baud rate */
        LPC_UART2->DLM = Fdiv / 256;
        LPC_UART2->DLL = Fdiv % 256;
            LPC_UART2->LCR = 0x03;               /* DLAB = 0 */
        LPC_UART2->FCR = 0x07;           /* Enable and reset TX and RX FIFO. */
    
            NVIC_EnableIRQ(UART2_IRQn);
    
        LPC_UART2->IER = IER_RBR | IER_THRE | IER_RLS;   /* Enable UART1 interrupt */
        return (TRUE);
      }
    
    
        else if ( PortNum == 3 )
      {
            LPC_PINCON->PINSEL9 &= ~0x0F000000;
            LPC_PINCON->PINSEL9 |= 0x0F000000; /* RxD3 is P4.29 and TxD3 is P4.28 */
    
            /* By default, the PCLKSELx value is zero, thus, the PCLK for
            all the peripherals is 1/4 of the SystemFrequency. */
            /* Bit 18,19 are for UART2 */
            pclkdiv = (LPC_SC->PCLKSEL1 >> 18) & 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_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 UART1 interrupt */
        return (TRUE);
      }
    

  • Are you making sure that UART2 and UART3 are powered up before you try to initialize them?