Function generator on a microcontroller. Multifunctional generator on PIC16F870 Pulse generator on pic controller

The TV signal generator probe is assembled on the basis of a microcontroller of the pic12f629 series, and in terms of its dimensions, current consumption, cost of manufacturing the device and functionality for a TV technician, it is simply irreplaceable. Supply voltage 3 volts, i.e. two AA batteries. The current consumption in generation mode is 11 milliamps, in sleep mode - only 3 microamps.

Schematic diagram of a TV signal generator

PCB drawing


This probe can generate five pictures, which is quite enough for checking and repairing the horizontal and vertical scanning of a TV, adjusting the convergence and geometric distortion of the raster, color balance, and monitoring the passage of signals through the TV circuits. When you briefly press the button, it wakes up and begins to generate the first picture; with subsequent clicks on it, the pictures switch in a circle. If you hold the button for a long time, the generator goes into sleep mode when released. It also goes into sleep mode automatically if it is turned on for more than 5 minutes.


An archive is attached to the article, which contains a diagram, a probe board, and two firmware. The video shows that the picture on my TV is slightly non-linear - this is because the TV is 12 years old, or maybe something is wrong with the video input.

Radio amateurs and circuit engineers sometimes need to configure some digital device, such as a pulse counter, tachometer, oscilloscope, etc. Or just find out if it works. It is very convenient to use a generator that produces rectangular pulses of various frequencies.

I would like to propose a project for such a generator.

First the pulse generator circuit:

The device is based on a popular microcontroller ATmega8 from Atmel.

Description of the scheme. The entire circuit is powered by a voltage of 5 V. The microcontroller is clocked at a frequency of 8 MHz, which is stabilized by X1 quartz. Timer/counter No. 1 is used to generate pulses. An encoder is shown in the form of buttons in the diagram connected to pins PC3, PC4 and PC5. The two outer buttons replace the switching of the encoder during rotation, and the button in the middle is an encoder button that closes when you press its axis. Rectangular pulses of a frequency set using an encoder with an amplitude of 5 V are removed from the output of timer 1 (OCR1A). To display the output frequency, a 16-character single-line LCD display WH1601 is used, connected to port D of the microcontroller. The display is also common, based on the HD44780 driver. Resistor R1 adjusts the contrast of the display. Data exchange between the MK and the display is organized using a 4-wire bus. Connector J1 for in-circuit programming of the MK.

Now about the program for the microcontroller.

The program is written in the development environment CodeVisionAVR. This environment contains ready-made libraries for working with the display, and setting up the MK is clear and simple. I used the version before release CodeVisionAVR version 3.12. It is slightly different in generating code using Wizarda. But basically everything is the same. Below everything is described using an example of working with CodeVisionAVR version 3.12. The Internet is full of links to study this environment, for example: learning the integrated development environment CodeVisionAVR.

Let's launch CVAVR. Create a new project ( New Project). The program will prompt you to use the project creation wizard.

We agree. Then select the controller family.

Configuring I/O ports. You need to make bit 1 of port B (PB1) the output - the generated frequency is taken from it. We leave port D as is for now. And set the pins from which the encoder state will be read (PC3, PC4, PC5) to the input ( Data Direction: In) and turn on the internal power supply ( Pullup/Output Value- meaning P).

Go to the tab Timers/Counters. Here you need to configure 2 timers: Timer0 And Timer1, leave the remaining timers disabled ( Clock Value: Stopped).

Setting the frequency Timer0 125 kHz. This timer is necessary for periodically polling the encoder status. Polling will occur every time the timer reaches the upper value. Because the Timer0 8-bit, then its upper value is 255. And in order for the controller to interrupt the execution of the main program to poll the encoder, you need to enable the overflow interrupt Timer0 (Overflow Interrupt).

Setting up Timer1. You need to select a mode ( Mode) CTC (Clear Timer on Compare– Reset if there is a match). In this mode, the timer output will switch to log. 0 as soon as the contents of the counting register TCNT1 matches case OCR1A. By changing the value in the register OCR1A we will change the frequency of the output pulses. The circuit uses output A of timer 1. You need to select a value for it Toggle on Compare Match(switch to another state if there is a match). In general, look at the picture:

