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

📄 serial.c

📁 NXP ARM7串口通讯程序
💻 C
字号:
/*
*******************************************************************************
*                     (c) Copyright 2008 
*                             All Rights Reserved
*
*                                   Version V1.00
*
* Product Number: 
* Compiler Tool : 
* MCU		: 
* File 		: Serial.c
* By   		: Tom.Yin
* Date 		: 
*******************************************************************************
*/


/*
*******************************************************************************
* 			     INCLUDE FILE
*******************************************************************************
*/
#include "Include.h"


/*
*******************************************************************************
*				      GLOBAL 
*******************************************************************************
*/
#define RBR_INTERRUPT_EN			0X01
#define THRE_INTERRUPT_EN			0X02
#define RX_STATUS_INTERRUPT_EN		0X04

#define IIR_UART_PENDING_FLG		BIN(00000001)
#define IIR_MASK					BIN(00001110)
#define IIR_RLS_FLG					BIN(00000110)	//RX Line status
#define IIR_RDA_FLG					BIN(00000100)	//RXD
#define IIR_CTI_FLG					BIN(00001100)	//Character time out indicator
#define IIR_THRE_FLG				BIN(00000010)	//transmit holding register empty

unsigned char RXD_Buffer[UART0_BUFFER_LENGTH];
unsigned char TXD_Buffer[UART0_BUFFER_LENGTH];

unsigned char In_Transmit_Flg;
unsigned int  Transmit_Length;
unsigned int  Transmit_Index;

unsigned int  Receive_Length;
unsigned int  Receive_Index;
unsigned char Received_Head_Flg;
unsigned int  Receive_Event;

//	Desible_Interrupt(UART_0_FLG);
//	Tick_Timer(Receive_Time_Out_Interval);
//	Enable_Interrupt(UART_0_FLG);
	
/*
*******************************************************************************
* 		             LOCAL DEFINE
*******************************************************************************
*/
void Uart0_ISR (void) __irq;

/*
*******************************************************************************
* 		             LOCAL PARAMETER
*******************************************************************************
*/


/*
*******************************************************************************
* 		            FUNCTION DECLARE
*******************************************************************************
*/
void UART0_IO_Config(void)
{
	PINSEL0 |= 0x0000005;                  		/* Enable RxD1 and TxD1   	 */          
}


/*
*******************************************************************************
*                             Init_Uart0
* Description: Setup the UART0, 8 bits, no Parity, 1 Stop bit,
*
* Arguments  : baudrate---------------------------target baudrate
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void Init_Uart0 (unsigned int baudrate)  				/* Initialize Serial Interface   */
{   
	unsigned int i;

	for (i = 0; i < UART0_BUFFER_LENGTH; i++)
	{
		RXD_Buffer[i]	= 0;
		TXD_Buffer[i]	= 0;
	}
	
	In_Transmit_Flg		= false;
	Transmit_Length		= 0;
	Transmit_Index		= 0;
	
	Receive_Length		= 0;
	Receive_Index		= 0;
	Received_Head_Flg	= 0;
	Receive_Event		= RECEIVE_NORMAL;
	
	UART0_IO_Config();
	
	U0LCR				= 0x83;                          	/* 8 bits, no Parity, 1 Stop bit */
	//U0DLM				= ((FPCLK / 16) / baudrate) / 256;	/* Set Baudrate   				 */
	//U0DLL				= ((FPCLK / 16) / baudrate) % 256;
	
	U0DLM				= FPCLK / (16 * 256 * baudrate);	/* Set Baudrate   				 */
	U0DLL				=(FPCLK / (16 * baudrate)) % 256;
	
	
	U0LCR				= 0x03;                          	/* DLAB = 0                      */	
	
  	VICVectAddr1		= (unsigned long)Uart0_ISR;          //set uart0 interrupt  		
  	VICVectCntl1		= INTERRUPT_CTRL_ENABLE | UART_0_FLG;   
	Enable_Interrupt(UART_0_FLG);
	
	U0IER				= RBR_INTERRUPT_EN | THRE_INTERRUPT_EN;	
}

/*
*******************************************************************************
*                             Clear_Receive_Event
* Description: Clear Reseive Event 
*
* Arguments  : None
*				
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void Clear_Receive_Event(void)
{
	Desible_Interrupt(UART_0_FLG);
	Receive_Event		= RECEIVE_NORMAL;
	Received_Head_Flg	= false;
	Enable_Interrupt(UART_0_FLG);
}

/*
*******************************************************************************
*                             Get_Receive_Event
* Description: Get Current Receive Event
*
* Arguments  : None
*				
* Returns    : Receive Event status
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
unsigned int Get_Receive_Event(void)
{
	return 	  Receive_Event;
}

/*
*******************************************************************************
*                             Is_Transmitting
* Description: Get Transmit status
*
* Arguments  : None
*				
* Returns    : Transmit flag, true---Transmitting, false-----transmit finished
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
unsigned int Is_Transmitting(void)
{
	return In_Transmit_Flg;
}

/*
*******************************************************************************
*                             Send_Msg
* Description: set Send message to transmit buffer and start transmit msg
*
* Arguments  : *buf---------------------------transmit message
*				len---------------------------message length
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void Send_Msg(unsigned char *buf, unsigned int len)
{
	unsigned int i;
	
	if (len <= 0)
	{
		return;
	}
	
	if (!In_Transmit_Flg)
	{
		for (i = 0; i < len; i++)
		{
			TXD_Buffer[i]	= *buf;
			buf++;
		}
		
		Transmit_Index	= 0;
		Transmit_Length	= len;
		In_Transmit_Flg	= true;
		U0THR			= TXD_Buffer[Transmit_Index];
	}
}
              
/*
*******************************************************************************
*                             TXD_Process
* Description: Transmit process
*
* Arguments  : None
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void TXD_ISR_Process(void)
{
	if (In_Transmit_Flg)
	{
		if (Transmit_Length == Transmit_Index)
		{
			In_Transmit_Flg	= false;
			Transmit_Index	= 0;
		}
		else
		{
			if (Transmit_Index < UART0_BUFFER_LENGTH)
			{
				Transmit_Index ++;
				U0THR	= TXD_Buffer[Transmit_Index];
			}
			else
			{
				In_Transmit_Flg	= false;
				Transmit_Index	= 0;
				//error process
			}
		}
	}
}


/*
*******************************************************************************
*                             RXD_Process
* Description: Receive process
*
* Arguments  : None
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void RXD_ISR_Process(void)
{
	if (!Received_Head_Flg)
	{
		unsigned char r_temp;
		
		r_temp	= U0RBR;
		
		if (r_temp == FRAME_HEAD)
		{
			Received_Head_Flg	= true;
			Receive_Index		= 0;
			Receive_Length		= 0;
		}

		RXD_Buffer[Receive_Index] = r_temp;
		
		Receive_Index++;	
	}
	else
	{
		RXD_Buffer[Receive_Index] = U0RBR;
		
		Receive_Index++;
		
		if (Receive_Length == 0)
		{
			if (Receive_Index == LENGTH_FIELD_SIZE)
			{
				Receive_Length	= RXD_Buffer[1]<<24 | RXD_Buffer[2]<<16 | RXD_Buffer[3]<<8 | RXD_Buffer[4];
			}
		}
		else
		{
			if (Receive_Index >= (Receive_Length + LENGTH_FIELD_SIZE))
			{
				Receive_Event	= RECEIVE_A_FRAME;
			}
		}
	
		if (Receive_Index >= UART0_BUFFER_LENGTH)
		{
			Received_Head_Flg	= false;
			Receive_Index		= 0;
			Receive_Length		= 0;
			Receive_Event		= RECEIVE_NORMAL;
			//U0IER = U0IER & (~0x01);
			//error process
		}
	}
}

/*
*******************************************************************************
*                             Uart0_ISR
* Description: Uart0 Interrupt service program
*
* Arguments  : None
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void Uart0_ISR (void) __irq
{
	unsigned char iir;
	
	while (((iir = U0IIR) & IIR_UART_PENDING_FLG) == 0)
	{
		switch (iir & IIR_MASK)
		{
			case IIR_RLS_FLG:
				//read U0LSR
				break;
				
			case IIR_RDA_FLG:
				RXD_ISR_Process();
				break;
				
			case IIR_CTI_FLG:
				// U0IER = U0IER & (~0x01);
				break;
				
			case IIR_THRE_FLG:
				TXD_ISR_Process();
				break;
				
			default:
				break;
		}
	}

	VICVectAddr	= 0; 
}




⌨️ 快捷键说明

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