?? uart.c
字號(hào):
#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; // 接收緩沖區(qū)的當(dāng)前位置
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; //定時(shí)器1工作在方式2(自動(dòng)重裝載)
SCON=0x50; //設(shè)定串行口工作方式為方式1;允許接收
PCON=0;
TH1=USART_BAND;
TR1=1;
ES=1;
UartSend_wp = UartSend_rp = 0;
UartRecv_wp = UartRecv_rp = 0;
}
//----------------------------------------------------------------------------------
//串行口發(fā)送數(shù)據(jù)
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;
}
//-----------------------------------------------------------------------------------
//串行口數(shù)據(jù)發(fā)送與接收中斷函數(shù)
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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -