delay() timing problem on Atmega64a

21 views
Skip to first unread message

Levent Esen

unread,
Jul 22, 2016, 9:56:17 AM7/22/16
to Developers
I'm using https://github.com/MCUdude/MegaCore for Atmega64a.
There was a timing problem with delay() function which is tested only in Proteus simulation.
I reported that problem in this issue https://github.com/MCUdude/MegaCore
I think that it's an issue of the official Arduino core.
I changed the implementation of delay(ms) function and now the problem is resolved.

Here is the official Arduino core code in wiring.c:

void delay(unsigned long ms)
{
uint32_t start = micros();

while (ms > 0) {
yield();
while ( ms > 0 && (micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}


And this is my code:

void delay(signed long ms)
{
if (ms > 0)
{
unsigned long start = micros();
unsigned long stop = start + ms * 1000UL;
while (micros() <= stop)
{
yield();
}
}
}

Cristian Maglie

unread,
Jul 22, 2016, 10:04:09 AM7/22/16
to devel...@arduino.cc


Il 22/07/2016 13:40, Levent Esen ha scritto:
> I reported that problem in this issue https://github.com/MCUdude/MegaCore
> I think that it's an issue of the official Arduino core.

Please open an issue on github:
https://github.com/arduino/Arduino/issues/new

and post a *full sketch* that shows the problem.


> I changed the implementation of delay(ms) function and now the problem
> is resolved.

[...snip...]

> void delay(signed long ms)
> {
> if (ms > 0)
> {
> unsigned long start = micros();
> unsigned long stop = start + ms * 1000UL;
> while (micros() <= stop)
> {
> yield();
> }
> }
> }

this implementation doesn't handle roll-over correctly if "stop" value
overflows.

C

Reply all
Reply to author
Forward
0 new messages