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

📄 16c554.c

📁 tl16c554 bsp
💻 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<g_ucUD_RxPrt; i++)
		{
			UD_THR = g_ucUD_Rx[i];
			
/*
			if(g_ucUD_TxPrt != g_ucUD_TxFreePrt)
			{
				UD_THR = g_ucUD_Tx[g_ucUD_TxPrt];
				g_ucUD_TxPrt ++;
			}
			else
			{
				break;
			}
*/
		}
		g_ucUD_RxPrt = 0;
	}
}

/*
*******************************************************************************
	串口D发送处理
*******************************************************************************
*/
void UartDTxDeal(void)
{
	uchar m_ucLsr;
	uchar i;
	
	m_ucLsr = UD_LSR;
	if((m_ucLsr & 0x20) == 0x20)
	{
		for(i=0; i<16; i++)
		{

			if(g_ucUD_TxPrt != g_ucUD_TxFreePrt)
			{
				UD_THR = g_ucUD_Tx[g_ucUD_TxPrt];
				g_ucUD_TxPrt ++;
			}
			else
			{
				break;
			}
		}
	}
}

/*
***********************************************************************************
	串口D接收处理
***********************************************************************************
*/
void UartDRxDeal(void)
{
	//

}	
#endif
/**********************************************************************/
/*	函数功能:  外部中断1                                             */
/*  输入参数:			                                              */
/*  输出参数:                                                        */
/**********************************************************************/   
void Exint1(void) interrupt 2
{
	uchar m_ucBuff;
	m_ucBuff = U_READ_STATUS;
#if(UARTA == ENABLE)
    if(((m_ucBuff & 0x80) == 0x80) || (g_ucUartTxStatus == UA_TX) )
	{
		g_ucUartTxStatus &= UA_TX_CLEAR;
		UartADispasal(); 
	}
#endif

#if(UARTB == ENABLE)
	if( ((m_ucBuff & 0x40) == 0x40) || (g_ucUartTxStatus == UB_TX) )
	{
		g_ucUartTxStatus &= UB_TX_CLEAR;
		UartBDispasal(); 
	}
#endif

#if(UARTC == ENABLE)
	if( ((m_ucBuff & 0x20) == 0x20) || (g_ucUartTxStatus == UC_TX) )
	{
		g_ucUartTxStatus &= UC_TX_CLEAR;
		UartCDispasal(); 
	}
#endif

#if(UARTD == ENABLE)
	if( ((m_ucBuff & 0x10) == 0x10) || (g_ucUartTxStatus == UD_TX) )
	{
		g_ucUartTxStatus &= UD_TX_CLEAR;
		UartDDispasal(); 
	}
#endif
}





	

	

⌨️ 快捷键说明

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