Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

micros() problems #1

Open
larsimmisch opened this issue Dec 29, 2011 · 0 comments
Open

micros() problems #1

larsimmisch opened this issue Dec 29, 2011 · 0 comments

Comments

@larsimmisch
Copy link
Owner

I'm beginning to suspect that the Arduino::micros function (which is pretty much the original) isn't behaving vey well.

Simplified:

    static uint32_t micros()
        {
        uint32_t m;
        uint16_t t;
        uint8_t oldSREG = SREG;

        cli();
        t = TCNT0;

        if ((TIFR & _BV(TOV0)) && (t == 0))
            t = 256;

        m = timer0_overflow_count;
        SREG = oldSREG;

        return ((m << 8) + t) * (64 / (F_CPU / 1000000L));
        }

Once m reaches 2**24, the value wraps around to t * 4 (assuming a 16MHz clock).

And timer0_overflow_count itself wraps around at 2^32, so the function resets twice over its value range.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant