Micros and the check for less than 255

Looking at the micros() core code I see a check for the timer value to be less than 255. What is the purpose of that check. I found the bug that it was trying to fix but still don't understand it. I was hoping to write similar code for timer1 for a project I'm working on and wondering if I need to include something like this to handle combining the timer value and overflow count together.

Source code in question: ArduinoCore-avr/wiring.c at 24e6edd475c287cdafee0a4db2eb98927ce3cf58 · arduino/ArduinoCore-avr · GitHub

It's hard to comment on, unless you identify it.

Have you seen this?
https://www.gammon.com.au/timers

The core code was not written to learn from. It's better to build from the ground up, using tutorials and always the processor data sheet in hand.

Take a look at the code and think about what might go wrong if t==255, on an 8 bit timer, and the check were not done.

	t = TCNT0;
	if ((TIFR & _BV(TOV0)) && (t < 255))
		m++;

If that is not clear, then the tutorial linked in the post above will certainly help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.