The next step is connecting the display. IN CodeVisionAVR It is enough to indicate which port of the MK the display will be connected to. Select port D.

Now you need to generate the program code. Click Program ->Generate, Save and Exit

Now you need to go to settings Project -> Configure and check that the MK type and its clock frequency are set correctly:

Ready project for CVAVR

(316.0 KiB, 670 hits)

To flash the MK firmware you need a file with the extension .hex. In the finished project this is the file Gen_mega8.hex. It is located in the folder Release/Exe/.

If you want to write a program from scratch, then the project has comments on what commands are needed for what. Or you can simply paste the finished code from the file gen_mega8.c. And, changing it, see how it is reflected on the finished device. To generate the MK firmware file, you need to press the button Build the project. File with extension .hex will be generated into a folder Release/Exe/.
The controller's fuse bits are programmed to work with an external 8 MHz quartz resonator in accordance with the figure:

Now about controlling the pulse generator.

After power is applied, the display and encoder are initialized (the pins to which the encoder is connected are configured). Next, a bar runs across the display (an optional “trick”, it was made for training display output) and the inscription “Generator off” is displayed. After 2 seconds the display clears. The output frequency appears after rotating the encoder knob and changes by units of Hertz. When you press and hold the encoder button for about 0.5 seconds, the message “Release the button” is displayed on the screen. After this, by rotating the encoder knob, the frequency changes in tens of Hertz. To change the frequency by hundreds (thousands) of Hertz, you need to press the encoder button again (2 times). Then everything starts again in units of Hertz.

To increase the load capacity of the generator, the output of the MK can be turned on via a transistor.

About the accuracy of the output frequency.

The output frequency values ​​were checked with an oscilloscope. At low frequencies, up to approximately 200Hz, the values ​​coincide with those measured on an oscilloscope, then the higher the frequency, the greater the error (this is due to non-integer numbers written to the comparison register). The accuracy can be increased if you enter constants from the array into the comparison register (I didn’t need high frequencies, and I’m just too lazy to count and enter numbers into the array)). At high frequencies, to increase accuracy, you need to take a different timer frequency.

I recently purchased a very convenient and compact multimeter that can be used to measure frequency (up to 9.999 MHz). Here it is video review . And you can order by this link .

The microcontroller can be flashed with a special programmer or you can make a simple programmer yourself. For example, I successfully use the programmer USBasp. You can read about this programmer at

The first part of the article discusses the circuit design, structure and design of a DDS generator (generator with direct digital waveform synthesis) on the ATmega16 microcontroller. In addition to synthesizing signals of various shapes and frequencies, the device provides the ability to adjust the amplitude and offset of the output signal.

Main characteristics of the device:

  • simple circuit design, accessible components;
  • single-sided printed circuit board;
  • mains power supply;
  • dedicated frequency output from 1 MHz to 8 MHz;
  • DDS output with adjustable amplitude and offset;
  • DDS output signal shape: sine wave, rectangular pulses, sawtooth pulses, triangular pulses, ECG, noise;
  • a two-line LCD display is used to display current parameters;
  • five-button keyboard;
  • frequency tuning step: 1, 10, 10, 1000, 10000 Hz;
  • restore the last configuration when turned on;
  • offset adjustment: -5 V ... +5 V;
  • amplitude adjustment: 0 ... 10 V;
  • frequency adjustment: 0 ... 65534 Hz.

The basis of the device, or rather the operating algorithm of the microcontroller, was taken from the development of the Jesper Hansen DDS generator. The proposed algorithm has been slightly reworked and adapted for the WinAVR-GCC compiler

The signal generator has two outputs: a DDS signal output and a high-frequency (1 - 8 MHz) square wave output, which can be used to “revive” microcontrollers with incorrect Fuse bit settings or for other purposes.

