?? w77e58的雙串口通信源代碼.txt
字號:
W77E58 雙串口通信源代碼
注:此源程序只作參考,內(nèi)有些函數(shù)未有原型,借鑒在于其通信方式及其設(shè)置。
CRC檢校可另參考其它代碼。
#include <W77E58.h>
#include <Const.h>
#include <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;
/**************************************************
壓入單數(shù)據(jù)串口0發(fā)送隊列
**************************************************/
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接收隊列單數(shù)據(jù)
**************************************************/
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接收隊列指定序號數(shù)據(jù)
**************************************************/
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;
/**************************************************
壓入單數(shù)據(jù)串口1發(fā)送隊列
**************************************************/
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接收隊列單數(shù)據(jù)
**************************************************/
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為波特率發(fā)生器*/
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; /*關(guān)
串行口中斷*/
if(TI){
TI = 0; /*清
發(fā)送中斷標(biāo)志*/
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; /*關(guān)
串行口中斷*/
if(TI_1){
TI_1 = 0; /*清
發(fā)送中斷標(biāo)志*/
if(Seri1_SendNum==0)fSeri1_Send_Ok=0; /*已發(fā)送完畢*/
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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -