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

📄 uart.c

📁 這是LPC2368的UART範例~~~請大家有需要的可以下載
💻 C
字号:
/*****************************************************************************
 *   uart.c:  UART API file for NXP LPC23xx/24xx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.12  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC23xx.h"                        /* LPC23xx/24xx definitions */
#include "type.h"
#include "target.h"
#include "irq.h"
#include "uart.h"

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

/*****************************************************************************
** Function name:		UART0Handler
**
** Descriptions:		UART0 interrupt handler
**
** parameters:			None
** Returned value:		None
** 
*****************************************************************************/
void UART0Handler (void) __irq 
{
   	BYTE uart_data;    
    //IENABLE;				/* handles nested interrupt */	
	if((U0IIR & 0x0f) == 0x04)	/*判断是否为接收中断*/
    {
		 uart_data = U0RBR;			/*接收数据*/
    	 U0THR = uart_data;			/*发送数据*/
	}
    
    //IDISABLE;
    VICVectAddr = 0;		/* Acknowledge Interrupt */
}

/*****************************************************************************
** Function name:		UART1Handler
**
** Descriptions:		UART1 interrupt handler
**
** parameters:			None
** Returned value:		None
** 
*****************************************************************************/
void UART1Handler (void) __irq 
{
    BYTE uart_data;    
    //IENABLE;				/* handles nested interrupt */	
	if((U1IIR & 0x0f) == 0x04)	/*判断是否为接收中断*/
    {
		 uart_data = U1RBR;			/*接收数据*/
    	 U1THR = uart_data;			/*发送数据*/
	}
    
    //IDISABLE;
    VICVectAddr = 0;		/* Acknowledge Interrupt */
}

/*****************************************************************************
** Function name:		UARTInit
**
** Descriptions:		Initialize UART0 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
** 
*****************************************************************************/
DWORD UARTInit( DWORD PortNum, DWORD baudrate )
{
    DWORD Fdiv;

	if ( PortNum == 0 )
	{
		SCS		&= ~0x01;
		PINSEL0 = 0x00000050;       /* RxD0 and TxD0 */

    	U0LCR = 0x83;		/* 8 bits, no Parity, 1 Stop bit */
    	Fdiv = ( Fpclk / 16 ) / baudrate ;	/*baud rate */
    	U0DLM = Fdiv / 256;							
    	U0DLL = Fdiv % 256;
		U0LCR = 0x03;		/* DLAB = 0 */
    	U0FCR = 0x07;		/* Enable and reset TX and RX FIFO. */

    	if ( install_irq( UART0_INT, (void *)UART0Handler, HIGHEST_PRIORITY ) == FALSE )
    	{
			return (FALSE);
    	}
   
    	U0IER = IER_RBR | IER_THRE | IER_RLS;	/* Enable UART0 interrupt */
    	return (TRUE);
	}
	else if ( PortNum == 1 )
	{
#if EA_BOARD_LPC24XX
		PINSEL7 |= 0x0000000F;	/* P3.16 TXD1, P3.17 RXD1 */
#else							/* Default is Keil MCB2300 board */							
		PINSEL0 |= 0x40000000;	/* Enable TxD1 P0.15 */
		PINSEL1 |= 0x00000001;	/* Enable RxD1 P0.16 */
#endif
    	U1LCR = 0x83;		/* 8 bits, no Parity, 1 Stop bit */
    	Fdiv = ( Fpclk / 16 ) / baudrate ;	/*baud rate */
    	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);
	}
	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( DWORD portNum, BYTE *BufferPtr, DWORD Length )
{
	if ( portNum == 0 )
	{
    	while ( Length != 0 )
    	{
			/* THRE status, contain valid data */
			while ( !(UART0TxEmpty & 0x01) );	
			U0THR = *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) );	
			U1THR = *BufferPtr;
			UART1TxEmpty = 0;	/* not empty in the THR until it shifts out */
			BufferPtr++;
			Length--;
    	}
	}
    return;
}

void UARTSendByte( DWORD portNum, BYTE Ptr )
{
	if ( portNum == 0 )
	{	
		U0THR = Ptr;
    }
	else
	{	
		U1THR = Ptr;
	}
    return;
}
#define		UARTRCV_END 	(U0LSR & 0x01)
BYTE UARTn_RcvByte (void)
{
	BYTE rcv_dat;
	
	while (UARTRCV_END == 0);
	rcv_dat = U0RBR;
	
	return (rcv_dat);	
}

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

⌨️ 快捷键说明

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