搜索
bottom↓
回复: 4
打印 上一主题 下一主题

LPC1754串口2和3无法通信。

[复制链接]

出45入88汤圆

跳转到指定楼层
1
发表于 2019-9-17 22:32:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
demo里只有uart0,1,试过是可以正常收发。但修改成2,3都无法收发。什么原因?官方解疑在哪里?

原版:
/*****************************************************************************
*   uart.c:  UART API file for NXP LPC17xx Family Microprocessors
*
*   Copyright(C) 2009, NXP Semiconductor
*   All rights reserved.
*
*   History
*   2009.05.27  ver 1.00    Prelimnary version, first Release
*
******************************************************************************/
#include "lpc17xx.h"
#include "type.h"
#include "uart.h"

volatile uint32_t UART0Status, UART1Status;
volatile uint8_t UART0TxEmpty = 1, UART1TxEmpty = 1;
volatile uint8_t UART0Buffer[BUFSIZE], UART1Buffer[BUFSIZE];
volatile uint32_t UART0Count = 0, UART1Count = 0;

/*****************************************************************************
** Function name:                UART0_IRQHandler
**
** Descriptions:                UART0 interrupt handler
**
** parameters:                        None
** Returned value:                None
**
*****************************************************************************/
void UART0_IRQHandler (void)
{
  uint8_t IIRValue, LSRValue;
  uint8_t Dummy = Dummy;
       
  IIRValue = LPC_UART0->IIR;
   
  IIRValue >>= 1;                        /* skip pending bit in IIR */
  IIRValue &= 0x07;                        /* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS )                /* Receive Line Status */
  {
        LSRValue = LPC_UART0->LSR;
        /* Receive Line Status */
        if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
        {
          /* There are errors or break interrupt */
          /* Read LSR will clear the interrupt */
          UART0Status = LSRValue;
          Dummy = LPC_UART0->RBR;                /* Dummy read on RX to clear
                                                        interrupt, then bail out */
          return;
        }
        if ( LSRValue & LSR_RDR )        /* Receive Data Ready */                       
        {
          /* If no error on RLS, normal ready, save into the data buffer. */
          /* Note: read RBR will clear the interrupt */
          UART0Buffer[UART0Count] = LPC_UART0->RBR;
          UART0Count++;
          if ( UART0Count == BUFSIZE )
          {
                UART0Count = 0;                /* buffer overflow */
          }       
        }
  }
  else if ( IIRValue == IIR_RDA )        /* Receive Data Available */
  {
        /* Receive Data Available */
        UART0Buffer[UART0Count] = LPC_UART0->RBR;
        UART0Count++;
        if ( UART0Count == BUFSIZE )
        {
          UART0Count = 0;                /* buffer overflow */
        }
  }
  else if ( IIRValue == IIR_CTI )        /* Character timeout indicator */
  {
        /* Character Time-out indicator */
        UART0Status |= 0x100;                /* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )        /* THRE, transmit holding register empty */
  {
        /* THRE interrupt */
        LSRValue = LPC_UART0->LSR;                /* Check status in the LSR to see if
                                                                        valid data in U0THR or not */
        if ( LSRValue & LSR_THRE )
        {
          UART0TxEmpty = 1;
        }
        else
        {
          UART0TxEmpty = 0;
        }
  }
   
}

/*****************************************************************************
** Function name:                UART1_IRQHandler
**
** Descriptions:                UART1 interrupt handler
**
** parameters:                        None
** Returned value:                None
**
*****************************************************************************/
void UART1_IRQHandler (void)
{
  uint8_t IIRValue, LSRValue;
  uint8_t Dummy = Dummy;
       
  IIRValue = LPC_UART1->IIR;
   
  IIRValue >>= 1;                        /* skip pending bit in IIR */
  IIRValue &= 0x07;                        /* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS )                /* Receive Line Status */
  {
        LSRValue = LPC_UART1->LSR;
        /* Receive Line Status */
        if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
        {
          /* There are errors or break interrupt */
          /* Read LSR will clear the interrupt */
          UART1Status = LSRValue;
          Dummy = LPC_UART1->RBR;                /* Dummy read on RX to clear
                                                                interrupt, then bail out */
          return;
        }
        if ( LSRValue & LSR_RDR )        /* Receive Data Ready */                       
        {
          /* If no error on RLS, normal ready, save into the data buffer. */
          /* Note: read RBR will clear the interrupt */
          UART1Buffer[UART1Count] = LPC_UART1->RBR;
          UART1Count++;
          if ( UART1Count == BUFSIZE )
          {
                UART1Count = 0;                /* buffer overflow */
          }       
        }
  }
  else if ( IIRValue == IIR_RDA )        /* Receive Data Available */
  {
        /* Receive Data Available */
        UART1Buffer[UART1Count] = LPC_UART1->RBR;
        UART1Count++;
        if ( UART1Count == BUFSIZE )
        {
          UART1Count = 0;                /* buffer overflow */
        }
  }
  else if ( IIRValue == IIR_CTI )        /* Character timeout indicator */
  {
        /* Character Time-out indicator */
        UART1Status |= 0x100;                /* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )        /* THRE, transmit holding register empty */
  {
        /* THRE interrupt */
        LSRValue = LPC_UART1->LSR;                /* Check status in the LSR to see if
                                                                valid data in U0THR or not */
        if ( LSRValue & LSR_THRE )
        {
          UART1TxEmpty = 1;
        }
        else
        {
          UART1TxEmpty = 0;
        }
  }

}

/*****************************************************************************
** 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
**
*****************************************************************************/
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);
  }
  return( FALSE );
}

