Jump to content
Sign in to follow this  
jusic

function return problem

Recommended Posts

The problem occurs in this part of code I wrote:

 

result = RS232WaitReply(200, "OK^");	
if(result != 0)
   RS232SendCmd("yes\r\n^");
else if(result == 0)
   RS232SendCmd("no\r\n^");

 

Let's see what there is in asm:

 

;;;snip
call _RS232WaitReply
bcf STATUS, RP0
bcf STATUS, RP1
movwf _result_main
;;;;;;;; if(result != 0)
btfsc STATUS, Z      ;;; Z flag is not updated!
goto label_0004
;;;snip

 

Here is that wait reply function:

 

char RS232WaitReply( short timeout, const char *reply)
{
char rpyptr = 0;
char temp = 0;

for (Time=0; Time<timeout;) // loop until timeout
{
if(RdPtr != RxPtr)
{
temp = RS232BufGetChar();
RS232SendChar(temp);
if( reply[rpyptr] == temp) // if chars match
{
 rpyptr++;
 if(reply[rpyptr] == 0x5E) // reached replystr end?
 {
	 //rpyptr++;////////////////////// magic line

	 return 1;
 }
}
else // if chars don't match
{
 rpyptr=0; // take pointer back
 if( reply[rpyptr] == temp) // it is now the beginning?
 rpyptr++;
}
}
}
return 0;
}

 

In assembler:

 

;;;;snip
;;;;;;;; rpyptr++;////////////////////// magic line
bcf STATUS, RP0
bcf STATUS, RP1
incf _rpyptr_RS232WaitReply, F
;;;;;;;;
;;;;;;;;
;;;;;;;; return 1;
movlw D'1'
goto destrRS232WaitReply
return
;;;;snip

 

Reason: The STATUS,Z (zero) flag won't be updated after function return (by the movlw and/or movwf instruction) and that's why it won't work. The datasheet says about the STATUS,Z bit like this "The result of an arithmetic or logic operation is (not) zero", so it seems that movlw or movwf instructions have no affect on it. My compiler is C2C++ v5.0.6e and PIC I use is PIC16F877.

 

Reproducable: Yeps.

 

Workaround/Fix: ensure that the zero flag will be updated somehow right that it would work before/at the function return. By uncommenting that "magic line" it works rightly.

 

Regards

--Jussi

 

PS. I spotted another small bug related to the IDE and the Line/Column counter. The line number starts from 0 but in compilers it starts from 1. Secondly, when I put tabs (I use 8 in tab width), the Column counter doesn't count them as spaces but the number how many tab "characters" there are.

Share this post


Link to post
Share on other sites

Whops, that last code snip came from the "magic line uncommented version", sorry :)

 

Here's the asm code without the 'magic line':

 

;;;;snip
;;;;;;;; //rpyptr++;////////////////////// magic line
;;;;;;;;
;;;;;;;;
;;;;;;;; return 1;
movlw D'1'
goto destrRS232WaitReply
return
;;;;snip

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...