Topic : basic C function not working - help please

Forum : 8051

Original Post
Post Information Post
July 8, 2010 - 8:18pm
Guest

I am using a trial version of Ride 7 and I am attempting to develop a project based on earlier 8051 code that I ran in an older version of Ride back in 2004.

The problem appears to be in using a 'while' function. I broke it down to the most basic code as follows;

void wait (void)
{
unsigned int i = 1;

while ( i <= 1000000 );
{
i++;
}
}

void main (void)
{
ALL_OUTPUTS = 0x00; /* ALL_OUTPUTS is P1 */

wait(); /* if I comment this out, the port will flip as expected */

ALL_OUTPUTS = 0xFF;
)

This code builds fine. The simulator runs the routine and P1 toggles to 0x00, but the wait function either never terminates or doesn't run at all as indicated by the failure of P1 to retrun to 0xFF. I have tried inserting a return both in 'wait' and 'main', tried different indenting, and tried embedding the 'while' routine within main with no success. It's as if the while is not seen.
Now mind you, this code is reduced to the most basic function and the more complicated version ran perfectly on an earlier RIDE IDE.

I love the RIDE package as a whole but this issue may force me to seek an alternative tool for my project.

Please help if you have any clues as to what is happening.

Thank you

Replies
Post Information Post
+1
0
-1
July 9, 2010 - 10:40am
Raisonance Support Team

Hi,

Thanks for your report.

RC-51 is a 16-bit compiler, so your "unsigned int i" is limited to 65535 and will rollover to 0. This is a standard C behavior. So the comparison with 1000000 will always see the i variable with a lower value, hence will loop forever.

One solution is to make the i variable an unsigned long.
Another solution is to limit the length to a value lower than 65535 and to call it multiple times (which will lead to a better code than the "long" solution).

Regards

+1
0
-1
July 9, 2010 - 12:51pm
Guest

Thank you. Bruno. for the quick response.

You know, I thought of that after I posted but was not at my workstation to try it. I agree that a multiple call instead of the Unsigned long would be a better fit in this case. I thought I had tried setting i to 31000 so that I would not be contending with an overflow issue in the assignment, but I may have not.

In any case, I will try it this morning and report back with my results.

Thank you again for taking the time to review my problem.

Phil