Generating 8 MHz clock using Arduino Micro

Hello folks,

I’m stuck trying to add an 8 MHz clock on pin 9 to my Arduino Micro. I’d like to use pin 9 because pins 10:12 are already in use for something else and I’d rather not change my board layout. As far as I can see, I can’t use TimerOne library because it’s limited to 1 MHz max. frequency. But I found this thread and tried to adapt it to my needs: http://forum.arduino.cc/index.php/topic,105066.0.html

What I actually get on pin 9 is a clock frequency of 125 Hz (8ms period), so I guess I got the timer registers wrong. Could anyboy point me in the direction I need to look?

Thank you very much for your time!
matt

//
//
// Use of timer1 to generate 8 Mhz clock on pin 9
//
//

const int freqOutputPin = 9;   // OC1A output pin for ATmega32u4

// Constants are computed at compile time

// If you change the prescale value, it affects CS22, CS21, and CS20
// For a given prescale value, the eight-bit number that you
// load into OCR1A determines the frequency according to the
// following formulas:
//
// With no prescaling, an ocr1val of 3 causes the output pin to
// toggle the value every four CPU clock cycles. That is, the
// period is equal to eight slock cycles.
//
// With F_CPU = 16 MHz, the result is 2 MHz.
//
// Note that the prescale value is just for printing; changing it here
// does not change the clock division ratio for the timer!  To change
// the timer prescale division, use different bits for CS22:0 below
const int prescale  = 1;
const int ocr1aval  = 3; 
// The following are scaled for convenient printing
//



void setup()
{
    pinMode(freqOutputPin, OUTPUT);
    
 
    // Set Timer 1 CTC mode with no prescaling.  OC1A toggles on compare match
    //
    // WGM12:0 = 010: CTC Mode, toggle OC 
    // WGM2 bits 1 and 0 are in TCCR2A,
    // WGM2 bit 2 is in TCCR2B
    // COM1A0 sets OC1A (arduino pin 11 on Uno or Duemilanove) to toggle on compare match
    //
    TCCR1A = ((1 << WGM12) | (1 << COM1A0));

    // Set Timer 1  No prescaling  (i.e. prescale division = 1)
    //
   
    TCCR1B = (1 << CS10);

    // Make sure Compare-match register A interrupt for timer1 is disabled
    TIMSK1 = 0;
    // This value determines the output frequency
    OCR1A = ocr1aval;

    
}


void loop()
{
    // 
}

Change your fuses so the system clock is output, see 6.9 Clock Output Buffer The device can output the system clock on the CLKO pin. To enable the output, the CKOUT Fuse has to be programmed. This mode is suitable when the chip clock is used to drive other circuits on the system. The clock also will be output during reset, and the normal operation of I/O pin will be overridden when the fuse is programmed. Any clock source, including the internal RC Oscillator, can be selected when the clock is output on CLKO. If the System Clock Prescaler is used, it is the divided system clock that is output.

If you don't want system clock at 8 MHz, add an external flip flop to divide the 16 MHz in 2.

Thank you for your suggestion, but I’d like to keep system clock at 16 MHz. Adding external components should also be the very last resort. I’ve done some reading and can’t see anything that would prevent one from using a timer to generate the 8 MHz clock.

So I think I just haven’t got the register settings right yet, or am I missing something?

Never tried that myself, hopefully someone who has will chime in.

fOCnA = fclk_I/O / ( 2 * N * (1 + OCRnA) )
fclk_I/O = 16 MHz
N = 1
OCRnA = 3
fOCnA = 16000000 / ( 2 * 1 * (1 + 3) )
fOCnA = 2000000 = 2 MHz

So, there are two problems. The first is that you are using the wrong value for OCRnA. The second is that the output frequency is 125 Hz instead of 2 MHz.

The first problem is easy to solve...

const int ocr1aval  = 0;

For the second problem, how did you measure the frequency?

