Forum : 8051
Post Information | Post |
---|---|
July 8, 2010 - 8:18pm
|
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) while ( i <= 1000000 ); void main (void) 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. 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 |
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
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