How much time does it take to execute a digitalWrite instruction on Teensy 3.2?

Status
Not open for further replies.

Beavis

Active member
I've been reading https://learn.adafruit.com/ir-sensor where it says that a digitalWrite "takes about 3 microseconds to happen" (they use an Arduino board, but don't specify what model). It's a timing critical part where interrupts are briefly disabled, so I want to adjust for my Teensy 3.2's higher speed. Would it be a simple proportion like Arduino MHz / Teensy MHz which I would use to scale down the 3 microseconds? I get (16/96) * 3 = 0.5 microseconds. I'm not too confident with this assumption.
Before posting, I searched here using "digital write time" and looked over the 163 post results. I did find out about digitalWriteFast, do I understand correctly that this only takes one clock cycle?! So at 96MHz it would only be about 10 NANOseconds?! If so, that would simplify things greatly since I would just ignore that time altogether :)

Just in case you want to see the code from the tutorial that I'm pondering:

Code:
void pulseIR(long microsecs) {
  // we'll count down from the number of microseconds we are told to wait

  cli(); // this turns off any background interrupts

  while (microsecs > 0) {
    // 38 kHz is about 13 microseconds high and 13 microseconds low
    digitalWrite(IRledPin, HIGH); // this takes about 3 microseconds to happen
    delayMicroseconds(10); // hang out for 10 microseconds, you can also change this to 9 if its not working
    digitalWrite(IRledPin, LOW); // this also takes about 3 microseconds
    delayMicroseconds(10); // hang out for 10 microseconds, you can also change this to 9 if its not working

    // so 26 microseconds altogether
    microsecs -= 26;
  }

  sei(); // this turns them back on
}

I know there are Libraries for IR, but I've never used a library before (although I'm working towards using them). I like this tutorial because iit's real down'n'diirty so I can really cut my teeth on what is actually going on. Libraries are great for a good end result, but I need to learn the hard way for experience.

Thanks for your time.
 
For best speed, use digitalWriteFast. By default the pins use slew rate limiting, which is almost always a good thing because it greatly reduces noise and extremely high speed problems like ringing on ordinary wires not properly impedance terminated. But it you *really* want the fastest possible, check out this.

https://forum.pjrc.com/threads/4187...lator-example)?p=132363&viewfull=1#post132363

As you can see in that thread, the actual number of cycles taken depends on a number of complex factors involving how the compiler allocates registers....

However, the best way to create a waveform (which you code seems to be doing) is usually analogWriteFrequency() and analogWrite(). Details here:

https://www.pjrc.com/teensy/td_pulse.html

Letting the PWM hardware do the work will create a perfect waveform that doesn't suffer from slight jitter due to software. Even if you turn off interrupts, the code still executes from a tiny cache to the flash memory, and the switched bus matrix allows other bus masters like USB to access memory on some cycles. You just can't get a perfect cycle-accurate waveform from software synthesis. You can get very close, but not 100% perfect, but you can get perfect pretty easily from the timers/PWM.
 
digitalWriteFast() will work as quickly as possible - but it requires a compile time constant/fixed number for the pin to use the fast way instead of the digitalWrite() method which it will default to with a runtime set value.

Tables have been posted on the forum in the past - a quick sketch could show you. But the library if used would already be perfected.
 
Thanks for the quick reply. But I must say, y'all are way over my head with your answers. Remember, I said that I've never even used a Library before! I'm going for down'n'dirty here, not 100% perfection. My primary goal is to follow the lady ada tutorial as closely as possible, so speed isn't necessarily a priority. Even a sluggish 3 microseconds would be OK because I'd be equal to the tutorial. But having the blazing speed of digitalWriteFast() sure is nice since it makes this a non-issue (26 microseconds vs. 26.01 microseconds is trivial). Once I get this tutorial under my belt, my next goal is to use PWM for sure!
 
Remember, I said that I've never even used a Library before!

I highly recommend clicking File > Examples. The libraries provide examples to help you get started. Often there's an example which does exactly what you need, or something very close. It's quick and easy to just run the examples and see if they work.
 
Yeah, I got the PS2keyboard working today without injury or loss of property. The Teensy Threads > Tests really blew my mind, though. Got ahead of myself... I did manage to learn how to manually install a library. I was surprised though that your elapsedMicros() [which I installed automagically using Library Manager] wasn't installed with Teensyduino: during installation I didn't exclude anything, I went with all of the defaults. Thanks for your time, I must get back to my project!
 
Last edited:
Status
Not open for further replies.
Back
Top