delaymicroseconds()

Here is some simple code in which I am generating a square wave on A2 using the delaymicroseconds() function. It appears as though when I want to generate a wave using values of 5, 10 and 20 microseconds, the waveform I get on my scope is approximately twice as long ?

//************************************************
//* SCLK test code for MKRZERO
//* rev 0.1 September 2019
//************************************************

#define SDIO_LOW digitalWrite(A2, LOW) // SDIO_LOW = digitalWrite(A2, LOW)
#define SDIO_HIGH digitalWrite(A2, HIGH) // SDIO_HIGH = digitalWrite(A2, HIGH)

void setup() {

pinMode(A1, OUTPUT); // sets pin A1 as OUTPUT
pinMode(A2, OUTPUT); // sets pin A2 as OUTPUT
set_SCLK(); // call the function

}

void loop() {

SDIO_HIGH;
delayMicroseconds (20);
SDIO_LOW;
delayMicroseconds(20);

}

// ********** FUNCTION DEFINITIONS *******************

void set_SCLK() {

tone (A1, 100000); // generates a square wave of 100,000 Hz on pin A1

}

Maybe try

PORTC = B00000100;
delayMicroseconds (20);
PORTC = B00000000;
delayMicroseconds (20);

To see how fast that can go if nothing else is connected to the analog pins, else will need to use bitwise mask.

Whoopsies, your using MKRZERO board, that’s bit different.

According to the reference page for the tone function

frequency: the frequency of the tone in hertz. Allowed data types: unsigned int.

tone (A1, 100000);  // generates a square wave of 100,000 Hz on pin A1

well that is done using a timer and (hopefully direct port writes) pin A1 is not a PWM pin so this requires quite a few cycles in the background (i don't actually know if tone() uses PWM if available) then there is still the digitalWrite() 's themselves, they may account for a couple of uS as well. See how you go without the tone()

TheMemberFormerlyKnownAsAWOL:
According to the reference page for the tone function

Oh yeah that too, a 100.00hz !! wow, the value must have gotten truncated.