Homemade frequency counter on ATTINY2313. Homemade frequency counter on ATTINY2313 Frequency counter with dynamic indication without microcontrollers

In this article, we will look at how to build a small, cheap and simple frequency counter capable of measuring frequencies up to 40 MHz with an error below 1%. Such accuracy is quite enough for debugging most of your own analog and digital devices. The device will allow you to analyze many aspects of the operation of circuits.

The schematic diagram of the frequency meter is shown in Figure 1.

Fig.1. Schematic diagram of the device

The frequency meter is assembled on a breadboard, the basis is the Atmel ATmega16 microcontroller, the clock source is an internal 8 MHz RC oscillator (this must be remembered when programming the microcontroller). Additionally, the input part uses a 4-bit counter 74HC191 as a divider of the measured frequency by 16 before it is fed to the input of the microcontroller. As you can see, only the output Q3 of the counter is used, the frequency at this output will be equal to the input frequency divided by 16.

The device input (probe) is point W1, which is directly connected to the microcontroller port PB0 and, through a divider, to the PB1 port.

To display the value of the measured frequency, a 4-digit seven-segment LED indicator with a common anode is used. This solution reduces the number of conductors for connecting the indicator. In the absence of a display of this type, it is possible to use various types of seven-segment indicators, however, adaptation of the microcontroller software will be required.

The layout and pin assignment of the applied indicator is shown in Figure 2.


Fig.2. Location and pin assignment of the applied 4-digit LED indicator.

Pins E1…E4 are used to turn on the corresponding digits (E1 - to turn on the right least significant digit).

Each I/O line of the ATmega16 microcontroller can provide up to 40 mA output current, so we do not need to use transistors and the display control signals (E1…E4) are connected directly to the microcontroller port.

Connector for in-circuit programming of microcontroller J1. After assembling and programming the microcontroller, you will need to calibrate the device, set some variables (for example, to increase the brightness of the display, reduce the flickering of the display). In other words, you will need to update the microcontroller software, and therefore the indicated connector must be installed on the board.

Frequency measurement algorithm

We all know that frequency is the number of repeated pulses per unit of time. However, frequency measurement with digital instruments, such as a microcontroller, which has its limitations, requires some research to achieve the desired results.

The maximum frequency that can be processed by the counter of the ATmega16 microcontroller cannot exceed the clock frequency divided by 2.5. Let's denote the maximum frequency - Fmax. The clock frequency for our microcontroller is 8 MHz, so we can measure signals up to 3.2 MHz directly. To measure the frequency above this level, we use a 4-bit counter as the input frequency divider. Now we can measure frequencies up to 16 times Fmax, but here a limitation is imposed by the counter 74191 and the actual maximum measured frequency does not exceed 40 MHz.

The algorithm that has been developed measures the original (input) frequency (we denote F o) and the frequency obtained from the divider (we denote F d). As long as the condition that the frequency is less than Fmax condition is met:

F o = 16×F d ;

But as you get closer F o To F max, more and more pulses must be processed and the expression above becomes:

F o < 16 × F d ;

Therefore, the measurement limit of the microcontroller can be automatically detected.

The frequency meter starts to measure the original frequency (processing and displaying values ​​on the display), and as soon as it detects the approach to the maximum frequency F max(using the above method), selects the frequency after the divider to be measured.

The algorithm is summarized in the diagram (Fig. 3)

Fig.3 Algorithm of operation of the frequency meter on the microcontroller

microcontroller software

The source code of the microcontroller program is provided with detailed comments, but some points require a separate explanation:

  • The code is designed so that the measured value is displayed on the display in "kHz". For example, if you see the value "325.8" on the display, it means 325.8 kHz, the value "3983" means 3983 kHz (or 3.983 MHz).
  • Timer/counter 0 of the microcontroller is used to count the input pulses directly;
  • Timer/counter 1 of the microcontroller is used to count the input pulses after dividing by 16;
  • Timer/Counter 2 is configured as a prescaled timer by 1024 (CPU frequency is divided by 1024). Used to invoke the algorithm for calculating and selecting a frequency every period T of the timer. In our project T = 1024× 256/F cpu .
  • The constant "factor", defined at the beginning of the program with the value "31.78581", must be calibrated by measuring the reference frequency. Calculated according to the expression:

factor = F cpu /(1024× 256)=8.E6/(1024×256)=30.51757

