新闻中心

EEPW首页>嵌入式系统>设计应用> STC8954RD 串口接收

STC8954RD 串口接收

作者: 时间:2016-11-11 来源:网络 收藏
#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 = ;
}

//添加两读取超时函数
bit COMReadTimeout(char* str, unsigned char length, unsigned int timeout)
{
unsigned char i = 0;
unsigned int count = 0;
for(i = 0; i < length; i++)
{
count = 0;
while(RI == 0)
{
count++;
Delay(2);
if (count > timeout) return 1;
}
str[i] = SBUF;
}
return 0;
}
bit COMReadEnterTimeout(char* str, unsigned int timeout)
{
unsigned int count;
char temp;
do
{
count =0;
while(!RI)
{
count++;
_nop_();
_nop_();
if(count>timeout) return 1;
}
temp = SBUF;
*str = temp;
str++;
RI=0;
}while(temp!=n && temp!=r);
*str=;
return 0;
}

//debug 主函数
void main()
{
char comread[20];
char flag = 1;
InitCOM();
COMWriteNum(flag, "flag");
COMWrite("Beginn");
do
{
COMWrite("Whileloopn");
COMReadEnter(comread);
COMWrite(comread);
if(strstr(comread, "QUIT"))flag = 0;
else flag =1;
COMWriteNum(flag, "flag");
}while(flag != 0);
}
//针对超时函数的Debug function
/*
void main()
{
char comread[20];
char flag = 1;
char timeout = 0;
InitPort();
InitCOM();
COMWriteNum(flag, "flag");
COMWrite("Beginn");
do
{
do
{
COMWrite("keyinCommandn");
timeout=COMReadEnterTimeout(comread,0xDfff);
}while(timeout);
//COMReadEnter(comread);
COMWrite(comread);
if(strstr(comread, "QUIT"))flag = 0;
else flag =1;
COMWriteNum(flag, "flag");
}while(flag != 0);
}*/
后来在程式编辑过程中,发生错误如下:
Program Size: data=161.0 xdata=0 code=9892
Target not created
此为data溢出,而xdata却为0
因此可以再定义参数时适当的将部分参数移入xdata中
如 xdata charcomread[20];



关键词:STC8954RD串口接

评论


技术专区

关闭