www.pudn.com > tl16c554.rar > 16c554.c


 
#define _DEBUG_ 
 
/******************************************************/ 
/*	日期:                                            */ 
/*	功能:                                            */ 
/*	硬件:                                            */ 
/*	IO口:                                            */ 
/******************************************************/ 
#include "c8051F020.h" 
#include "Common.h" 
#include "Absacc.h" 
#include "16c554.h" 
#include "rtx51tny.h" 
#include "Main.h" 
#include "Hardware.h" 
/******************************************************/ 
/*	变量声明                                          */ 
#if(UARTD == ENABLE) 
	#define UD_RX_MAX    64 
	uchar xdata g_ucUD_Tx[256]; 
	uchar xdata g_ucUD_TxFreePrt; 
	uchar xdata g_ucUD_TxPrt; 
	uchar xdata g_ucUD_Rx[UD_RX_MAX]; 
	uchar xdata g_ucUD_RxPrt; 
#endif 
 
#if(UARTA == ENABLE) 
	#define GPS_TIME	10		    	//2 data frames interval  
	#define UA_RX_MAX 	256 
	#define UA_TX_MAX	64 
	uchar xdata g_ucUA_Tx[UA_TX_MAX]; 
	uchar xdata g_ucUA_TxPrt; 
	uchar xdata g_ucUA_TxFreePrt; 
	uchar xdata g_ucUA_Rx[UA_RX_MAX]; 
	uchar xdata g_ucUA_RxPrt; 
	uchar xdata g_ucUA_RxPrevPrt; 
	uchar xdata g_ucUA_RxStarPrt; 
	ulong xdata	g_ulGpsRxTime; 
	bit g_bGpsRx; 
	 
	extern uchar xdata g_ucGpsRx[];		// 
#endif 
 
uchar g_ucUartFlag;    //Tl16c554 sigle flag, flag define in "16c554.h 
uchar g_ucUartTxStatus; 
/* 
************************************************************************************* 
	外部变量声明 
************************************************************************************* 
*/ 
extern	ulong idata g_ulTimeCount; 
/*************************************************************/ 
/*	函数声明                                                 */ 
/*************************************************************/ 
void UartAIni(void); 
void UartBIni(void); 
void UartCIni(void); 
void UartDIni(void); 
void ExternUartIni(void); 
void UartADispasal(void); 
void UartBDispasal(void); 
void UartCDispasal(void); 
void UartDDispasal(void); 
void UartATxDeal(uchar *pMessage, uchar Len); 
void UartARxDeal(void); 
void UartBTxDeal(uchar *pMessage, uchar Len); 
void UartBRxDeal(void); 
void UartCTxDeal(uchar *pMessage, uchar Len); 
void UartCRxDeal(void); 
void UartDTxDeal(uchar *pMessage, uchar Len); 
void UartDRxDeal(void); 
 
void UART_Reset(void); 
/* 
*********************************************************************** 
	扩展串口任务处理 
*********************************************************************** 
*/ 
void TaskUart(void) _task_ UART_TASK_ID 
{ 
	char Event; 
	ExternUartIni(); 
	g_ucUartFlag = 0;              //clear all flag  
	g_ucUartTxStatus = 0; 
#if(UARTD == ENABLE) 
	g_ucUD_TxPrt = 0; 
	g_ucUD_TxFreePrt = 0; 
	g_ucUD_RxPrt = 0; 
	UartDIni(); 
#endif 
#if(UARTA == ENABLE) 
	g_ucUA_TxPrt = 0; 
	g_ucUA_TxFreePrt = 0; 
	g_ucUA_RxPrt = 0; 
	g_ucUA_RxPrevPrt = 0; 
	g_ucUA_RxStarPrt = 0; 
	g_bGpsRx = 0;                   //clear Gps receive flag 
	g_ulGpsRxTime = g_ulTimeCount;  //save currect time 
	UartAIni(); 
#endif 
#if(UARTB == ENABLE) 
	UartBIni(); 
#endif 
#if(UARTC == ENABLE) 
	UartCIni(); 
#endif 
 
#ifdef _DEBUG_ 
//	UD_THR = 0x55; 
#endif 
	while(1) 
	{ 
		//wait a sigle which obey Uart transmit a frame data or Uart receive a frame. 
		//sigle flag ----- g_ucUartFlag 
		Event = os_wait(K_TMO,10,0); 
 
	} 
} 
/**********************************************************************/ 
/*	函数功能:  初始化tl16c554                                        */ 
/*  输入参数:			                                              */ 
/*  输出参数:                                                        */ 
/**********************************************************************/ 
void ExternUartIni(void) 
{ 
	//reset tl16c554 
	UART_RST = 1; 
	os_wait(K_IVL,1,0);  //wait 20 mS 
	UART_RST = 0; 
	os_wait(K_IVL,10,0); //wait 200mS 
	IT1 = 1;	//when it is 0,INT1 is interrupted  
	EX1 = 1;	//enable INT1 
} 
 
