frequency generation tests

void setup(){
  pinMode (2, OUTPUT);
  cli();
}
void loop(){
    digitalWrite (2, !digitalRead(2));
}

59.042 KHz

void setup(){
  pinMode (2, OUTPUT);
  cli();
}
void loop(){
  while(1){
    digitalWrite (2, !digitalRead(2));
  }
}

63.7466 KHz

void setup(){
  pinMode (2, OUTPUT);
  cli();
}
void loop(){
  while(1){
    PORTD = PORTD - 0b00000100;  // also PORTD = PORTD ^ 0b00000100;
  }
}

1.60004 MHz!

void setup(){
  pinMode (2, OUTPUT);
  cli();
}
void loop(){
  while(1){
    PIND = PIND | 0b00000100; // also PIND |= 0b00000100;
  }
}

2.00005MHz!

void setup(){
  pinMode (2, OUTPUT);
  //cli();
}
void loop(){
  while(1){
    PIND = PIND | 0b00000100;
  }
}

1.988MHz with interrupt jitter

void setup(){
  pinMode (2, OUTPUT);
  cli();
}
void loop(){
  //while(1){
    PIND = PIND | 0b00000100; 
  //}
}

571.443 KHz - loop() really slows things down!
Is PWM the only other controllable high frequency solution? (i.e. w/o setting the system clock out fuse?)

Lower frequencies are no problem :wink:

If your controller should do more than only provide the signal, using timer/counter hardware offers fastest continuous operation (CTC mode).

Ok, not familiar with coding for that.

loop() really slows things down!

that's because loop() is called from main() in a tight loop that also checks Serial. And you have the overhead of a function call

int main(void)
{
    init();

    initVariant();

#if defined(USBCON)
    USBDevice.attach();
#endif

    setup();
    
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
        
    return 0;
}

BTW have you tried GOTO loop?

void setup(){
  pinMode (2, OUTPUT);
  cli();
}
void loop(){
    label:
    PIND = PIND | 0b00000100; 
    goto label;
}

"571.443 KHz - loop() really slows things down!"

I'm not a C expert but looks to me as if while() is speeding things up rather than loop() slowing it down. Very strange behaviour from the compiler!

I guess if you want faster output you need to go to assembly code. Is it possible to embed an assembly code routine with the Arduino IDE?

Russell.

I'm not surprised that loop is slowing it down - every iteration requires calling a function, whereas with just the while loop, it doesn't...

I think you can go up to half the system clock using fast pwm mode (or 32mhz, half the fast peripheral clock, if you're using a part with on-chip PLL using the timer that can be clocked off that)

russellz: Is it possible to embed an assembly code routine with the Arduino IDE?

Russell.

see - http://forum.arduino.cc/index.php?topic=37130.0 -

After all, what's the aim of this topic?

The closer we reach the clock frequency, the less frequency variations are possible. From a 16 MHz clock you can derive only 8, 4 or 2 MHz, before lower frequencies allow for scale factors other than 2.

I do miss the notification of replies feature!
The question comes up frequently as to what frequency can be generated,
for example here looking for 10KHz plus inverted signal,

I wanted to see for myself, so I tried a few things and posted the results.
I’d only gone to 20KHz previously using blink-without-delay to simulate a camera clock output for a project I was creating.