Thanks to integral promotion, a C compiler might treat the intermediate
value (t1 - t0) as a larger signed value when t1 and t0 are unsigned
values. Cast the intermediate result to unsigned in these cases, as
this is necessary to get the correct result in the case of timer value
roll-over.
TMR0IF = 0;
_tmr_ticks++;
for (tmr_bitno_t t = 0; t < TMR_COUNT; t++) {
TMR0IF = 0;
_tmr_ticks++;
for (tmr_bitno_t t = 0; t < TMR_COUNT; t++) {
- if (bit_get(_tmr_on, t) && _tmr_ticks - _tmr_t0[t] >= _tmr_elapsed[t]) {
+ if (bit_get(_tmr_on, t) &&
+ (tmr_time_t)(_tmr_ticks - _tmr_t0[t]) >= _tmr_elapsed[t]) {
bit_set(_tmr_flag, t);
if (bit_get(_tmr_periodic, t))
_tmr_t0[t] += _tmr_elapsed[t];
bit_set(_tmr_flag, t);
if (bit_get(_tmr_periodic, t))
_tmr_t0[t] += _tmr_elapsed[t];
/* Wait for c clocks, 0 <= c < 128. */
#define tmr_cwait(c) \
do { \
/* Wait for c clocks, 0 <= c < 128. */
#define tmr_cwait(c) \
do { \
- unsigned t0 = TMR0; \
- while (TMR0 - t0 <= c); \
+ unsigned char t0 = TMR0; \
+ while ((unsigned char)(TMR0 - t0) <= c); \
} while (0)
/* Wait for a number of us. This is pretty accurate. */
} while (0)
/* Wait for a number of us. This is pretty accurate. */