//欢迎您再次光临:http://www.bqmcu.com.cn
/***************************************************
说明:串口通信头文件
作者:独步雨林
email:[email protected]
***************************************************/
#ifndef UART_H //避免重复LINK
#define UART_H
/****************函数声明*************************/
void InitCom(void); //初始化,修改此函数工作于所需方式
void PutChar(unsigned char Char); //发送单字符
unsigned char GetChar(void); //接收单字符
void PutStr(unsigned char *Str); //发送字符串,带结束符'\0'
void PutStrA(unsigned char *Str,unsigned char Count); //发送字符串,不带结束符
void GetStr(unsigned char *Str,unsigned char Count); //接收字符串
unsigned char StrLen(unsigned char *Str); //获取字符串长度
/******************************************************
功能:串口初始化
******************************************************/
void InitCom(void)
{
SCON=0x50; //在11.0592MHz下,设置串行口波特率为9600,方式1,并允许接收
PCON=0X00; //波特率不加倍
TMOD=0x20; //定时器工作于方式2,自动装载方式
TH1=0xfd;
TR1=1;
TI=0;
RI=0;
}
/******************************************************
功能:发送单字符
入口参数:待发送的字符
出口参数:无
******************************************************/
void PutChar(unsigned char Char)
{
SBUF=Char;
while(!TI);
TI=0;
}
/******************************************************
功能:接收单字符
入口参数:无
出口参数:接收到的字符
******************************************************/
unsigned char GetChar(void)
{
unsigned char Char;
while(!RI);
Char=SBUF;
RI=0;
return Char;
}
/******************************************************
功能:发送字符串
入口参数:待发送的字符串的首地址
出口参数:无
******************************************************/
void PutStr(unsigned char *Str)
{
while(*Str!='\0')
{
PutChar(*Str);
Str++;
}
}
/******************************************************
功能:发送字符串
入口参数:待发送的字符串的首地址,发送字符个数
出口参数:无
******************************************************/
void PutStrA(unsigned char *Str,unsigned char Count)
{
while(Count--)
{
PutChar(*Str);
Str++;
}
}
//******************************************************
功能:接收字符串
入口参数:字符串的首地址,接收个数
出口参数:无
******************************************************/
void GetStr(unsigned char *Str,unsigned char Count)
{
while(Count--)
{
*Str=GetChar();
Str++;
}
*Str='\0';
}
/******************************************************
功能:字符串字符个数
入口参数:字符串的首地址
出口参数:字符个数<256
******************************************************/
unsigned char StrLen(unsigned char *Str)
{
unsigned char Length=0;
while( (*Str!='\0') )
{
Str++;
Length++;
if(!Length) break;
}
return Length;
}
#endif