user55532: I am trying to divide a incoming high frequency signal 600kHz by 4 and output wave at arduino MEGA2560 digital pin.

The input to arduino comes from incremental Encoder which is connected to motor that runs at 9000 rpm and the generates 1024 pulses per rotation.
I am trying to downsample the frequency I am getting from arduino to 250kHz or less.
I tried following approaches :
a. I counted the rising edge of the incoming wave and a interrupt is triggered as number of rising edge reaches certain value and ISR toggle an output pin.
b. I tried to get hardware counter work at T0 pin but I was not able to hit ISR when the count has reached specific value.

The problem with approach 1 is: As my frequency of incoming wave increases the output wave starts missing some pulses and generates one half cycle for 6/8/10 pulses of incoming wave. It should actually generate one half cycle for 4 pulses.
Here is approach 2 code:
#define outPin1 10
volatile short encoder = 4;

void setup ()
{
   pinMode (outPin1, OUTPUT);
   Serial.begin(9600);
   cli();
   // reset Timer 0
   TCCR0A = 0;
   TCCR0B = 0;

 }  // end of setup

 ISR(TIMER0_COMPA_vect)
 {
     digitalWrite(outPin1, !digitalRead(outPin1));

 }

 void loop ()
 {
    TCNT0 = 0;      //counter to zero

    // set compare match register to desired timer count:
   OCR0A = encoder;
    // turn on CTC mode:

   bitSet(TCCR0A, WGM01);
   // Timer 0 - counts events on pin D4

  bitSet(TIMSK0, OCIE0A);    // interrupt on Timer 0 CTC
   // start Timer 0
  // External clock source on T0 pin (D4). Clock on rising edge.

   bitSet(TCCR0B, CS02);
   bitSet(TCCR0B, CS01);
   bitSet(TCCR0B, CS00);
}

Please someone help me out with this issue

user7508: I am writing another answer because what I propose here is radically
different from the previous answer. In that answer I wrote that this is
too fast for being done in software, and here I am going to contradict
myself. :-/
At 600 kHz, the input stays low for roughly 13 CPU cycles,
then high for another 13 cycles. You cannot do much in 13 cycles.
Notably, the prologue of an ISR can quite often take more than this.
Also, whenever the Timer 0 overflow ISR from the Arduino core fires, it
can delay your own ISR for way more than 13 cycles. That's why it
is usually illusory to expect any job to be reliably done in this time
frame. Especially if interrupts are involved.
That being said, if the CPU has absolutely nothing else to do. And
specifically if it is never interrupted, Then it can keep up with that
pace. Here is a program that should do what you expect. I initially
thought about writing it in assembly, but it can also be written in C,
as the compiler translates it in a quite straightforward way. If in
doubt, however, check the generated assembly:
/*
 * From one square wave generate two waves in quadrature at 1/4 of the
 * original frequency.
 *
 * https://arduino.stackexchange.com/questions/63202
 *                               _   _   _   _   _   _
 *  input:  digital 13 = PB7    / \_/ \_/ \_/ \_/ \_/ \_/
 *                               _______         _______
 *  output: digital 10 = PB4    /       \_______/       \
 *                                   _______         ____
 *  output: digital 11 = PB5    ____/       \_______/
 */

int main(void)
{
    DDRB = _BV(PB4) | _BV(PB5);  // PB4 and PB5 as outputs
    for (;;) {
        loop_until_bit_is_clear(PINB, PB7);  // wait for rising input
        loop_until_bit_is_set(PINB, PB7);
        PORTB |= _BV(PB4);                   // rise PB4
        loop_until_bit_is_clear(PINB, PB7);  // wait for rising input
        loop_until_bit_is_set(PINB, PB7);
        PORTB |= _BV(PB5);                   // rise PB5
        loop_until_bit_is_clear(PINB, PB7);  // wait for rising input
        loop_until_bit_is_set(PINB, PB7);
        PORTB &= ~_BV(PB4);                  // fall PB4
        loop_until_bit_is_clear(PINB, PB7);  // wait for rising input
        loop_until_bit_is_set(PINB, PB7);
        PORTB &= ~_BV(PB5);                  // fall PB5
    }
}

