user55041: I want to generate a 1khz square signal with an Arduino Mega.
I have read the MCU contains several timers.
What i want to do is to configure one of this timers.
My first question is: Is there a way to automatically control an output pin from the timer, without having to execute any line of code in my arduino program ? Look at my code: i need to write a line in ISR(TIMER1_OVF_vect). Some MCU can do that but i did not find any information about it on documentation. (Some pins are connected on timer outputs, by hardware conception).
My second question is: What is wrong on my calculation on my code ?
I want 1khz and i get 990Hz
Here is what i've done:

Arduino Mega runs at 16Mhz = 16000Khz
I have set a /8 prescaler, so timer's frequency is 8000Khz
The frequency should by divide by two because i have 2 states (HIGH and LOW): 4000Khz
The interupt is fired when the counter reach 65536. So if i load 65536-1000 in TCNT1, the timer should count 1000 times in order to fire the interupt. So my frequency is 4000khz/1000 = 1khz.

What is wrong ?
Thanks a lot
void setup() {
  pinMode(14,OUTPUT);
  
  noInterrupts();
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 65536 - 1000; 
  TCCR1B |= (1 << CS11);    // Prescaler clock / 8
  TIMSK1 |= (1 << TOIE1);
  interrupts();
}

ISR(TIMER1_OVF_vect)
{
  TCNT1 = 65536 - 1000;
  digitalWrite(14, digitalRead(14) ^ 1);
}

void loop() {

}


user7508: As explained in the previous answer, you forgot to account for the time
taken by the CPU to handle the interrupt prologue. As a general rule, if
you want consistent timing, you should never reset the timer by
software after the initialization.
If you want to run the timer in normal mode, and use it to schedule
interrupts at a period that is not the timer's own period, it is
possible, but you should not use the overflow interrupt for this
purpose. You should instead use any of the “compare match” interrupts,
and adjust the compare register in the IRS in order to schedule the next
interrupt. For example:
void setup() {
    pinMode(14,OUTPUT);

    TCCR1A = 0;            // normal mode
    TCCR1B = _BV(CS11);    // clock @ F_CPU/8
    TCNT1  = 0;            // clear timer
    OCR1A  = 1000;         // first COMPA match in 1000 cycles
    TIFR1  = _BV(OCF1A);   // clear interrupt flag
    TIMSK1 = _BV(OCIE1A);  // enable TIMER1_COMPA interrupt
}

ISR(TIMER1_COMPA_vect) {
    OCR1A += 1000;      // schedule next interrupt
    PINJ   = _BV(PJ1);  // toggle PJ1 = digital 14
}

Note that the line OCR1A += 1000; can cause a wrap around, but this is
no problem, as the addition wraps around in exactly the same way as the
timer itself.
The advantage of this technique is that you can schedule up to three
different tasks, having different periods, with only one timer. And you
can even schedule tasks in a non-periodic fashion.
That being said, if you do not need the timer for anything else other
than scheduling a single periodic task (or up to four tasks sharing the
same period), then a better solution is to let the timer handle the
period all by itself. Configure the timer in mode 12 or 14 (CTC or fast
PWM, with TOP = ICR1), and set the period with ICR1 = 999;. Then the
ISR is reduced to:
ISR(TIMER1_COMPA_vect) { PINJ = _BV(PJ1); }

and you save a few cycles. This would be my preferred solution if I
absolutely had to output the signal on pin 14.
Note that this solution should give you the right average frequency,
but you will still have some jitter, because the interrupt can
occasionally be delayed by other interrupts. If you are free to choose
the output pin, then the best solution is to let the timer handle the
PWM generation all by itself. Set the timer to any PWM mode (fast PWM is
the simplest) and use one of the waveform generators in order to
generate the PWM signal. Only the PWM-capable pins (those marked with a
“~” symbol) can be used this way. For example, Timer 1 can output to
pins 11 (OC1A), 12 (OC1B) and 13 (OC1C).
Edit answering the question:

what is the goal of TIFR1 = _BV(OCF1A); // clear interrupt flag?

Whenever the timer value matches the contents of the compare register
OCR1A, the interrupt flag OCF1A (Output Compare Flag of timer 1,
channel A) is raised. This flag controls the interrupt request when the
OCIE1A bit (Output Compare Interrupt Enable 1A) is set. The flag is
automatically cleared when the ISR runs, and can be manually cleared by
writing a logic 1 to it (yes, it is kind of backwards).
It is quite likely that the flag raises early in the start process,
while the Arduino library is initializing. If the flag is up when you
enable the interrupt, an interrupt request is immediately triggered. In
order to avoid that, the common practice is to clear the flag before
enabling the interrupt.

user4143: 
My first question is: Is there a way to automatically control an output pin from the timer, without having to execute any line of code in my arduino program ?

Yes. It's called PWM with a 50% duty cycle.
You need to read up on 19. Output Compare Modulator (OCM1C0A) in the datasheet.

My second question is: What is wrong on my calculation on my code ? I want 1khz and i get 990Hz

You have to take into account the number of CPU cycles it takes to start executing your ISR. There's a short bit of code before your code is run that preserves any used registers on the stack and allocates any room needed for local variables. That takes time, and must be calculated into your timing factor.

