uart.c

来自「凌阳61单片机做的酒精控制器源码 凌阳61单片机做的酒精控制器源码 凌阳61单片」· C语言 代码 · 共 82 行

C
82 行
字号
#include"SPCE061A.H"
#include"Uart.h"

typedef struct _UartBufRx{
	unsigned int unRxData[RXBUFSIZE];
	unsigned char ucRxCount;
}UART_BUF_RX_s;
typedef struct _UartBufTx{
	unsigned int unTxData[TXBUFSIZE];
	unsigned char ucTxCount;
}UART_BUF_TX_s;

static UART_BUF_RX_s stRxData;
static UART_BUF_TX_s stTxData;

/******************************************************************
*初始化串口,中断形式
*******************************************************************/
void vUartSysInit(void){
	unsigned int temp;
	temp = *Port_IOB_Dir;
	temp &= 0xfc7f;
	temp |= 0x0040;
	*Port_IOB_Dir |= temp;
	*Port_IOB_Attrib |= 0x0600;
	*Port_UART_Command1 = 0x20;
	*Port_UART_Command2 = 0x00;
	*Port_UART_BaudScalarL = UART_RATE_L;
	*Port_UART_BaudScalarH = UART_RATE_H;
	*Port_UART_Command1 = 0x80;
	*Port_UART_Command2 = 0x80;	
	stRxData.ucRxCount = 0x00;
	stTxData.ucTxCount = TXCOUNT;
}
/******************************************************************
*读取串口数据寄存器的数据 
********************************************************************/  
unsigned int* unUartRx(void){
	if(RXCOUNT <= stRxData.ucRxCount){
		return stRxData.unRxData;
	}else{
		return NULL;
	}
}
/********************************************************************
*向串口数据寄存器写数据 
********************************************************************/ 
void vUartTx(unsigned int* unData){
	unsigned char temp;
	for(temp = 0; temp <TXBUFSIZE; temp++){
		stTxData.unTxData[temp] = unData[temp];
	}
	stTxData.ucTxCount = TXCOUNT;
	*Port_UART_Command1 |= 0x0060;
} 

void UART(void)__attribute__((ISR)){
	unsigned int temp1,temp2;
	temp1 = *Port_UART_Command2;
	if(temp1&0x80){
		if(RXCOUNT > stRxData.ucRxCount){
			stRxData.unRxData[stRxData.ucRxCount] = *Port_UART_Data;
			stRxData.ucRxCount++;
		}else{			
			temp2 = *Port_UART_Data;
		}
	}
	if(temp1&0x60){
		if(TXCOUNT > stTxData.ucTxCount){
			*Port_UART_Data = stTxData.unTxData[stTxData.ucTxCount];
			stTxData.ucTxCount--;
			if(0x00 >= stTxData.ucTxCount){
				*Port_UART_Command1 &= 0xffbf; /*禁止发送中断*/ 
			}
		}
	}
}




⌨️ 快捷键说明

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