Greetings-
I'd like to toggle a pin a bit faster than the ~ 2 MHz I get with successive digitalWrite() calls in a for() loop, to get a specific-length
burst out quickly.
I stumbled on the function digitalWriteFast() in some other threads, and it compiles without warnings, but it doesn't appear to work (at least not in what I'd consider a rational way).
If I substitute digitalWrite() inside the for() loop in the attached code, I get exactly the specified number of pulses a little
over 500ns apart (mostly; a few pulses per thousand are delayed a µs or two, but there's always the correct total number).
With digitalWriteFast() as shown, I get from 0 to 10 pulses, each 750 or 1200 ns long, at seemingly random intervals each time the loop executes.
Inserting a µs delay between the digitalWriteFast() calls does not cure the problem.
Changing the *first* call to a plain digitalWrite() results in a larger, but still random number of pulses less than requested.
Changing the *second* (LOW) call to plain digitalWrite() completes the specified number, but pulse time is only slightly faster than with both plain calls.
I could not easily find 'official' docs on digitalWriteFast(), maybe it's no longer supported? Sorry, I'm new to this!
Any guidance appreciated,
zike
(using Teensy 3.5, Arduino 1.8.2, Teensyduino 1.40)
I'd like to toggle a pin a bit faster than the ~ 2 MHz I get with successive digitalWrite() calls in a for() loop, to get a specific-length
burst out quickly.
I stumbled on the function digitalWriteFast() in some other threads, and it compiles without warnings, but it doesn't appear to work (at least not in what I'd consider a rational way).
If I substitute digitalWrite() inside the for() loop in the attached code, I get exactly the specified number of pulses a little
over 500ns apart (mostly; a few pulses per thousand are delayed a µs or two, but there's always the correct total number).
With digitalWriteFast() as shown, I get from 0 to 10 pulses, each 750 or 1200 ns long, at seemingly random intervals each time the loop executes.
Inserting a µs delay between the digitalWriteFast() calls does not cure the problem.
Changing the *first* call to a plain digitalWrite() results in a larger, but still random number of pulses less than requested.
Changing the *second* (LOW) call to plain digitalWrite() completes the specified number, but pulse time is only slightly faster than with both plain calls.
I could not easily find 'official' docs on digitalWriteFast(), maybe it's no longer supported? Sorry, I'm new to this!
Any guidance appreciated,
zike
Code:
#include <TimeLib.h>
#define ledPin 13 // to counter input
#define resetPin 37 // to counter reset
elapsedMillis r = 0; // update timer
elapsedMicros w = 0;
void setup() {
Serial.begin(115200);
pinMode(resetPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
long nout = 235959;
if (r > 1000) {
r = 0; // reset update timer
w = 0;
digitalWrite(resetPin, HIGH); // send reset pulse to counter
digitalWrite(resetPin, LOW);
while(r<30){} ; // delay to re-arm counter after reset pulse
for (long p = 0; p < nout; p++) {
digitalWriteFast(ledPin, HIGH);
digitalWriteFast(ledPin, LOW);
}
Serial.println(w/1000);
}
}
(using Teensy 3.5, Arduino 1.8.2, Teensyduino 1.40)