GAZ-53 GAZ-3307 GAZ-66

PIC interrupts. Pulse generator. Generator on PIC16F84A and AD9850 - Devices on microcontrollers - Schemes of devices on microcontrollers Rectangular pulse generator on a microcontroller pic

Once upon a time, in [L.1], a controlled TTL-level square wave generator on the ATTiny2313 microcontroller was described. It could generate 31 fixed frequencies (from 0.1 Hz to 4 MHz), had a very simple diagram and controlled by feed binary code to control inputs. Depending on the logical level on the five control bits, the generator generated one of the frequencies preset in its program, the number of which was set by connecting these control inputs to ground or to the power bus. Most likely, this circuit was created not as an independent device, but as a component, a module for use in other developments (for example, as a master oscillator in frequency meters, clocks, Christmas tree garlands, etc.) - hence, apparently, the not very convenient control method - the author of the circuit must have assumed that the frequency would be set by one times, at the design stage of the finished device, which will include its circuit.

I was interested in this circuit as a simple laboratory generator. Of course, this is not an alternative to serious industrial devices, but in some places such a “microcontroller multivibrator” can be useful (especially considering its simplicity, accuracy and low cost) - for example, it is perfect for calibrating frequency meters and in similar cases when it is necessary adjust some scale; it can be used during experiments with digital circuits or output stages of pulse power supplies; for testing transformers and ignition coils (by connecting them to a powerful current source through an appropriate transistor switch), various filters, etc. Yes, and it’s safer to make sure that the newly assembled ULF is working properly by applying a signal to its input from such a homemade generator, and not from your favorite MP-3 player or “tablet” - in case of installation errors (for example, if the supply voltage leaks to the ULF input ), in the worst case, a cheap output transistor will be destroyed, and not an expensive “toy”.

But the described circuit had a number of disadvantages that made it difficult to use as a laboratory device: firstly, the already mentioned inconvenience of control (buttons in in this case would be more appropriate); secondly, the absence of any indication of the selected mode; thirdly, the absence of a mains frequency (50 Hz) among the output frequencies, which is sometimes needed when working with transformers or network filters; and fourthly - an unregulated output voltage level (which is sometimes necessary when testing the same ULF). Therefore, using the scheme given in [L.1] as " technical task", I have developed a new device and firmware for it that meet the above requirements. The resulting diagram is presented in Fig.1 .

As in the original, this is a generator that produces square waves of a stable frequency with a period of 1:1 (i.e., a square wave with 50% filling), but the number of generated frequencies has been increased to 42. The values ​​​​of most of the generated frequencies differ from those in [L.1]; in addition, the number of frequencies less than 1 Hz, the need for which arises quite rarely, has been reduced. A list of all frequencies generated by the device is given in table 1 (at the end of the article).

The basis of the circuit is the widespread ATTiny2313 microcontroller, the clock frequency of which is stabilized by quartz at 10 MHz, which ensures minimal output signal error. Output pulses are generated by dividing the MC clock frequency using a 16-bit internal timer-counter T1 operating in the “reset on coincidence” mode; they are output by hardware to pin 15 of the microcircuit. The internal 8-bit timer-counter T0, also operating in the “reset on coincidence” mode, forms the intervals for switching the digits of the HG1 indicator, thereby realizing a dynamic indication. The dynamic indication outputs are lines PB0 and PB1 of the MK, they are amplified transistor switches on VT1 and VT2.

The generated output signal from pin 15 MK is fed to a current amplifier, which is assembled on a VT3 transistor connected according to an emitter follower circuit. The use of an amplification stage made it possible to decouple the MC output from the load and protect it from unexpected events (for example, from a short circuit or from applying voltage to the device output). Using variable resistor R12, you can adjust the level of the output signal - from zero to almost the supply voltage. Resistor R13 limits the current through the transistor to a safe level (approx. 125 mA), protecting the latter from failure during a short circuit in the load. Resistor R14 “pulls” the output to the ground potential - this is necessary, first of all, for working with digital logic: it is known that the absence of any voltage at its input (and with log.0 at pin 15 MK, transistor VT3 is closed, and there is no voltage at its emitter) most digital microcircuits interpret it as logic 1. Resistor R14 “pulls” the device’s output to ground at those moments when VT3 is closed, which is confidently recognized by digital microcircuits connected to this output as log.0. The device has the ability to turn off the output signal with the S1 button. In this state, there are no pulses at pin 15, transistor VT3 is closed, and log 0 is always present at the device output. Pressing S1 again turns on the output signal. LED HL1 is an indicator of the presence of output pulses.

The device is powered from a stabilized power source with a voltage of 3.5-5 V. If it is necessary to power the device from a source with a voltage exceeding 5 V (for example, from a Krona or a power supply from a game console), then a stabilizer with the appropriate output voltage must be inserted into the circuit - for example, integrated, on a 7805 type microcircuit, or parametric, on a zener diode and transistor. Diode VD1 protects the circuit from accidental reversal of the power supply - it can be anything, with a current of at least 150 mA (personally, for this I use “rare” D226 or D7 from old stocks - you have to put them somewhere :)). You don’t have to install a diode if the design of the power connector prevents polarity reversal (or if you are sure that you won’t make a mistake when connecting the power). If desired, you can also add a power switch.

The algorithm for working with the device is as follows: when the power is turned on, there are no pulses at pin 15 of the MK, the HL1 LED does not light up, and the number of the selected mode is displayed on the HG1 display. In order for pulses to appear at the output of the device, you must press the S1 button, after which HL1 will light up, signaling the appearance of pulses at pin 15 of the MK. Pressing S1 again will turn off the output signal again (i.e. S1 works like a trigger). You can switch the operating modes of the device both when the output signal is turned on and off with the S1 button. Button S2 increases and S3 decreases the mode number. The modes are switched in a circle - after mode No. 42 (5 MHz) comes the OF mode (no output signal); Another press on S2 will turn on mode No. 1 (0.5 Hz). Button S3 behaves similarly, only it switches in the opposite direction (No. 1-OF-No. 42). The value of the selected mode is stored in non-volatile memory (EEPROM), so the next time you turn on the device, the mode in which the device was operating before turning off the power will be active. The output state is not remembered - every time the power is turned on, the output signal is always absent - this is done to protect the circuit that will be connected to the generator from all kinds of accidents: first you need to set the required mode and minimum output voltage, and only then turn on the output signal with the S1 button.

The firmware for the MK was written in pure assembler (using the freely distributed AVR Studio-4 environment from the manufacturer of this chip), thanks to which it occupies only 602 bytes in its flash memory. Installation of fuse cells in the PonyProg program is shown on Fig.2 . (Attention! If serial programming is used, then do not touch the bitsSPIENAndRSTDISBL - they must be the same as the new oneMK - otherwise, further reprogramming of the MK will be unavailable! Just consider before programmingfuse-cells of “fresh” MK (Security and Configuration Bits -> Read), and when writing new values, leave the bitsSPIENAndRSTDISBLas they were read.)


The resistors used in the device are small-sized, with a power of 0.125 W (except for R13 - it is 0.25 W); their resistance may differ from the specified one within a fairly wide range. All transistors of the device operate in switching mode, so they can be of any low-power NPN structure - both domestic and foreign. It is only important that VT3 can confidently operate at the maximum output frequency of the device (5 MHz). As HG1, you can use any dual seven-segment indicator for dynamic indication with common anodes. If there is no dual one, then HG1 can be assembled on two single indicators, up to the “ancient” ALSxxx - the main thing is that the common terminal for all segments is the anode. LED HL1 - any that you don't mind :). All buttons are spring-loaded, without fixation. The output connector is "Asia" type.

The appearance of the generator connected to the oscilloscope is shown in Fig.3 (on the left is the power supply, it is 9V (from a Dendy type set-top box), but an integrated stabilizer of type 7805 was installed in its case - as a result, the output is 5V), as well as on Fig.4 , oscillogram of the output signal, taken using an S1-19B oscilloscope - on Fig.5 . I didn’t photograph the “insides” of the device, because... I assembled it quite a long time ago and put the lid on with glue, but I don’t feel like disassembling (i.e. breaking) the housing... A junction box for open electrical wiring was used as a housing for this generator, but other options are possible. For ease of operation, it is advisable to print out the table of modes and stick it with double-sided tape to the body of the device (I have it on the back side, not visible in the photo). The microcontroller must be installed on the socket so that it can be easily transferred to the programmer for writing the “firmware” (I usually do not make connectors for in-circuit ISP programming - transferring the microcircuit to the programmer socket does not personally cause me any inconvenience). The type of installation can be any (up to SMD); I used the classic one - printed, but I don’t give a drawing of the board, because... it turned out to be quite “crooked” (practically repeats schematic diagram); a good option- assemble everything on a “layout” with holes (size ~80x80 mm).

Literature:

1. Gorchuk N.V.- Generator with digital control, zh. "Radioconstructor", 12-2009, p. 18;

Table 1:

LIST OF GENERATOR MODES

frequency Hz

frequency Hz

frequency Hz

frequency Hz

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 switches the device to 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 - any piezoelectric sound emitter with a built-in generator, designed for a voltage of 5 V. A microassembly can be used as a clock generator (G1) crystal oscillator at frequencies up to 125 MHz, it is permissible to use a similar unit with quartz stabilization 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.

This project is a high-quality and universal function generator, which, despite some complexity of the circuit, at least in comparison with simpler ones, has very wide functionality, which justifies the cost of its assembly. It is capable of producing 9 different waveforms and also works with pulse synchronization.

Schematic diagram of the generator on the MK

Device settings

  • Frequency range: 10 Hz - 60 kHz
  • Digital frequency adjustment in 3 different steps
  • Waveforms: Sine, Triangle, Square, Saw, H-pulse, L-pulse, Burst, Sweep, Noise
  • Output range: 15V for sine and triangle, 0-5V for other modes
  • There is an output for pulse synchronization

The device is powered from 12 volts AC, which provides a sufficiently high (over 18 V) DC voltage necessary for normal operation of the 78L15 and 79L15, which form a bipolar 15 V circuit. This is done so that the LF353 microcircuit can output the full range of signals to the load 1 kOhm.

Level controller used ALPS SRBM1L0800. The circuit should use resistors with ±1% tolerance or better. LED current limiters - 4306R series resistors. Brightness can be increased depending on the preference of the performer. The generator is assembled in a plastic case 178x154x36 mm with aluminum front and rear panels.

Many contact components are mounted on the front and rear panels (buttons, knobs, RCA connectors, LED assemblies, power connector). Printed circuit boards are attached to the housing with bolts with plastic spacers. All other elements of the generator are mounted on printed circuit boards - the power supply is separate. The left button in the middle is to change the mode, the right one is to select the mode frequency.

The generator produces various signals and operates in three modes, which are selected using the "Select" key and indicated by the three upper (in the diagram) LEDs. The rotary control changes the signal parameters according to the following table:

Immediately after setting in mode 1, sine generation occurs. However, the starting frequency is quite low and at least one click of the encoder is needed to increase it. The board has a contact for connecting the device for programming, which allows you to quickly change the functionality of the signal generator, if necessary. All project files - PIC16F870 firmware, board drawings, are located

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. We create 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 it 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 MK output 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

This project is based on the diagram function generator described on the Mondo website. I only made very minor changes and corrected some obvious typos in the diagram. The code has been rewritten for Microchip syntax.

Generator characteristics:
Frequency range: 11 Hz - 60 kHz
Digital frequency adjustment in 3 different steps
Waveform: sine, triangle, square, pulse, burst, sweep, noise
Output voltage range: ±15V for sine and triangle, 0-5V for others
Synchronization: output for pulse signal.

