This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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

0