The high-frequency signal comes directly from the microcontroller, from pin OC1A (PD5). The DDS signal is generated by a microcontroller using a chain of resistors R2R (DAC), offset and amplitude adjustment is possible thanks to the use of a low-power operational amplifier LM358N.

Block diagram of DDS generator

As you can see, three voltages are required to power the device: +5 V, +12 V, -12 V. Voltages +12 V and -12 V are used for the analog part of the device on the operational amplifier to adjust the offset and amplitude.

The circuit diagram of the power supply is shown in the figure below.

The power supply uses voltage stabilizers LM7812, LM7805, LM7912 (negative voltage stabilizer -12 V).

Appearance of the power supply for the generator

It is possible to use a computer power supply of the ATX form factor; to do this, you need to solder the adapter in accordance with the diagram:

Schematic diagram of the device

To assemble the device you will need:

  • microcontroller ATmega16;
  • quartz resonator 16 MHz;
  • standard two-line LCD indicator based on the HD44780 controller;
  • R2R DAC made in the form of a chain of resistors;
  • dual operational amplifier LM358;
  • two potentiometers;
  • five buttons;
  • several connectors and sockets.

PCB drawing

The components used, with the exception of the microcontroller and connectors, are in surface mount (SMD) packages.

Device mounted in a housing

Test run

Downloads

Circuit diagram and printed circuit board (Eagle format) -
Project for simulation in the Proteus environment -

  • Who tried to pile?
  • See the Functional Generator thread, starting from post 4 there is a discussion of this design, and QED and Cuco users have assembled this generator. And it was tested in Proteus - it works.
  • Can someone please tell me the list of components for the power supply used in the first (http://www..html?di=69926) version of the generator. In particular, I am interested in what model of transformer and rectifier the author used. or at least complete analogues. From the request it is clear that I am not strong in electrical engineering, but I think I can assemble it without delving into the wilds of the subject. Just force majeure. Everything is clear with capacitors and 3 stabilizers. Actually this diagram is attached.
  • Any low-power transformer with two secondary windings with an output voltage of 15 V (variable). In particular, the author used a TS6/47 transformer (2x15 V/2x0.25 A). Any low-power diode bridge will also do. The photo in the article shows both the transformer and the diode bridge.
  • but please tell me what kind of connection there should be between the secondary output of the transformer and the rectifier, taking into account the author’s power supply circuit?: confused: well, I mean, if the output of the transformer is 15V (I think I found this one - TPS-7.2 (2x15V)sim. (7.2W) 15Vx2_7.2W_sim. (0.24A)x2 - 160.00 rubles), then what rectifier is for it? and in case there is 12V at the output of the transformer?
  • I don’t quite understand the question, to be honest... The transformer you indicated seems to be suitable... The bridge is fine, I think it would be suitable for example DB106
  • Vadzz, thank you very much for the tip. if the DB106 is suitable, then the W08, which has similar parameters, will be suitable. This is true? Simply, it is exactly what you have the opportunity (desire) to buy. and I still haven’t been able to figure out the values ​​of the capacitors in the author’s diagram, please tell me. Are they all in nF (nanofarad-nF)?
  • W08 is quite suitable. Are the capacitors in the power supply circuit or in the generator circuit itself? If the power supply is there, then all the capacitors are in microfarads (2000 µF, 100 µF, 0.1 µF). In the generator circuit, in my opinion, there are only two condensers in the quartz harness of 18 picofarads.
  • Vadzz, thank you endlessly. It seems that all questions have been resolved. The circuit diagram of the generator itself seems to be a little simpler (there is an EAGLE file). I will make it a reality. If everything goes well, I’ll try to post the printed circuit board (Eagle format) of the power supply.
  • Everything should definitely work out for you... Post a drawing of the printed circuit board, it will definitely be useful to someone...
  • I soldered it and am using it. To be honest, several problems arose along the way: 1) drawback - it is impossible to adjust the frequency when the generator is turned on. Those. if you need to change the frequency, then first turn off the signal generation, then adjust the frequency, then turn on the signal generation again. This is often inconvenient when you need to monitor the reaction of the device being adjusted to a smooth change in frequency. For example, to control the speed of a stepper, you only need to adjust the frequency smoothly. 2) drawback - the EEPROM crashed twice. The author has provided for storing the set modes in EEPROM, but this is not at all necessary. It would be better not to remember anything and not to use it at all. Or, as a last resort, if the EEPROM is damaged, it loads the “default” settings from FLASH. But it would be more reliable. Overall, I'm happy with the rest of the work. We ask those who understand writing programs for AVR to correct these two shortcomings.
  • Regarding frequency adjustment on the fly, you most likely need to use DMA, which is not available in such microcontrollers. Maybe I'm wrong... I need to look at the source code of the generator... As for the "EEPROM flies" - of course it would be interesting to find out the reason, but I think twice is not an indicator.
  • Ready-made generators for ad9850(51) are here: http://radiokit.tiu.ru/product_list/group_802113
  • Ready-made generators on the AD9850 are good devices, but it’s another matter when you assemble and set it up yourself...
  • Data destruction in EEPROM leads to complete inoperability of the generator. A very unpleasant problem at the most inopportune moment. I usually keep a spare programmed controller inside the generator housing. But this is not a way out of the situation. Why not provide for saving only current data, which will not affect overall performance if the EEPROM is destroyed? If data is lost from Flash, we load the default settings. Everything else related to the performance of the program is stored in Flash. It will work more reliably this way. I SUGGEST posting a list of links with other generator projects on AVR.
  • Here several people assembled this generator (in their words, of course), they did not say anything about this, whether they had such a problem or not...
  • Tell me, is it possible to change only the frequency or duty cycle in this generator?
  • The characteristics of the generator indicate that you can change the frequency, unfortunately there is no possibility to change the constraint...
  • Guys, tell me about the RESET jumper - when to turn it on and when to remove it..... thank you
  • The normal state of the jumper is open. And this is most likely not a jumper, but a connector for connecting a button with which you can reset the MK if suddenly something happened...
