*—————————————-
ComFun.c
C51串口输出输入函数
外部声明 ComFun.h
—————————————-*/
#include
//串口初始化 晶振为11.0592M 方法1 波特率300-57600
void InitCom(unsigned char BaudRate)
{
unsigned char THTL;
switch (BaudRate)
{
case 1: THTL = 64; break; //波特率300
case 2: THTL = 160; break; //600
case 3: THTL = 208; break; //1200
case 4: THTL = 232; break; //2400
case 5: THTL = 244; break; //4800
case 6: THTL = 250; break; //9600
case 7: THTL = 253; break; //19200
case 8: THTL = 255; break; //57600
default: THTL = 208;
}
SCON = 0x50; //串口方法1,答应接纳
TMOD = 0x20; //守时器1守时方法2
TCON = 0x40; //设守时器1开端计数
TH1 = THTL;
TL1 = THTL;
PCON = 0x80; //波特率加倍操控,SMOD位
RI = 0; //清收发标志
TI = 0;
TR1 = 1; //发动守时器
}
//向串口输出一个字符(非中止方法)
void ComOutChar(unsigned char OutData)
{
SBUF = OutData; //输出字符
while(!TI); //空语句判别字符是否发完
TI = 0; //清TI
}
//向串口输出一个回车换行
void ComOutEnter(void)
{
SBUF = 0x00;
while(!TI);
TI = 0;
SBUF = 0x0A;
while(!TI);
TI = 0;
}
//向串口输出一个字符串(非中止方法)
void ComOutStr(unsigned char *Str)
{
while(*Str != 0x00) //判别是否到了字符串的尾部
{
SBUF = *Str; //输出字符
while(!TI); //空语句判别字符是否发完
TI = 0; //清TI
Str++; //字符串指针加一
}
}
//从串口接纳一个字符(非中止方法)
unsigned char ComInChar()
{
unsigned char InData;
while(!RI); //空语句判别字符是否收完
InData = SBUF; //保存COM缓冲字符
RI = 0; //清RI
return (InData); //回来收到的字符
}
//从串口接纳一个定长字符串(非中止方法,只用于字符需有一个的idata字符串数组)
//如unsigned char *Str;
//unsigned char i[]=”abcd”;
//InHandStr = &i;
void ComInStr(unsigned char *Str)
{
while(*Str != 0x00) //判别是否到了字符串的尾部
{
while(!RI); //空语句判别字符是否收完
*Str = SBUF; //保存字符
RI = 0; //清RI
Str++; //字符串指针加一
}
}