Index: Utilities.c =================================================================== diff -u -rbe82a1c66a83e49efdf3ddbc288ae8560d678cc2 -r2932935752882cd7bc9808bfe238b03753d3e167 --- Utilities.c (.../Utilities.c) (revision be82a1c66a83e49efdf3ddbc288ae8560d678cc2) +++ Utilities.c (.../Utilities.c) (revision 2932935752882cd7bc9808bfe238b03753d3e167) @@ -176,6 +176,55 @@ return result; } +/*********************************************************************//** + * @brief + * The u16DiffWithWrap function calculates the difference between two given + * unsigned 16-bit numbers. If the second (ending) number is less than the + * first (starting) number, then the difference is calculated with a wrap past + * 16-bit full scale to zero. + * @details Inputs: none + * @details Outputs: none + * @param start first number to compute difference for + * @param end second number to compute difference for + * @return the difference between the two given numbers + *************************************************************************/ +U16 u16DiffWithWrap( U16 start, U16 end ) +{ + U16 result = ( end >= start ? end - start : ( HEX_64_K - start ) + end ); + + return result; +} + +/*********************************************************************//** + * @brief + * The u16BiDiffWithWrap function calculates the difference between two given + * unsigned 16-bit numbers. This version of function returns a signed value + * to allow for a negative difference. + * @details Inputs: none + * @details Outputs: none + * @param start first number to compute difference for + * @param end second number to compute difference for + * @return the difference between the two given numbers + *************************************************************************/ +S16 u16BiDiffWithWrap( U16 start, U16 end ) +{ + S16 result; + + U16 incDelta = ( end >= start ? end - start : ( HEX_64_K - start ) + end ); + U16 decDelta = HEX_64_K - incDelta; + + if ( incDelta < decDelta ) + { + result = (S16)incDelta; + } + else + { + result = (S16)decDelta * -1; + } + + return result; +} + /*********************************************************************//** * @brief * The initTimeWindowedCount function initializes a given time windowed count.