The proposed device is a rectangular pulse generator controlled via a serial port from a computer. It was made to solve a specific problem literally in a day and may contain errors or shortcomings, I cannot guarantee that you will earn a lot of money by selling it. But all the basic functions have been tested.
The maximum frequency produced by the generator is slightly more than 13 kHz, the minimum is less than 0.01 Hz (for a quartz oscillator frequency of 4 MHz).

Scheme.

The scheme is quite simple. It is assembled on the basis of a PIC16C63A microcontroller, the signal is taken from its two pins, their state is always different. Without load, the one level differs from the supply voltage by less than 0.1 volt, the zero level is also very low. The pins are designed for currents up to 30 mA. The MAX232 chip is used to convert RS232 interface levels to TTL levels. To power the device, you need a 5 volt power supply; it is not shown in the figure.

Program.

To set the parameters of the signal produced by the microcontroller, you must use a special program. The program is written for Windows OS; below is the view of its window.

The controls are designed to set the frequency of the output signal, the ratio of the lengths of the positive and negative half-cycles. It is possible to limit the number of pulses issued (1...2 23 -1). Since the program in the microcontroller does not allow outputting any frequency, after pressing the “Send” button, the nearest possible frequency value will be calculated and it will be written in the frequency field instead of the one entered from the keyboard. The fields "Duration 1" and "Duration 0" contain the duration of the signal in arbitrary units with which the program works in PIC, these are integers greater than zero and less than 2 24 . Settings are provided to select the serial port number and the frequency of the quartz resonator used.

Source: svv.on.ufanet.ru


This diagram is also often viewed:

Measuring generators, in which the required frequency value is set using a keyboard, are known to readers of the magazine (see, for example, the article by Piskaev A. “Frequency meter-generator-clock” in “Radio”, 2002, No. 7, pp. 31, 32). As a rule, these devices are made on a microcontroller, the range of generated frequencies is limited to several megahertz, and obtaining an exact frequency value is impossible. The generator described in the article also contains a microcontroller, but it is used only to control a specialized microcircuit - the AD9850 frequency synthesizer. The use of this microcircuit made it possible to expand the range of generated frequencies from fractions of a hertz to 60 MHz, within which it is possible to obtain any frequency value with an accuracy of 1 Hz.