I changed the value for ocr1val from 3 to 0, but can’t see any change in output signal. I’m checking the signal on pin 9 using my DSO (UNI-T UTD2042C). Yesterday I was just looking at the waveform, today I’m using the built-in measurement capabilities. Turns out that the clock I’m getting on pin 9 isn’t 125 Hz but actually 104,1Hz.

I’ve found the problem: The bit WGM12 is actually in TCCR1B, not TCCR1A. The code below is working, generating a 8 MHz clock on pin 9 (Arduino Micro). Thanks to everybody for the help!

//
//
// Use of timer1 to generate 8 Mhz clock on pin 9
//
//

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

// Constants are computed at compile time

// If you change the prescale value, it affects CS22, CS21, and CS20
// For a given prescale value, the eight-bit number that you
// load into OCR1A determines the frequency according to the
// following formulas:
//
// With no prescaling, an ocr1val of 0 causes the output pin to
// toggle the value every  CPU clock cycle. That is, the
// period is equal to two clock cycles.
//
// With F_CPU = 16 MHz, the result is 8 MHz.
//
  
// To change the timer prescale division, use different bits for CS22:0 below

const int ocr1aval  = 0; 



void setup()
{
    pinMode(freqOutputPin, OUTPUT);
    
 
    // Set Timer 1 CTC mode with no prescaling.  OC1A toggles on compare match
    //
    // WGM12:0 = 010: CTC Mode, toggle OC 
    // WGM2 bits 1 and 0 are in TCCR1A,
    // WGM2 bit 2 and 3 are in TCCR1B
    // COM1A0 sets OC1A (arduino pin 9 on Arduino Micro) to toggle on compare match
    //
    TCCR1A = ( (1 << COM1A0));

    // Set Timer 1  No prescaling  (i.e. prescale division = 1)
    //
   
    TCCR1B = ((1 << WGM12) | (1 << CS10));

    // Make sure Compare-match register A interrupt for timer1 is disabled
    TIMSK1 = 0;
    // This value determines the output frequency
    OCR1A = ocr1aval;

    
}


void loop()
{
    // 
}

Thank you for the follow-up.

matt_s:
I’ve found the problem: The bit WGM12 is actually in TCCR1B, not TCCR1A. The code below is working, generating a 8 MHz clock on pin 9 (Arduino Micro). Thanks to everybody for the help!

//

//
// Use of timer1 to generate 8 Mhz clock on pin 9
//
//

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

// Constants are computed at compile time

// If you change the prescale value, it affects CS22, CS21, and CS20
// For a given prescale value, the eight-bit number that you
// load into OCR1A determines the frequency according to the
// following formulas:
//
// With no prescaling, an ocr1val of 0 causes the output pin to
// toggle the value every  CPU clock cycle. That is, the
// period is equal to two clock cycles.
//
// With F_CPU = 16 MHz, the result is 8 MHz.
//
 
// To change the timer prescale division, use different bits for CS22:0 below

const int ocr1aval  = 0;

void setup()
{
    pinMode(freqOutputPin, OUTPUT);

// Set Timer 1 CTC mode with no prescaling.  OC1A toggles on compare match
    //
    // WGM12:0 = 010: CTC Mode, toggle OC
    // WGM2 bits 1 and 0 are in TCCR1A,
    // WGM2 bit 2 and 3 are in TCCR1B
    // COM1A0 sets OC1A (arduino pin 9 on Arduino Micro) to toggle on compare match
    //
    TCCR1A = ( (1 << COM1A0));

// Set Timer 1  No prescaling  (i.e. prescale division = 1)
    //
 
    TCCR1B = ((1 << WGM12) | (1 << CS10));

// Make sure Compare-match register A interrupt for timer1 is disabled
    TIMSK1 = 0;
    // This value determines the output frequency
    OCR1A = ocr1aval;

}

void loop()
{
    //
}

Is there a way to modify the code to output 4MHz? I’m pretty good with Arduinos but I am SO lost with this…

leotakacs: Is there a way to modify the code to output 4MHz? I'm pretty good with Arduinos but I am SO lost with this....

1) Don't revive an old thread like this.

2) Change the value written to OCR1A.

  1. Do not cross-post. Responses belong here…