⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart1.c

📁 lpc2478+ucosII
💻 C
字号:
/*****************************************************************************
 *   uart.c:  UART API file for NXP LPC24xx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.12  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC2468.h"                        /* LPC24xx definitions */
#include "type.h"
#include "target.h"
#include "irq.h"
#include "uart1.h"


volatile DWORD UART1Status;
volatile BYTE UART1TxEmpty = 1;
volatile BYTE UART1Buffer[BUFSIZE]={0x11,0x11,0x11,0x11,0x11,0x22,0x22,0x22};
volatile DWORD UART1Count = 8;

/*****************************************************************************
** Function name:		UART0Handler
**
** Descriptions:		UART0 interrupt handler
**
** parameters:			None
** Returned value:		None
**
*****************************************************************************/
__irq __nested __arm void UART1Handler (void)
{
    BYTE IIRValue, LSRValue;
    volatile BYTE Dummy;

    __enable_interrupt();		/* handles nested interrupt */

    IIRValue = U1IIR;
    IIRValue >>= 1;			/* skip pending bit in IIR */
    IIRValue &= 0x07;			/* check bit 1~3, interrupt identification */
    if ( IIRValue == IIR_RLS )		/* Receive Line Status */
    {
		LSRValue = U1LSR;
		/* 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 = U1RBR;		/* Dummy read on RX to clear interrupt, then bail out */
	    	VICVectAddr = 0;	/* Acknowledge Interrupt */
	    	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] = U0RBR;
	    	UART1Count++;
	    	if ( UART1Count == BUFSIZE )
	    	{
			UART1Count = 0;	/* buffer overflow */
	    	}	
		}
    }
    else if ( IIRValue == IIR_RDA )	/* Receive Data Available */
    {
		/* Receive Data Available */
		UART1Buffer[UART1Count] = U0RBR;
		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 = U1LSR;	/* Check status in the LSR to see if valid data in U0THR or not */
		if ( LSRValue & LSR_THRE )
		{
	                UART1TxEmpty = 1;
		}
		else
		{
	                UART1TxEmpty = 0;
		}
    }

    VICVectAddr = 0;		        /* Acknowledge Interrupt */
    return;
}

/*****************************************************************************
** Function name:		UART0Init
**
** Descriptions:		Initialize UART0 port, setup pin select,
**						clock, parity, stop bits, FIFO, etc.
**
** parameters:			UART baudrate
** Returned value:		true or false, return false only if the
**						interrupt handler can't be installed to the
**						VIC table
**
*****************************************************************************/
DWORD UART1Init( DWORD baudrate )
{
    DWORD Fdiv;
    PINSEL0 |= 0x40000000;	/* Enable TxD1 P0.15 */
    PINSEL1 |= 0x00000001;	/* Enable RxD1 P0.16 */

    U1LCR = 0x83;		/* 8 bits, no Parity, 1 Stop bit */
    Fdiv = ( Fpclk / 16 ) / baudrate ;	/*baud rate */
//    Fdiv = (3000000 /16) / baudrate;
    U1DLM = Fdiv / 256;							
    U1DLL = Fdiv % 256;
    U1LCR = 0x03;		/* DLAB = 0 */
    U1FCR = 0x07;		/* Enable and reset TX and RX FIFO. */

    if ( install_irq( UART1_INT, (void *)UART1Handler, HIGHEST_PRIORITY ) == FALSE )
    {
		return (FALSE);
    }

    U1IER = IER_RBR | IER_THRE | IER_RLS;	/* Enable UART0 interrupt */
    return (TRUE);
}

/*****************************************************************************
** Function name:		UART0Send
**
** Descriptions:		向UART0发送一个字节数据
** parameters:			buffer pointer
** Returned value:		None
**
*****************************************************************************/
void UART1SendByte(BYTE *BufferPtr)
{
    U1THR = *BufferPtr;
    while((U1LSR & 0x40) == 0);   //检测THR是否为空,数据发送是否完毕

    return;
}

/*****************************************************************************
** Function name:		UART0Recv
**
** Descriptions:		从UART0接收一个字节数据
** parameters:			buffer pointer, and data length
** Returned value:		None
**
*****************************************************************************/
void UART1RecvByte(BYTE *BufferPtr)
{
    while((U1LSR & 0x01) == 0);         /* 等待接收数据到达RBR */
    *BufferPtr = U1RBR;

    return;
}

/*****************************************************************************
** Function name:		UART0_puts
**
** Descriptions:		查询方式向UART0发送数据
** parameters:			buffer pointer
** Returned value:		None
**
*****************************************************************************/
void UART1_puts(BYTE *BufferPtr)
{
    while( *BufferPtr != 0)
      {
        UART1SendByte(BufferPtr);
        BufferPtr ++;
      }

    return;
}

/*****************************************************************************
** Function name:		UART0_gets
**
** Descriptions:		查询方式从UART0接收数据
** parameters:			buffer pointer
** Returned value:		None
**
*****************************************************************************/
void UART1_gets(BYTE *BufferPtr)
{
    while( 1 )
    {
      UART1RecvByte(BufferPtr);
      if(*BufferPtr == '\0')break;      // 如果收到的数据为0则认为数据传送结束

      BufferPtr ++;             // 未做边界检查,可能会有危险
    }

    return;
}

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -