51单片机串口接受协议

mxd12345678 2009-10-20 02:22:50
#include "reg51.h"
#include <string.h>

typedef unsigned char BYTE;
unsigned char *str;//发送字符串指针
unsigned char inbuf1[5];//接收缓存
unsigned char strbuf1=0;//接收缓存指针
void init_serialcomm(void)
{
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD = 0x20; //TMOD: timer 1, mode 2, 8-bit reload
PCON = 0x80; //SMOD=1;
TH1 = 0xF4; //Baud:4800 fosc=11.0592MHz
IE = 0x90; //Enable Serial Interrupt
TR1 = 1; // timer 1 run
// TI=1;
}
void serial () interrupt 4 using 3//串口中断
{
if(RI)
{RI=0;//接收中断
inbuf1[strbuf1]=SBUF;
if (SBUF=='\n')
{str =&inbuf1;//直接附地址
SBUF=*str;}
if (strbuf1==5)
{strbuf1=0;}
else
{strbuf1++;}
}
else
{TI=0;//发送中断
if (str != 0)
{ str ++ ;}//后移指针
if (*str != '\0')//判断字符串的结尾
{SBUF=*str ;}
else
{str=0;}
}
}

void FaSong(unsigned char *st)// 发送字符串
{
str=st;//吧字符串的首地址保存
SBUF=*str;//剩下的事交给中断程序
}

main()
{
init_serialcomm(); //初始化串口

while(1)
{
while(str!=0){}//等待串口发送不忙
FaSong("串口发送实验\n");
while(str!=0){}
FaSong("可以连续发送\n");
while(str!=0){}
FaSong("只需一个函数\n");
while(str!=0){}
FaSong("Bye! \n");

while(str!=0){}//等待串口发送不忙
str =&"不用调用函数\n";//直接附地址
SBUF=*str;
while(str!=0){}
str =&"直接附地址\n";//直接附地址
SBUF=*str;
}
}

有哪位大虾帮忙帖一下接收协议的实现
...全文
97 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
brucegong 2009-10-20
  • 打赏
  • 举报
回复






如果你不需要搞什么牛B的事情




/************************************************************
* Uart Program
* Donald, DONG ELECT. LAB.
* 2008.5.13 [complete]
* --------------------------------------------------
* 在PC上通过超级终端或者其它串口调试工具,将键盘按下的值显示
* 在终端上,并将输入的字符串再次输出到终端上。
*
* P30: RXD
* P31: TXD
***********************************************************/
#include <reg52.h>
#include <stdio.h>

typedef unsigned char uchar;
typedef unsigned int uint;

//#define CMD_LEN 75 // 命令长度
//#define CMD_TAG 1 // 提示符长度

//void UartInit();
//void UartSendChar(unsigned char ch);
//void UartSendStr(uchar *pStr);
//void UartCharPro(uchar ch);
//void UartCmdPro();

//uchar g_ucCmd[CMD_LEN + 1] = {0};
//uchar g_ucLen = 0; // 命令长度
//uchar g_ucCur = 1; // 光标位置
uchar buf[26];

void delay_ms(uchar n)
{
uchar i,j;
for(i=n;i>0;i--)
for(j=0;j<200;j++);
}

void main()
{
SCON= 0x50;
TMOD |= 0x20;
TH1= 0xf3;
TR1= 1;
TI= 1;


while(1)
{
scanf("%25s",buf);
printf("\r\n-- %s\r\n",buf);

}

}







gelu1040 2009-10-20
  • 打赏
  • 举报
回复

int getdata(char *buff,int len)
{
int count=0;
while(receive_buff_full&&(!timeout))
{
readbuff(buff);
count+=buff_len;
if(count>=len)
break;
}
return count;
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