当前位置:首页 > 单片机 > 单片机
[导读]/* 名称:单片机向主机发送字符串说明:单片机按一定的时间间隔向主机发送字符串,发送内容在虚拟终端显示。*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint ms)

/*  名称:单片机向主机发送字符串

说明:单片机按一定的时间间隔向主机

发送字符串,发送内容在虚拟终端显示。

*/

#include<reg51.h>

#define uchar unsigned char

#define uint unsigned int

//延时

void DelayMS(uint ms)

{

 

            uchar i;

while(ms--) for(i=0;i<120;i++);

}

//向串口发送字符

void Putc_to_SerialPort(uchar c)

{

SBUF=c;

while(TI==0);

TI=0;

}

//向串口发送字符串

void Puts_to_SerialPort(uchar *s)

{

while(*s!='')

{

Putc_to_SerialPort(*s);

s++;

DelayMS(5); 

}

}

//主程序

void main()

{

uchar c=0;

SCON=0x40; 串口模式 1  

TMOD=0x20; //T1 工作模式 2

TH1=0xfd;  //波特率 9600

TL1=0xfd;

PCON=0x00; 波特率不倍增

TI=0;

TR1=1;

DelayMS(200);

//向主机发送数据

Puts_to_SerialPort("Receiving From 8051...rn");

Puts_to_SerialPort("-------------------------------rn");

DelayMS(50);

while(1)

{

Putc_to_SerialPort(c+'A');

DelayMS(100);

Putc_to_SerialPort(' ');

DelayMS(100);

if(c==25) //每输出一遍后加横线

Puts_to_SerialPort("rn-------------------------------rn"); 

DelayMS(100); 

}

 c=(c+1)%26;

  }

}

if(c%10==0)  //每输出 10 个字符后换行

Puts_to_SerialPort("rn");

DelayMS(100);

 

     } 

扩展阅读:单片机之间双向通信程序

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