51单片机串口调试程序 点击:1388 | 回复:4



PCER

    
  • 精华:1帖
  • 求助:0帖
  • 帖子:9帖 | 11回
  • 年度积分:0
  • 历史总积分:56
  • 注册:2006年3月04日
发表于:2006-09-15 13:20:00
楼主


/*定义为中断方式串口处理*/
#define INTERSENDSTR
/*不为8032系列芯片 */ 
//#define CHIP_8032 

#include <absacc.h>
#include <reg51.h>
#include <string.h>
#include "err.h"


#ifdef INTERSENDSTR
unsigned char xdata sSendComBuf[256],sRecComBuf[256];
unsigned char data bOut,bIn,bSout,bSin;
#else
#define DEFSENDCHAR TI=0;SBUF=ACC;while(!TI);TI=0;
#endif
//bps设置 9600就是 PSetBps(96)(11.0592Mhz) 

unsigned char  PSetBps(unsigned int bps) SMALL
{
unsigned int t1;
unsigned char t2;
#ifdef INTERSENDSTR
/*如果使用中断方式发送数据,必须等待发送数据完毕才能更换波特率 */
while(bSin!=bSout)
{
;
}
ES=0;
#endif 

#ifdef CHIP_8032
if(3456%bps)
{
return ERR_SET_BPS;
}
t1=3456/bps;
t1--;t1=~t1;
RCAP2H=t1/256;RCAP2L=t1;
T2MOD=0x00;
/*使用定时器2做波特率发生器 */
T2CON=0x34;/*TF2=0;EXF2=0;RCLK=1;TCLK=1;EXEN2=0;TR2=1;C/T2=0;CP/RL2=0
*/
#else
t1=576/bps;
if((576%bps)||((t1==0))||(t1>=2*256)) 
{
return ERR_SET_BPS;
}
if(t1>256)
{
PCON=00;
t2=256-(288/bps);
}
else
{
PCON=0x80;
t2=256-t1;
}
TH1=t2;TL1=t2;
TR1=1;
#endif 
/*模式3*/
SM0=0;
RI=0;TI=0;
REN=1;
SM1=1;
SM2=1;
#ifdef INTERSENDSTR 
bOut=bIn;
bSout=bSin;
ES=1;
#endif 
return OK;
}
void PSendChar(unsigned char ch) SMALL
{

#ifdef INTERSENDSTR
unsigned char tch;

tch=bSin+1;
while(tch==bSout)
{
;
}
sSendComBuf[bSin]=ch;
ES=0;
if((bSout==bSin))
{
SBUF=sSendComBuf[bSin];
bSin++;
}
else
{
bSin++;               
}
ES=1;
#else 
ACC=ch;
DEFSENDCHAR;
#endif 
}
unsigned char PGetChar() SMALL
{
unsigned char ch;
#ifdef  INTERSENDSTR
ch=sRecComBuf[bOut];
bOut++;
return ch;
#else
ch=SBUF;
RI=0;
#endif 
}
bit PCharInCom() SMALL
{
#ifdef  INTERSENDSTR
if(bIn!=bOut)
{
return 1;
}
return 0;
#else
return RI;
#endif
}
void PSendString(unsigned char *st) SMALL
{
while(*st)
{
PSendChar(*st);
st++;
}
}
void PSendArray(unsigned char  *st,unsigned int  len) SMALL
{
unsigned int ii;
for(ii=0;ii<len;ii++)
{
PSendChar(st[ii]);
}
}
#ifdef  INTERSENDSTR
void PSerialInt() interrupt 4
{
if(RI) 
{
sRecComBuf[bIn]=SBUF;
bIn++;
RI=0;
}
if(TI)
{
TI=0;
if(bSout!=bSin)
{
bSout++;
}
if(bSout!=bSin)
{
SBUF=sSendComBuf[bSout];
}
}
}
#endif
[url=http://changxingfly.icpcn.com]http://changxingfly.icpcn.com[/url]



周公

  • 精华:10帖
  • 求助:0帖
  • 帖子:119帖 | 5549回
  • 年度积分:0
  • 历史总积分:6446
  • 注册:2002年11月26日
发表于:2006-09-24 17:45:00
1楼
上贴下面的链接是该生的求职主页

super star

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 23回
  • 年度积分:0
  • 历史总积分:29
  • 注册:2006年8月24日
发表于:2006-09-27 12:55:00
2楼
谢谢

PCER

  • 精华:1帖
  • 求助:0帖
  • 帖子:9帖 | 11回
  • 年度积分:0
  • 历史总积分:56
  • 注册:2006年3月04日
发表于:2006-09-29 10:18:00
3楼
是啊
那位老板缺人手 愿为其效劳

ottor

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 1回
  • 年度积分:0
  • 历史总积分:4
  • 注册:2006年9月29日
发表于:2006-09-29 19:37:00
4楼
同道中人!

热门招聘
相关主题

官方公众号

智造工程师