Generating a Clock at 20 MHz

SpasticHatchet

New member
I used the following code to get 20 MHz, but when I checked with an oscilloscope, I was down around 18.9 MHz. I'm really not needing anything fancy. I used the same method to get to 10 MHz and that was right on. I've been browsing the forum trying to understand what people are doing to get faster speeds but it alludes me thus far.
C++:
#define CLK 5
void setup() {
  analogWrite(CLK, 0);
  pinMode(CLK, OUTPUT);
 
  unsigned long freq = 20000000;
 
  analogWriteFrequency(CLK, freq);
  analogWrite(CLK, 128);
}
 
The PWM hardware can only create frequencies using integer division of the base clock, which is 150 MHz.

20 MHz would require divide by 7.5, which isn't an integer. Divide by 8 gives 18.75 MHz. My guess is that's the frequency you're really seeing with 18.9 MHz.
 
The PWM hardware can only create frequencies using integer division of the base clock, which is 150 MHz.

20 MHz would require divide by 7.5, which isn't an integer. Divide by 8 gives 18.75 MHz. My guess is that's the frequency you're really seeing with 18.9 MHz.
I'm a little confused on that. The 24 MHz crystal with PLL gets it up to 600 MHz, right? That's the CPU setting I have selected. 20 MHz is an integer factor of 600 MHz.
 
That's the CPU clock. The main peripheral clock is (typically) 150MHz, regardless of the chosen CPU speed.
 
Back
Top