735 khz square wave

Hello,

I've been searching a lot on the forums for how to make above-500 khz square wave on Arduino uno. I have done before low frequencies using sleep (microsconds). But for 735 khz I must use 1.35 microseconds sleep and that's impossible.

Any suggestions?

Thanks!

At that high of a frequency you'll have to use one of the timers. Fortunately, they are already set up for hardware PWM, so all you have to do is set up the top value to get the frequency and the compare value to set the duty cycle.

Have a look at the timers portion of the datasheet to see how the registers are set up. Ask questions if you don't understand something.

Not sure the wave will be actually square but more rectangular, does that matter?

How do you plan to use this 735KHz square wave, and what else does the program need to do?

Are you just generating a single pulse in response to an input?? Is it a repeating test signal for another program running elsewhere?

If you can tolerate the use of blocking code you can use the equivalent of delay() but using "no operation" commands which are at the resolution of processor ticks. The single nop is called with "nop\n\t"

For multiples, it's easy to use the AVR built in function/macro which calls multiple nops.
__builtin_avr_delay_cycles ();

1.35 microseconds is between 21 and 22 processor cycles at 16MHz.

This code could be used to generate 735Khz on pin 10. The timing can be tweaked to compensate for the toggle time.

void setup() 
  {
  pinMode (10, OUTPUT);
  }
void loop() 
{
while(1)
 {
  PINB = bit (2);   // toggle D10
  __builtin_avr_delay_cycles (11);//11 high, 11 low for 22 = 1.375 us
 }
}

cattledog:
How do you plan to use this 735KHz square wave, and what else does the program need to do?

Are you just generating a single pulse in response to an input?? Is it a repeating test signal for another program running elsewhere?

If you can tolerate the use of blocking code you can use the equivalent of delay() but using "no operation" commands which are at the resolution of processor ticks. The single nop is called with "nop\n\t"

For multiples, it's easy to use the AVR built in function/macro which calls multiple nops.
__builtin_avr_delay_cycles ();

1.35 microseconds is between 21 and 22 processor cycles at 16MHz.

This code could be used to generate 735Khz on pin 10. The timing can be tweaked to compensate for the toggle time.

void setup() 

{
  pinMode (10, OUTPUT);
  }
void loop()
{
while(1)
{
  PINB = bit (2);  // toggle D10
  __builtin_avr_delay_cycles (11);//11 high, 11 low for 22 = 1.375 us
}
}

No way. This is very poor method to generate PWM. It is possible to do in assembly but hardly in C. For example your code will generate slower frequency: you did not count 2 (or more?) ticks for jump to next while loop iteration and 2 ticks for toggling pin. Let alone glitches from millis interrupt. Not using hardware timer can be rarely justified in PWM generation.