/*****************************************************************************
** Function name:                UARTSend
**
** Descriptions:                Send a block of data to the UART 0 port based
**                                                on the data length
**
** parameters:                        portNum, buffer pointer, and data length
** Returned value:                None
**
*****************************************************************************/
void UARTSend( uint32_t portNum, uint8_t *BufferPtr, uint32_t Length )
{
  if ( portNum == 0 )
  {
    while ( Length != 0 )
    {
          /* THRE status, contain valid data */
          while ( !(UART0TxEmpty & 0x01) );       
          LPC_UART0->THR = *BufferPtr;
          UART0TxEmpty = 0;        /* not empty in the THR until it shifts out */
          BufferPtr++;
          Length--;
        }
  }
  else
  {
        while ( Length != 0 )
    {
          /* THRE status, contain valid data */
          while ( !(UART1TxEmpty & 0x01) );       
          LPC_UART1->THR = *BufferPtr;
          UART1TxEmpty = 0;        /* not empty in the THR until it shifts out */
          BufferPtr++;
          Length--;
    }
  }
  return;
}

/******************************************************************************
**                            End Of File
******************************************************************************/
下面是我修改的:
/*****************************************************************************
*   uart.c:  UART API file for NXP LPC17xx Family Microprocessors
*
*   Copyright(C) 2009, NXP Semiconductor
*   All rights reserved.
*
*   History
*   2009.05.27  ver 1.00    Prelimnary version, first Release
*
******************************************************************************/
#include "lpc17xx.h"
#include "type.h"
#include "uart.h"

volatile uint32_t UART0Status, UART1Status,UART2Status, UART3Status;
volatile uint8_t UART0TxEmpty = 1, UART1TxEmpty = 1,UART2TxEmpty = 1, UART3TxEmpty = 1;
volatile uint8_t UART0Buffer[BUFSIZE], UART1Buffer[BUFSIZE],UART2Buffer[BUFSIZE], UART3Buffer[BUFSIZE];;
volatile uint32_t UART0Count = 0, UART1Count = 0,UART2Count = 0, UART3Count = 0;;

