Frequency Generator

Hello

I am trying to make the Arduino put out what ever frequency of square wave pulses, 50% duty, that I want between 1 hz and 20 khz.

Currently I have it set up with the delayMicroseconds() for half the period (ON) and half the period (OFF).

The problem with this is that you can only make frequencies that have even, integer periods.

For example,

50 us on and 50 us off, for a period of 100 us, is 10,000 Hz, but
49 us on and 49 us off, for a period of 98 us, is 10,204.1 Hz

So there is no way to tune into the frequencies in between.

I have searched the Arduino site, and forums, and come up dry for an obvious solution.

Knowing the Arduino has a clock of 16 MHz, it should be possible to get this accuracy of frequencies.

What would make life easy is a delayNanoseconds() function, but I haven't found one.

I come to you humbly, asking for advice, help, clues, and if possible, a solution!

Thank you
Donald

This might be useful as one means of generating frequencies using DDS principals:

http://www.adrianfreed.com/content/arduino-sketch-high-frequency-precision-sine-wave-tone-sound-synthesis

Lefty

A synchronious microprocessor's clock just controlls the fastest activity in the chip; there cannot be any controlled activity faster, which does not imply that there are noticeable activities at all at that speed, of half the speed, or 1/100 that speed.

It is in fact possible to lay-out assembly instructions to generate a very precise cyclical signal pattern. as this will have do be done in a loop, some care has to be taken to avoid "jitter", but I think it will be possible to generate any square wave you want with a precision of 1 us. This can be done by explicitly generating the code to be executed on the fly. Such technique (self modifying code!) is much easier on a processor where the instructions are executed from RAM in the first place; I have once written a similar program for the Parallax Propeller.

A better solution, if only square waves are to be considered, is using a hardware timer which can be programmed up to a precision of 62.5 ns (I think, or maybe 125 ns). You can easily out check this by using the Tone library!

However the tiny 16 bit timers now show a restriction in the other direction: it will be not possible to generate extreme LOW frequencies. A technique call "pre-scaling" must be used, which reduces the resolutions down to 64/16M = 4 us. And even at this resolution the lowest frequency generated is 1/(24u * 65,536) =1/(2 0.256) s = 2 Hz

As well as DDS, a useful search term is "phase accumulator"

what can you guys tell me about how timers work and interrupts

know anything about using nanoseconds too?

anyone with experience that can offer some help please?

I can tell you a lot of how timers work - and also the rest of the AVR hardware - and it's also in the datasheet :slight_smile:
But what do you want to know exactly?? I think I made all relevant statements in my posting #2. There are typos in it, but I think it's not absolutely unreadable...