The device is powered by a 12-volt transformer, which provides fairly high (more than 18 V) constant pressure, necessary for normal operation of stabilizers 78L15 and 79L15. A power supply of ±15 V is necessary to ensure that the LF353 output amplifier provides a full range of signals at a 1 kOhm load. When using ±12 V power, this resistor must be at least 3 kOhm.

The rotation sensor (rotary encoder) that I used was ALPS SRBM1L0800 in the form of two switches in a circle in the diagram. The author probably used a different one, so some changes to the controller program code were necessary. My sensor has two groups of contacts: OFF and ON (when the rotor moves in the corresponding direction). Thus, a change in PORTB interrupt must be generated if one of the pin pairs is shorted. This is achieved by connecting both groups of contacts to the PIC16 pins (RB4 - RB7), which are checked by the program for changes in state. Luckily RB4 was not used in the original design, so I just redirected RB3 to RB4. Another modification is caused by the use of a rotary encoder, so I slightly changed the firmware interrupts. I made the regulator maintain its state for 100 consecutive measurements instead of 10 in the original design. Note that some PIC pins are used to redirect +5V to simplify PCB layout, so they are configured as port inputs.

The printed circuit board provides three resistor assemblies. One – R/2R – for the DAC from the Bourns 4310R series. The DAC resistor assembly can also be built using discrete resistors according to the diagram above. Resistors with a tolerance of ±1% or better should be used. LED limiting resistors Bourns 4306R series. The brightness of the LEDs can be increased by changing the resistance of the limiting resistors to 220 - 330 Ohms.

The generator is housed in a 179x154x36 mm plastic case with aluminum front and rear panels. The output signal level is regulated by a variable resistor Alfa 1902F series. All other components are installed on the front and rear panels (buttons, connectors, LED assemblies, power connector). The boards are attached to the case with 6mm bolts with plastic spacers.

The generator produces 9 different waveforms and operates in three modes, which are selected using the “Select” button and their indication is displayed on the top three (according to the diagram) LEDs. The rotation sensor adjusts the signal parameters in accordance with the following table:


Mode\Form

Triangle

Mode 1

Mode 2

Mode 3

Immediately after switching on, the generator goes into mode 1 and generates a sine wave. However, the starting frequency is quite low and at least one click of the control will be enough to increase it.

P.S. I’ll add on my own behalf: when repeating the device with the author’s printed circuit board, the device refused to start (perhaps there is an error on the printed circuit board), and when mounted on a breadboard, the generator started working immediately.

Below you can download asm sources, firmware and PCB files ()

List of radioelements

Designation Type Denomination Quantity NoteShopMy notepad
Generator circuit.
MicrocontrollerPIC16F8701 To notepad
Shift register

CD74HC164

1 To notepad
Operational amplifier

LF353

1 To notepad
Multiplexer/demultiplexer

CD4053B

1 To notepad
Linear regulator

LM7805

1 To notepad
Linear regulator

LM78L15

1 To notepad
Linear regulator

LM79L15

1 To notepad
Rectifier diode

1N4002

3 To notepad
Capacitor22 pF2 To notepad
Capacitor51 pF1 To notepad
Capacitor100 pF1 To notepad
Capacitor1000 pF1 To notepad
Capacitor0.1 µF1 To notepad
1 µF2 To notepad
Electrolytic capacitor4.7 µF1 To notepad
Electrolytic capacitor100 µF2 To notepad
Electrolytic capacitor500 µF1 To notepad
Resistor

470 Ohm

6 To notepad
Variable resistor1 kOhm1 To notepad
Resistor

2.7 kOhm

1 To notepad
Resistor

4.7 kOhm

1 To notepad
Resistor

10 kOhm

4 To notepad
Resistor

15 kOhm

1 To notepad
Resistor

22 kOhm

1