diff options
author | David A. Mellis <d.mellis@arduino.cc> | 2008-11-26 14:14:59 +0000 |
---|---|---|
committer | David A. Mellis <d.mellis@arduino.cc> | 2008-11-26 14:14:59 +0000 |
commit | 051eb371a6c277a9df95fd88f8a5abdd7fa60440 (patch) | |
tree | 884886e9b5579a91562caf21427bd617d311452d /cores/arduino/wiring.c | |
parent | bcbd3a6ef4980682c5b6d83947098bc53b2c855d (diff) |
Adding micros() function.
Diffstat (limited to 'cores/arduino/wiring.c')
-rwxr-xr-x | cores/arduino/wiring.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c index 157d1d2..2896647 100755 --- a/cores/arduino/wiring.c +++ b/cores/arduino/wiring.c @@ -24,11 +24,13 @@ #include "wiring_private.h" +volatile unsigned long timer0_overflow_count = 0; volatile unsigned long timer0_clock_cycles = 0; volatile unsigned long timer0_millis = 0; SIGNAL(TIMER0_OVF_vect) { + timer0_overflow_count++; // timer 0 prescale factor is 64 and the timer overflows at 256 timer0_clock_cycles += 64UL * 256UL; while (timer0_clock_cycles > clockCyclesPerMicrosecond() * 1000UL) { @@ -51,6 +53,21 @@ unsigned long millis() return m; } +unsigned long micros() { + unsigned long m, t; + uint8_t oldSREG = SREG; + + cli(); + t = TCNT0; + if ((TIFR0 & _BV(TOV0)) && (t == 0)) + t = 256; + + m = timer0_overflow_count; + SREG = oldSREG; + + return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond()); +} + void delay(unsigned long ms) { unsigned long start = millis(); |