Micro Function Generator

By Murray Greenman ZL1BPU

An audio function generator and sweep generator with wide range, high accuracy and great versatility.

It's really great to have a good audio signal generator in the shack, but they're not cheap, and in addition, take up valuable bench space and tend not to be very versatile. There are two main types: the most common is the Wein Bridge oscillator variety, which has low distortion, but offers only sinewave output and can suffer from amplitude bounce as you change frequency. The type which offers greater versatility of waveform and frequency range, the usual function generator type, typically lack stability and accuracy. This unit meets all these requirements, and is small - fits in the palm of your hand - and is battery operated. It also has no "front panel" - you control it from a PC or computer terminal.

This is one of a family of three signal generators which use identical hardware:

The generators are quite inexpensive to build - just two or three ICs and a couple of transistors. One of the ICs is an AVR RISC microcontroller - inexpensive, and quite powerful. It operates in an unusual manner, using a Direct Digital Synthesis (DDS) technique developed by Jesper Hansen to create very accurate signals. Some of the features of the Function Generator are: Many of the parameters, such as frequency range, step size and frequency response can be tailored to suit other applications. The picture below, of the simple PC control panel for the generator, shows a 4MHz version in use, with an upper limit of about 133 kHz. The absolute upper frequency limit of any generator using this microcontroller technique is set by the maximum clock frequency (currently 16 MHz). The upper limit of the generator is thus about 500 kHz. There is no lower limit. A 12.8MHz version for use as an Exciter for the Amateur LF bands is also available.

The PC Control Panel
Click on image to view full size


Power for the unit comes from a tiny 5V regulator, which runs from a DC supply (battery or AC adaptor) with an output of between 6V and 15V. The supply is reverse voltage protected. The RS232 communications with the PC uses simple transistor level shifters. The transmit signal from the microcontroller requires a negative voltage supply, and for simplicity, this voltage is derived from the mostly idle PC transmit data line, which is normally negative. Communications is half - duplex, using a 19200 bps data rate (19200-N-8-1). Except at start-up, the generator only replies when spoken to.

The microcontroller, an Atmel AT90S2313-10PC, has a very simple main program consisting of a very tight and very fast sampling process, taking data from one of four tables at a fixed rate, to place the data on an output port. The Direct Digital Synthesis technique works not as you would expect, by stepping sequentially through a table of waveform values at a variable rate, but instead uses a fixed very high frequency (about 1 MHz) sampling rate, and a user controlled step size to address the table.

The counter used to control the table address is called the phase accumulator, and this accumulator and the user controlled step value are both very large (24 bit) numbers. The most significant eight bits of the accumulator select the correct table value at each sample point. The eight bit digital values at the port are converted to an analog waveform by a simple R-2R resistive ladder. The required waveform is reconstructed by an anti-aliasing interpolative filter at the output (otherwise known as a low pass filter!)

While the synthesizer is running, nothing is allowed to interrupt the process, as interruptions would slow down or distort the waveform. Commands to change the operating parameters are received from the serial communications link, which operates via an interrupt and stops the generator while the command is processed. The interruption can be as short as 2µs (in sweep mode), or as long as several milliseconds, depending on the command sent. At audio frequencies the glitch in the generated waveform caused by the shorter commands is hardly noticeable.

1kHz sine spectrum
1kHz sine wave signal spectral response is very clean

Commands are provided to change the frequency, offset the frequency (by microscopic amounts), change the output level, the waveform, and to control the sweep function. Commands to the microcontroller are in hexadecimal form, but the PC program does the necessary translations for the user. However, if you wish to write your own control program (please do!), you will need to know the control syntax.

Add offset of xx resolution steps, where xx is "00" to "FF" in two hexadecimal characters.

Set frequency to this value times the resolution. The value hhmmll is a 24 bit binary number expressed as six hexadecimal characters, "00" to "FF".

Simple help message listing these commands.

Sets the waveform mode, where n is a number 0 to 3.

Sets the output signal level if the level controlled amplifier option is used. n is a hexadecimal number, "0" to "F".

Requests a message giving the current settings.

Store current settings. Saves FREQUENCY, MODE and POWER.

Sets sweep step size, where mm is "00" to "FF" in two hexadecimal characters. Each step is 256 x mm resolution steps in the up-frequency direction. With an 8 MHz crystal, the range is 13.5 Hz/step (W01) to about 3.5 kHz (WFF). The start frequency for the sweep is set by the F command. There are 20 steps at 5ms intervals.

Each command consists of one command letter (upper or lower case) and zero to six following hexadecimal characters. No "Enter" or other control characters follow. The correct number of characters must be used. Unexpected characters, misunderstood commands and some impossible values will result in the Microcontroller responding "?", and the synthesizer will continue with the current settings. If a command is not completed, the synthesizer will remain stopped. Sending any wrong character (such as <CR>) will restore operation.

It is quite possible to send impractical frequencies (even negative frequencies) and impractical sweep step sizes. Negative frequencies (the hexadecimal values are signed, so those from FFFFFF downwards are negative) result in reversed table stepping - no difference will be noted except that the RAMP waveform will be negative going instead of positive. The Nyquist Frequency occurs with a setting of F800000 and would generate meaningless signals if used.

When the device is first powered up, it issues a message identifying itself. This includes the software version number. If using a dumb terminal, start the terminal before the generator. If using the PC software, start the generator first. The software will command the generator to 1 kHz sinewave mode when it starts up.

The superb sweep facility in action
The superb sweep facility in action

The frequency resolution of the Signal Generator is dependent on the microcontroller clock crystal frequency. The resolution is the Clock frequency in Hz divided by 9 x 224, or:

Resolution = fclock/150994944

For an 8.000 MHz crystal, the resolution is 8000000/(9 x 224) = 0.0529819064 Hz. For example, to set to 100 kHz, 100000 / 0.0529819064 = 1887436.8 = 1CCCCDHEX. Why is there a "9" in the calculation? Because there are nine clock cycles in every DDS synthesizer loop. 224 corresponds to the size of the biggest number in the accumulator register.

The upper frequency limit of signal generation is related to sampling theory. The Nyquist Frequency, the frequency above which it is not theoretically possible to reconstruct the sampled waveform accurately, is one half of the sample rate, so is given by:

Nyquist Frequency = fclock/(2 x 9)

In practical applications such as this it is not possible to approach the Nyquist Frequency without very fancy filters, and fclock/30 is considered a reasonable upper limit. With an 8MHz clock, this is about 267 kHz.

The unit can be very small. Even built on "Veroboard", it can be under 100mm square. The prototype was built on a DSE H5608 prototyping board (47 x 72mm), but with a board this small it would be a tight squeeze to fit in the power amplifier. The simplest version of the unit consists of the microcontroller, a 3-terminal regulator, two transistors, a crystal and about 25 resistors and capacitors. Most of this is shown on the Schematic Sheet 1. You will also need a 10k pot for level adjustment (not shown on the schematic, but it replaces R35 - the wiper goes to the output), a BNC connector for the output, another for the sweep trigger output, and a coaxial DC power connector. These are shown on the right of Schematic Sheet 2. The panel mount DB9 or DB25 RS232 connector is on the right of Sheet 1.

For the more advanced version with power amplifier and micro controlled output level, you will also need a TDA7052A audio amp, and a few more resistors and capacitors. For both models it is also a good idea to use sockets for the dual-inline ICs. The power amplifier is shown on Schematic Sheet 2. If the power amplifier is not used, delete all the related components, including the resistors related to the VC output of the microcontroller (left side of Sheet 1).

The unit can be built (with or without power amplifier) on a PCB made to the design offered here. The circuit board suits all three signal generators, and is doubles sided. The author does not have boards available for sale.

Silk Screen Layer    Bottom Copper Layer    Top Copper Layer

Software is offered for 4, 8, and 10MHz versions. Note that inexpensive TCXO reference oscillators are readily available and use of one of these would make this generator extremely stable. The connection point for a TCXO is shown on the schematic. If a TCXO is used, omit C2, C3 and X1.


