aboutsummaryrefslogtreecommitdiff
path: root/cores
diff options
context:
space:
mode:
authorDavid A. Mellis <d.mellis@arduino.cc>2008-11-26 14:14:59 +0000
committerDavid A. Mellis <d.mellis@arduino.cc>2008-11-26 14:14:59 +0000
commit051eb371a6c277a9df95fd88f8a5abdd7fa60440 (patch)
tree884886e9b5579a91562caf21427bd617d311452d /cores
parentbcbd3a6ef4980682c5b6d83947098bc53b2c855d (diff)
Adding micros() function.
Diffstat (limited to 'cores')
-rwxr-xr-xcores/arduino/wiring.c17
-rwxr-xr-xcores/arduino/wiring.h1
2 files changed, 18 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();
diff --git a/cores/arduino/wiring.h b/cores/arduino/wiring.h
index 46d5334..9600a0f 100755
--- a/cores/arduino/wiring.h
+++ b/cores/arduino/wiring.h
@@ -113,6 +113,7 @@ int serialRead(void);
void serialFlush(void);
unsigned long millis(void);
+unsigned long micros(void);
void delay(unsigned long);
void delayMicroseconds(unsigned int us);
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);