EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
单片机C语言程序设计:单片机向主机发送字符串
7 P: v7 E! ?2 y4 O: }6 P! K# c6 m$ R: F; n) ?0 w2 Y
, o; W/ B! h6 }3 `
/* 名称:单片机向主机发送字符串 说明:单片机按一定的时间间隔向主机 发送字符串,发送内容在虚拟终端显示。 */ #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++); 1 E% J- @) P3 O9 Z8 B
} //向串口发送字符 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 ! O& {' B( a1 C- P5 T; i( h- r# e
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); ! ?0 K+ b! a/ `( g3 O/ z8 ?, n' M
} c=(c+1)%26; } }
; [; z9 w6 G; ]9 a0 k' J# y* X; f / V1 \- \7 N2 q7 d8 r
if(c%10==0) //每输出 10 个字符后换行 { Puts_to_SerialPort("rn"); DelayMS(100); }
. c/ H" n; i2 o, R! R
; ^: ~7 H; C2 k: ^1 o
- |# }$ \9 j* T$ r. J5 @3 r! _ f' x; z4 Y
, T4 h/ W7 j' z5 w, \ |