|
W77E58 双串口通信源代码 注:此源程序只作参考,内有些函数未有原型,借鉴在于其通信方式及其设置。 CRC检校可另参考其它代码。 #i nclude <W77E58.h> #i nclude <Const.h> #i nclude <CRC8.h>
bit fSeri0_Send_Ok; bit fSeri1_Send_Ok;
bit fSeri1_Odd;
/************************************************** 串口0队列 **************************************************/ unsigned char xdata xBuf_Send_0[XBUFSERIAL0SEND]; unsigned char xdata xBuf_Reci_0[XBUFSERIAL0RECI];
unsigned char xdata *Seri0_ReadSendPoint=xBuf_Send_0; unsigned char xdata *Seri0_WriteSendPoint=xBuf_Send_0; unsigned char data Seri0_SendNum;
unsigned char xdata *Seri0_ReadReciPoint=xBuf_Reci_0; unsigned char xdata *Seri0_WriteReciPoint=xBuf_Reci_0; unsigned char data Seri0_ReciNum;
/************************************************** 压入单数据串口0发送队列 **************************************************/ void Seri0_PushSend(unsigned char mData) { *Seri0_WriteSendPoint=mData; if(Seri0_WriteSendPoint==(xBuf_Send_0+XBUFSERIAL0SEND-1)) Seri0_WriteSendPoint=xBuf_Send_0; else Seri0_WriteSendPoint++; Seri0_SendNum++; if(fSeri0_Send_Ok==0){fSeri0_Send_Ok=1;TI=1;} }
/************************************************** 弹出串口0接收队列单数据 **************************************************/ unsigned char Seri0_PopReci() { unsigned char i; if(Seri0_ReciNum==0)return 0;
i=*Seri0_ReadReciPoint; if(Seri0_ReadReciPoint==(xBuf_Reci_0+XBUFSERIAL0RECI-1)) Seri0_ReadReciPoint=xBuf_Reci_0; else Seri0_ReadReciPoint++; Seri0_ReciNum--; return i; }
/************************************************** 读出串口0接收队列指定序号数据 **************************************************/ unsigned char Seri0_ReadReci(unsigned char mId) { unsigned char xdata *pTemp; pTemp=Seri0_ReadReciPoint+mId;
if(pTemp<(xBuf_Reci_0+XBUFSERIAL0RECI))return(*pTemp); else return(*(pTemp-XBUFSERIAL0RECI)); }
/************************************************** 串口1队列 **************************************************/ unsigned char xdata xBuf_Send_1[XBUFSERIAL1SEND]; unsigned char xdata xBuf_Reci_1[XBUFSERIAL1RECI];
unsigned char xdata *Seri1_ReadSendPoint=xBuf_Send_1; unsigned char xdata *Seri1_WriteSendPoint=xBuf_Send_1; unsigned int data Seri1_SendNum;
unsigned char xdata *Seri1_ReadReciPoint=xBuf_Reci_1; unsigned char xdata *Seri1_WriteReciPoint=xBuf_Reci_1; unsigned char data Seri1_ReciNum;
/************************************************** 压入单数据串口1发送队列 **************************************************/ void Seri1_PushSend(unsigned char mData) { *Seri1_WriteSendPoint=mData; if(Seri1_WriteSendPoint==(xBuf_Send_1+XBUFSERIAL1SEND-1)){ Seri1_WriteSendPoint=xBuf_Send_1; } else Seri1_WriteSendPoint++; Seri1_SendNum++; if(fSeri1_Send_Ok==0){fSeri1_Send_Ok=1;TI_1=1;} }
/************************************************** 弹出串口1接收队列单数据 **************************************************/ unsigned char Seri1_PopReci() { unsigned char i; i=*Seri1_ReadReciPoint; if(Seri1_ReadReciPoint==(xBuf_Reci_1+XBUFSERIAL1RECI-1)) Seri1_ReadReciPoint=xBuf_Reci_1; else Seri1_ReadReciPoint++; Seri1_ReciNum--; return i; }
/************************************************** CY=0,使用T1;CY=1,使用T2 入口:Location=0,串口0;否则串口1 mBps,波特率。参考Const.h中的定义 mMode,方式。2慰糃onst.h中的定义 **************************************************/ void Init_Serial(unsigned char Location,unsigned char mBps,unsigned char mMode) { if(CY==0){ /*T1*/ TMOD=(TMOD&0x0f)|0x20; /*自动重装*/ if(Location){ /*second*/ ACC=mBps; if(ACC7)CKCON|=0x10; /*x3*/ ACC=mBps; if(ACC6)WDCON|=0x80; /*x2*/ } else { /*first*/ ACC=mBps; if(ACC7)CKCON|=0x10; /*x3*/ ACC=mBps; if(ACC6)PCON|=0x80; /*x2*/ } mBps&=0x3f; TL1=TH1=(~mBps)+1; TR1=1; } else { /*T2*/ ACC=mBps; if(ACC7)CKCON|=0x20; /*x3*/ ACC=mBps; if(ACC6)mBps<<=1; /*x2*/ mBps&=0x3f;
if(CKCON&0x20)mBps*=4; else mBps*=12;
TL2= RCAP2L=(~mBps)+1; TH2=RCAP2H=0xff; T2CON|=0x30; /*选择T2为波特率发生器*/ TR2=1; }
if(Location==0){ SCON|=0x40; /*方式8位*/ REN=1; ES=1; } else { ACC=mMode; if(ACC3){ SCON1|=0xc0; /*方式9位*/ } else { SCON1|=0x40; /*方式8位*/ } fSeri1_Odd=ACC4; /*奇偶效验方式,0奇*/
REN_1=1; ES1=1; } }
/************************************************** 串口0中断 **************************************************/ void Int_serial0() interrupt 4 using 1{ // ES=0; /*关 串行口中断*/ if(TI){ TI = 0; /*清 发送中断标志*/ if(Seri0_SendNum==0)fSeri0_Send_Ok=0; else { SBUF=*Seri0_ReadSendPoint;
if(Seri0_ReadSendPoint==(xBuf_Send_0+XBUFSERIAL0SEND-1)) Seri0_ReadSendPoint=xBuf_Send_0; else Seri0_ReadSendPoint++; Seri0_SendNum--; }
}
if(RI){ RI = 0; *Seri0_WriteReciPoint=SBUF; if(Seri0_WriteReciPoint==(xBuf_Reci_0+XBUFSERIAL0RECI-1)) Seri0_WriteReciPoint=xBuf_Reci_0; else Seri0_WriteReciPoint++; Seri0_ReciNum++; }
// ES=1; /*开 中断*/ }
/************************************************** 串口1中断 **************************************************/ void Int_serial1() interrupt 7 using 1{ // ES1=0; /*关 串行口中断*/ if(TI_1){ TI_1 = 0; /*清 发送中断标志*/
if(Seri1_SendNum==0)fSeri1_Send_Ok=0; /*已发送完毕*/ else { if((SCON1&0xc0)==0xc0){ ACC=*Seri1_ReadSendPoint; if(fSeri1_Odd==0)TB8=~P; /*奇效验*/ else TB8=P; /*偶效验*/ }
SBUF1=*Seri1_ReadSendPoint; if(Seri1_ReadSendPoint==(xBuf_Send_1+XBUFSERIAL1SEND-1)) Seri1_ReadSendPoint=xBuf_Send_1; else Seri1_ReadSendPoint++; Seri1_SendNum--; } // SBUF1=0xaa; }
if(RI_1){ RI_1 = 0; *Seri1_WriteReciPoint=SBUF1; if(Seri1_WriteReciPoint==(xBuf_Reci_1+XBUFSERIAL1RECI-1)) Seri1_WriteReciPoint=xBuf_Reci_1; else Seri1_WriteReciPoint++; Seri1_ReciNum++; }
// ES1=1; /*开中断*/ } /*********************************************************** 串口1打包程序 ***********************************************************/ void Seri1_Block() { unsigned char i,j;
if(Seri1_ReciNum==0)return;
/*包*/ if(Seri1_ReciNum>BLOCKMAX)i=BLOCKMAX; else i=Seri1_ReciNum;
/*--------------------------------------*/ Seri0_PushSend(STX); Crc8_ClrSum(); Seri0_PushSend(SERIAL1); Crc8_AddSum(SERIAL1); Seri0_PushSend(i); Crc8_AddSum(i);
while(i>0){ j=Seri1_PopReci(); Seri0_PushSend(j); Crc8_AddSum(j); i--; }
Seri0_PushSend(Crc8_GetSum()); Seri0_PushSend(ETX); }
/************************************************** 串口1接收缓冲清除 **************************************************/ void Seri1_BufCls() { Seri1_ReadReciPoint=xBuf_Reci_1; Seri1_WriteReciPoint=xBuf_Reci_1; Seri1_ReciNum=0; }
/* code unsigned char tTest2[]={0x02,0x00,0x02,0x0C,0x03,0xC3,0x03}; code unsigned char tTest1[]={0x02,0x00,0x02,0x0B,0x03,0x9D,0x03};
void Ser0_Test() { unsigned char i;
for(i=0;i<7;i++){ *Seri0_WriteReciPoint=tTest1[i]; if(Seri0_WriteReciPoint==(xBuf_Reci_0+XBUFSERIAL0RECI-1)) Seri0_WriteReciPoint=xBuf_Reci_0; else Seri0_WriteReciPoint++; Seri0_ReciNum++; }
for(i=0;i<7;i++){ *Seri0_WriteReciPoint=tTest2[i]; if(Seri0_WriteReciPoint==(xBuf_Reci_0+XBUFSERIAL0RECI-1)) Seri0_WriteReciPoint=xBuf_Reci_0; else Seri0_WriteReciPoint++; Seri0_ReciNum++; } } */
#define NONEPARITY 0x0 #define ODDPARITY 0x08 #define EVENPARITY 0x18 #define DATA7BIT 0x80
/*th=256-2^n*f/384/B*/ //#define BPS115200_22M 1|0x40 //#define BPS57600_22M 1 //#define BPS38400_22M 3|0x40 //#define BPS19200_22M 3 //#define BPS9600_22M 6 //#define BPS4800_22M 12 //#define BPS2400_22M 24 //#define BPS1200_22M 48
#define BPS115200_30M_77E58 2|0x80 /**3*/ #define BPS57600_30M_77E58 4|0x80 /**3*/ #define BPS38400_30M_77E58 2 #define BPS19200_30M_77E58 4 #define BPS9600_30M_77E58 8 #define BPS4800_30M_77E58 16 #define BPS2400_30M_77E58 32 |