Note that, since this is not using the Arduino core, I wrote main()
rather than setup() and loop(). This overrides the main() provided
by the core, thus you don't have the initializations that come with it
and, in particular, you don't have the Timer 0 interrupt.

user7508: This is probably too fast to have have software involved in the loop.
You should try to use a hardware-only solution. Note the Arduino timers
should be usable as hardware frequency dividers:

configure a timer to be clocked off the input signal, so that it
behaves like a counter.
configure it for delivering a PWM output with a period of 4 timer
clock cycles and a duty cycle of 50%.

You may try this, but beware I have not tested it:
void setup()
{
    // Configure Timer 5 as a frequency divider:
    //   input: digital pin 47 = PL2 = T5
    //   ouput: digital pin 46 = PL3 = OC5A
    DDRL  |= _BV(PL3);    // set pin PL3 as output
    TCCR5A = 0;           // undo the Arduino core's configuration
    TCCR5B = 0;
    ICR5   = 4 - 1;       // period = 4 timer clock cycles
    OCR5A  = 2 - 1;       // output HIGH for 2 timer clock cycles
    TCCR5A = _BV(COM5A1)  // non-inverting PWM on pin OC5A
           | _BV(WGM51);  // mode 14: fast PWM, TOP = ICR5
    TCCR5B = _BV(WGM52)
           | _BV(WGM53)
           | _BV(CS50)    // clock on rising edges of T5
           | _BV(CS51)
           | _BV(CS52);
}

void loop(){}


55532: <p>I am trying to divide a incoming high frequency signal 600kHz by 4 and output wave at arduino MEGA2560 digital pin.</p>

<ul>
<li>The input to arduino comes from incremental Encoder which is connected to motor that runs at 9000 rpm and the generates 1024 pulses per rotation.</li>
<li>I am trying to downsample the frequency I am getting from arduino to 250kHz or less.</li>
<li><p>I tried following approaches :</p>

<p>a. I counted the rising edge of the incoming wave and a interrupt is triggered as number of rising edge reaches certain value and ISR toggle an output pin.
b. I tried to get hardware counter work at T0 pin but I was not able to hit ISR when the count has reached specific value.</p></li>
</ul>

<p>The problem with approach 1 is: As my frequency of incoming wave increases the output wave starts missing some pulses and generates one half cycle for 6/8/10 pulses of incoming wave. It should actually generate one half cycle for 4 pulses.</p>

<p><strong>Here is approach 2 code:</strong></p>

<pre><code>#define outPin1 10
volatile short encoder = 4;

void setup ()
{
   pinMode (outPin1, OUTPUT);
   Serial.begin(9600);
   cli();
   // reset Timer 0
   TCCR0A = 0;
   TCCR0B = 0;

 }  // end of setup

 ISR(TIMER0_COMPA_vect)
 {
     digitalWrite(outPin1, !digitalRead(outPin1));

 }

 void loop ()
 {
    TCNT0 = 0;      //counter to zero

    // set compare match register to desired timer count:
   OCR0A = encoder;
    // turn on CTC mode:

   bitSet(TCCR0A, WGM01);
   // Timer 0 - counts events on pin D4

  bitSet(TIMSK0, OCIE0A);    // interrupt on Timer 0 CTC
   // start Timer 0
  // External clock source on T0 pin (D4). Clock on rising edge.

   bitSet(TCCR0B, CS02);
   bitSet(TCCR0B, CS01);
   bitSet(TCCR0B, CS00);
}
</code></pre>

<p><strong>Please someone help me out with this issue</strong></p>
 7508: <p>I am writing another answer because what I propose here is radically
different from the previous answer. In that answer I wrote that this is
too fast for being done in software, and here I am going to contradict
myself. :-/</p>

