《匠人手记》推荐网上购书渠道:
互动出版网(china-pub)购书入口   >>>
当当网(dangdang)购书入口   >>>
卓越亚马逊网 购书入口   >>>
淘宝网(taobao)购书入口   >>>
更多购书渠道……   >>> 

设为首页加入收藏联系匠人管理入口21IC首页21IC博客21IC社区侃单片机回复的贴参与的贴

天气预报
百宝日历
载入中...

百宝专栏

载入中...
最新货色

载入中...

粉丝评论

载入中...

载入中...



百宝信息

载入中...

百宝流量

(2006-07-01开始)


匠人手记

 匠人观点: 好记性不如烂笔头  
 黑色幽默:三鹿门——后世畅想

W77E58 双串口通信源代码
程序匠人 发表于 2005-10-4 20:56:00  阅读全文 | 回复(0) | 引用通告 | 编辑

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

看《匠人手记》,与匠人同行!北航出版,正在热卖!

发表评论:
载入中...

芯片专题

器件专题

软件专题

硬件专题

综合专题

项目专题

原创专题

器件检测
LCD LED
按键 触摸键
E2PROM
电池 电机
电阻 电容 电感

指令系统
软件算法
编程规范
滤波算法
串行通讯

PCB设计
I2C PWM
红外遥控
充电技术
中断 ADC 

匠人手记
匠人夜话
网络心路
一周热点串烧
从零开始玩PIC
DIY旋转时钟

广告5号位 [投放]


学习板、开发板、编程器、下载器、仿真器(查看详情……)

广告3号位 [投放]

站内搜索


站外搜索


百度  google
mp3  歌词 
图片  FLASH 
知道  文档
新闻  词典 
地图  mp3 
软件  天网 
雅虎  爱问 
搜狗  讯雷 
网讯  华军 
天空 

21IC器件搜索
百宝箱分站
  • 《匠人的百宝箱》21IC站
  • 《匠人的百宝箱》21IC笔记团队
  • 《匠人手记》21IC书友会
  • 《匠人的百宝箱》MCUBLOG站
  • 《匠人的百宝箱》MCUBLOG笔记团队
  • 《匠人的百宝箱》EDN站
  • 《匠人手记》EDN书友会
  • 《匠人的百宝箱》与非网站
  • 《匠人的百宝箱》新浪站
  • 《匠人的百宝箱》百度站
  • 《匠人的百宝箱》网易126站
  • 《匠人的百宝箱》网易163站
  • 《匠人的百宝箱》互动出版网站
  • 广告4号位 [投放]

     
     

    匠人原创

    往日酷贴

     
     
     

    大千八卦

    友情连接

    新浪新闻:
    新浪财经:
    AK58新闻:
    新浪股票:
    新浪股票:
    证券之星:

     [更多酷站连接]

     

     

    [欢迎交换连接]

    [百宝箱之与非门分舵]

    [电脑圈圈的家当]

    [IC921的博客]

    [柔月阁]

    [八楼的呼吸]

    [hotpower 的水潭]

    [xwj的文君阁]

    [所长的BLOG]

    [阿摆手记]

    [电子伙伴]

    [unaided的笔记]

    [小飞的笔记]

    [单片机开发联盟]

    [网址之家]

    [好东西网址大全]

    [美萍中文精选]

    [数字电视之家]

    [SMARTCODE电子书斋]

    [软件开发之窗]

    [Armoric]

    [我爱研发网]

    [infernal的笔记]

    [雄鹰的空中加油站]

    [SunK]

    [逍遥电子]

    [ningpanda的博客]

    [C-Design]

    [一网见天下]

    [海边淘沙]

    [嵌入式365]

    [水牛的仓库]

    [股剩是怎样炼成的]

    [PIC论坛]

    [ICC AVR开发网]

    [中国高校自动化网]

     

     

     

    MCU博客-中国电子工程师博客网 

    大学生电子网 

     

     

     

     

     

    !!! 《匠人的百宝箱》 !!!