Jump to content
Sign in to follow this  
2sb18

Conditional Problem

Recommended Posts

Hi,

 

I just got BoostC acouple days ago. I'm having to relearn C as I haven't used it since university. Things are going well, but I have a simple problem:

 

char cursorPosition;

void MoveCursorToDestination(char cursorDestination) {
ScreenReturnHome();
while( cursorDestination > cursorPosition ) {
	ShiftCursorToRight();
}
}

void ShiftCursorToRight() {
SendByteToScreen(shiftToRightByte,0);
cursorPosition++;
}

 

When I call MoveCursorToDestination(4) and cursorPosition is 0, the while loop conditional computes properly the first time and ShiftCursorToRight() is called. Then cursorPosition is incremented to 1, and the for some crazy reason the while loop conditional is computed to be false even though cursorDestination is 4 and cursorPosition is 1. I'm using the "watch" feature to display the values of the variables.

 

Can somebody explain what's happening?

 

Thanks,

Steve.

Share this post


Link to post
Share on other sites

Steve,

 

I couldn't see anything wrong with your code :(

I tried you code, had to edit it a bit as I didn't have all your functions.

I can't see any problems, it works just as expected.

 

Try my code:

 

void MoveCursorToDestination(char cursorDestination);
void ShiftCursorToRight();

void main()
{
    MoveCursorToDestination( 4 );
    while( 1 );
}

char cursorPosition = 0;

void MoveCursorToDestination(char cursorDestination) 
{
   // ScreenReturnHome();
   while( cursorDestination > cursorPosition ) 
   {
       ShiftCursorToRight();
   }
}

void ShiftCursorToRight() 
{
   // SendByteToScreen(shiftToRightByte,0);
   cursorPosition++;
}

 

Regards

Dave

Share this post


Link to post
Share on other sites

Thanks Dave,

 

I'm pretty sure there wasn't anything wrong with my code either. I even used MPLAB SIM to run through the asm and it worked out as expected. When I make it:

 

void MoveCursorToDestination(char cursorDestination) {
//ScreenReturnHome();
while( cursorDestination > cursorPosition ) {
	cursorPosition++;
	//ShiftCursorToRight();
}
}

void ShiftCursorToRight() {
SendByteToScreen(shiftToRightByte,0);
cursorPosition++;
}

 

It doesn't work in the Debug. When I make it

 

void MoveCursorToDestination(char cursorDestination) {
//ScreenReturnHome();
while( cursorDestination > cursorPosition ) {
	ShiftCursorToRight();
}
}

void ShiftCursorToRight() {
SendByteToScreen(shiftToRightByte,0);
cursorPosition++;
}

 

It works in Debug. Either way, it works correctly in the PIC. Am I going insane?!?

Share this post


Link to post
Share on other sites

Steve,

 

I just tried the code you said didn't work, it worked for me in the debugger on a PIC16F84 target.

 

Which PIC are you using ?

 

Regards

Dave

Share this post


Link to post
Share on other sites

I'm using a PIC16F688.

 

Another thing I noticed is that sometimes I hit "build" and it fails, and then I hit hit "build" again and it is successful. This isn't really a problem, I'm just curious why that happens.

 

Also, I just wanted to thank you and Pavel for creating such a good piece of software. It's nice to know that two people with enough gusto can rival (and in many ways do better) a big company like Microchip (their C18 compiler) in terms of quality and price.

 

Thanks,

Steve.

Share this post


Link to post
Share on other sites

Steve,

 

Here is my code which works for me on a PIC16F688 target.

Please take it just as it is, compile it on its own, and let me know if it works. It so simple it can't fail.

 

char cursorPosition = 0;

void MoveCursorToDestination(char cursorDestination);

void main()
{
    MoveCursorToDestination( 4 );
    while( 1 );
}




void MoveCursorToDestination(char cursorDestination) 
{
   //ScreenReturnHome();
   while( cursorDestination > cursorPosition ) 
   {
       cursorPosition++;
   }
}

 

I suspect that maybe you code else where is over running the end of an array and corrupting data.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Yes, that worked.

 

As for arrays, I don't have any. Are their internal arrays created by the compiler that could be overrun? Wouldn't this cause the PIC to operate improperly?

 

Steve.

Share this post


Link to post
Share on other sites

Steve,

 

As for arrays, I don't have any. Are their internal arrays created by the compiler that could be overrun? Wouldn't this cause the PIC to operate improperly?

 

Yes you are right (I forgot you said it was ok on actual device).

 

Please zip up and send a simple project that demonstrates the problem to support@picant.com, and I will take a look.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Steve,

 

With the project files you sent, everything works fine for me.

I just can't see how this is happening to you.

 

Regards

Dave

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...