/*****************************************************************************
** Function name:                UART0_IRQHandler
**
** Descriptions:                UART0 interrupt handler
**
** parameters:                        None
** Returned value:                None
**
*****************************************************************************/
void UART0_IRQHandler (void)
{
  uint8_t IIRValue, LSRValue;
  uint8_t Dummy = Dummy;
       
  IIRValue = LPC_UART0->IIR;
   
  IIRValue >>= 1;                        /* skip pending bit in IIR */
  IIRValue &= 0x07;                        /* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS )                /* Receive Line Status */
  {
        LSRValue = LPC_UART0->LSR;
        /* Receive Line Status */
        if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
        {
          /* There are errors or break interrupt */
          /* Read LSR will clear the interrupt */
          UART0Status = LSRValue;
          Dummy = LPC_UART0->RBR;                /* Dummy read on RX to clear
                                                        interrupt, then bail out */
          return;
        }
        if ( LSRValue & LSR_RDR )        /* Receive Data Ready */                       
        {
          /* If no error on RLS, normal ready, save into the data buffer. */
          /* Note: read RBR will clear the interrupt */
          UART0Buffer[UART0Count] = LPC_UART0->RBR;
          UART0Count++;
          if ( UART0Count == BUFSIZE )
          {
                UART0Count = 0;                /* buffer overflow */
          }       
        }
  }
  else if ( IIRValue == IIR_RDA )        /* Receive Data Available */
  {
        /* Receive Data Available */
        UART0Buffer[UART0Count] = LPC_UART0->RBR;
        UART0Count++;
        if ( UART0Count == BUFSIZE )
        {
          UART0Count = 0;                /* buffer overflow */
        }
  }
  else if ( IIRValue == IIR_CTI )        /* Character timeout indicator */
  {
        /* Character Time-out indicator */
        UART0Status |= 0x100;                /* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )        /* THRE, transmit holding register empty */
  {
        /* THRE interrupt */
        LSRValue = LPC_UART0->LSR;                /* Check status in the LSR to see if
                                                                        valid data in U0THR or not */
        if ( LSRValue & LSR_THRE )
        {
          UART0TxEmpty = 1;
        }
        else
        {
          UART0TxEmpty = 0;
        }
  }
   
}

/*****************************************************************************
** Function name:                UART1_IRQHandler
**
** Descriptions:                UART1 interrupt handler
**
** parameters:                        None
** Returned value:                None
**
*****************************************************************************/
void UART1_IRQHandler (void)
{
  uint8_t IIRValue, LSRValue;
  uint8_t Dummy = Dummy;
       
  IIRValue = LPC_UART1->IIR;
   
  IIRValue >>= 1;                        /* skip pending bit in IIR */
  IIRValue &= 0x07;                        /* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS )                /* Receive Line Status */
  {
        LSRValue = LPC_UART1->LSR;
        /* Receive Line Status */
        if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
        {
          /* There are errors or break interrupt */
          /* Read LSR will clear the interrupt */
          UART1Status = LSRValue;
          Dummy = LPC_UART1->RBR;                /* Dummy read on RX to clear
                                                                interrupt, then bail out */
          return;
        }
        if ( LSRValue & LSR_RDR )        /* Receive Data Ready */                       
        {
          /* If no error on RLS, normal ready, save into the data buffer. */
          /* Note: read RBR will clear the interrupt */
          UART1Buffer[UART1Count] = LPC_UART1->RBR;
          UART1Count++;
          if ( UART1Count == BUFSIZE )
          {
                UART1Count = 0;                /* buffer overflow */
          }       
        }
  }
  else if ( IIRValue == IIR_RDA )        /* Receive Data Available */
  {
        /* Receive Data Available */
        UART1Buffer[UART1Count] = LPC_UART1->RBR;
        UART1Count++;
        if ( UART1Count == BUFSIZE )
        {
          UART1Count = 0;                /* buffer overflow */
        }
  }
  else if ( IIRValue == IIR_CTI )        /* Character timeout indicator */
  {
        /* Character Time-out indicator */
        UART1Status |= 0x100;                /* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )        /* THRE, transmit holding register empty */
  {
        /* THRE interrupt */
        LSRValue = LPC_UART1->LSR;                /* Check status in the LSR to see if
                                                                valid data in U0THR or not */
        if ( LSRValue & LSR_THRE )
        {
          UART1TxEmpty = 1;
        }
        else
        {
          UART1TxEmpty = 0;
        }
  }

}

void UART2_IRQHandler (void)
{
  uint8_t IIRValue, LSRValue;
  uint8_t Dummy = Dummy;
       
  IIRValue = LPC_UART2->IIR;
   
  IIRValue >>= 1;                        /* skip pending bit in IIR */
  IIRValue &= 0x07;                        /* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS )                /* Receive Line Status */
  {
        LSRValue = LPC_UART2->LSR;
        /* Receive Line Status */
        if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
        {
          /* There are errors or break interrupt */
          /* Read LSR will clear the interrupt */
          UART2Status = LSRValue;
          Dummy = LPC_UART2->RBR;                /* Dummy read on RX to clear
                                                                interrupt, then bail out */
          return;
        }
        if ( LSRValue & LSR_RDR )        /* Receive Data Ready */                       
        {
          /* If no error on RLS, normal ready, save into the data buffer. */
          /* Note: read RBR will clear the interrupt */
          UART2Buffer[UART2Count] = LPC_UART2->RBR;
          UART2Count++;
          if ( UART2Count == BUFSIZE )
          {
                UART2Count = 0;                /* buffer overflow */
          }       
        }
  }
  else if ( IIRValue == IIR_RDA )        /* Receive Data Available */
  {
        /* Receive Data Available */
        UART2Buffer[UART2Count] = LPC_UART2->RBR;
        UART2Count++;
        if ( UART2Count == BUFSIZE )
        {
          UART2Count = 0;                /* buffer overflow */
        }
  }
  else if ( IIRValue == IIR_CTI )        /* Character timeout indicator */
  {
        /* Character Time-out indicator */
        UART2Status |= 0x100;                /* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )        /* THRE, transmit holding register empty */
  {
        /* THRE interrupt */
        LSRValue = LPC_UART2->LSR;                /* Check status in the LSR to see if
                                                                valid data in U0THR or not */
        if ( LSRValue & LSR_THRE )
        {
          UART2TxEmpty = 1;
        }
        else
        {
          UART2TxEmpty = 0;
        }
  }

}
void UART3_IRQHandler (void)
{
  uint8_t IIRValue, LSRValue;
  uint8_t Dummy = Dummy;
       
  IIRValue = LPC_UART3->IIR;
   
  IIRValue >>= 1;                        /* skip pending bit in IIR */
  IIRValue &= 0x07;                        /* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS )                /* Receive Line Status */
  {
        LSRValue = LPC_UART3->LSR;
        /* Receive Line Status */
        if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
        {
          /* There are errors or break interrupt */
          /* Read LSR will clear the interrupt */
          UART3Status = LSRValue;
          Dummy = LPC_UART3->RBR;                /* Dummy read on RX to clear
                                                                interrupt, then bail out */
          return;
        }
        if ( LSRValue & LSR_RDR )        /* Receive Data Ready */                       
        {
          /* If no error on RLS, normal ready, save into the data buffer. */
          /* Note: read RBR will clear the interrupt */
          UART3Buffer[UART3Count] = LPC_UART3->RBR;
          UART3Count++;
          if ( UART3Count == BUFSIZE )
          {
                UART3Count = 0;                /* buffer overflow */
          }       
        }
  }
  else if ( IIRValue == IIR_RDA )        /* Receive Data Available */
  {
        /* Receive Data Available */
        UART3Buffer[UART3Count] = LPC_UART3->RBR;
        UART3Count++;
        if ( UART3Count == BUFSIZE )
        {
          UART3Count = 0;                /* buffer overflow */
        }
  }
  else if ( IIRValue == IIR_CTI )        /* Character timeout indicator */
  {
        /* Character Time-out indicator */
        UART3Status |= 0x100;                /* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )        /* THRE, transmit holding register empty */
  {
        /* THRE interrupt */
        LSRValue = LPC_UART3->LSR;                /* Check status in the LSR to see if
                                                                valid data in U0THR or not */
        if ( LSRValue & LSR_THRE )
        {
          UART3TxEmpty = 1;
        }
        else
        {
          UART3TxEmpty = 0;
        }
  }

}


/*****************************************************************************
** 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
**
*****************************************************************************/
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->PINSEL4 &= ~0x000f0000;
        LPC_PINCON->PINSEL4 |=  0x000a0000;        /* Enable RxD1 P2.9, TxD1 P2.8 */
       
        /* 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 UART2 interrupt */
    return (TRUE);
  }
  else if ( PortNum == 3 )
  {
        LPC_PINCON->PINSEL9 &= ~0x0f000000;
        LPC_PINCON->PINSEL9 |=  0x0f000000;        /* Enable RxD1 P4.29, TxD1 P4.28 */
       
        /* 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 >> 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 UART2 interrupt */
    return (TRUE);
  }  
  return( FALSE );
}

/*****************************************************************************
** Function name:                UARTSend
**
** Descriptions:                Send a block of data to the UART 0 port based
**                                                on the data length
**
** parameters:                        portNum, buffer pointer, and data length
** Returned value:                None
**
*****************************************************************************/
void UARTSend( uint32_t portNum, uint8_t *BufferPtr, uint32_t Length )
{
  if ( portNum == 0 )
  {
    while ( Length != 0 )
    {
          /* THRE status, contain valid data */
          while ( !(UART0TxEmpty & 0x01) );       
          LPC_UART0->THR = *BufferPtr;
          UART0TxEmpty = 0;        /* not empty in the THR until it shifts out */
          BufferPtr++;
          Length--;
        }
  }
  else if ( portNum == 1 )
  {
    while ( Length != 0 )
      {
      /* THRE status, contain valid data */
      while ( !(UART1TxEmpty & 0x01) );
      LPC_UART1->THR = *BufferPtr;
      UART1TxEmpty = 0; /* not empty in the THR until it shifts out */
      BufferPtr++;
      Length--;
      }
  }
  else if ( portNum == 2 )
  {
    while ( Length != 0 )
    {
          /* THRE status, contain valid data */
          while ( !(UART2TxEmpty & 0x01) );       
          LPC_UART2->THR = *BufferPtr;
          UART2TxEmpty = 0;        /* not empty in the THR until it shifts out */
          BufferPtr++;
          Length--;
        }
  }
  else
  {
        while ( Length != 0 )
    {
          /* THRE status, contain valid data */
          while ( !(UART3TxEmpty & 0x01) );       
          LPC_UART3->THR = *BufferPtr;
          UART3TxEmpty = 0;        /* not empty in the THR until it shifts out */
          BufferPtr++;
          Length--;
    }
  }
  return;
}

/******************************************************************************
**                            End Of File
******************************************************************************/

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

2
发表于 2019-9-18 07:14:55 来自手机 | 只看该作者
nxp也有库,例程看看。

出45入88汤圆

3
 楼主| 发表于 2019-9-18 07:22:18 | 只看该作者
解决了,原来串口0,1默认是打开了电源,串口2,3要设置寄存器才能打开电源。

出0入0汤圆

4
发表于 2019-9-18 09:04:36 | 只看该作者
nxp有个功率寄存器要使能的

出0入4汤圆

5
发表于 2019-9-18 20:23:56 | 只看该作者
感谢分享
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 21:21

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表