55041: <p>I want to generate a 1khz square signal with an Arduino Mega.</p>
<p>I have read the MCU contains several timers.
What i want to do is to configure one of this timers.</p>
<p>My first question is: Is there a way to automatically control an output pin from the timer, without having to execute any line of code in my arduino program ? Look at my code: i need to write a line in ISR(TIMER1_OVF_vect). Some MCU can do that but i did not find any information about it on documentation. (Some pins are connected on timer outputs, by hardware conception).</p>
<p>My second question is: What is wrong on my calculation on my code ?
I want 1khz and i get 990Hz</p>
<p>Here is what i've done:</p>
<ul>
<li>Arduino Mega runs at 16Mhz = 16000Khz</li>
<li>I have set a /8 prescaler, so timer's frequency is 8000Khz</li>
<li>The frequency should by divide by two because i have 2 states (HIGH and LOW): 4000Khz</li>
<li>The interupt is fired when the counter reach 65536. So if i load 65536-1000 in TCNT1, the timer should count 1000 times in order to fire the interupt. So my frequency is 4000khz/1000 = 1khz.</li>
</ul>
<p>What is wrong ?</p>
<p>Thanks a lot</p>
<pre><code>void setup() {
  pinMode(14,OUTPUT);
  
  noInterrupts();
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 65536 - 1000; 
  TCCR1B |= (1 &lt;&lt; CS11);    // Prescaler clock / 8
  TIMSK1 |= (1 &lt;&lt; TOIE1);
  interrupts();
}

ISR(TIMER1_OVF_vect)
{
  TCNT1 = 65536 - 1000;
  digitalWrite(14, digitalRead(14) ^ 1);
}

void loop() {

}
</code></pre>
 7508: <p>As explained in the previous answer, you forgot to account for the time
taken by the CPU to handle the interrupt prologue. As a general rule, if
you want consistent timing, you should <em>never</em> reset the timer by
software after the initialization.</p>
<p>If you want to run the timer in normal mode, and use it to schedule
interrupts at a period that is not the timer's own period, it is
possible, but you should not use the overflow interrupt for this
purpose. You should instead use any of the “compare match” interrupts,
and adjust the compare register in the IRS in order to schedule the next
interrupt. For example:</p>
<pre class="lang-cpp prettyprint-override"><code>void setup() {
    pinMode(14,OUTPUT);

    TCCR1A = 0;            // normal mode
    TCCR1B = _BV(CS11);    // clock @ F_CPU/8
    TCNT1  = 0;            // clear timer
    OCR1A  = 1000;         // first COMPA match in 1000 cycles
    TIFR1  = _BV(OCF1A);   // clear interrupt flag
    TIMSK1 = _BV(OCIE1A);  // enable TIMER1_COMPA interrupt
}

ISR(TIMER1_COMPA_vect) {
    OCR1A += 1000;      // schedule next interrupt
    PINJ   = _BV(PJ1);  // toggle PJ1 = digital 14
}
</code></pre>
<p>Note that the line <code>OCR1A += 1000;</code> can cause a wrap around, but this is
no problem, as the addition wraps around in exactly the same way as the
timer itself.</p>
<p>The advantage of this technique is that you can schedule up to three
different tasks, having different periods, with only one timer. And you
can even schedule tasks in a non-periodic fashion.</p>
<p>That being said, if you do not need the timer for anything else other
than scheduling a single periodic task (or up to four tasks sharing the
same period), then a better solution is to let the timer handle the
period all by itself. Configure the timer in mode 12 or 14 (CTC or fast
PWM, with TOP = ICR1), and set the period with <code>ICR1 = 999;</code>. Then the
ISR is reduced to:</p>
<pre class="lang-cpp prettyprint-override"><code>ISR(TIMER1_COMPA_vect) { PINJ = _BV(PJ1); }
</code></pre>
<p>and you save a few cycles. This would be my preferred solution if I
absolutely had to output the signal on pin 14.</p>
<p>Note that this solution should give you the right <em>average</em> frequency,
but you will still have some jitter, because the interrupt can
occasionally be delayed by other interrupts. If you are free to choose
the output pin, then the best solution is to let the timer handle the
PWM generation all by itself. Set the timer to any PWM mode (fast PWM is
the simplest) and use one of the waveform generators in order to
generate the PWM signal. Only the PWM-capable pins (those marked with a
“~” symbol) can be used this way. For example, Timer 1 can output to
pins 11 (OC1A), 12 (OC1B) and 13 (OC1C).</p>
<p><strong>Edit</strong> answering the question:</p>
<blockquote>
<p>what is the goal of <code>TIFR1 = _BV(OCF1A); // clear interrupt flag</code>?</p>
</blockquote>
<p>Whenever the timer value matches the contents of the compare register
<code>OCR1A</code>, the interrupt flag <code>OCF1A</code> (Output Compare Flag of timer 1,
channel A) is raised. This flag controls the interrupt request when the
<code>OCIE1A</code> bit (Output Compare Interrupt Enable 1A) is set. The flag is
automatically cleared when the ISR runs, and can be manually cleared by
writing a logic 1 to it (yes, it is kind of backwards).</p>
<p>It is quite likely that the flag raises early in the start process,
while the Arduino library is initializing. If the flag is up when you
enable the interrupt, an interrupt request is immediately triggered. In
order to avoid that, the common practice is to clear the flag before
enabling the interrupt.</p>
 4143: <blockquote>
<p>My first question is: Is there a way to automatically control an output pin from the timer, without having to execute any line of code in my arduino program ?</p>
</blockquote>
<p>Yes. It's called PWM with a 50% duty cycle.</p>
<p>You need to read up on <strong>19. Output Compare Modulator (OCM1C0A)</strong> in the datasheet.</p>
<blockquote>
<p>My second question is: What is wrong on my calculation on my code ? I want 1khz and i get 990Hz</p>
</blockquote>
<p>You have to take into account the number of CPU cycles it takes to start executing your ISR. There's a short bit of code <em>before</em> your code is run that preserves any used registers on the stack and allocates any room needed for local variables. That takes time, and must be calculated into your timing factor.</p>
