Variable over written

Dear all,

I am completely stumped. I have a piece of code in which (normally) the process will not enter in to. It concerns a uart routine where it enters into the routine if a packet size has been detected. For some reason it enters into the routine even if there is no comm data. The variable(PacketSize) is previously initialized to "0".
When the target device is powered up, I should have no output on the comm port. I do not recieve normal string data but garbage. Due to it sending anything at all I conclude that the variable PacketSize must be over written at some point. I have read on previous postings that it could have something to do with the pointers. I just can't figure out what. By the way, the simulator runs perfectly, the target device (AT89C51ED2) in a live enviroment gives the problem. Any advise will be welcome. Sorry if I'm not very clear in explaining the problem, it just that it's not that clear to me either.


void main(void)
	{
	InitVars();
	com_baudrate ();
	while(1)
		{
	  	if (PacketSize > 0)
			{
			if (DecodeStringData())
				{
				MainConveyorMotor = 1;
				SendComm("Hello", 0);
				}
			PacketSize = 0;
			}

		}
	}





unsigned char DecodeStringData(void)
	{
	unsigned char i;
	unsigned short 	shReadCheckSum;
	unsigned char		chMode;
	unsigned char		chPos;
	unsigned short		shChecksum;
	chMode = 0;
	chPos = 0;

	SBUF = PacketSize + '0';
	shChecksum = calculateChecksum(&ReceivedPacket[1], PacketSize - 6);
	shReadCheckSum = 0;
	for (i = 1; i < (PacketSize - 1);i++)
		{
		if (chMode == 0)
			{
			// get command
			if ((ReceivedPacket[i] == ',') ||
		       (ReceivedPacket[i] == ';'))
				{
			    Command[chPos] = 0;
				 chPos = 0;
				 if (ReceivedPacket[i] == ',')

etc....etc....

void SendComm(unsigned char * pStringbuffer,unsigned char Stringlength) reentrant
	{
	idata unsigned char Ctr;

	if (Stringlength != 0)
		{
		for(Ctr=0; Ctr<Stringlength; Ctr++)
   		{
			tbuf[t_in++] = pStringbuffer[Ctr];
			}
	 }
	 else
		{
		for(Ctr=0; Ctr < 256; Ctr++)
   		{
			if (pStringbuffer[Ctr] == 0)
				{
				break;
				}
			tbuf[t_in++] = pStringbuffer[Ctr];
			}
		}
	if( t_in != t_out)
		{
		TI = 1;
		}
	}

Thanks in advance for any advise.

Regards
John Garrelts

Parents
  • Your code is quite strange. The Decode() function writes to SBUF, which seems odd, and it doesn't wait for TI, which may cause garbled serial output.

    Your SendComm() function, or at least the part of it you have shown, doesn't seem to send anything, but sets TI. Have you got a mixture of interrupt driven and polled serial comms going on here?

    You don't show us how PacketSize normally gets modified. Does this happen in an ISR?

    "idata unsigned char Ctr;
    for(Ctr=0; Ctr < 256; Ctr++)"

    This is an infinite loop.

    The SendComm function is declared reentrant. I've never used a reentrant function, but doesn't it mean locals are stored on a stack in XDATA? How does using an idata local fit in with that?

    Note that the simulator does not behave the same as real code when dealing with the UART. It is quite possible to see good stuff in the serial window and see garbage on the target for the same code.

    If your complete program isn't too big I think you ought to post the whole thing so we can see what's going on. Please detail any changes you have made to startup.a51, also which memory model you are using.

Reply
  • Your code is quite strange. The Decode() function writes to SBUF, which seems odd, and it doesn't wait for TI, which may cause garbled serial output.

    Your SendComm() function, or at least the part of it you have shown, doesn't seem to send anything, but sets TI. Have you got a mixture of interrupt driven and polled serial comms going on here?

    You don't show us how PacketSize normally gets modified. Does this happen in an ISR?

    "idata unsigned char Ctr;
    for(Ctr=0; Ctr < 256; Ctr++)"

    This is an infinite loop.

    The SendComm function is declared reentrant. I've never used a reentrant function, but doesn't it mean locals are stored on a stack in XDATA? How does using an idata local fit in with that?

    Note that the simulator does not behave the same as real code when dealing with the UART. It is quite possible to see good stuff in the serial window and see garbage on the target for the same code.

    If your complete program isn't too big I think you ought to post the whole thing so we can see what's going on. Please detail any changes you have made to startup.a51, also which memory model you are using.

Children
More questions in this forum