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

Keil APP NOTE 105

Refer to this app note, page 4. The routine to delay for a while. Rignt or wrong, this while will never end if 65535 - startcount is less than count?(ie..the number of ticks needed for the delay is more than the number the variable can increase before overflowing back to zero)

  • I think that what you're saying is only true if we were to ignore the actual data type of the variable being passed to timer0_delay. Because this variable is declared as unsigned and timer0_count returns type unsigned and start_count is type unsigned, this function will never go into an endless loop. What might happen, however, is that if you were to do something like cast an unsigned long to an unsigned int, its value would be changed and the function would not delay as long as you might have wanted.

  • Refer to this app note, page 4. The routine to delay for a while. Rignt or wrong, this while will never end if 65535 - startcount is less than count?(ie..the number of ticks needed for the delay is more than the number the variable can increase before overflowing back to zero)

    That's not at all correct.

    The type of all variables used is unsigned. There are no negative numbers with unsigned types.

    For example, suppose we want to delay for 100 counts and that the current timer tick is 65530.

    This means that we need to delay until the timer tick is 95. Now, we all know that

    95 - 65530 = -65435

    However, you can't represent negative numbers in unsigned types. In an unsigned type 0 - 1 is 65535 (not -1). Actually, it-s 65536 + -1.

    So, 95 - 65530 is 65536 + -65435 which is 101. And that's correct (since we delay while the different is <= 100 -- 101 is the first number that is > 100).

    Jon