微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STC8954RD 串口接收

STC8954RD 串口接收

时间: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:%d\n", 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("\ndata\n");

temp = COMGetc();

*str=temp;

//COMPutc(temp);

str++;

}while(temp!=\n && temp!=\r);

*str = \0;

}

//添加两读取超时函数

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=\0;

return 0;

}


//debug 主函数

void main()

{

char comread[20];

char flag = 1;

InitCOM();

COMWriteNum(flag, "flag");

COMWrite("Begin\n");

do

{

COMWrite("Whileloop\n");

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("Begin\n");

do

{

do

{

COMWrite("keyinCommand\n");

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];


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top