Create 16MHz output

hey there
I have an Arduino UNO which I wish to create a 16MHz frequency on pin8 OUTPUT.
here is my code`//https://www.instructables.com/id/Arduino-Timer-Interrupts/





//storage variables
boolean toggle0 = 0;

void setup(){
  
  //set pins as outputs
  pinMode(8, OUTPUT);

cli();//stop interrupts


  TCCR0A = 0;// set entire TCCR2A register to 0
  TCCR0B = 0;// same for TCCR2B
  TCNT0  = 0;//initialize counter value to 0
  
  OCR0A = 0;
  // turn on CTC mode
  TCCR0A |= (1 << WGM01);

  TCCR0B |= (1 << CS00);
  // enable timer compare interrupt
  TIMSK0 |= (1 << OCIE0A);

sei();//allow interrupts

}//end setup

ISR(TIMER0_COMPA_vect){

  if (toggle0){
    digitalWrite(8,HIGH);
    toggle0 = 0;
  }
  else{
    digitalWrite(8,LOW);
    toggle0 = 1;
  }
}

void loop(){
  //do other things here
}`

but when I measure pin8 frequency it is around 59.23KHz... why?

Only this command (digitalWrite) is needed approximately 4 to 6 microseconds on 16MHz Arduino UNO/Nano to execute.

1 Like

Quick answer: how much time do you think is taken by your ISR?

Also try a higher TOP value. I didn't check you control bits, you should help others by adding comments about your prescaler and mode settings.

16MHz output from a 16MHz Microcontroller is not possible, the absolute best is 8MHz, assuming you have no interrupts.

2 Likes

But --
In Mode-15 fast PWM at DPin-9, probably it is possible (narrow spikes):

f = 16 MHz/(Nx(1+TOP))
==> f = 16 MHz/(1x1)
==> f = 16 MHz (max)

For continuous 16 MHz output on the ATmega processors with a 16 MHz crystal or resonator, set the CKOUT fuse. It will appear on the CLKO pin.

4 Likes

To get 16 MHz CLKO (Clock Output) signal at pin-14 (Fig-1), it requires taking out the MCU from the socket (if DIP package, Fig-2) of UNO Board, get a parallel programmer, program the CKOUT fuse bit, and then put back the chip on the socket. Is it practically doable? (Some UNO Boards contain soldered TQFP packaged (Fig-2) MCU for which there no easy way to program the CKOUT fuse bit. )
atmega328pPicture
Figure-1:
UNODipPQ

Figure-2:

1 Like

higher TOP values result in lower frequencies right?

you're right
so how can I generate 8MHz?

can you explain it with a code?

how can I do that? more explanations maybe...

1 Like

it really helped me! huge thanks :pray: :pray: :pray: :pray: :pray: :pray: :pray:

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