/*Abstract :USART communication.
*
*Version NO.:1.0
*Writer :Woy
*Date :2015-06-10
**************************************************/
#include <c8051f340.h>
#include <system.h>
#include <uart.h>
//======================================================================================//
uchar code *DTCMD[CMDNUM] ={"$DTECH","$DTPOS","$DTMOD","$DTSET"};
uchar code DTERR[] = "$DTERR\r\n";//*0\r\n";//10-2
//======================================================================================//
bit Uart0OkFlag;
bit Uart1OkFlag;
bit Uart0ErroFlag; //Uart0 recieved erro flag
bit Uart1ErroFlag; //Uart1 recieved erro flag
uchar U0ByteNum;
uchar U1ByteNum;
uchar U0index;
uchar U1index;
uchar Uart0ProtocolFlag;
uchar Uart1ProtocolFlag;
uchar xdata Uart0RxBuff[100];
uchar xdata Uart1RxBuff[100];
//======================================================================================//
/***UART0 string transmit****/
void Uart0_Tx(uchar *string, uchar snum)
{
uchar *tstr,iloop;
tstr = string;
ES0 = 0; //Disable uart0 interrupt when transmitting
for (iloop=0; iloop<snum; iloop++)
{
SBUF0 = *tstr ;
while (TI0==0);
TI0 = 0;
tstr++;
}
ES0 = 1; //Enable uart0 interrupt when transmitted
}
//======================================================================================//
void Uart0_Tx_End(uchar *string)
{
uchar iLoop;
iLoop=0;
ES0 = 0;
while((*string!='\0')&&(iLoop<100))
{
SBUF0 = *string;
while(TI0==0);
TI0 = 0;
string++;
iLoop++;
}
ES0 = 1;
}
//======================================================================================//
/******Uart0 Received interrupt************/
void Uart0_Rx_ISR4() interrupt 4
{
uchar CharBuff;
if(RI0)
{
RI0 = 0;
CharBuff = SBUF0;
if((U0index == 0)&&(CharBuff == '$'))//协议1:以$开始
{
Uart0RxBuff[U0index++] = CharBuff;
Uart0OkFlag = 0;//接收还没完成
Uart0ProtocolFlag = 1;//协议1
}//if(U0index == 0)
else if(U0index ==2)//协议0:以$+两个互补字节引导
{
if(CharBuff==0xff-Uart0RxBuff[1]) Uart0ProtocolFlag = 0;//选择协议0
else Uart0ProtocolFlag = 1;//选择协议1
Uart0RxBuff[U0index++] = CharBuff;
}
else if((U0index > 0) && (U0index <100))
{
if(Uart0ProtocolFlag)//协议1
{
if(CharBuff == '\n')//以\n结束字符串接收
Uart0OkFlag = 1;//接收完成标志
Uart0RxBuff[U0index++] = CharBuff;
}//if(Uart0ProtocolFlag)
else//协议0
{
if(U0index == 5)
U0ByteNum = Uart0RxBuff[4]*0x100 + Uart0RxBuff[3]+6;//1(SOH)+1(ID)+1(~ID)+2(LEN)+1(CHECKNUM);
if((U0index > 4) && (U0index == U0ByteNum-1))
Uart0OkFlag = 1;
Uart0RxBuff[U0index++] = CharBuff;
}
}//else if
}//if
}
//=============================================================================//
void Uart0_Rx_Parse()
{
uchar iLoop;
char cmpValue;
//char cmpValue2;
// uchar NorCheck = 0;
if(Uart0OkFlag || (U0index==100))//收到一条有效的信息或者缓冲区饱和
{
if(Uart0OkFlag)//收到有效信息
{
Uart0OkFlag = 0;
if(Uart0ProtocolFlag)//协议1
{
for(iLoop=0; iLoop<CMDNUM; iLoop++)
{ //比较 Uart0RxBuff和DTCMD[iLoop]前n个字节
cmpValue = memcmp(Uart0RxBuff,DTCMD[iLoop],6);
if(cmpValue==0)//find the command
{
switch(iLoop)
{
case 0:
if(Uart0RxBuff[7] == 0x31)
{
Echo_On_Off(1) ;
onOff = 1;
Uart0_Tx("$DTECH,1,OK\r\n",13);
}
else if(Uart0RxBuff[7] == 0x30)
{
Echo_On_Off(0) ;
onOff = 0;
Uart0_Tx("$DTECH,0,OK\r\n",13);
}
else
{
Uart0_Tx(DTERR,8);
}
break;
case 1:
if(Uart0RxBuff[7] == 0x31)
{
POS_On_Off(1) ;
Uart0_Tx("$DTPOS,1,OK\r\n",13);
}
else if(Uart0RxBuff[7] == 0x30)
{
POS_On_Off(0) ;
Uart0_Tx("$DTPOS,0,OK\r\n",13);
}
else
{
Uart0_Tx(DTERR,8);
}
break;
case 2:
if(Uart0RxBuff[7] == 0x31)
{
workMode = 1;
Uart0_Tx("$DTMOD,1,OK\r\n",13);
}
else if(Uart0RxBuff[7] == 0x30)
{
workMode = 0;
Uart0_Tx("$DTMOD,0,OK\r\n",13);
}
else
{
Uart0_Tx(DTERR,8);
}
break;
case 3:
sleeperDis = currentDis;
Uart0_Tx("$DTSET,",7);
Uart0_Tx(distance,3);
Uart0_Tx(",OK\r\n",5);
break;
default:
Uart0_Tx(DTERR,8);
break;
}//switch(iLoop)
break;
}//if(cmpValue1==0)//find the command
}//for(iLoop=0; iLoop<CMDNUM; iLoop++)
}
else
{
switch (Uart0RxBuff[1])
{
case 0x50: //80 command :update firmware
RSTSRC |= 0x10;
break;
default:
break;
}
// }
}//else
}//if(Uart0OkFlag)
for(iLoop=0; iLoop<100; iLoop++)
Uart0RxBuff[iLoop] = 0;
U0index = 0;
U0ByteNum =0;
} //if(Uart0OkFlag || (U0index==100))//收到一条有效的信息或者缓冲区饱和
}
//=====================================no more==========================================//