# Output frequency

**URL:** <https://forum.arduino.cc/t/output-frequency/311304>\
**Category:** Programming\
**Created:** [May 13, 2015, 5:01pm UTC](https://forum.arduino.cc/t/output-frequency/311304 "2015-05-13T17:01:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_alx](https://avatars.discourse-cdn.com/v4/letter/_/c6cbf5/32.png) [@\_alx](https://forum.arduino.cc/u/_alx)\
**Post date:** [May 13, 2015, 5:01pm UTC](https://forum.arduino.cc/t/output-frequency/311304/1 "2015-05-13T17:01:51Z")

</div>

Hello,

can someone tell me, what is the output frequency on the PORTE pins if I use this code?  
or:  
How can i get the highest switching-frequency on an Arduino-Port?

```cpp
 for (n = 0; n < 2800; n++) {
              PORTE = B11111111;
              PORTE = B00000000;
            }

```

I'am using an Olimexino 32u4 (Leonardo-compatible)

Thank you

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [May 13, 2015, 5:18pm UTC](https://forum.arduino.cc/t/output-frequency/311304/2 "2015-05-13T17:18:08Z")

</div>

I think you would have to measure that with an oscilloscope to be really accurate.

You could record the value of micros() before and after the FOR loop and figure out how long the total loop takes.

It is hard to envisage anything being much faster. But the Arduino cannot do anything else while it is working through the FOR loop.

...R

---

<div class="post-metadata">

**Author:** ![CrossRoads](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/crossroads/32/214033_2.png) [@CrossRoads](https://forum.arduino.cc/u/CrossRoads)\
**Post date:** [May 13, 2015, 5:38pm UTC](https://forum.arduino.cc/t/output-frequency/311304/3 "2015-05-13T17:38:29Z")

</div>

The for:loop adds time to test for the end condition.  
If you just want the frequency to be fast and nothing else, this will be pretty quick.  
Unstoppable, but quick

```cpp
while(1){
PINE = 0b11111111; // toggle output by writing to Input register
}

```

"However, writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register."

You can also program a fuse to output the system clock. Also, not stoppable.  
"Port C, Bit 7  
CLKO: When the corresponding fuse is enabled, this pin outputs the internal microcontroller  
working frequency. If the clock prescaler is used, this will affect this output frequency."

If you're looking for a burst of frequency, then system clock gated with an IO pin (2-input AND gate) can give bursts of 16 MHz for as long as the IO pin is high, control with a for loop or while counter, or simple time track using micros().

Lots of options.

---

<div class="post-metadata">

**Author:** ![DrAzzy](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@DrAzzy](https://forum.arduino.cc/u/DrAzzy)\
**Post date:** [May 13, 2015, 6:29pm UTC](https://forum.arduino.cc/t/output-frequency/311304/4 "2015-05-13T18:29:39Z")

</div>

You can also take over a timer, turn off the prescaler, and get up to half the system clock out of it using fast pwm mode - but this only gets you a few pins, not a whole port (do you want a whole port? I'm not sure what the use case might be...)

---

<div class="post-metadata">

**Author:** ![\_alx](https://avatars.discourse-cdn.com/v4/letter/_/c6cbf5/32.png) [@\_alx](https://forum.arduino.cc/u/_alx)\
**Post date:** [May 14, 2015, 8:31am UTC](https://forum.arduino.cc/t/output-frequency/311304/5 "2015-05-14T08:31:17Z")

</div>

Thank you for the answers, now things become clearer.

I need pulses with a duty cycle of exactly 50% on the pins of PORTB (not PORTE).

```cpp
for (n = 0; n < 2800; n++) {
              PORTB = B11111111;
              
              __asm__ ("nop\n\t");  //How many nops do i need?

              PORTB = B00000000;
            }

```

Now my question is, how many NOPs have I to insert, that the signal get's perfectly symmetrical?  
Is it possible to make the the duty cyle exactly 50% without any jittering?  
The resulting frequency is not critical, it should be in the range of some MHz

This is for a IR LED beacon which sends morse code but with pulsing carrier.

Thank you.

---

<div class="post-metadata">

**Author:** ![\_alx](https://avatars.discourse-cdn.com/v4/letter/_/c6cbf5/32.png) [@\_alx](https://forum.arduino.cc/u/_alx)\
**Post date:** [May 14, 2015, 2:27pm UTC](https://forum.arduino.cc/t/output-frequency/311304/6 "2015-05-14T14:27:08Z")

</div>

@CrossRoads

> If you're looking for a burst of frequency, then system clock gated with an IO pin (2-input AND gate) can \> give bursts of 16 MHz for as long as the IO pin is high, control with a for loop or while counter, or simple \> time track using micros().

That's it!

But I will use an external XTAL-Oscillator on a AND Gate port and a Arduino pin on the other port.

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:17pm UTC](https://forum.arduino.cc/t/output-frequency/311304/7 "2021-05-05T21:17:28Z")

</div>

Closed on May 5, 2021, 9:17pm UTC


