Frequency generator on pic diagram. Multimode controlled rectangular pulse (frequency) generator on the pic12f675 microcontroller. Main characteristics of the device

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. 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 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

To generate a video signal, just one microcontroller and two resistors are enough. That is, you can literally make a pocket-sized video signal generator the size of a keychain. Such a device will be useful to a TV technician. It can be used when mixing a kinescope, adjusting color purity and linearity.

Generator operation and its characteristics.
The generator is connected to the video input of the TV, usually this is a “tulip” or “SCART” connector.
The device generates six fields:
- text field of 17 lines;
- 8x6 mesh;
- mesh 12x9;
- small chess field 8x6;
- large chess field 2x2;
- white field.

Switching between fields is carried out by briefly (lasting less than 1 s.) pressing the S2 button. Holding this button pressed for a longer time (longer than 1 s) turns off the generator (the microcontroller goes into the “SLEEP” state). The generator is turned on by pressing the S1 button. The status of the device (on/off) is indicated by an LED.

Technical characteristics of the device:
- clock frequency - 12 MHz;
- supply voltage 3 - 5 V;
- current consumption in operating mode:
- at a supply voltage of 3V - about 5mA;
- at a supply voltage of 5V - about 12mA;
- frame rate - 50 Hz;
- number of lines in a frame - 625.

Scheme.
The scheme is very simple.
All work on the formation
video signal
executed by the program
sewn into microcon-
troller. Two resistors
along with resistance
TV video input
provide the necessary
possible voltage levels
video signal:
- 0 V - synchronization level;
- 0.3 V - black level;
- 0.7 V - gray level;
- 1 V - white level.

To generate a video signal, the zero bit of PORTA and the entire PORTB are used. (This port operates in shift mode. Even though the signal is taken from only its zero bit, the program uses all of it. Therefore, all PORTB bits are configured as outputs.) The first bit of PORTA is used to indicate the status of the oscillator. When the device is turned on, the LED lights up. When the device is turned off, the LED is off. The third PORTA bit is used to switch the generator operating modes and turn it off. Briefly pressing the S2 button allows you to move from one generator field to another. When you keep this button pressed for more than 1 second. the device turns off (the microcontroller goes into the "SLEEP" state). A reset must be performed to turn the generator on. This is done by pressing the S1 button. The supply voltage of the device can be selected within the range of 3 - 5 V. In this case, the resistor values ​​must be selected accordingly.
3V...– R5=456Ohm and R6=228Ohm
3.5V – R5=571Ohm and R6=285Ohm
4V...– R5=684Ohm and R6=342Ohm
4.5V – R5=802Ohm and R6=401Ohm
5V...- R5=900Ohm and R6=450Ohm
Estimated values ​​are shown here. In reality, you can install resistors from the standard range, for example for 5V - 910 Ohm and 470 Ohm, and for 3V - 470 Ohm and 240 Ohm.
The generator supply voltage may be less than 3V. For each specific PIC, the minimum should be determined experimentally. For example, my 20 MHz PIC from 2001 worked at 2.3 V.

Program.
The program generates 6 fields. Each field consists of 301 lines (300 information lines + one black line). In general, the calculated number is 305 (625 raster lines - 15 frame synchronization lines = 610. Information in the frame is displayed through a line (see more about this here), so 610 / 2 = 305). But with this number of lines, the vertical size of the raster is slightly larger than what forms the video signal transmitted by the television center.
The first line in each field is black. At this time, the state of button S2 is queried, the time it is held pressed is calculated, and the need to move from one field to another is determined.
There are slight distortions in the vertical lines in the graphic fields. This is due to the fact that the length of some lines is a couple of clock cycles longer than others due to the need to install loop counters. In general, the routines that generate graphic fields are very simple, so there is no need to comment on them.
Let's take a closer look at the part of the program that generates the text field. This is the most complex section of the program, occupies most of it, uses maximum microcontroller resources (all data memory and a significant part of RAM). The code used here is taken from the game Pong, written by Rickard Gunee.
The text field consists of 17 lines, each of which can consist of no more than eight characters. Characters are displayed across a line, that is, one line of text occupies 17 raster lines. (This display is due to the limitations of the PIC.) Symbol graphics information is stored in program memory in the table section. Information about the text of the lines is stored in the data memory (64 words = 8 lines of 8 characters). For example, in line 08h (addresses from 08h to 0Fh) the following is written:.20.60.48.50.90.58.20 20. Each value is the coordinate (offset from the beginning) of a character in the table. Value.20. matches space, .60. - letter "B", .48. - the letter "I", and so on. And everything together forms “_VIDEO__”.
Let's look at an example of how text is displayed. According to the program, in the 12th text line of the screen it is necessary to display the information referenced by data memory line 28h (A0 B8 68 C8 D8 70 E0 D0). Thus, the following 17 lines of the raster should display the text: "p i c 1 6 f 8 4 ". It goes like this. The first of 17 lines displays only the black level. During these 64 μs, while a black line is displayed on the screen, the “upper values” of the characters are rewritten into the RAM registers: 00h from “p”, 08h from “i”, 00h from “c” 18h from “1” and so on. During the next line, this data is sequentially transferred to PORTB, that is, to the video output. The third line is black again. During its execution, the “second from top” symbol values ​​are rewritten into the buffer: 00h from “p”, 00h from “i”, 00h from “c” 1Ch from “1”... In the fourth line, this data is displayed on the screen. And so on until the entire line is displayed.
The frame synchronization routine is taken entirely from the game Pong, which was written by Rickard Gunee. This routine is short but quite intricate. If you explain how it works, it will turn out even longer and more confusing. It is best to put the text of the subroutine and a drawing of the oscillogram of the frame sync pulses next to each other, and take your time to parse each line of code. Let me just say that the subroutine starts executing not from the top line, but from the middle (:-)), from the “vertsync” label.

Overclocking PIC16F84.
As can be seen from the diagram in this project, the microcontroller operates at a frequency of 12 MHz. Today, three versions of PIC16F84 are available: 4 MHz, 10 MHz and 20 MHz. (as of January 1, 2002, the price ratio is approximately: $3.5, $5.3 and $6.3) In his Pong project, Rickard Gunee claims that he used a 4 MHz PIC16F84 and they worked for hours at 12 MHz without problems. I tried it, and indeed the 4 MHz PIC works normally at a frequency that is three times (!!!) higher than its permissible frequency (although I did not tempt fate and turned on the generator only for a few minutes). At the same time, the current consumption of the 4 MHz PIC was 10..20% higher than that of the 20 MHz one (hence, apparently, the frequency limitation). I think that a 10 MHz microcontroller can be overclocked to 12 MHz without risk, but in commercial projects this, of course, should not be done.

Manufacturing.

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.

The proposed generator is based on the AD9850 chip from Analog Devices, which is a full DDS (Direct Digital Synthesis) frequency synthesizer with a built-in comparator. Such synthesizers are unique in their accuracy, and are practically not subject to temperature drift and aging (the only element that has the instability characteristic of analog devices is the digital-to-analog converter). Due to the high technical characteristics of DDS synthesizers, they have recently been replacing conventional analog frequency synthesizers. Their main advantage is very high frequency and phase resolution, which is controlled digitally. The digital interface allows easy implementation of micro-controller control. A more detailed description of the principles of direct digital frequency synthesis can be found, for example, in.

Fig.1

The block diagram of the AD9850 synthesizer is shown in Fig. 1. Its basis is a phase accumulator, which forms the instantaneous phase code of the output signal. This code is converted into a digital value of a sinusoidal signal, which is converted into an analog signal using a DAC and filtered. The comparator allows you to obtain a rectangular output signal. Its frequency fout (in hertz) is determined by the formula f out = A fin /232, where f m is the clock frequency, Hz; A is a 32-bit frequency code value. The maximum value of f^ cannot exceed half the clock frequency.

Main technical characteristics AD 9850 (at supply voltage 5V)

Clock frequency 1…125

Maximum current consumption (at f in =125 MHz), mA 95

Number of DAC bits 10

Maximum DAC output current (at R set =3.9 kOhm), mA 10.24

Maximum integral nonlinearity of the DAC, MZR 1

Comparator output voltage, V:

minimum high level 4.8

maximum low level 0.4

To download data, the AD9850 chip provides parallel and serial interfaces. In the latter case, data (a 40-bit word) is entered through its D7 input. Each data bit is accompanied by a pulse of positive polarity at the clock input W_CLK. After loading the control word by a pulse of positive polarity at the FQJJD input, the generation parameters are replaced with new ones. The assignment of the control word bits is given in Table. 1.

The schematic diagram of the generator is shown in Fig. 2. Controls the synthesizerDD2 microcontroller DD1.



Fig.2

It polls the keyboard SB1-SB16, displays information on the LCD indicator HG1, 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 if it exceeds it, the message “Error” appears. 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.

Bit number

Purpose

Bit 0 frequency code

Bit 1 frequency code

……..

…………

Bit 31 frequency code

Control bit (must be 0)

Power control bit (on at 0, off at 1)

Bit 0 phase code

Bit 1 phase code

……….

…………….

Bit 4 phase code

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) and "D" (Down) buttons allow you to stepwise change the generator's output frequency, 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.

LITERATURE
1. Ridico L. DDS: direct digital frequency synthesis - Components and technologies. 2001.№ 7. p. 50-54.
2. AD9650, Complete DDS Synthesizer - http://www-analog.com

This project is based on the function generator circuit 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 a sufficiently high (more than 18 V) constant voltage necessary for the normal operation of the 78L15 and 79L15 stabilizers. 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