#if(UARTA == ENABLE) 
/**********************************************************************/ 
/*	函数功能:  初始化串口A                                           */ 
/*  输入参数:			                                              */ 
/*  输出参数:                                                        */ 
/**********************************************************************/ 
void UartAIni(void) 
{ 
	UA_FCR = FCR_CONFIG1; 
	UA_LCR = 0X80;          //允许访问DLL,DLM  
	UA_DLL = DLL_A; 
	UA_DLM = DLM_A; 
	UA_LCR = LCR_CONFIG1; 
	UA_MCR = MCR_CONFIG1; 
	UA_IER = IER_CONFIG1; 
} 
 
 
/**********************************************************************/ 
/*	函数功能:  串口A中断处理                                         */ 
/*  输入参数:			                                              */ 
/*  输出参数:                                                        */ 
/**********************************************************************/  
void UartADispasal(void) 
{ 
	uchar i; 
	uchar m_ucLsr; 
	uchar m_ucIir; 
	m_ucIir = UA_IIR; 
	m_ucLsr = UA_LSR; 
	if((m_ucLsr & 0x01) == 0x01)	     //receive new charater 
	{ 
		i = 0; 
		do 
		{ 
			g_ucUA_Rx[g_ucUA_RxPrt] = UA_RBR; 
			g_ucUA_RxPrt ++; 
			m_ucLsr = UA_LSR; 
			i ++; 
		}while( ((m_ucLsr & 0x01) == 0x01) && (i < 16) ); 
		 
		//receive datas,send signal to Uart-task  
		g_ucUartFlag |= UARTA_RX;	 
		isr_send_signal(UART_TASK_ID); 
	} 
	if((m_ucLsr & 0x20) == 0x20) 
	{ 
		//can transmit new datas 
		for(i=0; i<16; i++) 
		{ 
			if(g_ucUA_TxPrt != g_ucUA_TxFreePrt) 
			{ 
				UA_THR = g_ucUA_Tx[g_ucUA_TxPrt]; 
				g_ucUA_TxPrt ++; 
			} 
			else 
			{ 
				break; 
			} 
		} 
	} 
} 
 
/* 
******************************************************************************* 
	串口A发送处理 
******************************************************************************* 
*/ 
void UartATxDeal(uchar *pMessage, uchar Len) 
{ 
	uchar ucUA_TxFreePrt; 
	uchar i; 
	 
	ucUA_TxFreePrt = g_ucUA_TxFreePrt; 
	for(i = 0; i < Len; i ++) 
	{ 
		g_ucUA_Tx[g_ucUA_TxFreePrt] = pMessage[i]; 
		g_ucUA_TxFreePrt ++; 
	} 
	if( ucUA_TxFreePrt == g_ucUA_TxPrt ) 
	{ 
		//UA transmit buffer is null, setb INIT 1 interrupt flag 
		g_ucUartTxStatus |= UA_TX; 
		TCON |= 0X08; 
	} 
} 
 
 
/* 
*********************************************************************************** 
	串口A接收处理 
*********************************************************************************** 
*/ 
void UartARxDeal(void) 
{ 
	//check if have already receive star character 
	uchar i; 
	if(g_bGpsRx) 
	{ 
		while(g_ucUA_RxPrevPrt != g_ucUA_RxPrt) 
		{ 
			if(g_ucUA_Rx[g_ucUA_RxPrevPrt] == 0x0d)		//receive over character 
			{ 
				g_bGpsRx = 0; 
				i = 1; 
				while(g_ucUA_RxStarPrt != g_ucUA_RxPrevPrt) 
				{ 
					g_ucGpsRx[i] = g_ucUA_Rx[g_ucUA_RxStarPrt]; 
					g_ucUA_RxStarPrt ++; 
					i ++; 
				} 
				os_send_signal(GPS_TASK_ID);	//send a single to GPS_TASK 
				break;                        	//return  
			} 
			g_ucUA_RxPrevPrt ++; 
		} 
	} 
	else 
	{ 
		while(g_ucUA_RxPrevPrt != g_ucUA_RxPrt) 
		{ 
			if(g_ucUA_Rx[g_ucUA_RxPrevPrt] == '$') 
			{ 
				g_bGpsRx = 1; 
				g_ucUA_RxStarPrt = g_ucUA_RxPrevPrt;	//save frame first point 
			} 
			g_ucUA_RxPrevPrt ++; 
		} 
	} 
} 
#endif 
 
