4 Replies Latest reply on Oct 30, 2013 4:21 AM by deckard026354

    delayMicroseconds() and Timer1

    Daniel_F11X

      Which timer does the function delayMicroseconds() use? Can I use Timer1 and delayMicroseconds() concurrently?

        • 1. Re: delayMicroseconds() and Timer1
          arduino_4_life

          deckard026354 is the one with the answer....

          • 2. Re: delayMicroseconds() and Timer1
            deckard026354

            You can use both concurrently yes.

             

            Timer1 is driven by the HPET. delayMicroseconds() in the current release - uses a Linux system call. In later versions of the run-time libraries - we will switch to a x86 time-stamp-counter (TSC) based version of delayMicroseconds();

             

            Here's an example sketch I have that happens to do both HPET based Timer1 and delayMircoseconds();

             

             

            #include <TimerOne.h>

             

            void setup()

            {

              // Initialize the digital pin as an output.

              // Pin 20 has an LED connected on most Arduino boards

              pinMode(20, OUTPUT);  

             

              Timer1.initialize(10000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)

              Timer1.attachInterrupt( timerIsr,  10000); // attach the service routine here - at 10 times less the rate of the HPET 1Hz

            }

             

             

            unsigned long before, after;

             

             

            void loop()

            {

              // Main code loop

              // TODO: Put your regular (non-ISR) logic here

              //Serial.write("hello\r\n");

              //printf("did serial write!\n");

              //fflush(stdout);

             

              before = micros();

              usleep(100);

              after = micros();

             

              printf("before %lu after %lu diff %lu using sleep(1);\n", before, after, after-before);

              fflush(stdout);

             

              before = micros();

              delayMicroseconds(100);

              after = micros();

             

              printf("before %lu after %lu diff %lu using delayMicroseconds(1000000);\n", before, after, after-before);

              fflush(stdout);

              sleep(2);

            }

             

            /// --------------------------

            /// Custom ISR Timer Routine

            /// --------------------------

            void timerIsr()

            {

                // Toggle LED

                digitalWrite( 20, digitalRead( 20 ) ^ 1 );

            }

            • 3. Re: delayMicroseconds() and Timer1
              Daniel_F11X

              Thanks for the reply. Yes, I am interested in using the Timer1 interrupt in addition to delayMicroseconds().

               

              I am curious why you used this line:

               

              Timer1.attachInterrupt( timerIsr,  10000);


              Instead of simply:


              Timer1.attachInterrupt( timerIsr);


              What is the difference between these two statements?

              • 4. Re: delayMicroseconds() and Timer1
                deckard026354

                The reference sketch from the Arduino stack did it that way.


                Also - initialising Timer1 this way, allows for different granularity <= Timer.initialize


                • Timer1.initialize(10000);
                • Timer1.attachInterrupt( timerIsr,  10000);
                • Timer1.attachInterrupt( timerIsr2,  20000);
                • Timer1.attachInterrupt( timerIsr3,  30000);
                • Timer1.attachInterrupt( timerIsrX,  xxxxxx); // etc
                • Add arbitrary timers which are an even division of the initial Timer.initialize

                 

                void timerIsr(void)

                {

                     // runs @ freq 10000

                }

                 

                void timerIsr2(void)

                {

                     // runs @ freq 20000

                }

                 

                void timerIsr3(void)

                {

                     // runs @ freq 30000

                }