Highest frequency using Arduino Timers

Can anyone tell what is the highest frequency that can be produced using Arduino???
My goal is 200KHz but I'm stucked with 20Khz. I've tried using different timers (0 to 4 ) and boards (UNO, nano, Mega 2560) and the result is the same. Up to 20KHz they work perfectly fine, but after that, timers don't work properly. For a board using a crystal of 16MHz, 200KHz shouldn't be a problem (theorically) but I'm missing something I can't figure out..
Here's my code for Timer1 using Mega 2560:

#define     pinLED  10 
void setup()
{
  pinMode(pinLED, OUTPUT);
  // TIMER 1 for interrupt frequency 2000000 Hz:
  cli(); // stop interrupts
  TCCR1A = 0; // set entire TCCR1A register to 0
  TCCR1B = 0; // same for TCCR1B
  TCNT1  = 0; // initialize counter value to 0
  // set compare match register for 2000000 Hz increments
  OCR1A = 79; // = 16000000 / (1 * 2000000) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS12, CS11 and CS10 bits for 1 prescaler
  TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10);
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  sei(); // allow interrupts
}

ISR(TIMER1_COMPA_vect)
{
  //interrupt commands for TIMER 1 here
  TCNT1=0;
  digitalWrite(pinLED, digitalRead(pinLED)^1); 

}

void loop()
{

}

What kind of Arduino? There are many types of Arduino board and many types of Arduino compatible board. Some have clock speeds of 8MHz, some have clock speeds of 600MHz. That will make a big difference to the highest frequency they can make using their timers.

Yep, the obvious, the highest frequency depends on the 'Arduino' and whether you include boards that can be programmed through the Arduino IDE that are not actually 'Arduinos'.

Or maybe the question was "what is the highest frequency using the timers on the Arduino Mega" ?

digitalWrite() and digitalRead() instructions are exremely slow.
For fast output to the pin you have to use direct port access via PORT instruction.
For doing this even faster you should avoid touching the pin manually and use hardware pin output via the timers.

The maximum frequency that the AVR can outputs to the pins is a half of the clock speed, so the Nano with 16MHz clock can generate signal of 8 MHz

try to change your ISR this way:

ISR(TIMER1_COMPA_vect)
{
  //interrupt commands for TIMER 1 here
  TCNT1=0;
 PINB = (1<<4);    // Mega pin 10 (PB4)

}
1 Like

There is also the interrupt latency of using the compare match interrupt. If you can use the output of one of the hardware output pins associated with the timer that can be eliminated.

:smiley_cat:

1 Like

I was looking for a general response, like using a formula where you consider the clock. As I explained, I'm trying with 3 different boards but the all have the same clock value.

8MHz can be obtained using assembly right? BUT I'm trying direct access to the port to see how much it can improve. Thanx for the tip.

I'm running out of pins but the idea is worth giving a chance.

Not in assembly only, also in C language, if I understood your question correctly

1 Like

By assigning directly the port value increased the speed by a factor of 10. Right now I'm on my goal frequency but as you said, limit should be half the clock frequency. Now I can't pass the 200Khz barrier. Do you know if there's a document/table where I can consult how many clock cycles are needed to execute an instruction???

Sorry I misled you. 8 MHz will be the pin inversion frequency, respectively, the maximum frequency of the generated signal is 4 MHz

4 MHz Arduino Nano (Atmega328) code:

void setup() {
  // put your setup code here, to run once:
  TCNT1 = 0;
  TCCR1B = B00001001;  // CTC mode, Prescaler =1
  TCCR1A = B01000000;  // Toggle OC1A on match
  OCR1A = 1;             // Toggle frequency = 16 MHz / (1+1) = 8MHz
  pinMode(9, OUTPUT);
}

void loop() {}

Note that hardware generation is possible only on pins, that belongs to timer output channels.
For example, for Timer1 that pins are 9 (OC1A) and 10 (OC1B)

1 Like

This link may be useful

Section 31 shows clock cycles/instruction These are RISC processors...

:smiley_cat:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.