Frequency Range
0 to 250 kHz in 0.0529819064 Hz steps (with 8.00 MHz crystal). Software for 4 MHz and 10 MHz is also available. Scale the frequency range and resolution accordingly.
Frequency Accuracy
Settable to within 0.03Hz of any required frequency in the above range (8MHz version).Setting accuracy dependent on calibration, achieved by trimming the crystal frequency and/or using PC software calibration feature. Warmup drift and ageing dependent on the quality of the reference crystal used. With ordinary micro crystals, typically 1 part in 106 warmup drift in the first hour and 1 part in 107 ageing per day.
Output Amplitude
Direct Output: 0 to +5V DC coupled output (direct from D-A converter) into high impedance load. 0 to +1V output into 1k Ohm load. Risetime 0.3µs into 5pf, 5µs into 100pF load.
Amplifier Output: AC coupled 2V RMS into 10 Ohm load. Output adjustable via microcontroller Pn command in 16 steps over about 40dB range. Output can also be turned off with the F000000 (zero frequency) command.
Waveform Accuracy
Eight bit digitally sampled waveforms. Step linearity is good and the D-A converter is monotonic with 256 discrete steps (no steps in a ramp output give values smaller than the previous step or larger than the next step). Harmonic distortion is very low. No harmonics, noise or spurs on output worse than -60dBc over the range 1 Hz - 20 kHz. LF range spurious output better than -40dBc up to 250 kHz.
Frequency Response
Direct Output: 0 to 100kHz 1dB with a simple single pole low pass filter, approx 250 kHz without filter. Performance could be improved with a better filter.
Amplifier Output: Depends on design. Approximately 1kHz to 200kHz with the TDA7052A device suggested - depends on the transformer used.
Offset Steps
0 to 255 resolution steps (0 to 13.49Hz at 8 MHz). Offset steps are lost when the frequency is changed.
Non-Volatile Data
Frequency, Mode and Power (output level) are remembered with the STORE command and restored on power-up.
SINE, SQUARE, TRIANGLE (positive and negative ramps), RAMP (positive sawtooth from 0V). Negative ramp can be generated using 24 bit signed negative frequency commands. For example F001000 M3 will generate a positive ramp; FFFEFFF M2 will generate a similar negative ramp.
Sweep Function
Start frequency set by F command. Step size 0 - 255 units, set by the Wmm command, where unit is 256 x resolution step size. With 8 MHz crystal this is about 13.5 Hz to 3.5 kHz per step. There are 20 discrete steps in the up-frequency direction, each 5ms wide. Total sweep time is thus 100ms. Steps are phase synchronous and steps are imperceptible at audio frequencies (see picture above). Sweep is available using all waveforms (although what use this might be escapes me as yet). If a 24 bit signed negative frequency is set with the F command, the sweep will be DOWN instead of UP, and the ramp waveform will be negative. The generator can be made to sweep through zero.
Sweep Sync
The sweep sync output PD6 (pin 11) is high during the lowest frequency step (frequency set by F command). The output is +5V CMOS, pulse width 5ms, risetime 10ns. The positive going edge of this output could be used to reset an integrator used to generate a ramp sweep waveform for an X-Y oscilloscope. The sweep and sync speed are fixed. The time between pulses is 95ms.
Power Supply
+6V to +15V DC at about 25mA. On-board +5V regulator and reverse voltage protection. Current consumption is to some extent load dependent. Current drawn is about 200mA with the amplifier option at full load. The amplifier operates from the unregulated supply.


This Signal Generator is unusual enough, offering the ability and agility to go anywhere in its operating range virtually instantly with no drift, chirp, or settling time. However, the built-in sweep function is even more unusual. While the number of discrete frequency steps is low (20) and the sweep speed fixed (200 steps / second, or 100ms per sweep), the step sizes can be changed over a wide range, from about 25 Hz to over 3 kHz, making the function useful at audio and LF frequencies.

The sweep is generated by a timer function, which adds the selected step to the operating frequency every 5ms, and returns to the starting frequency after the 20th step. A timer interrupt stops the generator very briefly in order to change the frequency, but because the interruption is very brief (about 2µs), the output effectively remains phase synchronous at all but the highest frequencies. Observe the sweep oscilloscope shots closely to see how good the signal is - the frequency steps are invisible.

The sweep step size and step duration were chosen as a reasonable compromise for audio and LF use. Using the SYNC output, a conventional Y-T oscilloscope can be externally triggered (triggered off the positive edge of the SYNC signal), offering a stable sweep display. The oscilloscope should be set to a sweep time of 10ms/division, which results in exactly one sweep across the screen, and two frequency steps per division.

Two commands control the sweep function. The F command sets the sweep start frequency, the frequency of the first of the 20 discrete steps. The W command sets the frequency difference between the steps. The 8-bit value given is added to the centre eight bits of the 24 bit frequency register, and so steps in increments of 256 times the basic resolution step size. For example, in the 8 MHz version, the command W01 will cause steps about 13.5 Hz apart, and a total sweep width of 3.5 kHz. The command W4A will cause 1 kHz steps. The maximum step WFF is about 3 kHz, giving a maximum sweep width of 70 kHz.

When the command W00 is received, the sweep is turned off (the timer function is stopped) and the frequency reverts to the start frequency (set by the F command). Sweep settings are not saved by the S (STORE) command, or restored when power is reapplied. No other settings are affected by the sweep commands, and no other commands than F and W affect the sweep settings.

A simple three-wire serial interface is used. For use with a PC, you will probably need a 9 pin female connector (DB9F). The pin numbers shown correspond to the pins on the DB9F connector. With this configuration, the device will plug directly into the PC serial port, or it can be connected by a short pin-for-pin modem cable (i.e. not a "null modem" cable). Some PCs have 25 pin serial ports. A 25 - 9 pin adaptor is the simplest solution here, although of course a DB25 connector could also be used. The generator can also be used with a "dumb terminal" or other terminal emulator, provided the user enjoys sending HEX commands.

At 4 MHz and 8 MHz the generator can use 19200 bps communications. At 10 Mhz the best data clock rate available at 19200 bps is unacceptably inaccurate, so an accurate value for 9600 bps is used.

It is possible to purchase just the executable code, or both the executables and source code. In both cases the PC software is also supplied. See the Micro Page for details.

The PC controller program has a configuration file which is used to set the PC serial comms port, the baud rate, and the Signal Generator clock frequency. This value can be altered to precisely match the crystal frequency, errors and all, and precise output frequencies will result.

Copyright Murray Greenman 1997-2005. All rights reserved. Contact the author before using any of this material.