?? seral-note.txt
字號:
#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 int 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;
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|=0x20; /*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|=0x40; /*x3*/
ACC=mBps;
if(ACC6==0)mBps<<=1; /*x2*/
mBps&=0x3f;
mBps*=6;
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);
}
//Const.h
#define STX 0x02
#define ETX 0x03
#define BLOCKMAX 50
#define NONEPARITY 0x0
#define ODDPARITY 0x08
#define EVENPARITY 0x18
#define DATA7BIT 0x80
#define LOCAL 0
#define SERIAL1 1
#define SERIAL5540 2
#define SERIAL5541 3
#define SERIAL5542 4
#define SERIAL5543 5
#define SERIAL25540 6
#define SERIAL25541 7
#define SERIAL25542 8
#define SERIAL25543 9
#define RIGHT 1
#define ERROR 2
/*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 4|0x80 /**3*/
//#define BPS57600_30M_77E58 8|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
#define XBUFSERIAL0SEND 250
#define XBUFSERIAL0RECI 2000
#define XBUFSERIAL1SEND 1800
#define XBUFSERIAL1RECI 100
#define XBUFSERIAL554RECI 186
sbit ACC0=ACC^0;
sbit ACC1=ACC^1;
sbit ACC2=ACC^2;
sbit ACC3=ACC^3;
sbit ACC4=ACC^4;
sbit ACC5=ACC^5;
sbit ACC6=ACC^6;
sbit ACC7=ACC^7;
//調用
/*初始化內部串行口,0=115200X8,1=9600*9*/
Serial0_Init
(); /*使用T2*/
CY=0;Init_Serial(1,BPS38400_22M,NONEPARITY); /*printer,使用T1*/
#include" w77c32.h"static int flag_1,flag_0;char c1[]="hello china";char c2[]="hello World";main(){ int i; flag_1=1; flag_0=1; //system init ,其中串口0用定時器2,串口1用定時器1 //串口1的設置 IE=0x90; //允許總中斷和串口0的中斷 TMOD=0x20; //定時器1工作在模式1 TL1=0xf4; //baud rate=2400 TH1=0xf4; SCON1=0x58; //工作在模式1,允許接收 PCON=0x00; // SM0=0 SM1=1 在10位異步收發模式 SMOD=0 溢出速率/32 ES1=1; //串口1中斷允許 SCON=0x58; //工作在模式1,接收允許 T2CON=0x30; //用定時器2做串口0的波特率發生器 RCAP2H=0xff; // 11.0592M晶振下,baud rate=2400 RCAP2L=0x70; TR2=1; TR1=1; while(1) { if(flag_0) {TI=1;flag_0=0;} for(i=0;i<1000;i++) ; if(flag_1) {TI_1=1;flag_1=0;} for(i=0;i<1000;i++) ; }} void serial_0(void)interrupt 4{ int i; if(RI) { flag_0=1; RI=0; } if(TI) { for(i=0;i<10;i++) { TI=0; SBUF=c1[i]; while(TI==0) ; TI=0; } }} void serial_1(void)interrupt 7 { int i; if(RI_1) { flag_1=1; RI_1=0; } if(TI_1) { for(i=0;i<10;i++) { TI_1=0; SBUF1=c2[i]; while(TI_1==0) ; TI_1=0; } }}
Serial I/OThe W77E58 has two enhanced serial ports that are functionally similar to the serial port of the original8052 family. However the serial ports on the W77E58 can operate in different modes in order to obtaintiming similarity as well. Note that the serial port 0 can use Timer 1 or 2 as baud rate generator,but the serial port 1 can only use Timer 1 as baud rate generator. The serial ports have theenhanced features of Automatic Address recognition and Frame Error detection.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -