# Arduino Forum

## Using Arduino => Programming Questions => Topic started by: ref110 on Jun 03, 2011, 02:25 pm

Title: Clock
Post by: ref110 on Jun 03, 2011, 02:25 pm
Hello,

I know that the Arduiono is running with a 16 mhz frequency. I want to connect the Arduino to the DDC 101. This element needs a frequency of 2 mhz. Now my question is, how can I create a 2mhz frequency?
Another possibility would be, that I take the 16 mhz and with ICs I chance this frequency into 2 mhz. But I do not really know where can I get the 16 Mhz pulse.

Thanks for your help!
Title: Re: Clock
Post by: PaulS on Jun 03, 2011, 04:22 pm
Hz is an abbreviation of Hertz. As a sign of respect to the man, you really should capitalize the H.

Quote
I know that the Arduiono is running with a 16 mhz frequency.

No, it doesn't. It runs at 16 MHz. M is an abbreviation of mega, while m is an an abbreviation of milli. A pendulum might swing at 16 milli-Hertz. The Arduino is a bit faster than that, though.

Quote
I want to connect the Arduino to the DDC 101.

Might be good to tell us what a DDC 101 is. It would be even better to provide a link.

Quote
This element needs a frequency of 2 mhz.

For what? This is pretty slow.
Title: Re: Clock
Post by: johnwasser on Jun 03, 2011, 04:35 pm
The ATmega168/328 has a clock output pin (CLK0) which will give you a buffered copy of the 16MHz clock when you program the CKOUT fuse.

There are also several counter/timers that can produce programmable signals on output pins.  I think you should be able to program one of those timers to generate a 2 MHz square wave.
Title: Re: Clock
Post by: davekw7x on Jun 04, 2011, 01:26 am

DDC101...how can I create a 2mhz frequency?...

To create a jitter-free constant frequency like that ADC likes to see, you can use one of the ATmega Output Compare register output pins.  The exact frequency isn't terribly critical, but you want the clock to be as "clean" as possible.  I mean, this is a 20-bit converter for goodness sake!

Understanding the timer registers from the data sheets takes a little (maybe more than a little for some of us) study, but the knowledge will make stuff like this "easy."

To get you started, here's a program that I use to create 2 MHz on Arduino Pin 11 of my Duemilanove.  Experiment with the timer register settings to see some of the things you can do with the simplest timer in its simplest mode.  Larger values of ocr2aval slow the clock down.  Sometimes that makes things quieter.
Code: [Select]
`//// Use of timer2 to generate a signal for a particular frequency on pin 11//// davekw7x//const int freqOutputPin = 11;   // OC2A output pin for ATmega328 boards//const int freqOutputPin = 10; // OC2A output for Mega boards// 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 OCR2A determines the frequency according to the// following formulas://// With no prescaling, an ocr2val 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 belowconst int prescale  = 1;const int ocr2aval  = 3; // The following are scaled for convenient printing//// Period in microsecondsconst float period    = 2.0 * prescale * (ocr2aval+1) / (F_CPU/1.0e6);// Frequency in Hzconst float freq      = 1.0e6 / period;void setup(){    pinMode(freqOutputPin, OUTPUT);    Serial.begin(9600);    // Set Timer 2 CTC mode with no prescaling.  OC2A toggles on compare match    //    // WGM22:0 = 010: CTC Mode, toggle OC    // WGM2 bits 1 and 0 are in TCCR2A,    // WGM2 bit 2 is in TCCR2B    // COM2A0 sets OC2A (arduino pin 11 on Uno or Duemilanove) to toggle on compare match    //    TCCR2A = ((1 << WGM21) | (1 << COM2A0));    // Set Timer 2  No prescaling  (i.e. prescale division = 1)    //    // CS22:0 = 001: Use CPU clock with no prescaling    // CS2 bits 2:0 are all in TCCR2B    TCCR2B = (1 << CS20);    // Make sure Compare-match register A interrupt for timer2 is disabled    TIMSK2 = 0;    // This value determines the output frequency    OCR2A = ocr2aval;    Serial.print("Period    = ");    Serial.print(period);    Serial.println(" microseconds");    Serial.print("Frequency = ");    Serial.print(freq);    Serial.println(" Hz");}void loop(){    // Do (almost) anything you want here.  Just don't do analogWrite to    // pins controlled by Timer 2.  In fact, don't do anything that messes    // with the registers in Timer 2.}`

In addition to creating a jitter-free clock, using this method requires no program intervention after the initial setup, so the "good stuff" in your program will not be bothered by anything here.

I recommend a small resistor between the Arduino Pin 11 output and the external device.  I usually use 100 Ohms or so to keep the ringing off of the output clock for short runs.  Don't forget to connect the external device ground to Arduino ground.

Regards,

Dave
Title: Re: Clock
Post by: ref110 on Jun 04, 2011, 10:58 am
Hello,

thanks for your posts.

The Datasheet of the DDC101 can be found here:

http://www.datasheetcatalog.org/datasheet2/a/0afl0ect5614dah40t143t8gajky.pdf

I´m not really sure if the clock is very solid if I take your advice. What do you think?

The other possibility would be, that I create the 2MHz frequency with FlipFlops (ICs). A friend told me, that that would be better.
Where can I get an 16MHz frequency? Has the Arduino Uno a clock output, or how can I create 16MHz at a pin?#

Thanks for your answers.
Title: Re: Clock
Post by: davekw7x on Jun 04, 2011, 02:57 pm

I´m not really sure if the clock is very solid...
It is as solid and jitter-free as the Arduino clock.

Quote from: ref110
if I take your advice. What do you think?

I was not necessarily offering advice.  (I'll save that for the end of this post.)  You asked for a way to get a 2 MHz clock.  I showed how you can generate a 2 MHZ clock using an Arduino timer.

Quote from: ref110

The other possibility would be, that I create the 2MHz frequency with FlipFlops (ICs). A friend told me, that that would be better.
Define "better." Has your friend ever used this ADC?  Where did he/she get the clock?  Has your friend ever created a clock using an Arduino timer for any purpose?  Any purpose at all?

I would be interested in knowing in what way a "clock created with flip-flops" is "better."  I mean, I can see where a clock generated by a properly specified oscillator module rather than one derived from the Arduino clock might have better frequency stability and accuracy and better jitter characteristics, but would this necessarily make it "better" for your application than a clock generated with an Arduino timer???

Quote from: ref110

Where can I get an 16MHz frequency?
Ask your friend to show you what he/she has in mind and to cobble one up for you

Quote from: ref110
. Has the Arduino Uno a clock output, or how can I create 16MHz at a pin?
If you are going to use the Arduino clock as a source for your derived 2 MHz output, what's the point?  If you don't need Timer 2 for other things, it costs nothing and requires no external circuitry to create a 2 MHz clock on pin 11.

If you need pin 11 for something else, you can consult the ATmega data sheet to find what other pin can be used with Timer 2.

If you need Timer 2 for something else, you can, maybe, use Timer 1 with either of its Output Compare pins.

If you need both Timer 1 and Timer 2 for other things and/or you need the associated Output Compare pins for something else, or if the Arduino-generated clock is not, for any reason, suitable for you application, then my advice is to buy a 2 MHz oscillator to connect to your ADC.  See Footnote.

Regards,

Dave

Footnote:
Bottom line: Good advice from a good friend may be more valuable than advice from a stranger on the Internet.  My advice is free and is freely given.  It is worth exactly as much as you want it to be worth.

davekw7x: "Over and out."
Title: Re: Clock
Post by: ref110 on Jul 01, 2011, 03:42 pm
Hi,

ok I have to say sorry. Your solution is better than the other. Because I´m new, I do not really understand which other pins are also possible. I need pin 10 to 13 for SPI. So how can I get 2MHz frequency on another pin than these?

Thanks for your help!