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

📄 uart.c

📁 用8052写的一个LED大屏幕显示程序
💻 C
字号:
#include	<reg52.h>
#include	<string.h>
#include	<intrins.h>
//#include	"init.h"
#include	"uart.h"
#include	"xsp.h"


static data unsigned int		UartSend_rp, UartSend_wp;
static data unsigned int		UartRecv_rp, UartRecv_wp;

unsigned char		UartSendBuffer[UART_BUFFER_SIZE];
unsigned char		UartRecvBuffer[UART_BUFFER_SIZE];

unsigned char		GPRSRecvBuff[UART_RECV_SIZE];	
unsigned char		GPRSSendBuff[UART_RECV_SIZE];	
unsigned char		GPRSSaveBuff[UART_RECV_SIZE];	

static 	data unsigned int		GPRSRecvSize;				// 接收缓冲区的当前位置
static  data unsigned char	GPRSDBusFlag;

//unsigned char		UartData[UART_RECV_SIZE];

//void UART_init(void);
//void UARTSend(BYTE * Data,BYTE LenStr);


//***********************************************************************
//串行口初始化
void UART_init(void){
	TMOD&=0x0f;
	TMOD|=0x20;	   //定时器1工作在方式2(自动重装载)
	SCON=0x50;	   //设定串行口工作方式为方式1;允许接收
	PCON=0;
	TH1=USART_BAND;
	TR1=1;
	ES=1;
	UartSend_wp = UartSend_rp = 0;
   	UartRecv_wp = UartRecv_rp = 0;
}

//----------------------------------------------------------------------------------
//串行口发送数据

void UARTSend(unsigned char *a, unsigned char n)
{
//    bit intTI;
//    intTI=0;
    while (n > 0) {
    	UartSendBuffer[UartSend_wp++] = *a++;
     	UartSend_wp &= UART_BUFFER_MASK;
	n--;
    }
//	if (!intTI) {TI=1;intTI=1;}
    TI=1;
}

unsigned char UARTReceive(unsigned char *a, unsigned char n)
{
    unsigned char		ReceiveCount = 0;

    while (n > 0 && UartRecv_wp != UartRecv_rp) {
	*a++ = UartRecvBuffer[UartRecv_wp++];
	UartRecv_wp &= UART_BUFFER_MASK;
	ReceiveCount++;
	n--;
    }
    return ReceiveCount;
}

//-----------------------------------------------------------------------------------
//串行口数据发送与接收中断函数
void UartInt() interrupt 4 {
	if (RI) {
	    RI=0;
	    UartRecvBuffer[UartRecv_rp++] = SBUF;
    	    UartRecv_rp &= UART_BUFFER_MASK;
	}
	else{
	    TI=0;
	    if (UartSend_rp != UartSend_wp) {
    	    	SBUF = UartSendBuffer[UartSend_rp++];
	    	UartSend_rp &= UART_BUFFER_MASK;
            }
	}
}



unsigned char GPRSRecvResponse(void)
{
data    unsigned int		UartReceivedBytes;
data    unsigned int		UartBuffPoint;
static data  unsigned int	GPRSDBusPos1;
static data  unsigned int	GPRSDBusPos2;
data    unsigned int		i;
data    unsigned int		j;
//idata    unsigned char		Result;
data    unsigned char		DataLength;
data    unsigned char		XorSum;

    if ((UartReceivedBytes = UARTReceive(GPRSRecvBuff + GPRSRecvSize, UART_RECV_SIZE)) == 0)
	return FALSE;

    GPRSRecvSize += UartReceivedBytes;


    for (i = 0; i < UartReceivedBytes; i++) {
	UartBuffPoint = GPRSRecvSize - UartReceivedBytes + i + 1;
        if (UartBuffPoint < 2)	continue;
	    if (!GPRSDBusFlag) {
	   	 if (GPRSRecvBuff[UartBuffPoint - 1] == 0x55 &&
			GPRSRecvBuff[UartBuffPoint - 2] == 0xaa) {
			GPRSDBusFlag = TRUE;
			GPRSDBusPos1 = UartBuffPoint - 2;
	    	}
	    }	
	    else{
	    	if (UartBuffPoint > GPRSDBusPos1+3) DataLength = GPRSRecvBuff[GPRSDBusPos1+3];
	    	if(UartBuffPoint >= GPRSDBusPos1+DataLength+5+1){
		    	GPRSDBusFlag = FALSE;
		    	GPRSDBusPos2 = UartBuffPoint - 1;
		    	GPRSRecvSize -= UartBuffPoint;
			XorSum = 0;
    			for (j = 0; j < DataLength + 5; j++) {
				XorSum ^= GPRSRecvBuff[GPRSDBusPos1+j];
    			}
    			if (XorSum != GPRSRecvBuff[GPRSDBusPos1+DataLength + 5])	return FALSE;		
	            	memcpy(GPRSSaveBuff, GPRSRecvBuff + GPRSDBusPos1,GPRSDBusPos2 - GPRSDBusPos1 + 1);
		    	return TRUE;
		}
	    }
    }
    return FALSE;
}

⌨️ 快捷键说明

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