Is it solved by a code I misunderstood that outputs a pulse in microseconds?
Since post the code that outputs the actual nanosecond pulse, might as well.
enum pulseWidth {
ns250_0, // 250.0ns
ns312_5, // 312.5ns
ns375_0, // 375.0ns
ns437_5, // 437.5ns
ns500_0 // 500.0ns
};
void setup() {
pinMode(8, OUTPUT); // Pulse output is D8
}
void loop() {
pulseBegin(60, ns250_0); // 60Hz,250ns
delay(3000);
pulseBegin(1000, ns500_0); // 1kHz,500ns
delay(3000);
pulseBegin(100000, ns500_0); // 100kHz,500ns
delay(3000);
pulseBegin(2.5, ns312_5); // 2.5Hz,312.5ns
delay(3000);
pulseEnd(); // Pulse output disable
delay(3000);
}
void pulseBegin(float Hz, unsigned char pW) { // Support 1Hz to 400kHz
if (Hz <= 0) pulseEnd();
else if (Hz > 400000) pulseEnd();
else {
byte x = 0x18;
if (Hz > (F_CPU >> 16)) {
x += 1; Hz = F_CPU / Hz;
} else if (Hz > (F_CPU >> 19)) {
x += 2; Hz = (F_CPU >> 3) / Hz;
} else if (Hz > (F_CPU >> 22)) {
x += 3; Hz = (F_CPU >> 6) / Hz;
} else {
x += 4; Hz = (F_CPU >> 8) / Hz;
}
GPIOR2 = pW;
TCCR1A = 0x03;
cli();
TIFR1 = 0x02;
TIMSK1 = 0x02;
TCCR1B = x;
OCR1A = Hz - 1;
sei();
}
}
void pulseEnd(void) {
TIMSK1 = 0;
TCCR1A = 0;
TCCR1B = 0;
GPIOR2 = 0;
}
ISR(TIMER1_COMPA_vect, ISR_NAKED) {
__asm__ __volatile__ ( // 29cy + reti
"push r30 \n in r30,0x3f \n push r30 \n push r31 \n"
"ldi r30,lo8(gs(p_end)) \n in r31,0x2b \n sub r30,r31 \n"
"ldi r31,hi8(gs(p_end)) \n sbci r31,0 \n sbi 0x05,0 \n"
"ijmp \n nop \n nop \n nop \n nop \n p_end: cbi 0x05,0 \n"
"pop r31 \n pop r30 \n out 0x3f,r30 \n pop r30 \n reti");
}
#if F_CPU != 16000000UL
#error "UNSUPPORTED CLOCK! RUN 16MHz ONLY!"
#endif
Pulse output pin is D8.
The pulse width can be selected from 5 types, 250ns to 500ns in 62.5ns steps.
(Because Arduino running at 16MHz, so one cycle is 62.5ns.)
The frequency can be set from 1Hz to 400kHz. (Decimal point allowed)