1.02 MHz clock output

Hello, for a project of mine I need to output a 1.02 Mhz clock signal on Arduino Mega.

I have been able to generate a 1.0 Mhz clock with timer 4, but is it possible to get any closer to 1.02 Mhz? Maybe combining two clocks?

Edit: this code i wrote for 1 Mhz

 TCCR4A = _BV(COM4B1) | _BV(WGM41) | _BV(WGM40); 
  TCCR4B = _BV(WGM43) | _BV(WGM42) | _BV(CS41); // prescaler = 8 
  
  // Set the output compare register for Timer 4
  OCR4A = 1; 

Try with
https://www.arduinoslovakia.eu/application/timer-calculator

Add: It looks like the closest value you can get is 1.066 MHz.

This should get 1.23 MHz with an 8 bit timer on the UNO (16MHz crystal driven)

If your Mega has a 8 bit timer you could see if you can set it similar.

/**
  * URL: https://dbuezas.github.io/arduino-web-timers/#mcu=ATMEGA328P&timer=0&timerMode=FPWM&topValue=OCR0A&OCR0A=11
  * Mode     : FPWM
  * Period   : 812.5 ns
  * Frequency: 1.23077 MHz
  * Outputs  : none
  */
void setup(){
  noInterrupts();
  TCCR0A = 
    1 << WGM01 |
    1 << WGM00;
  TCCR0B = 
    1 << WGM02 |
    1 << CS00;
  OCR0A = 12;
  interrupts();
}

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