It polls the SB1-SB16 keyboard, displays information on the HG1 LCD indicator, calculates the frequency code value and transmits it via the serial interface to the DD2 synthesizer. Sound emitter HA1 serves to confirm pressing of keyboard buttons. The AD9850 (DD2) chip is used in the standard connection. Filter Z1 is turned on at the output of its DAC. After the filter, a sinusoidal signal is supplied to socket XW2 and to the input of the comparator of the DD2 chip (pin 16). From the output of the latter, a rectangular signal is supplied to socket XW1. The G1 quartz oscillator is used as a clock generator for DDS. Trimmer resistor R7 adjusts the contrast of the image on the HG1 indicator.
After resetting the microcontroller, the HG1 LCD indicator is configured for 4-bit bus exchange mode, which is necessary to reduce the number of input/output lines required for recording information.



The generator is controlled using a keyboard consisting of buttons SB1-SB16. Since all port B input lines are connected to the power supply through resistors, there is no need for external resistors to pull up ports RB4 - RB7 to the power line. Resistors R3-R6 protect the microcontroller outputs from overload when several buttons are accidentally pressed at the same time.
The required frequency is set from the keyboard. To do this, click on the buttons with the corresponding numbers, enter the desired value (in hertz) and press the “*” button. If the frequency does not exceed the maximum permissible, the message “OK” appears on the indicator for a short time and the generator goes into operating mode, and ifexceeds - message "Error". In this case, you need to press the "C" ("Reset") button and re-enter the correct value. They do the same if there is an error during the frequency input process. Pressing this button twice puts the device into operating mode with the previously set frequency value.
In operating mode, the asterisk symbol flashes in the far right area of ​​the indicator. If the current frequency value is entered from an external control unit (for example, from a computer), then to return to the frequency displayed on the indicator, just press the “*” button.
The "U" (Up - up) and "D" (Down - down) buttons allow you to stepwise change the output frequency of the generator, respectively increasing or decreasing the decimal place value by one. The required decimal place is selected by moving the cursor using the "L" (Left - left) and "R" (Right - right) buttons.
When you press the "*" button, the frequency value and cursor position are saved in the non-volatile memory of the microcontroller, so that the next time the power is turned on, the interrupted operating mode is automatically restored. Since the computing capabilities of the microcontroller are limited, the output frequency is set with an accuracy of about 1 Hz, which is sufficient for most cases. To fully realize the capabilities of the synthesizer, it can be controlled using a PC. To do this, the generator must be modified by adding a unit, the diagram of which is shown in Fig. 3. PC (or other control device) is connected to the outlet
XS1. When the logic level at the address inputs A is low, the multiplexers of the DD3 chip connect the synthesizer control inputs to the microcontroller DD1, and when the logic level is high, to an external device. Control signals are supplied through the "ENABLE" contact of the XS1 socket. Resistor R19 provides a low logic level at the address inputs of DD3 when the control device is not connected.
The generator is assembled and tested on a breadboard. If you cannot purchase a board for the SSOP housing for the DD2 chip, you can use short (10-15 mm long) pieces of tinned wire with a diameter of 0.2 mm to connect its pins to the corresponding pads. Pins 1,2,5,10,19, 24, 26, 27, 28 are connected to the common wire with one longer segment.
LCD indicator HG1 - 1TM1601 (16-character single-line with built-in controller). HA1 is any piezoelectric sound emitter with a built-in generator, designed for a voltage of 5 V. As a clock generator (G1), you can use a microassembly of a quartz oscillator with a frequency of up to 125 MHz; it is permissible to use a similar unit with quartz stabilization and on discrete elements.
The control program of the microcontroller depends on the frequency of the clock generator.
When programming the microcontroller, the following bit values ​​are set in the configuration word: generator type (OSC) - RC. watchdog timer (WDT) - disabled, power-on delay (PWRTE) - enabled.