标题: 单片机向主机发送字符串程序 [打印本页]

作者: 豆浆油条666    时间: 2020-5-11 17:44
标题: 单片机向主机发送字符串程序


/*
名称:单片机向主机发送字符串
说明:单片机按一定时间间隔向主机发送字符串,发送内容在虚拟终端显示
并显示在数码管上
*/        
#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!='\0')
        {
                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...\r\n");
        Puts_to_SerialPort("-------------------------------\r\n");
        DelayMS(50);
        while(1)
        {
                Putc_to_SerialPort(c+'A');
                DelayMS(100);
                Putc_to_SerialPort(' ');
                DelayMS(100);
                if(c==25)//每输出一遍后加横线
                {
                        Puts_to_SerialPort("\r\n-------------------------------\r\n");
                        DelayMS(100);
                }
                c=(c+1)%26;
                if(c%10==0)
                {
                        Puts_to_SerialPort("\r\n");
                        DelayMS(100);
                }
        }
}
单片机向主机发送字符串.zip (22.87 KB, 下载次数: 14)







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1