<p>At 600&nbsp;kHz, the input stays low for roughly 13&nbsp;CPU cycles,
then high for another 13 cycles. You cannot do much in 13&nbsp;cycles.
Notably, the prologue of an ISR can quite often take more than this.
Also, whenever the Timer 0 overflow ISR from the Arduino core fires, it
can delay your own ISR for way more than 13&nbsp;cycles. That's why it
is usually illusory to expect any job to be reliably done in this time
frame. Especially if interrupts are involved.</p>

<p>That being said, if the CPU has <em>absolutely nothing else</em> to do. And
specifically if it is never interrupted, Then it can keep up with that
pace. Here is a program that should do what you expect. I initially
thought about writing it in assembly, but it can also be written in C,
as the compiler translates it in a quite straightforward way. If in
doubt, however, check the generated assembly:</p>

<pre class="lang-c prettyprint-override"><code>/*
 * From one square wave generate two waves in quadrature at 1/4 of the
 * original frequency.
 *
 * https://arduino.stackexchange.com/questions/63202
 *                               _   _   _   _   _   _
 *  input:  digital 13 = PB7    / \_/ \_/ \_/ \_/ \_/ \_/
 *                               _______         _______
 *  output: digital 10 = PB4    /       \_______/       \
 *                                   _______         ____
 *  output: digital 11 = PB5    ____/       \_______/
 */

int main(void)
{
    DDRB = _BV(PB4) | _BV(PB5);  // PB4 and PB5 as outputs
    for (;;) {
        loop_until_bit_is_clear(PINB, PB7);  // wait for rising input
        loop_until_bit_is_set(PINB, PB7);
        PORTB |= _BV(PB4);                   // rise PB4
        loop_until_bit_is_clear(PINB, PB7);  // wait for rising input
        loop_until_bit_is_set(PINB, PB7);
        PORTB |= _BV(PB5);                   // rise PB5
        loop_until_bit_is_clear(PINB, PB7);  // wait for rising input
        loop_until_bit_is_set(PINB, PB7);
        PORTB &amp;= ~_BV(PB4);                  // fall PB4
        loop_until_bit_is_clear(PINB, PB7);  // wait for rising input
        loop_until_bit_is_set(PINB, PB7);
        PORTB &amp;= ~_BV(PB5);                  // fall PB5
    }
}
</code></pre>

<p>Note that, since this is not using the Arduino core, I wrote <code>main()</code>
rather than <code>setup()</code> and <code>loop()</code>. This overrides the <code>main()</code> provided
by the core, thus you don't have the initializations that come with it
and, in particular, you don't have the Timer&nbsp;0 interrupt.</p>
 7508: <p>This is probably too fast to have have software involved in the loop.
You should try to use a hardware-only solution. Note the Arduino timers
should be usable as hardware frequency dividers:</p>

<ul>
<li>configure a timer to be clocked off the input signal, so that it
behaves like a counter.</li>
<li>configure it for delivering a PWM output with a period of 4 timer
clock cycles and a duty cycle of 50%.</li>
</ul>

<p>You may try this, but beware I have not tested it:</p>



<pre><code>void setup()
{
    // Configure Timer 5 as a frequency divider:
    //   input: digital pin 47 = PL2 = T5
    //   ouput: digital pin 46 = PL3 = OC5A
    DDRL  |= _BV(PL3);    // set pin PL3 as output
    TCCR5A = 0;           // undo the Arduino core's configuration
    TCCR5B = 0;
    ICR5   = 4 - 1;       // period = 4 timer clock cycles
    OCR5A  = 2 - 1;       // output HIGH for 2 timer clock cycles
    TCCR5A = _BV(COM5A1)  // non-inverting PWM on pin OC5A
           | _BV(WGM51);  // mode 14: fast PWM, TOP = ICR5
    TCCR5B = _BV(WGM52)
           | _BV(WGM53)
           | _BV(CS50)    // clock on rising edges of T5
           | _BV(CS51)
           | _BV(CS52);
}

void loop(){}
</code></pre>
