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)

Parents
  • 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

Reply
  • 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

Children
No data