The Anti-Flickering function is rather complicated, but very effective, especially when measuring non-constant frequencies. This function completely saves the indicator from quickly switching between different values, while continuing to display the exact value, and quickly changes the reading if the measured frequency has really changed.

Note

The ATmega16 microcontroller comes factory set to operate from an internal 1MHz RC oscillator. It is necessary to set the Fuse-bits CKSEL3..0 to “0100” using the serial programmer, which corresponds to the inclusion of the internal 8 MHz RC oscillator.

APPS:

- The source code of the microcontroller program

Translation that you can say:

The development of the design was prompted by a remark read on the DDS forum that there should have been other high-frequency dividers besides the 193 and 500 series, as well as a timely seen scheme of a new synthesizer for FM2006. After experiments, a simple frequency meter was born on LMX 2306, ATtiny 2313 microcircuits and a BC 1602 sign-synthesizing liquid crystal indicator with the following characteristics:

  • Measuring frequency range from 300 Hz to 450 MHz
  • Sensitivity from 50 mV to 200 mV
  • Minimum measuring step:
  • In the range from 300 Hz to 4.5 MHz 1 Hz
  • 4.5 MHz to 80 MHz 25 Hz
  • 80 MHz to 450 MHz 100 Hz
  • Measurement time 0.1 sec / 1 sec
  • Measurement accuracy is not worse than 0.007%
  • Supply voltage 9V…15V
  • Current consumption (without indicator light) 20 mA

Description and configuration of the scheme (fig.1).

The signal from input F goes to the amplifying stage on the transistor VT1 from which it diverges to a programmable high-frequency divider, which is part of the DD1 microcircuit, as well as to the SA1 sliding switch, which selects the measurement range (up to 4.5 MHz / above 4.5 MHz). Further, the signal is further amplified and fed to the DD2 chip, which performs frequency counting, data output to the LCD and control of the DD1 chip. The circuit is powered by the DA1 stabilizer.

Switch SA2 selects the counting time and, accordingly, the measurement accuracy. The SB1 button is used to calibrate the frequency meter. To do this, an exemplary frequency of 1 MHz is applied to the input F and by pressing SB1 hold it until the readings on the LCD display are as close as possible to 1 MHz. Further calibration can be omitted.

You can also use the standard tuning procedure by applying any reference frequency to the F input and selecting C9 and C10 to achieve the desired LCD readings.

The chain D1, R5, R6, C7, together with the cascade on the transistor VT2, expands the pulses coming from the DD1 chip. When the maximum possible frequency is applied to the F input, but not more than 450 MHz, by selecting the resistor R5, stable LCD readings are achieved (if the oscilloscope is connected to the 9th leg of DD2, there should be something close to a meander). Capacitor C7 in the design we assembled moved to the collector VT2.

The Prog connector is used for in-circuit programming of the ATtiny 2313. If the microcircuit is flashed in the programmer, then the connector is not soldered. It is better to install the microcircuit in the socket.

Details.

Fixed resistors and ceramic capacitors size 0805 (surface mount). Transistor VT1 KT368 will be replaced by KT399, VT2 KT368 - by a lower frequency KT315 (with board adjustment). Chip DD2 ATtiny 2313-20 (with a clock frequency of up to 20 MHz) in a DIP package is installed on the side of the printed conductors. DA1 (also installed on the print side) - any 5-volt stabilizer with a current of more than 1 A, but if you do not use the LCD backlight, you can also use low-current 78L05. Quartz resonator Q1 - 11.0592 MHz in any design. Switches SA1 and SA2 - B1561(DPDT) or SS21 with a lever length of more than 5 mm. Tact button SB1 - TS-A1PS (TS-A2PS, TS-A3PS, TS-A4PS, TS-A6PS). Indicator BC1602 or BC1601, BC1604, as well as similar with HD-44780 controller from other manufacturers. Be sure to check the consistency of the conclusions! We can replace the VD2 1N4007 diode with any one with a suitable operating current. Power connector - AUB series 3.5mm stereo or similar with some board adjustment. Any low-power AC adapter with a suitable voltage is used for power supply. The signal to the board is fed through a single-core wire with a diameter of approximately 0.8 mm and a length of 5-8 cm.

You can exclude C4, R4 and switch SA1 from the circuit by connecting C8 with a jumper to the VT2 base. 6 leg DD2 should hang in the air. In this embodiment, the lower cutoff frequency becomes 1.5 MHz.

The printed circuit board is divorced in Sprint-Layout and made of one-sided foil fiberglass ( rice. 2).

Structurally, the device consists of a display formed by seven 7-segment LED indicators, a microcontroller and several transistors and resistors. The microcontroller performs all the necessary functions, so the use of any additional microcircuits is not required.

The circuit diagram of the device is quite simple and is shown in Figure 2. The Eagle project (circuit diagram and PCB) is available for download in the download section.

The tasks performed by the microcontroller are simple and obvious: counting the number of input pulses in 1 second and displaying the result on a 7-digit indicator. The most important point here is the accuracy of the master generator (time base), which is provided by the built-in 16-bit Timer1 timer in CTC mode. The second, 8-bit, timer-counter operates in the mode of counting the number of pulses at its input T0. Every 256 pulses cause an interrupt, the handler of which increments the value of the coefficient. When the duration of 1 s is reached with the 16-bit timer, an interrupt occurs, but in this case, the factor is multiplied by 256 (left shift by 8 bits) in the interrupt handler. The remaining number of pulses registered by the counter is added to the result of the multiplication. The resulting value is then divided into separate numbers, which are displayed on a separate indicator in the corresponding category. After that, just before exiting the interrupt handler, both counters are simultaneously reset and the measurement cycle is repeated. In "free time", the microcontroller outputs information to the indicator using the multiplexing method. In the source code of the microcontroller program, the author gave additional comments that will help to understand in detail the algorithm of the microcontroller.

Resolution and measurement accuracy

The measurement accuracy depends on the clock source for the microcontroller. By itself, the program code can introduce an error (adding one pulse) at high frequencies, but this practically does not affect the measurement result. The quartz resonator used in the device must be of good quality and have a minimum error. The best choice would be a resonator whose frequency is divisible by 1024, such as 16 MHz or 22.1184 MHz. To obtain a measurement range of up to 10 MHz, it is necessary to use a quartz resonator at a frequency of 21 MHz and higher (for 16 MHz, as in the diagram, the measurement range becomes slightly lower than 8 MHz). A 22.1184 MHz quartz resonator is ideal for our device, but acquiring one with a minimum error for many radio amateurs will be a difficult task. In this case, you can use a quartz resonator at a different frequency (for example, 25 MHz), but you must perform the master oscillator calibration procedure using an oscilloscope that supports hardware measurements and a trimmer capacitor in the quartz resonator circuit (Figure 3, 4).

In the download section, several versions of firmware for various quartz resonators are available for download, but users can compile the firmware for an existing quartz resonator on their own (see comments in the source code).

Input signal

In the general case, a signal of any shape with an amplitude of 0 ... 5 V can be applied to the input of the device, and not just rectangular pulses. You can apply a sinusoidal or triangular signal; the pulse is determined by a falling edge at a level of 0.8 V. Please note: the frequency meter input is not protected from high voltage and is not pulled up to power, this is a high-resistance input that does not load the circuit under test. The measurement range can be extended up to 100 MHz with a resolution of 10 Hz by using an appropriate high-speed frequency divider at the input.

Display

The device uses seven LED 7-segment indicators with a common anode as a display. If the brightness of the indicators is insufficient, you can change the value of the resistors that limit the current through the segments. However, do not forget that the magnitude of the pulse current for each output of the microcontroller should not exceed 40 mA (indicators also have their own operating current, do not forget about its value). In the diagram, the author indicated the value of these resistors is 100 ohms. Insignificant zeros are suppressed when displaying the measurement result, which makes reading the readings more comfortable.

Printed circuit board

The double-sided printed circuit board has dimensions of 109 × 23 mm. The free version of the Eagle PCB design environment does not have seven-segment LED indicators in the component library, so they were hand-drawn by the author. As can be seen in the photographs (Figures 5, 6, 7) of the author's version of the printed circuit board, it is additionally necessary to make several connections with a mounting wire. One connection on the front side of the board is power to the Vcc pin of the microcontroller (through a hole in the board). There are two more connections on the underside of the board, which are used to connect the decimal point segment pins of the indicators in digits 4 and 7 through 330 ohm resistors to ground. For in-circuit programming of the microcontroller, the author used a 6-pin connector (in the diagram, this connector is shown as a composite JP3 and JP4), located at the top of the printed circuit board. This connector does not have to be soldered to the board, the microcontroller can be programmed in any way possible.

Downloads

Schematic diagram and drawing of the printed circuit board, source code and microcontroller firmware -

This instrument is designed to measure frequencies between 0-9999 Hz, but when using a frequency divider at the input, this range is extended accordingly. The maximum input voltage is 3V, provided that there is no additional voltage divider, the minimum is 0.15V, also provided that it is absent. The maximum measurement frequency can be extended by changing the program code, but more on that later.

The device circuit is relatively simple and is shown below:

The circuit is based on an 8-bit microcontroller manufactured by Atmega8A-PU. To clock the core of the microcontroller, an oscillator with an external quartz resonator is used. The choice of such a generator is due to the requirements for the frequency stability of the latter. A seven-segment four-digit LED indicator with a common anode and dynamic indication is used as an indicator. The current of the indicator segments is not limited by resistors, since dynamic indication is used, and of course, the current is pulsed, which the indicator segments successfully withstand, as well as the microcontroller port. The input node is made on the elements R2, D1, D2, C3, R3, R4, R1, Q1. This node provides amplification / limiting of the signals coming to its input (the resistor and diodes at the input of the frequency counter limit the input signal, the transistor is responsible for amplifying the signal to the TTL level). The printed circuit board of the device is also not complicated. It is made of one-sided foil material (it was originally planned to be made of two-sided, but it was not available, so I settled on one-sided). The topology of the board is shown below.

As for the program for the microcontroller, it was developed in the environment (the project file is attached). To count pulses, I used interrupts at the INT0 input of the microcontroller, and to limit the counting time, I used timer interrupts TMR0. Since this timer has a prescaler with a division factor of 1/256 (due to the fact that it is eight-digit), the interrupt frequency is calculated as follows: Fprev.=F gen.÷256÷thr. affairs. In my design, I chose an interruption frequency of 200 Hz. As I wrote above, the measurement frequency can be increased. To do this, you only need to limit the measurement time. This is done by changing the number 200 to 2 (measurement time is not 1s but 10ms, the cutoff frequency is 99999Hz), in the code, as shown in the figure in the source code in C.

To program the microcontroller, I used a parallel programmer. As you can see, in my design, the external reset pin is used as a normal port. If you do not have a parallel programmer, then here is a circuit option where port D of the microcontroller is used, and there is no need to use the RESET pin as a normal output port.

Here is an example of setting fuses in the program:

Here is a diagram of a frequency counter option using RESET for its intended purpose:

And also here is the board topology for the second version of the circuit:

To power the circuit, a stabilized 5V power supply is used (I used a computer power supply, so there are no stabilizing elements in the circuit).

There are no scarce parts in this scheme, but I will still list the list of replacements and analogues. So the Atmega8A-PU microcontroller can be replaced with a similar Atmega8-16PU (by the way, the latter is more preferable). Resistors can be taken at a power of 0.125 W, with the exception of R2, it is better to take it at 0.5 W. Capacitors - for the generator are disk, ceramic, and for the input unit - any one suitable in terms of parameters. The transistor can be replaced with a domestic KT3102 (as experiments have shown for KT315, the current transfer coefficient h21E is too low). You can take any LED indicator that is suitable in size (and they are not critical). The quartz resonator was used at a frequency of 3267800 Hz (3.2768 MHz).

Photos of the finished device are shown below.

The first photo shows a resistor that is connected to the RESET MK power supply (left from the second firmware check, with a reset).

List of radio elements

Designation Type Denomination Quantity NoteShopMy notepad
U1 MK AVR 8-bit

ATmega8A

1 ATmega 8-16PU To notepad
Q1 bipolar transistor

KT3102

1 2N3390 To notepad
VD1, VD2 rectifier diode

1N4148

2 To notepad
C1, C2 Capacitor27 pF2 To notepad
C3 Capacitor22 nF1 To notepad
R1, R4 Resistor

470 ohm

2 To notepad
R2 Resistor

100 ohm

1
A very useful and simple device, which is simply indispensable in the creative laboratory of a radio amateur, can be made on the PIC16F628A MK. To measure frequencies up to 30 MHz, this digital frequency meter is designed on a common PIC16F628A controller chip. Its circuit diagram consists of a basic module with an input driver connected to its counting input. The frequency meter circuit is shown in the figure below:

