/*********************************************************** * Delay for the given number of microseconds. The driver must * be initialized before calling this function. ***********************************************************/ void udelay(uint32_t usec) { assert((timer_ops != NULL) && (timer_ops->clk_mult != 0U) && (timer_ops->clk_div != 0U) && (timer_ops->get_timer_value != NULL)); uint32_t start, delta, total_delta; assert(usec < (UINT32_MAX / timer_ops->clk_div)); start = timer_ops->get_timer_value(); /* Add an extra tick to avoid delaying less than requested. */ total_delta = div_round_up(usec * timer_ops->clk_div, timer_ops->clk_mult) + 1U; do { /* * If the timer value wraps around, the subtraction will * overflow and it will still give the correct result. */ delta = start - timer_ops->get_timer_value(); /* Decreasing counter */ } while (delta < total_delta); }
#include <stdio.h> int main(int argc, const char *argv[]) { unsigned char a, b, c; a = 0x20; b = 0x00; c = a - b; printf("a(0x%x) - b(0x%x) = %x\n", a, b, c); a = 0x00; b = 0xE0; c = a - b; printf("a(0x%x) - b(0x%x) = %x\n", a, b, c); a = 0x00; b = 0x20; c = a - b; printf("a(0x%x) - b(0x%x) = %x\n", a, b, c); a = 0xF0; b = 0x10; c = a - b; printf("a(0x%x) - b(0x%x) = %x\n", a, b, c); return 0; }
a(0x20) - b(0x0) = 20 a(0x0) - b(0xe0) = 20 a(0x0) - b(0x20) = e0 a(0xf0) - b(0x10) = e0
能夠看到,若是是以無符號數輸出的話,上面的大數減少數和小數減大數的結果是同樣的。git