20MHz Clock and PWM signal

I just started working with the arduino and I am trying to produce a 10-33 MHz output clock to interface with a Quadrature Decoder. I also want to be able to produce a PWM signal of atleast 10MHz but preferably at 20MHz.

I have been looking around because I assumed custom output clocks and PWM signals are pretty common. However, I cant get the code I have found to work with the Due. I found this segment of code that produces a 1MHz square wave.

void setup()
   DDRD = (1<<DDC0); 
   TIMSK |= ((1<<OCIE0) | (1<<TOIE0)); // use both interrupts
   OCR0 = 10; // 10 out of 256 means very short on period (low voltage) 
   TCCR0 = (1<<CS00); // timer on - nice high PWM frequency 

void loop() {


but I get the error messages:

Arduino: 1.5.8 (Windows 7), Board: "Arduino Due (Programming Port)"

sketch_oct20a.ino: In function 'void setup()':
sketch_oct20a.ino:8:4: error: 'DDRC' was not declared in this scope
sketch_oct20a.ino:8:15: error: 'DDC0' was not declared in this scope
sketch_oct20a.ino:9:4: error: 'TIMSK' was not declared in this scope
sketch_oct20a.ino:9:18: error: 'OCIE0' was not declared in this scope
sketch_oct20a.ino:9:31: error: 'TOIE0' was not declared in this scope
sketch_oct20a.ino:10:4: error: 'OCR0' was not declared in this scope
sketch_oct20a.ino:11:4: error: 'TCCR0' was not declared in this scope
sketch_oct20a.ino:11:16: error: 'CS00' was not declared in this scope
Error compiling.

As for the PWM I cant find anything.

I was thinking I could add delays between the turn on and turn off but because the delay() function only works in milliseconds, I cant get anywhere near my target frequencies. I also suspect that inserting delays like this might stick me in a loop that doesn’t allow any of my other code to run but I’ll work on that once I can get these pieces working.

Did I miss some website that has code for the Due or does anyone have any ideas for what I can do?

Also, is there a website or document somewhere that explains how to reference counters/timers/clocks in the code? I saw a few people say to read the AVR datasheet but that didn’t help me figure out what any of the above code meant.


That code is AVR specific. Look at the definition of analogWrite in the sources for how things get set up, there are helpful functions in samlib you can use to convert frequencies to setup relevant registers.

Hey, I am feeling really slow but what do you mean by “the definition of analogWrite in the sources”? Do you mean in the references? Also, what is and where can i find the samlib?


You have the Arduino software, then you have the sources. Its open source... You are encouraged to look inside :) A tool like grep is invaluable for navigation I find.