资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
串口发送接收程序模块/* ;* 公 司: xx ;* 模 块: serial.c ;* 功 能: 串口中断服务程序,仅需做简单调用即可完成串口输入输出的处理; ;* 出入均设有缓冲区,大小可任意设置 ;* 芯 片: AMEGA16 ;* 说 明: 未利用串口发送硬件 BUFF ;* 设 计: 李耀峰 ;* 时 间: 2005-11-24 ;* 版 本: V1.0 ;* 记 录: ;*/ /* 可供使用的函数名: extern void PutByte(byte c); /放入一个字节到发送缓冲区 extern void PutString(byte *puts); /发送一个定义在程序存储区的字符串到串口 extern void PutBytes(byte *outplace,byte j); /发送一串数据 extern void PutHEX(byte c); /发送一个字节的hex 码,分成两个字节发 extern byte GetByte (void); /从接收缓冲区取一个byte extern void SerialInit (word baud); /串口初始化 extern byte inbufsign; /接收缓冲区数据,有数据=1。 #define CR PutString(r ) /发送一个回车换行 #define NUL putstring(0) /发送一个空格 */ #include #include #define byte unsigned char #define word unsigned int #define OLEN 20 /串口发送缓冲大小 #define ILEN 20 /串口接收缓冲大小 byte outbufOLEN; /发送缓冲 byte inbufILEN; /接收数据缓冲 byte *outlast=outbuf; /最后由中断传输出去的字节位置 byte *putlast=outbuf; /最后放入发送缓冲区的字节位置 byte *inlast=inbuf; /最后接收到接收缓冲区的字节位置 byte *getlast=inbuf; /最后从发送缓冲区取走的字节位置 struct data /位定义 unsigned bit0:1; unsigned bit1:1; unsigned bit2:1; unsigned bit3:1; unsigned bit4:1; unsigned bit5:1; unsigned bit6:1; unsigned bit7:1; bit_flag; #define outbufsign0 bit_flag.bit0 /缓冲区数据发完标志 发完0 #define outbufsign bit_flag.bit1 /发送缓冲区非空标志 有=1 #define inbufful bit_flag.bit2 /接收缓冲区满标志 满=1 /#define inbufsign bit_flag.bit3 /接收缓冲区非空标志 有=1 /byte outbufsign0; /缓冲区数据发完标志 发完0 /byte outbufsign; /发送缓冲区非空标志 有=1 /byte inbufful; /接收缓冲区满标志 满=1 byte inbufsign; /接收缓冲区非空标志 有=1 #define CR PutString(r ) /CR=回车换行 #define SPACE PutByte(0x20) /发送一个空格。 #pragma interrupt_handler SerialIncept_handler:12 /串口接收中断函数 #pragma interrupt_handler SerialSend_handler:14 /串口发送中断函数 /* /函 数 名: void PutByte(byte c) /功 能: 放入一个字节到发送缓冲区 /说 明: /参 数: /返 回 值: /示 范: PutByte(0x00); /* void PutByte(byte c) CLI(); /暂停串行中断 ,以免数据比较时出错 while(outlast-putlast)=2)&(outlast putlast )|(outlast 4)&0x0f; PutByte(hex_ch); ch=c&0x0f; PutByte(hex_ch); SPACE; /* /函 数 名: byte GetByte (void) /功 能: 从接收缓冲区取一个byte /说 明: 如不想等待则在调用前检测inbufsign是否为1 /参 数: 无 /返 回 值: 接收到的数据 /示 范: i=GetByte(); /* byte GetByte (void) char c ; while (!inbufsign); /缓冲区空等待 CLI(); c=*getlast; /取数据 getlast+; /最后取走的数据位置加 1 inbufful=0; /输入缓冲区的满标志清零 if (getlast=inbuf+ILEN) getlast=inbuf; /地址到顶部回到底部 if (getlast=inlast) inbufsign=0; /地址相等置接收缓冲区空空标志, 再取数前要检该标志 SEI(); return (c); /取回数据 /* /函 数 名: void SerialSend_handler (void) /功 能: 串口发送中断处理 /说 明: /参 数: /返 回 值: /* void SerialSend_handler (void) UCSRA|=(18); UBRRL=(byte)baud; UCSRB=(1RXCIE)|(1TXCIE)|(1RXEN)|(1TXEN); /接收中断使能,发送中断使能,接收器与发送器使能 UCSRC=(1URSEL)|(3UCSZ0); /设置帧格式: 8 个数据位, 1 个停止位*/ SEI(); /开全局中断
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号