Skip to content

Only disable timer interrupt instead of all interrutps in millis()/micros() #1442

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions hardware/arduino/cores/arduino/wiring.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,21 @@ ISR(TIMER0_OVF_vect)
unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;

// disable interrupts while we read timer0_millis or we might get an
// disable overflow interrupt while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
// See: http://forum.arduino.cc/index.php?topic=168864.0
TIMSK0 &= ~(1 << TOIE0); // Clear Overflow Interrupt Enable
m = timer0_millis;
SREG = oldSREG;

TIMSK0 |= 1 << TOIE0; // Sets Overflow Interrupt Enable
return m;
}

unsigned long micros() {
unsigned long m;
uint8_t oldSREG = SREG, t;
uint8_t t;

cli();
TIMSK0 &= ~(1 << TOIE0); // Clear Overflow Interrupt Enable
m = timer0_overflow_count;
#if defined(TCNT0)
t = TCNT0;
Expand All @@ -101,7 +100,7 @@ unsigned long micros() {
m++;
#endif

SREG = oldSREG;
TIMSK0 |= 1 << TOIE0; // Sets Overflow Interrupt Enable

return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}
Expand Down