#if(UARTB == ENABLE) 
/**********************************************************************/ 
/*	函数功能:  初始化串口B                                           */ 
/*  输入参数:			                                              */ 
/*  输出参数:                                                        */ 
/**********************************************************************/ 
void UartBIni(void) 
{	 
	UB_FCR = FCR_CONFIG1; 
	UB_LCR = 0X80;          //允许访问DLL,DLM  
	UB_DLL = DLL_B; 
	UB_DLM = DLM_B; 
	UB_LCR = LCR_CONFIG1; 
	UB_MCR = MCR_CONFIG1; 
	UB_IER = IER_CONFIG1; 
} 
 
 
/**********************************************************************/ 
/*	函数功能:  串口B中断处理                                         */ 
/*  输入参数:			                                              */ 
/*  输出参数:                                                        */ 
/**********************************************************************/  
void UartBDispasal(void) 
{ 
	 
} 
 
/* 
******************************************************************************* 
	串口B发送处理 
******************************************************************************* 
*/ 
void UartBTxDeal(void) 
{ 
	uchar m_ucLsr; 
	uchar i; 
	 
	m_ucLsr = UB_LSR; 
	if((m_ucLsr & 0x20) != 0x20) 
	{ 
		for(i=0; i<16; i++) 
		{ 
			if(g_ucUB_TxPrt != g_ucUB_TxFreePrt) 
			{ 
				UA_THR = g_ucUA_Tx[g_ucUB_TxPrt]; 
				g_ucUB_TxPrt ++; 
			} 
			else 
			{ 
				break; 
			} 
		} 
	} 
} 
 
/* 
*********************************************************************************** 
	串口B接收处理 
*********************************************************************************** 
*/ 
void UartBRxDeal(void) 
{ 
	// 
	// 
	// 
} 
#endif 
 
#if( UARTC == ENABLE ) 
/**********************************************************************/ 
/*	函数功能:  初始化串口C                                           */ 
/*  输入参数:			                                              */ 
/*  输出参数:                                                        */ 
/**********************************************************************/ 
void UartCIni(void) 
{	 
	UC_FCR = FCR_CONFIG1; 
	UC_LCR = 0X80;          //允许访问DLL,DLM  
	UC_DLL = DLL_C; 
	UC_DLM = DLM_C; 
	UC_LCR = LCR_CONFIG1; 
	UC_MCR = MCR_CONFIG1; 
	UC_IER = IER_CONFIG1; 
} 
 
/**********************************************************************/ 
/*	函数功能:  串口C中断处理                                         */ 
/*  输入参数:			                                              */ 
/*  输出参数:                                                        */ 
/**********************************************************************/  
void UartCDispasal(void) 
{ 
	 
} 
 
/* 
******************************************************************************* 
	串口C发送处理 
******************************************************************************* 
*/ 
void UartCTxDeal(void) 
{ 
	uchar m_ucLsr; 
	uchar i; 
	 
	m_ucLsr = UC_LSR; 
	if((m_ucLsr & 0x20) != 0x20) 
	{ 
		for(i=0; i<16; i++) 
		{ 
			if(g_ucUC_TxPrt != g_ucUC_TxFreePrt) 
			{ 
				UC_THR = g_ucUC_Tx[g_ucUC_TxPrt]; 
				g_ucUC_TxPrt ++; 
			} 
			else 
			{ 
				break; 
			} 
		} 
	} 
} 
 
/* 
*********************************************************************************** 
	串口C接收处理 
*********************************************************************************** 
*/ 
void UartCRxDeal(void) 
{ 
	// 
	// 
	// 
} 
#endif 
 
#if(UARTD == ENABLE) 
/**********************************************************************/ 
/*	函数功能:  初始化串口D                                           */ 
/*  输入参数:			                                              */ 
/*  输出参数:                                                        */ 
/**********************************************************************/ 
void UartDIni(void) 
{ 
	UD_LCR = 0X80;          //允许访问DLL,DLM  
	UD_DLL = DLL_D; 
	UD_DLM = DLM_D; 
	UD_LCR = LCR_CONFIG1; 
	UD_FCR = FCR_CONFIG1; 
	UD_MCR = MCR_CONFIG1; 
	UD_IER = IER_CONFIG1; 
} 
 
 
/**********************************************************************/ 
/*	函数功能:  串口D中断处理                                         */ 
/*  输入参数:			                                              */ 
/*  输出参数:                                                        */ 
/**********************************************************************/  
void UartDDispasal(void) 
{ 
	uchar i; 
	uchar m_ucLsr; 
	uchar m_ucIir; 
	m_ucIir = UD_IIR; 
	m_ucLsr = UD_LSR; 
	if((m_ucLsr & 0x01) == 0x01)	     //receive new charater 
	{ 
		i = 0; 
		do 
		{ 
			g_ucUD_Rx[g_ucUD_RxPrt] = UD_RBR; 
			g_ucUD_RxPrt ++; 
			m_ucLsr = UD_LSR; 
			i ++; 
		}while(((m_ucLsr & 0x01) != 0) && (i < 16) ); 
		 
		//receive datas,send signal to Uart-task  
		g_ucUartFlag |= UARTD_RX;	 
		isr_send_signal(UART_TASK_ID); 
	} 
	if((m_ucLsr & 0x20) == 0x20) 
	{ 
	 
		//can transmit new datas 
		for(i=0; i