新聞中心

STC8954RD 串口接收

作者: 時(shí)間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
#include
#include
#include
#include
#include
//初始化串口
void InitCOM(void)
{
#if 1
SCON = 0x50; //8bit
TMOD = 0x20; //timer1 mode 2
PCON = 0x00; //SMOD=1, double baud
TL1 = 0xFD;
TH1 = 0xFD; //Baud = 9600, 11.0592MHz
//IE |= 0x90; //Serail break enable
TR1 = 1; //timer1 start
ES = 0;
EA = 0; //disable interrupt
#else
SCON = = 0x50;
TH2 = 0XFF;
TL2 = 0xFD //baud = 115200, 11.0592MHz
RCAP2H = 0xFF;
RCAP2L = 0xFD; //
TCLK = 1;
RCLK = 1;
C-T2 = 0;
TR2 = 1;
#endif
}
//寫串口
void COMPutc(char cdata)
{
SBUF = cdata;
while(!TI);
TI = 0;
}
void COMWrite(char* str)
{
while(*str)
COMPutc(*str++);
}
void COMWriteNum(int num, char* str)
{
char d[32];
sprintf(d, "%s:%dn", str, num);
COMWrite(d);
}
//讀取串口
char COMGetc(void)
{
char temp;
while(!RI);
temp = SBUF;
RI = 0;
return temp;
}
void COMRead(char* str, unsigned char length)
{
unsigned char i = 0;
for(i = 0; i < length; i++)
{
str[i] = COMGetc();
}
}
void COMReadEnter(char* str)
{
char temp;
do
{
//Delay(1);
//COMWrite("ndatan");
temp = COMGetc();
*str=temp;
//COMPutc(temp);
str++;
}while(temp!=n && temp!=r);
*str =