RSS

串口中断服务函数集

发布时间:2008-05-05 关键字: 函数 服务 中断   // & free if  char 

ES=1;
return (c); //取回数据
}
//*****************************************
//接收一行数据,必须定义放数据串的指针位置和大小 del=0x7f,backspace=0x08,cr=0x0d,lf=0x0a
void getline (uchar idata *line, unsigned char n) 
{ unsigned char cnt = 0; //定义已接收的长度
char c;
do {
if ((c = getbyte ()) == 0x0d) c = 0x00; //读一个字节,如果是回车换成结束符
if (c == 0x08 || c == 0x7f) //BACKSPACE 和 DEL 的处理
{ if (cnt != 0) //已经输入退掉一个字符
{cnt--; //总数目减一
line--; //指针减一
putbyte (0x08); //屏幕回显的处理
putbyte (‘ ‘);
putbyte (0x08);
}
}
else 
{ putbyte (*line = c); //其他字符取入,回显
line++; //指针加一
cnt++; //总数目加一
}
} while (cnt < n - 1 && c != 0x00 && c!=0x1b); //数目到了,回车或ESC停止 
*line = 0; //再加上停止符0
}
//****************************
//人工输入一个字符到输入缓冲区
putinbuf(uchar c)
{ES=0; if(!inbufful)
{*inlast= c; //放入数据 
inlast++; //最后放入的位置加一 
if (inlast==inbuf+ILEN) inlast=inbuf;//地址到顶部回到底部
if (inlast==getlast) inbufful=1; //接收缓冲区满置满标志
inbufsign=1; 
}
ES=1;
}
//*****************************************
//串口中断处理
serial () interrupt 4 
{ if (TI ) 
{ TI = 0; 
if (outbufsign) 
//if (putlast==outlast) outbufsign=0;
//else 
{SBUF=*outlast; //未发送完继续发送
outlast++; //最后传出去的字节位置加一
if (outlast==outbuf+OLEN) outlast=outbuf;//地址到顶部回到底部
if (putlast==outlast) outbufsign=0; //数据发送完置发送缓冲区空标志 

else outbufsign0=0;
}
if (RI) 
{ RI = 0; 
if(!inbufful)
{
*inlast= SBUF; //放入数据 
inlast++; //最后放入的位置加一 
inbufsign=1; 
if (inlast==inbuf+ILEN) inlast=inbuf;//地址到顶部回到底部
if (inlast==getlast) inbufful=1; //接收缓冲区满置满标志

}
}
}
//*****************************
//串口初始化 0xfd=19200,0xfa=9600,0xf4=4800,0xe8=2400,0xd0=1200
serial_init () {
SCON = 0x50; /* mode 1: 8-bit UART, enable receiver */
TMOD |= 0x20; /* timer 1 mode 2: 8-Bit reload */
PCON |= 0x80; TH1 = 0xfA;//fa, //baud*2 /* reload value 19200 baud */
TR1 = 1; /* timer 1 run */
ES = 1; REN=1; EA=1; SM2=1; //SM2=1时收到的第9位为1才置位RI标志
TMOD |=0x01; //th1 auto load 2X8,th0 1X16
TH0=31; TL0=0; //X 32 =1S
TR0=1; //ET0=1;</P><P>}
//*****************
//测试用主函数 

void main(void)
{char c;
idata unsigned char free[16]; 
unsigned char idata *freep=free; 
serial_init();</P><P>
putstring("jdioptuejls;j;klj");
delay(10);</P><P> while(1) 
{ putstring("com is ready! ");}
c=getbyte();
putbyte(0x20);
puthex(c);
switch(c)
{case ‘r‘:
putbytes(inbuf,ILEN); 
break;
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册