您的位置 首页 产品

STC8954RD 串口接纳

#includestdio.h>#includereg52.h>#includestdlib.h>#includeintrins.h>#includestring.h>/

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

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/xinpin/chanpin/274482.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部