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

Parents
  • Thanks for your response. I here with add the part where the initiation is done.

    void InitVars(void)
    	{
    	EA = 0;
    	ES = 0;
      	TR0 = 0;
    	TR1 = 0;
    	TR2 = 0;
    	MainConveyorMotor = 0;
    	check = 0;
    	PacketSize = 0;
    	SensorDelay = 1000;
    	TransportDistance = 0x2E;
    	}
    
    This is called from the main function.
    To be complete I also add the variable and function definitions which are in a separate "c" file.

    unsigned char *Stringbuffer;
    
    extern unsigned char DecodeStringData(void);
    extern unsigned short calculateChecksum(unsigned char *pBuffer,int iSize);
    extern void SendComm(unsigned char *Stringbuffer,unsigned char Stringlength);
    extern void EncodePacket(unsigned char * pBuffer);
    extern void InitVars(void);
    extern void com_baudrate(void);
    extern void CommandAction(void);
    
    
    unsigned char xdata PusherOutput1 		_at_ 0x2000;
    unsigned char xdata PusherDirection1 	_at_ 0x4000;
    unsigned char xdata PusherPosInput1 	_at_ 0x8000;
    unsigned char xdata DipSwitchSetting	_at_ 0xC000;
    unsigned char xdata DistanceSwSetting	_at_ 0xA000; 		// hex switches
    
    //------------- Pointers --------------------
    
    extern unsigned char *Stringbuffer;
    
    //------------- Bits -----------------------
    
    //----------- Chars -------------------------
    
    idata unsigned char PacketSize;
    idata unsigned char Stringlength;
    
    //------------ Integers ---------------------
    
    //unsigned int count;  	  				// incremented from ticktimer for general timing
    idata unsigned int SensorDelay;
    idata unsigned int TransportDistance;
    
    //------------ Arrays -----------------------
    
    
    xdata unsigned char TempString[128];
    xdata unsigned char ReceivedPacket[128];
    xdata unsigned char TransmitPacket[128];
    xdata unsigned char Data_1[24];
    xdata unsigned char Data_2[24];
    xdata unsigned char Command[8];
    

    Hopefully this will help define the problem. Thanks for the help

    Regards
    John Garrelts

Reply
  • Thanks for your response. I here with add the part where the initiation is done.

    void InitVars(void)
    	{
    	EA = 0;
    	ES = 0;
      	TR0 = 0;
    	TR1 = 0;
    	TR2 = 0;
    	MainConveyorMotor = 0;
    	check = 0;
    	PacketSize = 0;
    	SensorDelay = 1000;
    	TransportDistance = 0x2E;
    	}
    
    This is called from the main function.
    To be complete I also add the variable and function definitions which are in a separate "c" file.

    unsigned char *Stringbuffer;
    
    extern unsigned char DecodeStringData(void);
    extern unsigned short calculateChecksum(unsigned char *pBuffer,int iSize);
    extern void SendComm(unsigned char *Stringbuffer,unsigned char Stringlength);
    extern void EncodePacket(unsigned char * pBuffer);
    extern void InitVars(void);
    extern void com_baudrate(void);
    extern void CommandAction(void);
    
    
    unsigned char xdata PusherOutput1 		_at_ 0x2000;
    unsigned char xdata PusherDirection1 	_at_ 0x4000;
    unsigned char xdata PusherPosInput1 	_at_ 0x8000;
    unsigned char xdata DipSwitchSetting	_at_ 0xC000;
    unsigned char xdata DistanceSwSetting	_at_ 0xA000; 		// hex switches
    
    //------------- Pointers --------------------
    
    extern unsigned char *Stringbuffer;
    
    //------------- Bits -----------------------
    
    //----------- Chars -------------------------
    
    idata unsigned char PacketSize;
    idata unsigned char Stringlength;
    
    //------------ Integers ---------------------
    
    //unsigned int count;  	  				// incremented from ticktimer for general timing
    idata unsigned int SensorDelay;
    idata unsigned int TransportDistance;
    
    //------------ Arrays -----------------------
    
    
    xdata unsigned char TempString[128];
    xdata unsigned char ReceivedPacket[128];
    xdata unsigned char TransmitPacket[128];
    xdata unsigned char Data_1[24];
    xdata unsigned char Data_2[24];
    xdata unsigned char Command[8];
    

    Hopefully this will help define the problem. Thanks for the help

    Regards
    John Garrelts

Children
  • MainConveyorMotor = 0;
    check = 0;
    PacketSize = 0;

    this seems to indicate that you have done something "funny" with startup.a51, have you?

    AT89C51ED2
    another possibility is that you do not set the SFR that indicates "use internal RAM" in the very beginning of startup.a51. If you are in the C is C mode and set it in the beginning of main() you will lose all initialization of internal RAM.

    I know this should not affect idata, but the initialization code does strange things when it talks to "open" RAM (justifiably so).

    Erik

  • "AT89C51ED2
    another possibility is that you do not set the SFR that indicates "use internal RAM" in the very beginning of startup.a51. If you are in the C is C mode and set it in the beginning of main() you will lose all initialization of internal RAM."

    This device defaults to using internal RAM from addresses 0-0x2ff, but can be configured for internal RAM up to 0x6ff.