<< Back

 

Cesky: , English:

FreqGen

Frequency function generator with ATmega8 processor (1 Hz to 8 MHz)

The equipment of the ATmega8 processor is directly tempting to build a simple frequency function generator. Although the quality of the output signal is not world-class (the sine wave is interfered with by PWM control and the generator has no adjustable voltage level), due to the simplicity of the circuitry the utility value of such a generator is great.

Note: Click to enlarge the images on the page

Features of the generator

Generator description

The first generator, Gen1, uses the 16-bit Timer1 timer and its output OC1A (pin PB1). The second generator, Gen2, uses an 8-bit Timer2 timer, with output OC2 (PB3).

The rectangular output is provided by simply switching the state of the LOW and HIGH output pins, using a prescaler and divider. In addition, the second Gen2 generator also uses software division by interrupts (the output is not switched by the counter, but during the interrupt). This makes the characteristics of the second (8-bit) generator comparable to the first (16-bit) generator. On the other hand, the first Gen1 generator also allows the use of a fast mode, where the output is switched in the middle and at the end of the count (the alternation in this case does not have to be 1:1) and can therefore generate more frequencies at high frequencies. The rectangular output is taken from the generators from the sockets marked with a rectangle.

PWM control with a carrier frequency of 62.5 kHz (31.25 kHz for the noise generator) and a level resolution of 8 bits is used to generate the shaped oscillations. A simple RC filter is connected to the generator output. The carrier filtering is not sufficient (in order not to suppress too much the useful signal at higher frequencies), but it would probably be worthwhile to add a higher order filter. The waveform signals are taken from the generator from the sockets marked with a waveform. The only difference between the two types of outputs is the RC filter separation.

The generator is powered from a 9V AC supply, via an internal rectifier and regulator, but that's only because I had such supplies from old modems. Of course, it can be replaced with a +5V supply via the USB connector from a USB charger (the rectifier and stabilizer would be removed), which are more common supplies nowadays.

Using the generator

Use the Mode buttons (valid for both Gen1 and Gen2 generators) to switch the generator mode. The Down and Up buttons change the frequency. Holding the button down quickly switches the frequency. Rectangular signals are recommended to be sampled from the output labeled rectangle, other waveform oscillations are sampled from the waveform output (these outputs contain an RC filter to smooth the PWM carrier).

Rectangular signal waveform - at higher frequencies there may already be rounded edges.

Sinusoidal signal waveform. The signal is generated by PWM pulse modulation and therefore a PWM carrier frequency of 62.5 kHz (the "notching" in the figure) may be evident in the signal. When listening, this carrier frequency will appear as interference. It could be improved by using a higher order RC filter.

Brown noise (contains low frequencies):

Pink noise (contains low and medium frequencies):

White noise (contains low, medium and high frequencies):

With noise, it is necessary to remember that it is generated by PWM pulse modulation with a carrier frequency of 31.25 kHz and a resolution of 8 bits - the spectrum distribution in the spectrum analyzer will not be completely uniform, especially at higher frequencies may be distorted.

Wiring diagram

In the schematic I used ATmega8L, which is designed for low voltages and low frequencies, but even at 5V with 16 MHz it worked fine. The generator is powered from a 9V AC power supply with a rectifier bridge and a regulator. The stabilizer requires cooling. It would be more convenient to change the power supply to a USB connector without the stabilizer and power the generator from a regular USB charger. The outputs of the generators (OC1A and OC2) are fed before a simple RC filter (low pass). Despite this, it is necessary to take into account the signal distortion by the PWM control carrier frequency (62.5 kHz and 31.25 kHz respectively).

A two-line LCD display (2x16 characters) is connected to the processor, operating in 4-bit mode. The LCD two-line displays are usually electrically compatible (usually compatibility with the Hitachi HD44780 controller is maintained), but there are differences with the pin layout. Therefore, be careful with other displays - you may have pins 15 and 16 (swapped LED- and LED+) differently against the schematic!

Click on the image to enlarge...

Surface connection and mounting

The PCB is designed as a single-sided connection with several wire jumpers. I also show the top side here, in case of a double-sided connection, but just replace it with wire jumpers.

The power supply is a 9V AC power supply (from an old modem), a bridge rectifier and a 7805 regulator. The stabilizer requires a heat sink. To fit under the display, I had to bend the fins of the heatsink. If you use a USB power supply (from a +5V USB charger), you eliminate the need for the rectifier and heatsink.

A series of pins are soldered to the display and it plugs into a connector in the PCB. I highly recommend the connector solution, rather than a wired cable connection - much easier to handle and you can swap displays easily. The difficulty you may have is in selecting the display. Electrically, the displays tend to be compatible (the controller is compatible with the Hitachi HD44780), but the pinouts may differ. The display I used has a pin layout that most closely matches the WINSTAR WH1602A-YGH-ET display - which means: the pins are located on the bottom left, in the order 14 (=DB7) ... 1 (=Vss), 16 (=K LED-), 15 (=A LED+). While you can use a different display (may even work out cheaper), you may need to change the pins on the PCB, such as reversing the pin order and moving them up.

Before assembling, check how the electronics will fit in the box in portrait mode. I used a box similar to the KM78 box, only it is a few millimeters taller. You may need to skimp on the height of the connector for the LCD (choose a lower connector) and consider whether you can fit a possible heatsink stabilizer and power connector in there (possibly tuck the lid of the box under to create more space).

I recommend you fly the power circuits first and check the supply voltage. After the other components are soldered, it is hard to find power supply errors or even some circuits may be damaged.

Bottom side (joint side):

Top side (component side, just replace with wire jumpers):

Printing of the components side:

Fitting components:

Printing of the box (I printed on an inkjet printer):

The PCB is screwed to the bottom of the box (KM78 can be used, but may need to use a lower LCD connector (precision hollows), lower stabilizer heatsink and lower power connector). The joint is fixed with 2 M3 screws at the bottom and 2 M3 posts at the top.

The display is attached to the bottom side with a connector and to the top side with screws to the posts. The top holes in the display may need to be widened a bit with a file to accommodate the M3 screws.

There are holes drilled in the back of the box for 6 buttons and a power connector.

There are 5 banana holes fixed into the lid of the box and a hole cut for the display.

I glued the labels on the top of the box and re-glued it with anti-abrasion adhesive.

Programming and revival

The code to program the processor in HEX or BIN format can be found in the source file package.

Important - unfortunately, the entire processor code does not fit into ROM. Some tables (specifically the tables for Mode 2 - Integer Square) had to be placed in EEPROM. Therefore, in addition to ROM, the contents of the EEPROM memory had to be programmed. In the translation using MAKEFILE, the location of the EEPROM section is set to address 0x810000, which is a standard setting expected even by common programming tools. If you know the EEPROM address used by your programming tool, you can change it in the MAKEFILE file in the parameter "-Wl,--section-start=.eeprom=0x810000". Otherwise, you can use separate binary files: freqGen_rom.bin is the contents of ROM without EEPROM, freqGen_eeprom.bin is the contents of EEPROM without ROM, and freqGen.bin is the ROM and EEPROM immediately following each other (the contents of ROM end at address 0x2000).

After programming, set the fuses as follows (16 MHz crystal configuration):

low = 0xEF (11101111), high = 0xC9 (11001001).

When power is applied, the display should light up as follows:

Check that the contents of the EEPROM have also been programmed. Use the Mode buttons to switch the generators to Mode 2 (Integer Square). If the EEPROM is not programmed correctly, you will see the following display contents and the displayed frequency cannot be changed:

You can animate the generator with headphones or speakers. Plug in your headphones and you will hear a tone that you can change.

If you want to modify the program, it is compiled using WinAvr 20100110 (avr-gcc 4.3.3). The installer can be downloaded from e.g. SourceForge https://sourceforge.net/projects/winavr/. The program is compiled with the command file c.bat (=compile). The files Timer1_16M.c, Timer2_16M.c and Waves.c contain tables for setting the generators. The tables are generated by the FreqGen_Timers program (the program is compiled in Visual Studio 2005) - in case you want to use a different crystal frequency. In the generated files you have to make some modifications according to the old files - some tables are marked as EEMEM (=loaded into EEPROM) and the dimensions of the tables are given by symbolic constants.

Calibration

When fitted with a conventional crystal, the achieved frequency accuracy at room temperature is around 50 ppm (= 0.005% deviation) - the deviation appears on the penultimate digit of the reading. The accuracy of the crystal can be increased by calibrating it by 1 order of magnitude. Calibration of frequency and time is an activity most easily achieved in domestic conditions, as we have an accurate time standard available, e.g. via the internet.

To calibrate the frequency generator crystal, the ClockTest program can be used. Once the program is loaded into the processor and the power is turned on, the display starts counting down the time. The first line shows the elapsed time in seconds, rounded to the nearest minute. This is followed by the deviation from a whole minute, in seconds (in the range -30 .. +30 seconds). That is, from the 30th second onwards, a negative deviation is displayed, and from the whole minute onwards, a positive deviation. The second line displays the calculated processor frequency, which would correspond to the current elapsed time in whole minutes. According to the picture - if exactly 4 minutes have passed and the display reads "240-7", it means that the processor is 7 seconds late, it thinks only "240 - 7" seconds have passed, the crystal frequency in that case would be 15534440 Hz.

After a certain period of time, the elapsed time indicated on the display and the actual elapsed time will be deducted. The corrected CPU frequency is then F = F_CPU * time_indicated / time_actual. The corrected processor frequency is written to the F_CPU parameter in the MAKEFILE of the generator source code, and the corrected program is compiled and written to the processor. Although the generator modified in this way will still generate the same frequencies, the display will show the correct value. Thus, we will know from the display that, for example, when generating a frequency of 1 kHz, the actual frequency is 1.000005 kHz.

Another option is to correct the crystal speed. If the crystal oscillates at a higher frequency than needed, it is possible to reduce the crystal frequency slightly by connecting a small capacitor (e.g. tens of pF) in parallel to capacitor C2 (i.e. between processor pin 7 and ground). Thus, it is possible to refine the generated frequency.

Example of use:

I start the calibration measurement at 19:55:00. I will measure the calibration for 1 day. On the second day, I take a reading on the display at 19:26:00, i.e. in 84660 seconds. The display reads 84660+5. That means - 84660 seconds of whole minutes have passed, but the processor thinks it is 5 seconds more. The actual processor frequency is then: F = 16000000 * 84665 / 84660 = 16000945 Hz (the same value should appear in the second line of the display). I will write this value to the F_CPU variable in the makefile, translate it and burn it into the processor. The generator will now display the exact value of 1.000059 kHz when generating a 1 kHz signal. I can now repeat the refinement measurement with a longer time.

Note: It is advisable to choose a measurement time such that the deviation is a few seconds (so that no inaccuracy error occurs in the time reading) and yet less than 30 seconds (so that the whole minute reading on the display agrees with the actual elapsed time). Before calibration, it is advisable to leave the instrument on for a few minutes to allow the internal temperatures to stabilise. Also next time, if you want an accurate full digit reading, it is advisable to let the instrument stabilize first to achieve the same temperature conditions as during calibration. Of course, the calibration will no longer be valid if you use the generator in freezing temperatures or in hot summers; the generator does not have temperature stabilization.

During calibration you can use the programs in the "util" folder. The CLOCK.EXE program is a Windows program to display the current time (Windows 10 can also display the time with seconds, but it is not a window that stays permanently on the display). The AboutTime program is a program to synchronize the PC time with an accurate time server. If the default servers are not in use, the tik.cesnet.cz and tak.cesnet.cz servers can be used.

After proper calibration, the generator and meter data should differ only minimally.

Deficiencies and corrections

Downloads

Source codes of FreqGen with firmware

Timers program for generating timer tables

Wiring diagram in Eagle Free

Graphic documents (schematic and circuit board)

ClockTest program for crystal calibration

Complete download of FreqGen documents

Used components

The component selection was tailored to a GM Electronics store that I have close by. The display I chose here is one that matches the display I used with its pins, i.e. the pins are on the bottom left. A different display (2 rows of 16 characters) should work with the same functionality, and may be cheaper, but may have a different pin layout. Typically, many displays have the pins on the top left, which will mean the PCB will need to be modified. Anyway, still try wiring the LED for the backlight (on my displays, the pins are reversed from the datasheet).

Total price for everything is 480 Kc (including box, photocuprextit, more expensive display and more expensive connector).

Miroslav Nemecek

<< Back