请问大家这是什么原因导致的
时间:10-02
整理:3721RD
点击:
这是一个串口接收带校验的测试程序,在checksum中,当运行到return 1时就从头开始了而不是进入if,请问大家这是什么原因?
void main()
{
char i=0;
SCON=0x50;
PCON=0x00;
TMOD = 0x20;
TL1 = 0xfd;
TH1 = 0xfd; //2¨ì??ê?a9600
AUXR=0x00;
TR1 = 1;
ES = 1;
EA = 1;
while(1)
{
SendData(0x01);
if(checksum(card)<0)
SendData(0x02);
SendData(0x07);
for(i=0;i<14;i++)
{
SendData(card);
}
}
//SendString("STC15F2K60S2\r\nUart Test !\r\n");
}
uchar Asc2Hex(char i)
{
char temp1;
if((i&0xf0)==0x30)temp1=i&0x0f;
else if((i&0xf0)==0x40)temp1=(i&0x0f)|0x08;
else if(((i&0xf0)!=0x30)&&((i&0xf0)!=0x40))
return 0;
return temp1;
}
uchar checksum(volatile uchar *s)
{
//s++;
char i;
char arrayH[6],arrayL[6],arraysum[6];
SendData(0x03);
for(i=0;i<=5;i++)
{
arrayH=Asc2Hex(s[2*i+1])<<4;
}
SendData(0x05);
for(i=0;i<=5;i++)
{
arrayL=Asc2Hex(s[2*i+2]);
}
SendData(0x04);
for(i=0;i<=6;i++)
{
arraysum=arrayH+arrayL;
}
SendData(0x06);
if(arraysum[5]==arraysum[0]^arraysum[1]^arraysum[2]^arraysum[3]^arraysum[4])
return 0;
//while(busy);
return 1;
}
void main()
{
char i=0;
SCON=0x50;
PCON=0x00;
TMOD = 0x20;
TL1 = 0xfd;
TH1 = 0xfd; //2¨ì??ê?a9600
AUXR=0x00;
TR1 = 1;
ES = 1;
EA = 1;
while(1)
{
SendData(0x01);
if(checksum(card)<0)
SendData(0x02);
SendData(0x07);
for(i=0;i<14;i++)
{
SendData(card);
}
}
//SendString("STC15F2K60S2\r\nUart Test !\r\n");
}
uchar Asc2Hex(char i)
{
char temp1;
if((i&0xf0)==0x30)temp1=i&0x0f;
else if((i&0xf0)==0x40)temp1=(i&0x0f)|0x08;
else if(((i&0xf0)!=0x30)&&((i&0xf0)!=0x40))
return 0;
return temp1;
}
uchar checksum(volatile uchar *s)
{
//s++;
char i;
char arrayH[6],arrayL[6],arraysum[6];
SendData(0x03);
for(i=0;i<=5;i++)
{
arrayH=Asc2Hex(s[2*i+1])<<4;
}
SendData(0x05);
for(i=0;i<=5;i++)
{
arrayL=Asc2Hex(s[2*i+2]);
}
SendData(0x04);
for(i=0;i<=6;i++)
{
arraysum=arrayH+arrayL;
}
SendData(0x06);
if(arraysum[5]==arraysum[0]^arraysum[1]^arraysum[2]^arraysum[3]^arraysum[4])
return 0;
//while(busy);
return 1;
}
调试跟踪一下变量了
在keil中用的软件调试,单步操作到return 1时应该是直接跳到if中的,可是没有。它是将checksum运行完,在跳到main的开头