51单片机串行口中断服务程序

上传人:cn****1 文档编号:493519675 上传时间:2022-12-07 格式:DOCX 页数:5 大小:13.21KB
返回 下载 相关 举报
51单片机串行口中断服务程序_第1页
第1页 / 共5页
51单片机串行口中断服务程序_第2页
第2页 / 共5页
51单片机串行口中断服务程序_第3页
第3页 / 共5页
51单片机串行口中断服务程序_第4页
第4页 / 共5页
51单片机串行口中断服务程序_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《51单片机串行口中断服务程序》由会员分享,可在线阅读,更多相关《51单片机串行口中断服务程序(5页珍藏版)》请在金锄头文库上搜索。

1、51 单片机串行口中断服务程序/串口中断服务程序,仅需做简单调用即可完成串口输入输出的处理编程:聂小猛。该资料来自“51单片机世界” http:/ /出入均设有缓冲区,大小可任意设置。/可供使用的函数名:/char getbyte(void);从接收缓冲区取一个byte,如不想等待则在调用前检测inbufsign是否为1。getline(char idata *line, unsigned char n);获取一行数据回车结束,已处理backspce和delete,必须定义最大输入字符 数/putinbuf(uchar c);模拟接收到一个数据/putbyte(char c);放入一个字节到发

2、送缓冲区/putbytes(unsigned char *outplace,j);放一串数据到发送缓冲区,自定义长度/putstring(unsigned char code *puts);发送一个字符串到串口/puthex(unsigned char c);发送一个字节的hex码,分成两个字节发。/putchar(uchar c,uchar j);发送一个字节数据的asc码表达方式,需要定义小数点的位置/putint(uint ui,uchar j);发送一个整型数据的asc码表达方式,需要定义小数点的位置/CR;发送一个回车换行#include /该头文件包括了5 1 , 52, 8032

3、0的特殊寄存器,用在51, 52上也可#define uchar unsignedchar #define uint unsigned int #define OLEN 64/* size of serial transmission buffer */ idata unsigned char outbufOLEN;/* storage for transmission buffer */unsigned char idata *outlast=outbuf; /最后由中断传输出去的字节位置 unsigned char idata *putlast=outbuf;/最后放入发送缓冲区的字节位置

4、#define ILEN 2 /* size of serial receiving buffer */idata unsigned char inbufILEN;unsigned char idata *inlast=inbuf; /最后由中断进入接收缓冲区的字节位置 unsignedchar idata *getlast=inbuf; /最后取走的字节位置 bit outbufsign; /输出缓冲区非空标志有=1 bit inbufsign;/接收缓冲区非空标志 有=1bit inbufful;/输入缓冲区满标志 满=1#define CR putstring(rn) /CR=回车换行/

5、放入一个字节到发送缓冲区putbyte(char c) uchar i,j; ES=0;/*暂停串行中断,以免数据比较时出错? */if (outlast=putlast ) i=(0-TH1);doi-;j=36;do j-;while(j!=0);while(i!=0);/延时一个字节发送时间*putlast=c; /放字节进入缓冲区 putlast+; /发送缓冲区指针加一 if(putlast=outbuf+OLEN) putlast=outbuf; /指针到了顶部换到底部 if (!outbufsign)outbufsign=1;TI=1; / 缓冲区开始为空置为有,启动发送ES=1

6、; putbytes(unsigned char *outplace,unsigned char j) int i; for(i=0;i4)&0x0f; putbyte(hex_ch); ch=c&0x0f; putbyte(hex_ch); /*从接收缓冲区取一个byte,如不想等待则在调用前检测inbufsign是否为1。uchar getbyte (void) char idata c; while (!inbufsign); /缓冲区空等待ES=0; c= *getlast; /取数据getlast+; /最后取走的数据位置加一inbufful=0; /输入缓冲区的满标志清零if (g

7、etlast=inbuf+ILEN) getlast=inbuf; /地址到顶部回到底部if (getlast=inlast) inbufsign=0; /地址相等置接收缓冲区空空标志,再取数前要检该标志ES=1; return (c);/取回数据 /* /接收一行数据,必须定义放数据串的指针位置和大小 del=0x7f,backspace=0x08,cr=0x0d,lf=0x0avoid getline (uchar idata *line, unsigned char n) unsigned char cnt = 0; /定义已接收的长度 char c;do if (c = getbyte

8、 () = 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); / 数目到了,回车或

9、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;地址到顶部回到底部

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号