Arduino 1 MHz/ 2MHz / 4MHz / 8 MHz CLOCK OUTPUT

Hey everyone

So I found this thread where matt_s provided a code to output 8MHz from an Arduino Micro. I want to hook up my Arduino Micro to my GBA's oscillator slot so I can control it's clock speed from the Arduino, with a few buttons. I want to be able to output 1MHz, 2MHz, 4MHz, and 8MHzl. Here is the code that output's 8MHz.
All I need is help modifying it to out put 1/2/4 MHz. Thank you!

const int freqOutputPin = 9;   // OC1A output pin for ATmega32u4 (Arduino Micro)
const int ocr1aval  = 0; 

void setup()
{
    pinMode(freqOutputPin, OUTPUT);
    
    TCCR1A = ( (1 << COM1A0));
   
    TCCR1B = ((1 << WGM12) | (1 << CS10));

    TIMSK1 = 0;
    
    OCR1A = ocr1aval;    
}

void loop()
{
   
}

Thank you!

I think, you need just to set the value of ocr1aval. Try 0, 1, 2, 4. It is compare register for timer. Higher value = slower frequency. I'm also recommending to read the ATmega datasheet, Timer/Counter1 chapter.

Budvar10:
I think, you need just to set the value of ocr1aval. Try 0, 1, 2, 4. It is compare register for timer. Higher value = slower frequency. I'm also recommending to read the ATmega datasheet, Timer/Counter1 chapter.

Thanks so much!

0, 1, 3, 7 I think.

Damn, you are right.

DrAzzy:
0, 1, 3, 7 I think.

Just to clarify:

0 = 8MHz
1 = 4MHz
3 = 2MHZ
7 = 1MHz

?

Yes. It is counter top value for interrupt generating and have to be 'required-1'.

Don't mean to revive an old(er) thread but could I change the value of OCR1A inside of void loop()?

Like this:

const int freqOutputPin = 9;   // OC1A output pin for ATmega32u4 (Arduino Micro)
const int ocr1aval  = 0; 

void setup()
{
   pinMode(freqOutputPin, OUTPUT);
   
   TCCR1A = ( (1 << COM1A0));
  
   TCCR1B = ((1 << WGM12) | (1 << CS10));

   TIMSK1 = 0;
   
   OCR1A = ocr1aval;  

   pinMode(2, INPUT);

   pinMode(3, INPUT);  
}

void loop()
{

if (digitalRead(2) == HIGH){
OCR1A = 1;
}

else if (digitalRead(2) == HIGH){
OCR1A = 3;
}
  
}

Bumping, since I'm assuming no one has seen the most recent post.

Yes, you can change OCR1A inside of loop but disable interrupts before and enable them after change.

I'm still have a bit trouble understanding..... Can you show me some example code? Thanks!

?

if(digitalRead(2) == HIGH) {
  cli();
  OCR1A = 1;
  sei();
} else if(digitalRead(3) == HIGH) {
  cli();
  OCR1A = 3;
  sei();
}

Does it help?

Ah, yes! Thank you!

leotakacs:
Don't mean to revive an old(er) thread but could I change the value of OCR1A inside of void loop()?

Why don't you try it and see what happens?

Yes, you can change OCR1A inside the function named loop. Whether it does what you want you can see by experimentation.

bumb! Anyone could help me port this excellent oscillator to the ATTiny13 using MicroCore?

Budvar10:
?

if(digitalRead(2) == HIGH) {

cli();
  OCR1A = 1;
  sei();
} else if(digitalRead(3) == HIGH) {
  cli();
  OCR1A = 3;
  sei();
}



Does it help?

How change this code that the value at freqOutputPin = 0 until digitalRead () == HIGH?

Setting the clock source to 0 will stop the timer.
Disabling the interrupt will stop the generation of the output signal, even if the timer is running.

In both cases, you can set the output manually using digitalWrite.

Have a look how it's all done in the tone library. Keywords to search for are void tone, void notone, prescalarbits and void disableTimer.

The tone library does not support the high frequencies due to the 'unsigned int' that is used for the frequency parameter; if you change the type of the parameter to uint32_t (or 'unsigned long'), you can use the tone library.

I don't have a means to test it (no measurement equipment).