This meter can be used in two modes - digital scale and frequency meter. When the power is turned on, the frequency meter switches to the mode in which it worked before the last power off. If it was a frequency counter mode, the frequency counter mode "F." will be displayed in the left digit of the indicator. Also, "0" will be displayed in the low-order digit of the indicator. The frequency meter will automatically enter the frequency measurement mode and will be in standby mode. When a signal is applied to the input, the sign of the frequency counter mode is "F." is extinguished and the indicator will display the value of the measured frequency in kilohertz.
The scheme of the input driver of the frequency meter - digital scale, is shown in the figure:


If at the time of power-on, there is a measured signal at the input of the frequency meter, then, after turning on the power, the sign of the frequency meter "F." will light up for 1 second, and then go out.
In order to switch to a measurement time of 0.1 sec. or 10 seconds, you must press either button No. 1, or simultaneously press button No. 1 and button No. 2, respectively (see the keyboard layout for the frequency counter mode), then wait for the decimal point position to change, and then release the button (buttons). If after that it is necessary to return to the measurement time of 1 second, then it is necessary to press button No. 2 and wait for the change in the position of the decimal point, and then release the button. For any measurement time, the decimal point marks the kilohertz.


Counter Mode Keyboard Layout

Button No. 1 0.1 sec. Change to measurement time 0.1 sec.
Button #2 1 sec. Change to measurement time 1 sec.
Button #1 +
button no. 2 10 sec. Change to measurement time 10 sec.
(buttons are pressed simultaneously)

If before turning off the power there was work in the digital scale mode, then the next time the power is turned on, this mode will be set, and inside the digital scale mode, exactly the submode (“minus IF” or “plus IF”) will be set, in which work took place until the last power off. Signs of submodes of the digital scale ("L." or "H." respectively) will be constantly displayed in the left digit of the indicator. If there is no signal at the input of the digital scale, the indicator will show the value of the intermediate frequency stored in the controller's memory, and if it is present, the result of subtracting or adding the frequency of the signal present at the input of the digital scale and the value of the intermediate frequency recorded in the non-volatile memory of the PIC controller.


The digital scale mode has 4 sub-modes.
- When you press button No. 1, the submode "minus IF" occurs.
- In this case, in the left digit of the indicator, the sign of the submode "L." will be displayed.
- When you press the button No. 2, the transition to the "plus IF" submode takes place.
- In this case, in the left digit of the indicator, the sign of the submode "H." will be displayed.

In the process of "flashing" the controller, the value of the intermediate frequency = 5.5 MHz is written to its non-volatile memory, but then it can independently write any value into it and use it as an intermediate one. To do this, you need to apply an external signal to the input of the digital signal with a frequency, which will then be used as an intermediate one. You can control the value of this frequency by switching to the frequency counter mode.

Numeric scale mode keyboard layout:
Buttons Measuring time Explanations
Button No. 1 "minus IF" The intermediate frequency is subtracted from
measured frequency
Button No. 2 "plus IF" Intermediate frequency is summed with
measured frequency
Button #1 +
button No. 2 IF setting Write to RAM value
measured frequency (IF)
Re:
Button #1 +
button No. 2 Write IF Copying the value of the measured frequency from RAM to non-volatile memory in order to further use it as an intermediate


When you change the operating mode, the keyboard layout changes. If button No. 1 is pressed for less than a certain time, then switching to another mode does not occur and button No. 1 can either set the measurement time to 0.1 sec. (in the frequency counter mode), or turn on the "minus IF" submode (in the digital scale mode). If this threshold is exceeded, a switch to another mode occurs. The value of this threshold is about 4 seconds, and this time interval is counted from the end of the counting cycle at the time of pressing button No. 1.


You can reduce the power consumption of the frequency counter circuit by increasing the values ​​of the resistors connecting the pins of port B to the indicator. In its design, I used a 9-digit LED indicator from a Soviet telephone with AON, with a common cathode and a red glow. In my frequency meter, in addition to mains power, there is also battery power (accumulators). The printed circuit board of the device is shown in the figure:


Firmware for the PIC16F84A microcontroller, as well as the full text of the article on the controller, download here. I tested the circuit - ZU77.

Loading...Loading...